/src/openssl/providers/implementations/kdfs/hkdf.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | /* |
11 | | * HMAC low level APIs are deprecated for public use, but still ok for internal |
12 | | * use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdlib.h> |
17 | | #include <stdarg.h> |
18 | | #include <string.h> |
19 | | #include <openssl/hmac.h> |
20 | | #include <openssl/evp.h> |
21 | | #include <openssl/kdf.h> |
22 | | #include <openssl/core_names.h> |
23 | | #include <openssl/proverr.h> |
24 | | #include "internal/cryptlib.h" |
25 | | #include "internal/numbers.h" |
26 | | #include "internal/packet.h" |
27 | | #include "crypto/evp.h" |
28 | | #include "prov/provider_ctx.h" |
29 | | #include "prov/providercommon.h" |
30 | | #include "prov/implementations.h" |
31 | | #include "prov/provider_util.h" |
32 | | #include "internal/e_os.h" |
33 | | #include "internal/params.h" |
34 | | |
35 | | #define HKDF_MAXBUF 2048 |
36 | 0 | #define HKDF_MAXINFO (32*1024) |
37 | | |
38 | | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_new; |
39 | | static OSSL_FUNC_kdf_dupctx_fn kdf_hkdf_dup; |
40 | | static OSSL_FUNC_kdf_freectx_fn kdf_hkdf_free; |
41 | | static OSSL_FUNC_kdf_reset_fn kdf_hkdf_reset; |
42 | | static OSSL_FUNC_kdf_derive_fn kdf_hkdf_derive; |
43 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_hkdf_settable_ctx_params; |
44 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_hkdf_set_ctx_params; |
45 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn kdf_hkdf_gettable_ctx_params; |
46 | | static OSSL_FUNC_kdf_get_ctx_params_fn kdf_hkdf_get_ctx_params; |
47 | | static OSSL_FUNC_kdf_derive_fn kdf_tls1_3_derive; |
48 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_3_settable_ctx_params; |
49 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_3_set_ctx_params; |
50 | | |
51 | | static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
52 | | const unsigned char *salt, size_t salt_len, |
53 | | const unsigned char *key, size_t key_len, |
54 | | const unsigned char *info, size_t info_len, |
55 | | unsigned char *okm, size_t okm_len); |
56 | | static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
57 | | const unsigned char *salt, size_t salt_len, |
58 | | const unsigned char *ikm, size_t ikm_len, |
59 | | unsigned char *prk, size_t prk_len); |
60 | | static int HKDF_Expand(const EVP_MD *evp_md, |
61 | | const unsigned char *prk, size_t prk_len, |
62 | | const unsigned char *info, size_t info_len, |
63 | | unsigned char *okm, size_t okm_len); |
64 | | |
65 | | /* Settable context parameters that are common across HKDF and the TLS KDF */ |
66 | | #define HKDF_COMMON_SETTABLES \ |
67 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MODE, NULL, 0), \ |
68 | 0 | OSSL_PARAM_int(OSSL_KDF_PARAM_MODE, NULL), \ |
69 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), \ |
70 | 0 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), \ |
71 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), \ |
72 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0) |
73 | | |
74 | | typedef struct { |
75 | | void *provctx; |
76 | | int mode; |
77 | | PROV_DIGEST digest; |
78 | | unsigned char *salt; |
79 | | size_t salt_len; |
80 | | unsigned char *key; |
81 | | size_t key_len; |
82 | | unsigned char *prefix; |
83 | | size_t prefix_len; |
84 | | unsigned char *label; |
85 | | size_t label_len; |
86 | | unsigned char *data; |
87 | | size_t data_len; |
88 | | unsigned char *info; |
89 | | size_t info_len; |
90 | | } KDF_HKDF; |
91 | | |
92 | | static void *kdf_hkdf_new(void *provctx) |
93 | 0 | { |
94 | 0 | KDF_HKDF *ctx; |
95 | |
|
96 | 0 | if (!ossl_prov_is_running()) |
97 | 0 | return NULL; |
98 | | |
99 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) |
100 | 0 | ctx->provctx = provctx; |
101 | 0 | return ctx; |
102 | 0 | } |
103 | | |
104 | | static void kdf_hkdf_free(void *vctx) |
105 | 0 | { |
106 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
107 | |
|
108 | 0 | if (ctx != NULL) { |
109 | 0 | kdf_hkdf_reset(ctx); |
110 | 0 | OPENSSL_free(ctx); |
111 | 0 | } |
112 | 0 | } |
113 | | |
114 | | static void kdf_hkdf_reset(void *vctx) |
115 | 0 | { |
116 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
117 | 0 | void *provctx = ctx->provctx; |
118 | |
|
119 | 0 | ossl_prov_digest_reset(&ctx->digest); |
120 | | #ifdef FIPS_MODULE |
121 | | OPENSSL_clear_free(ctx->salt, ctx->salt_len); |
122 | | #else |
123 | 0 | OPENSSL_free(ctx->salt); |
124 | 0 | #endif |
125 | 0 | OPENSSL_free(ctx->prefix); |
126 | 0 | OPENSSL_free(ctx->label); |
127 | 0 | OPENSSL_clear_free(ctx->data, ctx->data_len); |
128 | 0 | OPENSSL_clear_free(ctx->key, ctx->key_len); |
129 | 0 | OPENSSL_clear_free(ctx->info, ctx->info_len); |
130 | 0 | memset(ctx, 0, sizeof(*ctx)); |
131 | 0 | ctx->provctx = provctx; |
132 | 0 | } |
133 | | |
134 | | static void *kdf_hkdf_dup(void *vctx) |
135 | 0 | { |
136 | 0 | const KDF_HKDF *src = (const KDF_HKDF *)vctx; |
137 | 0 | KDF_HKDF *dest; |
138 | |
|
139 | 0 | dest = kdf_hkdf_new(src->provctx); |
140 | 0 | if (dest != NULL) { |
141 | 0 | if (!ossl_prov_memdup(src->salt, src->salt_len, &dest->salt, |
142 | 0 | &dest->salt_len) |
143 | 0 | || !ossl_prov_memdup(src->key, src->key_len, |
144 | 0 | &dest->key , &dest->key_len) |
145 | 0 | || !ossl_prov_memdup(src->prefix, src->prefix_len, |
146 | 0 | &dest->prefix, &dest->prefix_len) |
147 | 0 | || !ossl_prov_memdup(src->label, src->label_len, |
148 | 0 | &dest->label, &dest->label_len) |
149 | 0 | || !ossl_prov_memdup(src->data, src->data_len, |
150 | 0 | &dest->data, &dest->data_len) |
151 | 0 | || !ossl_prov_memdup(src->info, src->info_len, |
152 | 0 | &dest->info, &dest->info_len) |
153 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
154 | 0 | goto err; |
155 | 0 | dest->mode = src->mode; |
156 | 0 | } |
157 | 0 | return dest; |
158 | | |
159 | 0 | err: |
160 | 0 | kdf_hkdf_free(dest); |
161 | 0 | return NULL; |
162 | 0 | } |
163 | | |
164 | | static size_t kdf_hkdf_size(KDF_HKDF *ctx) |
165 | 0 | { |
166 | 0 | int sz; |
167 | 0 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
168 | |
|
169 | 0 | if (ctx->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY) |
170 | 0 | return SIZE_MAX; |
171 | | |
172 | 0 | if (md == NULL) { |
173 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
174 | 0 | return 0; |
175 | 0 | } |
176 | 0 | sz = EVP_MD_get_size(md); |
177 | 0 | if (sz < 0) |
178 | 0 | return 0; |
179 | | |
180 | 0 | return sz; |
181 | 0 | } |
182 | | |
183 | | static int kdf_hkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
184 | | const OSSL_PARAM params[]) |
185 | 0 | { |
186 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
187 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
188 | 0 | const EVP_MD *md; |
189 | |
|
190 | 0 | if (!ossl_prov_is_running() || !kdf_hkdf_set_ctx_params(ctx, params)) |
191 | 0 | return 0; |
192 | | |
193 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
194 | 0 | if (md == NULL) { |
195 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
196 | 0 | return 0; |
197 | 0 | } |
198 | 0 | if (ctx->key == NULL) { |
199 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
200 | 0 | return 0; |
201 | 0 | } |
202 | 0 | if (keylen == 0) { |
203 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
204 | 0 | return 0; |
205 | 0 | } |
206 | | |
207 | 0 | switch (ctx->mode) { |
208 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND: |
209 | 0 | default: |
210 | 0 | return HKDF(libctx, md, ctx->salt, ctx->salt_len, |
211 | 0 | ctx->key, ctx->key_len, ctx->info, ctx->info_len, key, keylen); |
212 | | |
213 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_ONLY: |
214 | 0 | return HKDF_Extract(libctx, md, ctx->salt, ctx->salt_len, |
215 | 0 | ctx->key, ctx->key_len, key, keylen); |
216 | | |
217 | 0 | case EVP_KDF_HKDF_MODE_EXPAND_ONLY: |
218 | 0 | return HKDF_Expand(md, ctx->key, ctx->key_len, ctx->info, |
219 | 0 | ctx->info_len, key, keylen); |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | | static int hkdf_common_set_ctx_params(KDF_HKDF *ctx, const OSSL_PARAM params[]) |
224 | 0 | { |
225 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
226 | 0 | const OSSL_PARAM *p; |
227 | 0 | int n; |
228 | |
|
229 | 0 | if (params == NULL) |
230 | 0 | return 1; |
231 | | |
232 | 0 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx)) |
233 | 0 | return 0; |
234 | | |
235 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MODE)) != NULL) { |
236 | 0 | if (p->data_type == OSSL_PARAM_UTF8_STRING) { |
237 | 0 | if (OPENSSL_strcasecmp(p->data, "EXTRACT_AND_EXPAND") == 0) { |
238 | 0 | ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND; |
239 | 0 | } else if (OPENSSL_strcasecmp(p->data, "EXTRACT_ONLY") == 0) { |
240 | 0 | ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY; |
241 | 0 | } else if (OPENSSL_strcasecmp(p->data, "EXPAND_ONLY") == 0) { |
242 | 0 | ctx->mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY; |
243 | 0 | } else { |
244 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
245 | 0 | return 0; |
246 | 0 | } |
247 | 0 | } else if (OSSL_PARAM_get_int(p, &n)) { |
248 | 0 | if (n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND |
249 | 0 | && n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY |
250 | 0 | && n != EVP_KDF_HKDF_MODE_EXPAND_ONLY) { |
251 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
252 | 0 | return 0; |
253 | 0 | } |
254 | 0 | ctx->mode = n; |
255 | 0 | } else { |
256 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
257 | 0 | return 0; |
258 | 0 | } |
259 | 0 | } |
260 | | |
261 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL) { |
262 | 0 | OPENSSL_clear_free(ctx->key, ctx->key_len); |
263 | 0 | ctx->key = NULL; |
264 | 0 | if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->key, 0, |
265 | 0 | &ctx->key_len)) |
266 | 0 | return 0; |
267 | 0 | } |
268 | | |
269 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) { |
270 | 0 | if (p->data_size != 0 && p->data != NULL) { |
271 | 0 | OPENSSL_free(ctx->salt); |
272 | 0 | ctx->salt = NULL; |
273 | 0 | if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->salt, 0, |
274 | 0 | &ctx->salt_len)) |
275 | 0 | return 0; |
276 | 0 | } |
277 | 0 | } |
278 | | |
279 | 0 | return 1; |
280 | 0 | } |
281 | | |
282 | | static int kdf_hkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
283 | 0 | { |
284 | 0 | KDF_HKDF *ctx = vctx; |
285 | |
|
286 | 0 | if (params == NULL) |
287 | 0 | return 1; |
288 | | |
289 | 0 | if (!hkdf_common_set_ctx_params(ctx, params)) |
290 | 0 | return 0; |
291 | | |
292 | 0 | if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO, |
293 | 0 | &ctx->info, &ctx->info_len, |
294 | 0 | HKDF_MAXINFO) == 0) |
295 | 0 | return 0; |
296 | | |
297 | 0 | return 1; |
298 | 0 | } |
299 | | |
300 | | static const OSSL_PARAM *kdf_hkdf_settable_ctx_params(ossl_unused void *ctx, |
301 | | ossl_unused void *provctx) |
302 | 0 | { |
303 | 0 | static const OSSL_PARAM known_settable_ctx_params[] = { |
304 | 0 | HKDF_COMMON_SETTABLES, |
305 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0), |
306 | 0 | OSSL_PARAM_END |
307 | 0 | }; |
308 | 0 | return known_settable_ctx_params; |
309 | 0 | } |
310 | | |
311 | | static int kdf_hkdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
312 | 0 | { |
313 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
314 | 0 | OSSL_PARAM *p; |
315 | |
|
316 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) { |
317 | 0 | size_t sz = kdf_hkdf_size(ctx); |
318 | |
|
319 | 0 | if (sz == 0) |
320 | 0 | return 0; |
321 | 0 | return OSSL_PARAM_set_size_t(p, sz); |
322 | 0 | } |
323 | 0 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_INFO)) != NULL) { |
324 | 0 | if (ctx->info == NULL || ctx->info_len == 0) { |
325 | 0 | p->return_size = 0; |
326 | 0 | return 1; |
327 | 0 | } |
328 | 0 | return OSSL_PARAM_set_octet_string(p, ctx->info, ctx->info_len); |
329 | 0 | } |
330 | 0 | return -2; |
331 | 0 | } |
332 | | |
333 | | static const OSSL_PARAM *kdf_hkdf_gettable_ctx_params(ossl_unused void *ctx, |
334 | | ossl_unused void *provctx) |
335 | 0 | { |
336 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
337 | 0 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), |
338 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0), |
339 | 0 | OSSL_PARAM_END |
340 | 0 | }; |
341 | 0 | return known_gettable_ctx_params; |
342 | 0 | } |
343 | | |
344 | | const OSSL_DISPATCH ossl_kdf_hkdf_functions[] = { |
345 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new }, |
346 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, |
347 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, |
348 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, |
349 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive }, |
350 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
351 | | (void(*)(void))kdf_hkdf_settable_ctx_params }, |
352 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_set_ctx_params }, |
353 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
354 | | (void(*)(void))kdf_hkdf_gettable_ctx_params }, |
355 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_hkdf_get_ctx_params }, |
356 | | OSSL_DISPATCH_END |
357 | | }; |
358 | | |
359 | | /* |
360 | | * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)" |
361 | | * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and |
362 | | * "Cryptographic Extraction and Key Derivation: The HKDF Scheme" |
363 | | * Section 4.2 (https://eprint.iacr.org/2010/264.pdf). |
364 | | * |
365 | | * From the paper: |
366 | | * The scheme HKDF is specified as: |
367 | | * HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t) |
368 | | * |
369 | | * where: |
370 | | * SKM is source key material |
371 | | * XTS is extractor salt (which may be null or constant) |
372 | | * CTXinfo is context information (may be null) |
373 | | * L is the number of key bits to be produced by KDF |
374 | | * k is the output length in bits of the hash function used with HMAC |
375 | | * t = ceil(L/k) |
376 | | * the value K(t) is truncated to its first d = L mod k bits. |
377 | | * |
378 | | * From RFC 5869: |
379 | | * 2.2. Step 1: Extract |
380 | | * HKDF-Extract(salt, IKM) -> PRK |
381 | | * 2.3. Step 2: Expand |
382 | | * HKDF-Expand(PRK, info, L) -> OKM |
383 | | */ |
384 | | static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
385 | | const unsigned char *salt, size_t salt_len, |
386 | | const unsigned char *ikm, size_t ikm_len, |
387 | | const unsigned char *info, size_t info_len, |
388 | | unsigned char *okm, size_t okm_len) |
389 | 0 | { |
390 | 0 | unsigned char prk[EVP_MAX_MD_SIZE]; |
391 | 0 | int ret, sz; |
392 | 0 | size_t prk_len; |
393 | |
|
394 | 0 | sz = EVP_MD_get_size(evp_md); |
395 | 0 | if (sz < 0) |
396 | 0 | return 0; |
397 | 0 | prk_len = (size_t)sz; |
398 | | |
399 | | /* Step 1: HKDF-Extract(salt, IKM) -> PRK */ |
400 | 0 | if (!HKDF_Extract(libctx, evp_md, |
401 | 0 | salt, salt_len, ikm, ikm_len, prk, prk_len)) |
402 | 0 | return 0; |
403 | | |
404 | | /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */ |
405 | 0 | ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len); |
406 | 0 | OPENSSL_cleanse(prk, sizeof(prk)); |
407 | |
|
408 | 0 | return ret; |
409 | 0 | } |
410 | | |
411 | | /* |
412 | | * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)" |
413 | | * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2). |
414 | | * |
415 | | * 2.2. Step 1: Extract |
416 | | * |
417 | | * HKDF-Extract(salt, IKM) -> PRK |
418 | | * |
419 | | * Options: |
420 | | * Hash a hash function; HashLen denotes the length of the |
421 | | * hash function output in octets |
422 | | * |
423 | | * Inputs: |
424 | | * salt optional salt value (a non-secret random value); |
425 | | * if not provided, it is set to a string of HashLen zeros. |
426 | | * IKM input keying material |
427 | | * |
428 | | * Output: |
429 | | * PRK a pseudorandom key (of HashLen octets) |
430 | | * |
431 | | * The output PRK is calculated as follows: |
432 | | * |
433 | | * PRK = HMAC-Hash(salt, IKM) |
434 | | */ |
435 | | static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
436 | | const unsigned char *salt, size_t salt_len, |
437 | | const unsigned char *ikm, size_t ikm_len, |
438 | | unsigned char *prk, size_t prk_len) |
439 | 0 | { |
440 | 0 | int sz = EVP_MD_get_size(evp_md); |
441 | |
|
442 | 0 | if (sz < 0) |
443 | 0 | return 0; |
444 | 0 | if (prk_len != (size_t)sz) { |
445 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE); |
446 | 0 | return 0; |
447 | 0 | } |
448 | | /* calc: PRK = HMAC-Hash(salt, IKM) */ |
449 | 0 | return |
450 | 0 | EVP_Q_mac(libctx, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL, salt, |
451 | 0 | salt_len, ikm, ikm_len, prk, EVP_MD_get_size(evp_md), NULL) |
452 | 0 | != NULL; |
453 | 0 | } |
454 | | |
455 | | /* |
456 | | * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)" |
457 | | * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3). |
458 | | * |
459 | | * 2.3. Step 2: Expand |
460 | | * |
461 | | * HKDF-Expand(PRK, info, L) -> OKM |
462 | | * |
463 | | * Options: |
464 | | * Hash a hash function; HashLen denotes the length of the |
465 | | * hash function output in octets |
466 | | * |
467 | | * Inputs: |
468 | | * PRK a pseudorandom key of at least HashLen octets |
469 | | * (usually, the output from the extract step) |
470 | | * info optional context and application specific information |
471 | | * (can be a zero-length string) |
472 | | * L length of output keying material in octets |
473 | | * (<= 255*HashLen) |
474 | | * |
475 | | * Output: |
476 | | * OKM output keying material (of L octets) |
477 | | * |
478 | | * The output OKM is calculated as follows: |
479 | | * |
480 | | * N = ceil(L/HashLen) |
481 | | * T = T(1) | T(2) | T(3) | ... | T(N) |
482 | | * OKM = first L octets of T |
483 | | * |
484 | | * where: |
485 | | * T(0) = empty string (zero length) |
486 | | * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01) |
487 | | * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02) |
488 | | * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03) |
489 | | * ... |
490 | | * |
491 | | * (where the constant concatenated to the end of each T(n) is a |
492 | | * single octet.) |
493 | | */ |
494 | | static int HKDF_Expand(const EVP_MD *evp_md, |
495 | | const unsigned char *prk, size_t prk_len, |
496 | | const unsigned char *info, size_t info_len, |
497 | | unsigned char *okm, size_t okm_len) |
498 | 0 | { |
499 | 0 | HMAC_CTX *hmac; |
500 | 0 | int ret = 0, sz; |
501 | 0 | unsigned int i; |
502 | 0 | unsigned char prev[EVP_MAX_MD_SIZE]; |
503 | 0 | size_t done_len = 0, dig_len, n; |
504 | |
|
505 | 0 | sz = EVP_MD_get_size(evp_md); |
506 | 0 | if (sz <= 0) |
507 | 0 | return 0; |
508 | 0 | dig_len = (size_t)sz; |
509 | | |
510 | | /* calc: N = ceil(L/HashLen) */ |
511 | 0 | n = okm_len / dig_len; |
512 | 0 | if (okm_len % dig_len) |
513 | 0 | n++; |
514 | |
|
515 | 0 | if (n > 255 || okm == NULL) |
516 | 0 | return 0; |
517 | | |
518 | 0 | if ((hmac = HMAC_CTX_new()) == NULL) |
519 | 0 | return 0; |
520 | | |
521 | 0 | if (!HMAC_Init_ex(hmac, prk, prk_len, evp_md, NULL)) |
522 | 0 | goto err; |
523 | | |
524 | 0 | for (i = 1; i <= n; i++) { |
525 | 0 | size_t copy_len; |
526 | 0 | const unsigned char ctr = i; |
527 | | |
528 | | /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */ |
529 | 0 | if (i > 1) { |
530 | 0 | if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL)) |
531 | 0 | goto err; |
532 | | |
533 | 0 | if (!HMAC_Update(hmac, prev, dig_len)) |
534 | 0 | goto err; |
535 | 0 | } |
536 | | |
537 | 0 | if (!HMAC_Update(hmac, info, info_len)) |
538 | 0 | goto err; |
539 | | |
540 | 0 | if (!HMAC_Update(hmac, &ctr, 1)) |
541 | 0 | goto err; |
542 | | |
543 | 0 | if (!HMAC_Final(hmac, prev, NULL)) |
544 | 0 | goto err; |
545 | | |
546 | 0 | copy_len = (dig_len > okm_len - done_len) ? |
547 | 0 | okm_len - done_len : |
548 | 0 | dig_len; |
549 | |
|
550 | 0 | memcpy(okm + done_len, prev, copy_len); |
551 | |
|
552 | 0 | done_len += copy_len; |
553 | 0 | } |
554 | 0 | ret = 1; |
555 | |
|
556 | 0 | err: |
557 | 0 | OPENSSL_cleanse(prev, sizeof(prev)); |
558 | 0 | HMAC_CTX_free(hmac); |
559 | 0 | return ret; |
560 | 0 | } |
561 | | |
562 | | /* |
563 | | * TLS uses slight variations of the above and for FIPS validation purposes, |
564 | | * they need to be present here. |
565 | | * Refer to RFC 8446 section 7 for specific details. |
566 | | */ |
567 | | |
568 | | /* |
569 | | * Given a |secret|; a |label| of length |labellen|; and |data| of length |
570 | | * |datalen| (e.g. typically a hash of the handshake messages), derive a new |
571 | | * secret |outlen| bytes long and store it in the location pointed to be |out|. |
572 | | * The |data| value may be zero length. Returns 1 on success and 0 on failure. |
573 | | */ |
574 | | static int prov_tls13_hkdf_expand(const EVP_MD *md, |
575 | | const unsigned char *key, size_t keylen, |
576 | | const unsigned char *prefix, size_t prefixlen, |
577 | | const unsigned char *label, size_t labellen, |
578 | | const unsigned char *data, size_t datalen, |
579 | | unsigned char *out, size_t outlen) |
580 | 0 | { |
581 | 0 | size_t hkdflabellen; |
582 | 0 | unsigned char hkdflabel[HKDF_MAXBUF]; |
583 | 0 | WPACKET pkt; |
584 | | |
585 | | /* |
586 | | * 2 bytes for length of derived secret + 1 byte for length of combined |
587 | | * prefix and label + bytes for the label itself + 1 byte length of hash |
588 | | * + bytes for the hash itself. We've got the maximum the KDF can handle |
589 | | * which should always be sufficient. |
590 | | */ |
591 | 0 | if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0) |
592 | 0 | || !WPACKET_put_bytes_u16(&pkt, outlen) |
593 | 0 | || !WPACKET_start_sub_packet_u8(&pkt) |
594 | 0 | || !WPACKET_memcpy(&pkt, prefix, prefixlen) |
595 | 0 | || !WPACKET_memcpy(&pkt, label, labellen) |
596 | 0 | || !WPACKET_close(&pkt) |
597 | 0 | || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen) |
598 | 0 | || !WPACKET_get_total_written(&pkt, &hkdflabellen) |
599 | 0 | || !WPACKET_finish(&pkt)) { |
600 | 0 | WPACKET_cleanup(&pkt); |
601 | 0 | return 0; |
602 | 0 | } |
603 | | |
604 | 0 | return HKDF_Expand(md, key, keylen, hkdflabel, hkdflabellen, |
605 | 0 | out, outlen); |
606 | 0 | } |
607 | | |
608 | | static int prov_tls13_hkdf_generate_secret(OSSL_LIB_CTX *libctx, |
609 | | const EVP_MD *md, |
610 | | const unsigned char *prevsecret, |
611 | | size_t prevsecretlen, |
612 | | const unsigned char *insecret, |
613 | | size_t insecretlen, |
614 | | const unsigned char *prefix, |
615 | | size_t prefixlen, |
616 | | const unsigned char *label, |
617 | | size_t labellen, |
618 | | unsigned char *out, size_t outlen) |
619 | 0 | { |
620 | 0 | size_t mdlen; |
621 | 0 | int ret; |
622 | 0 | unsigned char preextractsec[EVP_MAX_MD_SIZE]; |
623 | | /* Always filled with zeros */ |
624 | 0 | static const unsigned char default_zeros[EVP_MAX_MD_SIZE]; |
625 | |
|
626 | 0 | ret = EVP_MD_get_size(md); |
627 | | /* Ensure cast to size_t is safe */ |
628 | 0 | if (ret <= 0) |
629 | 0 | return 0; |
630 | 0 | mdlen = (size_t)ret; |
631 | |
|
632 | 0 | if (insecret == NULL) { |
633 | 0 | insecret = default_zeros; |
634 | 0 | insecretlen = mdlen; |
635 | 0 | } |
636 | 0 | if (prevsecret == NULL) { |
637 | 0 | prevsecret = default_zeros; |
638 | 0 | prevsecretlen = mdlen; |
639 | 0 | } else { |
640 | 0 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
641 | 0 | unsigned char hash[EVP_MAX_MD_SIZE]; |
642 | | |
643 | | /* The pre-extract derive step uses a hash of no messages */ |
644 | 0 | if (mctx == NULL |
645 | 0 | || EVP_DigestInit_ex(mctx, md, NULL) <= 0 |
646 | 0 | || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { |
647 | 0 | EVP_MD_CTX_free(mctx); |
648 | 0 | return 0; |
649 | 0 | } |
650 | 0 | EVP_MD_CTX_free(mctx); |
651 | | |
652 | | /* Generate the pre-extract secret */ |
653 | 0 | if (!prov_tls13_hkdf_expand(md, prevsecret, mdlen, |
654 | 0 | prefix, prefixlen, label, labellen, |
655 | 0 | hash, mdlen, preextractsec, mdlen)) |
656 | 0 | return 0; |
657 | 0 | prevsecret = preextractsec; |
658 | 0 | prevsecretlen = mdlen; |
659 | 0 | } |
660 | | |
661 | 0 | ret = HKDF_Extract(libctx, md, prevsecret, prevsecretlen, |
662 | 0 | insecret, insecretlen, out, outlen); |
663 | |
|
664 | 0 | if (prevsecret == preextractsec) |
665 | 0 | OPENSSL_cleanse(preextractsec, mdlen); |
666 | 0 | return ret; |
667 | 0 | } |
668 | | |
669 | | static int kdf_tls1_3_derive(void *vctx, unsigned char *key, size_t keylen, |
670 | | const OSSL_PARAM params[]) |
671 | 0 | { |
672 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
673 | 0 | const EVP_MD *md; |
674 | |
|
675 | 0 | if (!ossl_prov_is_running() || !kdf_tls1_3_set_ctx_params(ctx, params)) |
676 | 0 | return 0; |
677 | | |
678 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
679 | 0 | if (md == NULL) { |
680 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
681 | 0 | return 0; |
682 | 0 | } |
683 | | |
684 | 0 | switch (ctx->mode) { |
685 | 0 | default: |
686 | 0 | return 0; |
687 | | |
688 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_ONLY: |
689 | 0 | return prov_tls13_hkdf_generate_secret(PROV_LIBCTX_OF(ctx->provctx), |
690 | 0 | md, |
691 | 0 | ctx->salt, ctx->salt_len, |
692 | 0 | ctx->key, ctx->key_len, |
693 | 0 | ctx->prefix, ctx->prefix_len, |
694 | 0 | ctx->label, ctx->label_len, |
695 | 0 | key, keylen); |
696 | | |
697 | 0 | case EVP_KDF_HKDF_MODE_EXPAND_ONLY: |
698 | 0 | return prov_tls13_hkdf_expand(md, ctx->key, ctx->key_len, |
699 | 0 | ctx->prefix, ctx->prefix_len, |
700 | 0 | ctx->label, ctx->label_len, |
701 | 0 | ctx->data, ctx->data_len, |
702 | 0 | key, keylen); |
703 | 0 | } |
704 | 0 | } |
705 | | |
706 | | static int kdf_tls1_3_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
707 | 0 | { |
708 | 0 | const OSSL_PARAM *p; |
709 | 0 | KDF_HKDF *ctx = vctx; |
710 | |
|
711 | 0 | if (params == NULL) |
712 | 0 | return 1; |
713 | | |
714 | 0 | if (!hkdf_common_set_ctx_params(ctx, params)) |
715 | 0 | return 0; |
716 | | |
717 | 0 | if (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND) { |
718 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
719 | 0 | return 0; |
720 | 0 | } |
721 | | |
722 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_PREFIX)) != NULL) { |
723 | 0 | OPENSSL_free(ctx->prefix); |
724 | 0 | ctx->prefix = NULL; |
725 | 0 | if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->prefix, 0, |
726 | 0 | &ctx->prefix_len)) |
727 | 0 | return 0; |
728 | 0 | } |
729 | | |
730 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_LABEL)) != NULL) { |
731 | 0 | OPENSSL_free(ctx->label); |
732 | 0 | ctx->label = NULL; |
733 | 0 | if (!OSSL_PARAM_get_octet_string(p, (void **)&ctx->label, 0, |
734 | 0 | &ctx->label_len)) |
735 | 0 | return 0; |
736 | 0 | } |
737 | | |
738 | 0 | OPENSSL_clear_free(ctx->data, ctx->data_len); |
739 | 0 | ctx->data = NULL; |
740 | 0 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_DATA)) != NULL |
741 | 0 | && !OSSL_PARAM_get_octet_string(p, (void **)&ctx->data, 0, |
742 | 0 | &ctx->data_len)) |
743 | 0 | return 0; |
744 | 0 | return 1; |
745 | 0 | } |
746 | | |
747 | | static const OSSL_PARAM *kdf_tls1_3_settable_ctx_params(ossl_unused void *ctx, |
748 | | ossl_unused void *provctx) |
749 | 0 | { |
750 | 0 | static const OSSL_PARAM known_settable_ctx_params[] = { |
751 | 0 | HKDF_COMMON_SETTABLES, |
752 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PREFIX, NULL, 0), |
753 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_LABEL, NULL, 0), |
754 | 0 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_DATA, NULL, 0), |
755 | 0 | OSSL_PARAM_END |
756 | 0 | }; |
757 | 0 | return known_settable_ctx_params; |
758 | 0 | } |
759 | | |
760 | | const OSSL_DISPATCH ossl_kdf_tls1_3_kdf_functions[] = { |
761 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new }, |
762 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, |
763 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, |
764 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, |
765 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_3_derive }, |
766 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
767 | | (void(*)(void))kdf_tls1_3_settable_ctx_params }, |
768 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_tls1_3_set_ctx_params }, |
769 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
770 | | (void(*)(void))kdf_hkdf_gettable_ctx_params }, |
771 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))kdf_hkdf_get_ctx_params }, |
772 | | OSSL_DISPATCH_END |
773 | | }; |