=pod

=head1 NAME

EVP_KDF_KB - The Key-Based EVP_KDF implementation

=head1 DESCRIPTION

The EVP_KDF_KB algorithm implements the Key-Based key derivation function
(KBKDF).  KBKDF derives a key from repeated application of a keyed MAC to an
input secret (and other optional values).

=head2 Numeric identity

B<EVP_KDF_KB> is the numeric identity for this implementation; it can be used with the
EVP_KDF_CTX_new_id() function.

=head2 Supported controls

The supported controls are:

=over 4

=item B<EVP_KDF_CTRL_SET_KB_MODE>

This control expects one argument: C<int mode>

Sets the mode for the KBKDF operation. There are two supported modes:

=over 4

=item B<EVP_KDF_KB_MODE_COUNTER>

The counter mode of KBKDF should be used. This is the default.

=item B<EVP_KDF_KB_MODE_FEEDBACK>

The feedback mode of KBKDF should be used.

=back

=item B<EVP_KDF_CTRL_SET_KB_MAC_TYPE>

This control expects one argument: C<int mac_type>

Sets the mac type for the KBKDF operation. There are two supported mac types:

=over 4

=item B<EVP_KDF_KB_MAC_TYPE_HMAC>

The HMAC with the digest set by B<EVP_KDF_CTRL_SET_MD> should be used as the mac.

=item B<EVP_KDF_KB_MAC_TYPE_CMAC>

The CMAC with the cipher set by B<EVP_KDF_CTRL_SET_CIPHER> should be used as the mac.

=back

=item B<EVP_KDF_CTRL_SET_MD>

=item B<EVP_KDF_CTRL_SET_CIPHER>

=item B<EVP_KDF_CTRL_SET_KEY>

=item B<EVP_KDF_CTRL_SET_SALT>

These controls work as described in L<EVP_KDF_CTX(3)/CONTROLS>.

=item B<EVP_KDF_CTRL_SET_KB_INFO>

This control expects two arguments: C<unsigned char *info>, C<size_t infolen>

=item B<EVP_KDF_CTRL_SET_KB_SEED>

This control expects two arguments: C<unsigned char *seed>, C<size_t seedlen>

It is used only in the feedback mode and the length must be the same
as the block length of the cipher in CMAC or the size of the digest in HMAC.

=back

The controls B<EVP_KDF_CTRL_SET_KEY>, B<EVP_KDF_CTRL_SET_SALT>,
B<EVP_KDF_CTRL_SET_KB_INFO>, and B<EVP_KDF_CTRL_SET_KB_SEED> 
correspond to KI, Label, Context, and IV (respectively) in SP800-108.
As in that document, salt, info, and seed are optional and may be
omitted.

Depending on whether mac is CMAC or HMAC, either digest or cipher is
required (respectively) and the other is unused.

=head1 NOTES

A context for KBKDF can be obtained by calling:

 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new_id(EVP_KDF_KB);

The output length of an KBKDF is specified via the C<keylen>
parameter to the L<EVP_KDF_derive(3)> function.

Note that currently OpenSSL only implements counter and feedback modes.  Other
variants may be supported in the future.

=head1 EXAMPLES

This example derives 10 bytes using COUNTER-HMAC-SHA256, with KI "secret",
Label "label", and Context "context".

 EVP_KDF_CTX *kctx;
 unsigned char out[10];

 kctx = EVP_KDF_CTX_new_id(EVP_KDF_KB);

 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256());
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KB_MAC_TYPE, EVP_KDF_KB_MAC_TYPE_HMAC);
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, "secret", strlen("secret"));
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "label", strlen("label"));
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KB_INFO, "context", strlen("context"));
 if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0)
     error("EVP_KDF_derive");

 EVP_KDF_CTX_free(kctx);

This example derives 10 bytes using FEEDBACK-CMAC-AES256, with KI "secret",
Label "label", Context "context", and IV "sixteen bytes iv".

 EVP_KDF_CTX *kctx;
 unsigned char out[10];
 unsigned char *iv = "sixteen bytes iv";

 kctx = EVP_KDF_CTX_new_id(EVP_KDF_KB);

 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_CIPHER, EVP_aes_256_cbc());
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KB_MAC_TYPE, EVP_KDF_KB_MAC_TYPE_CMAC);
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KB_MODE, EVP_KDF_KB_MODE_FEEDBACK);
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, "secret", strlen("secret"));
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "label", strlen("label"));
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KB_INFO, "context", strlen("context"));
 EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KB_SEED, iv, strlen(iv));
 if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0)
     error("EVP_KDF_derive");

 EVP_KDF_CTX_free(kctx);

=head1 CONFORMING TO

NIST SP800-108, IETF RFC 6803, IETF RFC 8009.

=head1 SEE ALSO

L<EVP_KDF_CTX(3)>,
L<EVP_KDF_CTX_new_id(3)>,
L<EVP_KDF_CTX_free(3)>,
L<EVP_KDF_ctrl(3)>,
L<EVP_KDF_size(3)>,
L<EVP_KDF_derive(3)>,
L<EVP_KDF_CTX(3)/CONTROLS>

=head1 HISTORY

This functionality was added to OpenSSL 3.0.

=head1 COPYRIGHT

Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
Copyright 2019 Red Hat, Inc.

Licensed under the Apache License 2.0 (the "License").  You may not use
this file except in compliance with the License.  You can obtain a copy
in the file LICENSE in the source distribution or at
L<https://www.openssl.org/source/license.html>.

=cut
