Skip to content

Commit c6a1d8e

Browse files
danvangeestpaulidale
authored andcommitted
HKDF updates
- prevent fixed-digest HKDF from having its digest changed - implement gettable params in HKDF - update fixed-digest HKDF tests Reviewed-by: Shane Lontis <shane.lontis@oracle.com> Reviewed-by: Tomas Mraz <tomas@openssl.org> Reviewed-by: Paul Dale <ppzgs1@gmail.com> (Merged from openssl#27247)
1 parent 2671a68 commit c6a1d8e

File tree

11 files changed

+261
-151
lines changed

11 files changed

+261
-151
lines changed

CHANGES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ OpenSSL 3.6
112112

113113
*Dimitri John Ledkov*
114114

115-
* HKDF with (SHA-256,SHA-384,SHA-512) has assigned OIDs. Added ability to load
115+
* HKDF with (SHA-256, SHA-384, SHA-512) has assigned OIDs. Added ability to load
116116
HKDF configured with these explicit digests by name or OID.
117117

118118
*Daniel Van Geest (CryptoNext Security)*

doc/man7/EVP_KDF-HKDF.pod

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@ of the KDF).
1717

1818
The output is considered to be keying material.
1919

20-
=head2 Fixed-Digest HKDF
21-
22-
B<HKDF-SHA256>, B<HKDF-SHA384> and B<HKDF-SHA512> are fixed-digest versions
23-
of the B<HKDF> algorithm. Each algorithm has its own OID. These algorithms
24-
are instantiated with the appropriate digest already configured, thus it is
25-
not necessary to set the digest using the B<OSSL_KDF_PARAM_DIGEST> parameter.
26-
An attempt to set the digest to anything other than the pre-configured digest
27-
will result in an error.
28-
2920
=head2 Identity
3021

3122
The following algorithms are available for this implementation; they
@@ -40,12 +31,20 @@ mere OID which came out in this form after a call to L<OBJ_obj2txt(3)>).
4031

4132
=item "HKDF"
4233

34+
The B<OSSL_KDF_PARAM_DIGEST> parameter must be set for B<HKDF> before it can
35+
be used.
36+
4337
=item "HKDF-SHA256", "id-alg-hkdf-with-sha256", "1.2.840.113549.1.9.16.3.28"
4438

4539
=item "HKDF-SHA384", "id-alg-hkdf-with-sha384", "1.2.840.113549.1.9.16.3.29"
4640

4741
=item "HKDF-SHA512", "id-alg-hkdf-with-sha512", "1.2.840.113549.1.9.16.3.30"
4842

43+
B<HKDF-SHA256>, B<HKDF-SHA384> and B<HKDF-SHA512> are fixed-digest versions
44+
of B<HKDF> with the appropriate digest already configured.
45+
L<EVP_KDF_CTX_reset(3)> will not reset the context's digest for fixed-digest
46+
versions.
47+
4948
=back
5049

5150
=head2 Supported parameters
@@ -58,6 +57,8 @@ The supported parameters are:
5857

5958
=item "digest" (B<OSSL_KDF_PARAM_DIGEST>) <UTF8 string>
6059

60+
Attempting to set the digest on a fixed-digest B<HKDF> will result in an error.
61+
6162
=item "key" (B<OSSL_KDF_PARAM_KEY>) <octet string>
6263

6364
=item "salt" (B<OSSL_KDF_PARAM_SALT>) <octet string>
@@ -84,8 +85,9 @@ up for HKDF will perform an extract followed by an expand operation in one go.
8485
The derived key returned will be the result after the expand operation. The
8586
intermediate fixed-length pseudorandom key K is not returned.
8687

87-
In this mode the digest, key, salt and info values must be set before a key is
88-
derived otherwise an error will occur.
88+
In this mode the key, salt and info values must be set before a key is
89+
derived otherwise an error will occur. For non-fixed mode (B<HKDF>) the digest
90+
must also be set.
8991

9092
=item "EXTRACT_ONLY" or B<EVP_KDF_HKDF_MODE_EXTRACT_ONLY>
9193

@@ -94,17 +96,19 @@ operation. The value returned will be the intermediate fixed-length pseudorandom
9496
key K. The I<keylen> parameter must match the size of K, which can be looked
9597
up by calling EVP_KDF_CTX_get_kdf_size() after setting the mode and digest.
9698

97-
The digest, key and salt values must be set before a key is derived otherwise
98-
an error will occur.
99+
The key and salt values must be set before a key is derived otherwise
100+
an error will occur. For non-fixed mode (B<HKDF>) the digest
101+
must also be set.
99102

100103
=item "EXPAND_ONLY" or B<EVP_KDF_HKDF_MODE_EXPAND_ONLY>
101104

102105
In this mode calling L<EVP_KDF_derive(3)> will just perform the expand
103106
operation. The input key should be set to the intermediate fixed-length
104107
pseudorandom key K returned from a previous extract operation.
105108

106-
The digest, key and info values must be set before a key is derived otherwise
107-
an error will occur.
109+
The key and info values must be set before a key is derived otherwise
110+
an error will occur. For non-fixed mode (B<HKDF>) the digest
111+
must also be set.
108112

109113
=back
110114

@@ -148,6 +152,8 @@ after setting the mode and digest on the B<EVP_KDF_CTX>.
148152

149153
=head1 EXAMPLES
150154

155+
=head2 HKDF Algorithm
156+
151157
This example derives 10 bytes using SHA-256 with the secret key "secret",
152158
salt value "salt" and info value "label":
153159

@@ -175,6 +181,33 @@ salt value "salt" and info value "label":
175181

176182
EVP_KDF_CTX_free(kctx);
177183

184+
=head2 HKDF-SHA256 Algorithm
185+
186+
This example derives 10 bytes using HKDF-SHA256 with the secret key "secret",
187+
salt value "salt" and info value "label":
188+
189+
EVP_KDF *kdf;
190+
EVP_KDF_CTX *kctx;
191+
unsigned char out[10];
192+
OSSL_PARAM params[4], *p = params;
193+
194+
kdf = EVP_KDF_fetch(NULL, "HKDF-SHA256", NULL);
195+
kctx = EVP_KDF_CTX_new(kdf);
196+
EVP_KDF_free(kdf);
197+
198+
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
199+
"secret", (size_t)6);
200+
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
201+
"label", (size_t)5);
202+
*p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
203+
"salt", (size_t)4);
204+
*p = OSSL_PARAM_construct_end();
205+
if (EVP_KDF_derive(kctx, out, sizeof(out), params) <= 0) {
206+
error("EVP_KDF_derive");
207+
}
208+
209+
EVP_KDF_CTX_free(kctx);
210+
178211
=head1 CONFORMING TO
179212

180213
RFC 5869 and RFC 8619

doc/man7/OSSL_PROVIDER-FIPS.pod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ L<https://www.openssl.org/source/>
588588

589589
=head1 HISTORY
590590

591-
The HKDF-SHA256, HKDF-SHA384 and HKDF-SHA512 digests were added in OpenSSL 3.6.
591+
The HKDF-SHA256, HKDF-SHA384 and HKDF-SHA512 algorithms were added in OpenSSL 3.6.
592592

593593
All other functionality was added in OpenSSL 3.0.
594594

doc/man7/OSSL_PROVIDER-default.pod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ L<OSSL_PROVIDER-base(7)>
530530

531531
The RIPEMD160 digest was added to the default provider in OpenSSL 3.0.7.
532532

533-
The HKDF-SHA256, HKDF-SHA384 and HKDF-SHA512 digests were added in OpenSSL 3.6.
533+
The HKDF-SHA256, HKDF-SHA384 and HKDF-SHA512 algorithms were added in OpenSSL 3.6.
534534

535535
All other functionality was added in OpenSSL 3.0.
536536

providers/defltprov.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,6 @@ static const OSSL_ALGORITHM deflt_keyexch[] = {
398398
#endif
399399
{ PROV_NAMES_TLS1_PRF, "provider=default", ossl_kdf_tls1_prf_keyexch_functions },
400400
{ PROV_NAMES_HKDF, "provider=default", ossl_kdf_hkdf_keyexch_functions },
401-
{ PROV_NAMES_HKDF_SHA256, "provider=default", ossl_kdf_hkdf_sha256_keyexch_functions },
402-
{ PROV_NAMES_HKDF_SHA384, "provider=default", ossl_kdf_hkdf_sha384_keyexch_functions },
403-
{ PROV_NAMES_HKDF_SHA512, "provider=default", ossl_kdf_hkdf_sha512_keyexch_functions },
404401
{ PROV_NAMES_SCRYPT, "provider=default",
405402
ossl_kdf_scrypt_keyexch_functions },
406403
{ NULL, NULL, NULL }
@@ -589,12 +586,6 @@ static const OSSL_ALGORITHM deflt_keymgmt[] = {
589586
PROV_DESCS_TLS1_PRF_SIGN },
590587
{ PROV_NAMES_HKDF, "provider=default", ossl_kdf_keymgmt_functions,
591588
PROV_DESCS_HKDF_SIGN },
592-
{ PROV_NAMES_HKDF_SHA256, "provider=default", ossl_kdf_keymgmt_functions,
593-
PROV_DESCS_HKDF_SHA256_SIGN },
594-
{ PROV_NAMES_HKDF_SHA384, "provider=default", ossl_kdf_keymgmt_functions,
595-
PROV_DESCS_HKDF_SHA384_SIGN },
596-
{ PROV_NAMES_HKDF_SHA512, "provider=default", ossl_kdf_keymgmt_functions,
597-
PROV_DESCS_HKDF_SHA512_SIGN },
598589
{ PROV_NAMES_SCRYPT, "provider=default", ossl_kdf_keymgmt_functions,
599590
PROV_DESCS_SCRYPT_SIGN },
600591
{ PROV_NAMES_HMAC, "provider=default", ossl_mac_legacy_keymgmt_functions,

providers/fips/fipsprov.c

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,6 @@ static const OSSL_ALGORITHM fips_keyexch[] = {
448448
{ PROV_NAMES_TLS1_PRF, FIPS_DEFAULT_PROPERTIES,
449449
ossl_kdf_tls1_prf_keyexch_functions },
450450
{ PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_keyexch_functions },
451-
{ PROV_NAMES_HKDF_SHA256, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_sha256_keyexch_functions },
452-
{ PROV_NAMES_HKDF_SHA384, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_sha384_keyexch_functions },
453-
{ PROV_NAMES_HKDF_SHA512, FIPS_DEFAULT_PROPERTIES, ossl_kdf_hkdf_sha512_keyexch_functions },
454451
{ NULL, NULL, NULL }
455452
};
456453

@@ -618,12 +615,6 @@ static const OSSL_ALGORITHM fips_keymgmt[] = {
618615
PROV_DESCS_TLS1_PRF_SIGN },
619616
{ PROV_NAMES_HKDF, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
620617
PROV_DESCS_HKDF_SIGN },
621-
{ PROV_NAMES_HKDF_SHA256, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
622-
PROV_DESCS_HKDF_SHA256_SIGN },
623-
{ PROV_NAMES_HKDF_SHA384, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
624-
PROV_DESCS_HKDF_SHA384_SIGN },
625-
{ PROV_NAMES_HKDF_SHA512, FIPS_DEFAULT_PROPERTIES, ossl_kdf_keymgmt_functions,
626-
PROV_DESCS_HKDF_SHA512_SIGN },
627618
{ PROV_NAMES_HMAC, FIPS_DEFAULT_PROPERTIES, ossl_mac_legacy_keymgmt_functions,
628619
PROV_DESCS_HMAC_SIGN },
629620
#ifndef OPENSSL_NO_CMAC

providers/implementations/exchange/kdf_exch.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ static void *kdf_newctx(const char *kdfname, void *provctx)
7979

8080
KDF_NEWCTX(tls1_prf, "TLS1-PRF")
8181
KDF_NEWCTX(hkdf, "HKDF")
82-
KDF_NEWCTX(hkdf_sha256, "HKDF-SHA256")
83-
KDF_NEWCTX(hkdf_sha384, "HKDF-SHA384")
84-
KDF_NEWCTX(hkdf_sha512, "HKDF-SHA512")
8582
KDF_NEWCTX(scrypt, "SCRYPT")
8683

8784
static int kdf_init(void *vpkdfctx, void *vkdf, const OSSL_PARAM params[])
@@ -209,9 +206,6 @@ static const OSSL_PARAM *kdf_settable_ctx_params(ossl_unused void *vpkdfctx,
209206

210207
KDF_SETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
211208
KDF_SETTABLE_CTX_PARAMS(hkdf, "HKDF")
212-
KDF_SETTABLE_CTX_PARAMS(hkdf_sha256, "HKDF-SHA256")
213-
KDF_SETTABLE_CTX_PARAMS(hkdf_sha384, "HKDF-SHA384")
214-
KDF_SETTABLE_CTX_PARAMS(hkdf_sha512, "HKDF-SHA512")
215209
KDF_SETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
216210

217211
static const OSSL_PARAM *kdf_gettable_ctx_params(ossl_unused void *vpkdfctx,
@@ -240,9 +234,6 @@ static const OSSL_PARAM *kdf_gettable_ctx_params(ossl_unused void *vpkdfctx,
240234

241235
KDF_GETTABLE_CTX_PARAMS(tls1_prf, "TLS1-PRF")
242236
KDF_GETTABLE_CTX_PARAMS(hkdf, "HKDF")
243-
KDF_GETTABLE_CTX_PARAMS(hkdf_sha256, "HKDF-SHA256")
244-
KDF_GETTABLE_CTX_PARAMS(hkdf_sha384, "HKDF-SHA384")
245-
KDF_GETTABLE_CTX_PARAMS(hkdf_sha512, "HKDF-SHA512")
246237
KDF_GETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
247238

248239
#define KDF_KEYEXCH_FUNCTIONS(funcname) \
@@ -263,7 +254,4 @@ KDF_GETTABLE_CTX_PARAMS(scrypt, "SCRYPT")
263254

264255
KDF_KEYEXCH_FUNCTIONS(tls1_prf)
265256
KDF_KEYEXCH_FUNCTIONS(hkdf)
266-
KDF_KEYEXCH_FUNCTIONS(hkdf_sha256)
267-
KDF_KEYEXCH_FUNCTIONS(hkdf_sha384)
268-
KDF_KEYEXCH_FUNCTIONS(hkdf_sha512)
269257
KDF_KEYEXCH_FUNCTIONS(scrypt)

0 commit comments

Comments
 (0)