/src/openssl/providers/implementations/kdfs/hkdf.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2016-2025 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 "prov/securitycheck.h" |
33 | | #include "internal/e_os.h" |
34 | | #include "internal/params.h" |
35 | | #include "internal/sizes.h" |
36 | | |
37 | | #define HKDF_MAXBUF 2048 |
38 | | #define HKDF_MAXINFO (32*1024) |
39 | 0 | #define HKDF_MAX_INFOS 5 |
40 | | |
41 | | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_new; |
42 | | static OSSL_FUNC_kdf_dupctx_fn kdf_hkdf_dup; |
43 | | static OSSL_FUNC_kdf_freectx_fn kdf_hkdf_free; |
44 | | static OSSL_FUNC_kdf_reset_fn kdf_hkdf_reset; |
45 | | static OSSL_FUNC_kdf_derive_fn kdf_hkdf_derive; |
46 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_hkdf_settable_ctx_params; |
47 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_hkdf_set_ctx_params; |
48 | | static OSSL_FUNC_kdf_gettable_ctx_params_fn hkdf_gettable_ctx_params; |
49 | | static OSSL_FUNC_kdf_get_ctx_params_fn hkdf_common_get_ctx_params; |
50 | | static OSSL_FUNC_kdf_derive_fn kdf_tls1_3_derive; |
51 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_tls1_3_settable_ctx_params; |
52 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_tls1_3_set_ctx_params; |
53 | | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_sha256_new; |
54 | | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_sha384_new; |
55 | | static OSSL_FUNC_kdf_newctx_fn kdf_hkdf_sha512_new; |
56 | | static OSSL_FUNC_kdf_settable_ctx_params_fn kdf_hkdf_fixed_digest_settable_ctx_params; |
57 | | static OSSL_FUNC_kdf_set_ctx_params_fn kdf_hkdf_fixed_digest_set_ctx_params; |
58 | | |
59 | | static void *kdf_hkdf_fixed_digest_new(void *provctx, const char *digest); |
60 | | static void kdf_hkdf_reset_ex(void *vctx, int on_free); |
61 | | |
62 | | static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
63 | | const unsigned char *salt, size_t salt_len, |
64 | | const unsigned char *key, size_t key_len, |
65 | | const unsigned char *info, size_t info_len, |
66 | | unsigned char *okm, size_t okm_len); |
67 | | static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
68 | | const unsigned char *salt, size_t salt_len, |
69 | | const unsigned char *ikm, size_t ikm_len, |
70 | | unsigned char *prk, size_t prk_len); |
71 | | static int HKDF_Expand(const EVP_MD *evp_md, |
72 | | const unsigned char *prk, size_t prk_len, |
73 | | const unsigned char *info, size_t info_len, |
74 | | unsigned char *okm, size_t okm_len); |
75 | | |
76 | | typedef struct { |
77 | | void *provctx; |
78 | | int mode; |
79 | | PROV_DIGEST digest; |
80 | | unsigned char *salt; |
81 | | size_t salt_len; |
82 | | unsigned char *key; |
83 | | size_t key_len; |
84 | | unsigned char *prefix; |
85 | | size_t prefix_len; |
86 | | unsigned char *label; |
87 | | size_t label_len; |
88 | | unsigned char *data; |
89 | | size_t data_len; |
90 | | unsigned char *info; |
91 | | size_t info_len; |
92 | | int fixed_digest; |
93 | | OSSL_FIPS_IND_DECLARE |
94 | | } KDF_HKDF; |
95 | | |
96 | | static void *kdf_hkdf_new(void *provctx) |
97 | 0 | { |
98 | 0 | KDF_HKDF *ctx; |
99 | |
|
100 | 0 | if (!ossl_prov_is_running()) |
101 | 0 | return NULL; |
102 | | |
103 | 0 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) { |
104 | 0 | ctx->provctx = provctx; |
105 | 0 | OSSL_FIPS_IND_INIT(ctx) |
106 | 0 | } |
107 | 0 | return ctx; |
108 | 0 | } |
109 | | |
110 | | static void kdf_hkdf_free(void *vctx) |
111 | 0 | { |
112 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
113 | |
|
114 | 0 | if (ctx != NULL) { |
115 | 0 | kdf_hkdf_reset_ex(vctx, 1); |
116 | 0 | OPENSSL_free(ctx); |
117 | 0 | } |
118 | 0 | } |
119 | | |
120 | | static void kdf_hkdf_reset(void *vctx) |
121 | 0 | { |
122 | 0 | kdf_hkdf_reset_ex(vctx, 0); |
123 | 0 | } |
124 | | |
125 | | static void kdf_hkdf_reset_ex(void *vctx, int on_free) |
126 | 0 | { |
127 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
128 | 0 | void *provctx = ctx->provctx; |
129 | 0 | int preserve_digest = on_free ? 0 : ctx->fixed_digest; |
130 | 0 | PROV_DIGEST save_prov_digest = { 0 }; |
131 | | |
132 | | /* For fixed digests just save and restore the PROV_DIGEST object */ |
133 | 0 | if (preserve_digest) |
134 | 0 | save_prov_digest = ctx->digest; |
135 | 0 | else |
136 | 0 | ossl_prov_digest_reset(&ctx->digest); |
137 | | #ifdef OPENSSL_PEDANTIC_ZEROIZATION |
138 | | OPENSSL_clear_free(ctx->salt, ctx->salt_len); |
139 | | #else |
140 | 0 | OPENSSL_free(ctx->salt); |
141 | 0 | #endif |
142 | 0 | OPENSSL_free(ctx->prefix); |
143 | 0 | OPENSSL_free(ctx->label); |
144 | 0 | OPENSSL_clear_free(ctx->data, ctx->data_len); |
145 | 0 | OPENSSL_clear_free(ctx->key, ctx->key_len); |
146 | 0 | OPENSSL_clear_free(ctx->info, ctx->info_len); |
147 | 0 | memset(ctx, 0, sizeof(*ctx)); |
148 | 0 | ctx->provctx = provctx; |
149 | 0 | if (preserve_digest) { |
150 | 0 | ctx->fixed_digest = preserve_digest; |
151 | 0 | ctx->digest = save_prov_digest; |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | | static void *kdf_hkdf_dup(void *vctx) |
156 | 0 | { |
157 | 0 | const KDF_HKDF *src = (const KDF_HKDF *)vctx; |
158 | 0 | KDF_HKDF *dest; |
159 | |
|
160 | 0 | dest = kdf_hkdf_new(src->provctx); |
161 | 0 | if (dest != NULL) { |
162 | 0 | if (!ossl_prov_memdup(src->salt, src->salt_len, &dest->salt, |
163 | 0 | &dest->salt_len) |
164 | 0 | || !ossl_prov_memdup(src->key, src->key_len, |
165 | 0 | &dest->key , &dest->key_len) |
166 | 0 | || !ossl_prov_memdup(src->prefix, src->prefix_len, |
167 | 0 | &dest->prefix, &dest->prefix_len) |
168 | 0 | || !ossl_prov_memdup(src->label, src->label_len, |
169 | 0 | &dest->label, &dest->label_len) |
170 | 0 | || !ossl_prov_memdup(src->data, src->data_len, |
171 | 0 | &dest->data, &dest->data_len) |
172 | 0 | || !ossl_prov_memdup(src->info, src->info_len, |
173 | 0 | &dest->info, &dest->info_len) |
174 | 0 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) |
175 | 0 | goto err; |
176 | 0 | dest->mode = src->mode; |
177 | 0 | dest->fixed_digest = src->fixed_digest; |
178 | 0 | OSSL_FIPS_IND_COPY(dest, src) |
179 | 0 | } |
180 | 0 | return dest; |
181 | | |
182 | 0 | err: |
183 | 0 | kdf_hkdf_free(dest); |
184 | 0 | return NULL; |
185 | 0 | } |
186 | | |
187 | | static size_t kdf_hkdf_size(KDF_HKDF *ctx) |
188 | 0 | { |
189 | 0 | int sz; |
190 | 0 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
191 | |
|
192 | 0 | if (ctx->mode != EVP_KDF_HKDF_MODE_EXTRACT_ONLY) |
193 | 0 | return SIZE_MAX; |
194 | | |
195 | 0 | if (md == NULL) { |
196 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
197 | 0 | return 0; |
198 | 0 | } |
199 | 0 | sz = EVP_MD_get_size(md); |
200 | 0 | if (sz <= 0) |
201 | 0 | return 0; |
202 | | |
203 | 0 | return sz; |
204 | 0 | } |
205 | | |
206 | | #ifdef FIPS_MODULE |
207 | | static int fips_hkdf_key_check_passed(KDF_HKDF *ctx) |
208 | | { |
209 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
210 | | int key_approved = ossl_kdf_check_key_size(ctx->key_len); |
211 | | |
212 | | if (!key_approved) { |
213 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
214 | | libctx, "HKDF", "Key size", |
215 | | ossl_fips_config_hkdf_key_check)) { |
216 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
217 | | return 0; |
218 | | } |
219 | | } |
220 | | return 1; |
221 | | } |
222 | | #endif |
223 | | |
224 | | static int kdf_hkdf_derive(void *vctx, unsigned char *key, size_t keylen, |
225 | | const OSSL_PARAM params[]) |
226 | 0 | { |
227 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
228 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
229 | 0 | const EVP_MD *md; |
230 | |
|
231 | 0 | if (!ossl_prov_is_running() || !kdf_hkdf_set_ctx_params(ctx, params)) |
232 | 0 | return 0; |
233 | | |
234 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
235 | 0 | if (md == NULL) { |
236 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
237 | 0 | return 0; |
238 | 0 | } |
239 | 0 | if (ctx->key == NULL) { |
240 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_KEY); |
241 | 0 | return 0; |
242 | 0 | } |
243 | 0 | if (keylen == 0) { |
244 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
245 | 0 | return 0; |
246 | 0 | } |
247 | | |
248 | 0 | switch (ctx->mode) { |
249 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND: |
250 | 0 | default: |
251 | 0 | return HKDF(libctx, md, ctx->salt, ctx->salt_len, |
252 | 0 | ctx->key, ctx->key_len, ctx->info, ctx->info_len, key, keylen); |
253 | | |
254 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_ONLY: |
255 | 0 | return HKDF_Extract(libctx, md, ctx->salt, ctx->salt_len, |
256 | 0 | ctx->key, ctx->key_len, key, keylen); |
257 | | |
258 | 0 | case EVP_KDF_HKDF_MODE_EXPAND_ONLY: |
259 | 0 | return HKDF_Expand(md, ctx->key, ctx->key_len, ctx->info, |
260 | 0 | ctx->info_len, key, keylen); |
261 | 0 | } |
262 | 0 | } |
263 | | |
264 | | struct hkdf_all_set_ctx_params_st { |
265 | | OSSL_PARAM *mode; |
266 | | OSSL_PARAM *propq; |
267 | | OSSL_PARAM *digest; |
268 | | OSSL_PARAM *key; |
269 | | OSSL_PARAM *salt; |
270 | | #ifdef FIPS_MODULE |
271 | | OSSL_PARAM *ind_k; |
272 | | OSSL_PARAM *ind_d; |
273 | | #endif |
274 | | OSSL_PARAM *prefix; |
275 | | OSSL_PARAM *label; |
276 | | OSSL_PARAM *data; |
277 | | OSSL_PARAM *info[HKDF_MAX_INFOS]; |
278 | | int num_info; |
279 | | }; |
280 | | |
281 | | #define hkdf_set_ctx_params_st hkdf_all_set_ctx_params_st |
282 | | #define hkdf_fixed_digest_set_ctx_params_st hkdf_all_set_ctx_params_st |
283 | | #define kdf_tls1_3_set_ctx_params_st hkdf_all_set_ctx_params_st |
284 | | |
285 | | #include "providers/implementations/kdfs/hkdf.inc" |
286 | | |
287 | | static int hkdf_common_set_ctx_params |
288 | | (KDF_HKDF *ctx, struct hkdf_all_set_ctx_params_st *p) |
289 | 0 | { |
290 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
291 | 0 | int n; |
292 | |
|
293 | 0 | if (p->digest != NULL) { |
294 | 0 | const EVP_MD *md = NULL; |
295 | |
|
296 | 0 | if (!ossl_prov_digest_load(&ctx->digest, p->digest, p->propq, libctx)) |
297 | 0 | return 0; |
298 | | |
299 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
300 | 0 | if (EVP_MD_xof(md)) { |
301 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED); |
302 | 0 | return 0; |
303 | 0 | } |
304 | 0 | } |
305 | | |
306 | 0 | if (p->mode != NULL) { |
307 | 0 | if (p->mode->data_type == OSSL_PARAM_UTF8_STRING) { |
308 | 0 | if (OPENSSL_strcasecmp(p->mode->data, "EXTRACT_AND_EXPAND") == 0) { |
309 | 0 | ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND; |
310 | 0 | } else if (OPENSSL_strcasecmp(p->mode->data, "EXTRACT_ONLY") == 0) { |
311 | 0 | ctx->mode = EVP_KDF_HKDF_MODE_EXTRACT_ONLY; |
312 | 0 | } else if (OPENSSL_strcasecmp(p->mode->data, "EXPAND_ONLY") == 0) { |
313 | 0 | ctx->mode = EVP_KDF_HKDF_MODE_EXPAND_ONLY; |
314 | 0 | } else { |
315 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
316 | 0 | return 0; |
317 | 0 | } |
318 | 0 | } else if (OSSL_PARAM_get_int(p->mode, &n)) { |
319 | 0 | if (n != EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND |
320 | 0 | && n != EVP_KDF_HKDF_MODE_EXTRACT_ONLY |
321 | 0 | && n != EVP_KDF_HKDF_MODE_EXPAND_ONLY) { |
322 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
323 | 0 | return 0; |
324 | 0 | } |
325 | 0 | ctx->mode = n; |
326 | 0 | } else { |
327 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
328 | 0 | return 0; |
329 | 0 | } |
330 | 0 | } |
331 | | |
332 | 0 | if (p->key != NULL) { |
333 | 0 | OPENSSL_clear_free(ctx->key, ctx->key_len); |
334 | 0 | ctx->key = NULL; |
335 | 0 | if (!OSSL_PARAM_get_octet_string(p->key, (void **)&ctx->key, 0, |
336 | 0 | &ctx->key_len)) |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | 0 | if (p->salt != NULL) { |
341 | 0 | OPENSSL_free(ctx->salt); |
342 | 0 | ctx->salt = NULL; |
343 | 0 | if (!OSSL_PARAM_get_octet_string(p->salt, (void **)&ctx->salt, 0, |
344 | 0 | &ctx->salt_len)) |
345 | 0 | return 0; |
346 | 0 | } |
347 | | |
348 | | /* Only relevant for HKDF not to the TLS 1.3 KDF */ |
349 | 0 | if (ossl_param_get1_concat_octet_string(p->num_info, p->info, |
350 | 0 | &ctx->info, &ctx->info_len) == 0) |
351 | 0 | return 0; |
352 | | |
353 | 0 | return 1; |
354 | 0 | } |
355 | | |
356 | | static int kdf_hkdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
357 | 0 | { |
358 | 0 | struct hkdf_all_set_ctx_params_st p; |
359 | 0 | KDF_HKDF *ctx = vctx; |
360 | |
|
361 | 0 | if (ctx == NULL || !hkdf_set_ctx_params_decoder(params, &p)) |
362 | 0 | return 0; |
363 | | |
364 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
365 | 0 | return 0; |
366 | | |
367 | 0 | if (!hkdf_common_set_ctx_params(ctx, &p)) |
368 | 0 | return 0; |
369 | | |
370 | | #ifdef FIPS_MODULE |
371 | | if (p.key != NULL) |
372 | | if (!fips_hkdf_key_check_passed(ctx)) |
373 | | return 0; |
374 | | #endif |
375 | | |
376 | 0 | return 1; |
377 | 0 | } |
378 | | |
379 | | static const OSSL_PARAM *kdf_hkdf_settable_ctx_params(ossl_unused void *ctx, |
380 | | ossl_unused void *provctx) |
381 | 0 | { |
382 | 0 | return hkdf_set_ctx_params_list; |
383 | 0 | } |
384 | | |
385 | | static const OSSL_PARAM *hkdf_gettable_ctx_params(ossl_unused void *ctx, |
386 | | ossl_unused void *provctx) |
387 | 0 | { |
388 | 0 | return hkdf_get_ctx_params_list; |
389 | 0 | } |
390 | | |
391 | | static int hkdf_common_get_ctx_params(void *vctx, OSSL_PARAM params[]) |
392 | 0 | { |
393 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
394 | 0 | struct hkdf_get_ctx_params_st p; |
395 | |
|
396 | 0 | if (ctx == NULL || !hkdf_get_ctx_params_decoder(params, &p)) |
397 | 0 | return 0; |
398 | | |
399 | 0 | if (p.size != NULL) { |
400 | 0 | size_t sz = kdf_hkdf_size(ctx); |
401 | |
|
402 | 0 | if (sz == 0) |
403 | 0 | return 0; |
404 | 0 | if (!OSSL_PARAM_set_size_t(p.size, sz)) |
405 | 0 | return 0; |
406 | 0 | } |
407 | | |
408 | 0 | if (p.digest != NULL) { |
409 | 0 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
410 | |
|
411 | 0 | if (md == NULL) |
412 | 0 | return 0; |
413 | 0 | else if (!OSSL_PARAM_set_utf8_string(p.digest, EVP_MD_get0_name(md))) |
414 | 0 | return 0; |
415 | 0 | } |
416 | | |
417 | | /* OSSL_KDF_PARAM_MODE has multiple parameter types, so look for all instances */ |
418 | 0 | if (p.mode != NULL) { |
419 | 0 | if (p.mode->data_type == OSSL_PARAM_UTF8_STRING) { |
420 | 0 | switch (ctx->mode) { |
421 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND: |
422 | 0 | if (!OSSL_PARAM_set_utf8_string(p.mode, "EXTRACT_AND_EXPAND")) |
423 | 0 | return 0; |
424 | 0 | break; |
425 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_ONLY: |
426 | 0 | if (!OSSL_PARAM_set_utf8_string(p.mode, "EXTRACT_ONLY")) |
427 | 0 | return 0; |
428 | 0 | break; |
429 | 0 | case EVP_KDF_HKDF_MODE_EXPAND_ONLY: |
430 | 0 | if (!OSSL_PARAM_set_utf8_string(p.mode, "EXPAND_ONLY")) |
431 | 0 | return 0; |
432 | 0 | break; |
433 | 0 | default: |
434 | 0 | return 0; |
435 | 0 | } |
436 | 0 | } else { |
437 | 0 | if (!OSSL_PARAM_set_int(p.mode, ctx->mode)) |
438 | 0 | return 0; |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | 0 | if (p.salt != NULL) { |
443 | 0 | if (ctx->salt == NULL || ctx->salt_len == 0) |
444 | 0 | p.salt->return_size = 0; |
445 | 0 | else if (!OSSL_PARAM_set_octet_string(p.salt, ctx->salt, ctx->salt_len)) |
446 | 0 | return 0; |
447 | 0 | } |
448 | | |
449 | 0 | if (p.info != NULL) { |
450 | 0 | if (ctx->info == NULL || ctx->info_len == 0) |
451 | 0 | p.info->return_size = 0; |
452 | 0 | else if (!OSSL_PARAM_set_octet_string(p.info, ctx->info, ctx->info_len)) |
453 | 0 | return 0; |
454 | 0 | } |
455 | | |
456 | 0 | if (!OSSL_FIPS_IND_GET_CTX_FROM_PARAM(ctx, p.ind)) |
457 | 0 | return 0; |
458 | | |
459 | 0 | return 1; |
460 | 0 | } |
461 | | |
462 | | const OSSL_DISPATCH ossl_kdf_hkdf_functions[] = { |
463 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new }, |
464 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, |
465 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, |
466 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, |
467 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive }, |
468 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
469 | | (void(*)(void))kdf_hkdf_settable_ctx_params }, |
470 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_set_ctx_params }, |
471 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
472 | | (void(*)(void))hkdf_gettable_ctx_params }, |
473 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))hkdf_common_get_ctx_params }, |
474 | | OSSL_DISPATCH_END |
475 | | }; |
476 | | |
477 | | static void *kdf_hkdf_fixed_digest_new(void *provctx, const char *digest) |
478 | 0 | { |
479 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
480 | 0 | KDF_HKDF *ctx; |
481 | 0 | OSSL_PARAM param; |
482 | |
|
483 | 0 | ctx = kdf_hkdf_new(provctx); |
484 | 0 | if (ctx == NULL) |
485 | 0 | return NULL; |
486 | | |
487 | 0 | param = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, |
488 | 0 | (char *)digest, 0); |
489 | 0 | if (!ossl_prov_digest_load(&ctx->digest, ¶m, NULL, libctx)) { |
490 | 0 | kdf_hkdf_free(ctx); |
491 | 0 | return NULL; |
492 | 0 | } |
493 | | |
494 | | /* Now the digest can no longer be changed */ |
495 | 0 | ctx->fixed_digest = 1; |
496 | |
|
497 | 0 | return ctx; |
498 | 0 | } |
499 | | |
500 | | static int kdf_hkdf_fixed_digest_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
501 | 0 | { |
502 | 0 | struct hkdf_all_set_ctx_params_st p; |
503 | 0 | KDF_HKDF *ctx = vctx; |
504 | |
|
505 | 0 | if (ctx == NULL || !hkdf_fixed_digest_set_ctx_params_decoder(params, &p)) |
506 | 0 | return 0; |
507 | | |
508 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_k)) |
509 | 0 | return 0; |
510 | | |
511 | 0 | if (!hkdf_common_set_ctx_params(ctx, &p)) |
512 | 0 | return 0; |
513 | | |
514 | | #ifdef FIPS_MODULE |
515 | | if (p.key != NULL) |
516 | | if (!fips_hkdf_key_check_passed(ctx)) |
517 | | return 0; |
518 | | #endif |
519 | | |
520 | 0 | return 1; |
521 | 0 | } |
522 | | |
523 | | static const OSSL_PARAM *kdf_hkdf_fixed_digest_settable_ctx_params |
524 | | (ossl_unused void *ctx, ossl_unused void *provctx) |
525 | 0 | { |
526 | 0 | return hkdf_fixed_digest_set_ctx_params_list; |
527 | 0 | } |
528 | | |
529 | | #define KDF_HKDF_FIXED_DIGEST_NEW(hashname, hashstring) \ |
530 | | static void *kdf_hkdf_##hashname##_new(void *provctx) \ |
531 | 0 | { \ |
532 | 0 | return kdf_hkdf_fixed_digest_new(provctx, hashstring); \ |
533 | 0 | } Unexecuted instantiation: hkdf.c:kdf_hkdf_sha256_new Unexecuted instantiation: hkdf.c:kdf_hkdf_sha384_new Unexecuted instantiation: hkdf.c:kdf_hkdf_sha512_new |
534 | | |
535 | | KDF_HKDF_FIXED_DIGEST_NEW(sha256, "SHA256") |
536 | | KDF_HKDF_FIXED_DIGEST_NEW(sha384, "SHA384") |
537 | | KDF_HKDF_FIXED_DIGEST_NEW(sha512, "SHA512") |
538 | | |
539 | | #define MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(hashname) \ |
540 | | const OSSL_DISPATCH ossl_kdf_hkdf_##hashname##_functions[] = { \ |
541 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_##hashname##_new }, \ |
542 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, \ |
543 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, \ |
544 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, \ |
545 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_hkdf_derive }, \ |
546 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, (void(*)(void))kdf_hkdf_fixed_digest_settable_ctx_params }, \ |
547 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_hkdf_fixed_digest_set_ctx_params }, \ |
548 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, (void(*)(void))hkdf_gettable_ctx_params }, \ |
549 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))hkdf_common_get_ctx_params }, \ |
550 | | OSSL_DISPATCH_END \ |
551 | | }; |
552 | | |
553 | | MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(sha256) |
554 | | MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(sha384) |
555 | | MAKE_KDF_HKDF_FIXED_DIGEST_FUNCTIONS(sha512) |
556 | | |
557 | | /* |
558 | | * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)" |
559 | | * Section 2 (https://tools.ietf.org/html/rfc5869#section-2) and |
560 | | * "Cryptographic Extraction and Key Derivation: The HKDF Scheme" |
561 | | * Section 4.2 (https://eprint.iacr.org/2010/264.pdf). |
562 | | * |
563 | | * From the paper: |
564 | | * The scheme HKDF is specified as: |
565 | | * HKDF(XTS, SKM, CTXinfo, L) = K(1) | K(2) | ... | K(t) |
566 | | * |
567 | | * where: |
568 | | * SKM is source key material |
569 | | * XTS is extractor salt (which may be null or constant) |
570 | | * CTXinfo is context information (may be null) |
571 | | * L is the number of key bits to be produced by KDF |
572 | | * k is the output length in bits of the hash function used with HMAC |
573 | | * t = ceil(L/k) |
574 | | * the value K(t) is truncated to its first d = L mod k bits. |
575 | | * |
576 | | * From RFC 5869: |
577 | | * 2.2. Step 1: Extract |
578 | | * HKDF-Extract(salt, IKM) -> PRK |
579 | | * 2.3. Step 2: Expand |
580 | | * HKDF-Expand(PRK, info, L) -> OKM |
581 | | */ |
582 | | static int HKDF(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
583 | | const unsigned char *salt, size_t salt_len, |
584 | | const unsigned char *ikm, size_t ikm_len, |
585 | | const unsigned char *info, size_t info_len, |
586 | | unsigned char *okm, size_t okm_len) |
587 | 0 | { |
588 | 0 | unsigned char prk[EVP_MAX_MD_SIZE]; |
589 | 0 | int ret, sz; |
590 | 0 | size_t prk_len; |
591 | |
|
592 | 0 | sz = EVP_MD_get_size(evp_md); |
593 | 0 | if (sz <= 0) |
594 | 0 | return 0; |
595 | 0 | prk_len = (size_t)sz; |
596 | | |
597 | | /* Step 1: HKDF-Extract(salt, IKM) -> PRK */ |
598 | 0 | if (!HKDF_Extract(libctx, evp_md, |
599 | 0 | salt, salt_len, ikm, ikm_len, prk, prk_len)) |
600 | 0 | return 0; |
601 | | |
602 | | /* Step 2: HKDF-Expand(PRK, info, L) -> OKM */ |
603 | 0 | ret = HKDF_Expand(evp_md, prk, prk_len, info, info_len, okm, okm_len); |
604 | 0 | OPENSSL_cleanse(prk, sizeof(prk)); |
605 | |
|
606 | 0 | return ret; |
607 | 0 | } |
608 | | |
609 | | /* |
610 | | * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)" |
611 | | * Section 2.2 (https://tools.ietf.org/html/rfc5869#section-2.2). |
612 | | * |
613 | | * 2.2. Step 1: Extract |
614 | | * |
615 | | * HKDF-Extract(salt, IKM) -> PRK |
616 | | * |
617 | | * Options: |
618 | | * Hash a hash function; HashLen denotes the length of the |
619 | | * hash function output in octets |
620 | | * |
621 | | * Inputs: |
622 | | * salt optional salt value (a non-secret random value); |
623 | | * if not provided, it is set to a string of HashLen zeros. |
624 | | * IKM input keying material |
625 | | * |
626 | | * Output: |
627 | | * PRK a pseudorandom key (of HashLen octets) |
628 | | * |
629 | | * The output PRK is calculated as follows: |
630 | | * |
631 | | * PRK = HMAC-Hash(salt, IKM) |
632 | | */ |
633 | | static int HKDF_Extract(OSSL_LIB_CTX *libctx, const EVP_MD *evp_md, |
634 | | const unsigned char *salt, size_t salt_len, |
635 | | const unsigned char *ikm, size_t ikm_len, |
636 | | unsigned char *prk, size_t prk_len) |
637 | 0 | { |
638 | 0 | int sz = EVP_MD_get_size(evp_md); |
639 | |
|
640 | 0 | if (sz <= 0) |
641 | 0 | return 0; |
642 | 0 | if (prk_len != (size_t)sz) { |
643 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_WRONG_OUTPUT_BUFFER_SIZE); |
644 | 0 | return 0; |
645 | 0 | } |
646 | | /* calc: PRK = HMAC-Hash(salt, IKM) */ |
647 | 0 | return |
648 | 0 | EVP_Q_mac(libctx, "HMAC", NULL, EVP_MD_get0_name(evp_md), NULL, salt, |
649 | 0 | salt_len, ikm, ikm_len, prk, EVP_MD_get_size(evp_md), NULL) |
650 | 0 | != NULL; |
651 | 0 | } |
652 | | |
653 | | /* |
654 | | * Refer to "HMAC-based Extract-and-Expand Key Derivation Function (HKDF)" |
655 | | * Section 2.3 (https://tools.ietf.org/html/rfc5869#section-2.3). |
656 | | * |
657 | | * 2.3. Step 2: Expand |
658 | | * |
659 | | * HKDF-Expand(PRK, info, L) -> OKM |
660 | | * |
661 | | * Options: |
662 | | * Hash a hash function; HashLen denotes the length of the |
663 | | * hash function output in octets |
664 | | * |
665 | | * Inputs: |
666 | | * PRK a pseudorandom key of at least HashLen octets |
667 | | * (usually, the output from the extract step) |
668 | | * info optional context and application specific information |
669 | | * (can be a zero-length string) |
670 | | * L length of output keying material in octets |
671 | | * (<= 255*HashLen) |
672 | | * |
673 | | * Output: |
674 | | * OKM output keying material (of L octets) |
675 | | * |
676 | | * The output OKM is calculated as follows: |
677 | | * |
678 | | * N = ceil(L/HashLen) |
679 | | * T = T(1) | T(2) | T(3) | ... | T(N) |
680 | | * OKM = first L octets of T |
681 | | * |
682 | | * where: |
683 | | * T(0) = empty string (zero length) |
684 | | * T(1) = HMAC-Hash(PRK, T(0) | info | 0x01) |
685 | | * T(2) = HMAC-Hash(PRK, T(1) | info | 0x02) |
686 | | * T(3) = HMAC-Hash(PRK, T(2) | info | 0x03) |
687 | | * ... |
688 | | * |
689 | | * (where the constant concatenated to the end of each T(n) is a |
690 | | * single octet.) |
691 | | */ |
692 | | static int HKDF_Expand(const EVP_MD *evp_md, |
693 | | const unsigned char *prk, size_t prk_len, |
694 | | const unsigned char *info, size_t info_len, |
695 | | unsigned char *okm, size_t okm_len) |
696 | 0 | { |
697 | 0 | HMAC_CTX *hmac; |
698 | 0 | int ret = 0, sz; |
699 | 0 | unsigned int i; |
700 | 0 | unsigned char prev[EVP_MAX_MD_SIZE]; |
701 | 0 | size_t done_len = 0, dig_len, n; |
702 | |
|
703 | 0 | sz = EVP_MD_get_size(evp_md); |
704 | 0 | if (sz <= 0) |
705 | 0 | return 0; |
706 | 0 | dig_len = (size_t)sz; |
707 | | |
708 | | /* calc: N = ceil(L/HashLen) */ |
709 | 0 | n = okm_len / dig_len; |
710 | 0 | if (okm_len % dig_len) |
711 | 0 | n++; |
712 | |
|
713 | 0 | if (n > 255 || okm == NULL) |
714 | 0 | return 0; |
715 | | |
716 | 0 | if ((hmac = HMAC_CTX_new()) == NULL) |
717 | 0 | return 0; |
718 | | |
719 | 0 | if (!HMAC_Init_ex(hmac, prk, (int)prk_len, evp_md, NULL)) |
720 | 0 | goto err; |
721 | | |
722 | 0 | for (i = 1; i <= n; i++) { |
723 | 0 | size_t copy_len; |
724 | 0 | const unsigned char ctr = i; |
725 | | |
726 | | /* calc: T(i) = HMAC-Hash(PRK, T(i - 1) | info | i) */ |
727 | 0 | if (i > 1) { |
728 | 0 | if (!HMAC_Init_ex(hmac, NULL, 0, NULL, NULL)) |
729 | 0 | goto err; |
730 | | |
731 | 0 | if (!HMAC_Update(hmac, prev, dig_len)) |
732 | 0 | goto err; |
733 | 0 | } |
734 | | |
735 | 0 | if (!HMAC_Update(hmac, info, info_len)) |
736 | 0 | goto err; |
737 | | |
738 | 0 | if (!HMAC_Update(hmac, &ctr, 1)) |
739 | 0 | goto err; |
740 | | |
741 | 0 | if (!HMAC_Final(hmac, prev, NULL)) |
742 | 0 | goto err; |
743 | | |
744 | 0 | copy_len = (dig_len > okm_len - done_len) ? |
745 | 0 | okm_len - done_len : |
746 | 0 | dig_len; |
747 | |
|
748 | 0 | memcpy(okm + done_len, prev, copy_len); |
749 | |
|
750 | 0 | done_len += copy_len; |
751 | 0 | } |
752 | 0 | ret = 1; |
753 | |
|
754 | 0 | err: |
755 | 0 | OPENSSL_cleanse(prev, sizeof(prev)); |
756 | 0 | HMAC_CTX_free(hmac); |
757 | 0 | return ret; |
758 | 0 | } |
759 | | |
760 | | /* |
761 | | * TLS uses slight variations of the above and for FIPS validation purposes, |
762 | | * they need to be present here. |
763 | | * Refer to RFC 8446 section 7 for specific details. |
764 | | */ |
765 | | |
766 | | /* |
767 | | * Given a |secret|; a |label| of length |labellen|; and |data| of length |
768 | | * |datalen| (e.g. typically a hash of the handshake messages), derive a new |
769 | | * secret |outlen| bytes long and store it in the location pointed to be |out|. |
770 | | * The |data| value may be zero length. Returns 1 on success and 0 on failure. |
771 | | */ |
772 | | static int prov_tls13_hkdf_expand(const EVP_MD *md, |
773 | | const unsigned char *key, size_t keylen, |
774 | | const unsigned char *prefix, size_t prefixlen, |
775 | | const unsigned char *label, size_t labellen, |
776 | | const unsigned char *data, size_t datalen, |
777 | | unsigned char *out, size_t outlen) |
778 | 0 | { |
779 | 0 | size_t hkdflabellen; |
780 | 0 | unsigned char hkdflabel[HKDF_MAXBUF]; |
781 | 0 | WPACKET pkt; |
782 | | |
783 | | /* |
784 | | * 2 bytes for length of derived secret + 1 byte for length of combined |
785 | | * prefix and label + bytes for the label itself + 1 byte length of hash |
786 | | * + bytes for the hash itself. We've got the maximum the KDF can handle |
787 | | * which should always be sufficient. |
788 | | */ |
789 | 0 | if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0) |
790 | 0 | || !WPACKET_put_bytes_u16(&pkt, outlen) |
791 | 0 | || !WPACKET_start_sub_packet_u8(&pkt) |
792 | 0 | || !WPACKET_memcpy(&pkt, prefix, prefixlen) |
793 | 0 | || !WPACKET_memcpy(&pkt, label, labellen) |
794 | 0 | || !WPACKET_close(&pkt) |
795 | 0 | || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen) |
796 | 0 | || !WPACKET_get_total_written(&pkt, &hkdflabellen) |
797 | 0 | || !WPACKET_finish(&pkt)) { |
798 | 0 | WPACKET_cleanup(&pkt); |
799 | 0 | return 0; |
800 | 0 | } |
801 | | |
802 | 0 | return HKDF_Expand(md, key, keylen, hkdflabel, hkdflabellen, |
803 | 0 | out, outlen); |
804 | 0 | } |
805 | | |
806 | | static int prov_tls13_hkdf_generate_secret(OSSL_LIB_CTX *libctx, |
807 | | const EVP_MD *md, |
808 | | const unsigned char *prevsecret, |
809 | | size_t prevsecretlen, |
810 | | const unsigned char *insecret, |
811 | | size_t insecretlen, |
812 | | const unsigned char *prefix, |
813 | | size_t prefixlen, |
814 | | const unsigned char *label, |
815 | | size_t labellen, |
816 | | unsigned char *out, size_t outlen) |
817 | 0 | { |
818 | 0 | size_t mdlen; |
819 | 0 | int ret; |
820 | 0 | unsigned char preextractsec[EVP_MAX_MD_SIZE]; |
821 | | /* Always filled with zeros */ |
822 | 0 | static const unsigned char default_zeros[EVP_MAX_MD_SIZE]; |
823 | |
|
824 | 0 | ret = EVP_MD_get_size(md); |
825 | | /* Ensure cast to size_t is safe */ |
826 | 0 | if (ret <= 0) |
827 | 0 | return 0; |
828 | 0 | mdlen = (size_t)ret; |
829 | |
|
830 | 0 | if (insecret == NULL) { |
831 | 0 | insecret = default_zeros; |
832 | 0 | insecretlen = mdlen; |
833 | 0 | } |
834 | 0 | if (prevsecret == NULL) { |
835 | 0 | prevsecret = default_zeros; |
836 | 0 | prevsecretlen = mdlen; |
837 | 0 | } else { |
838 | 0 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
839 | 0 | unsigned char hash[EVP_MAX_MD_SIZE]; |
840 | | |
841 | | /* The pre-extract derive step uses a hash of no messages */ |
842 | 0 | if (mctx == NULL |
843 | 0 | || EVP_DigestInit_ex(mctx, md, NULL) <= 0 |
844 | 0 | || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { |
845 | 0 | EVP_MD_CTX_free(mctx); |
846 | 0 | return 0; |
847 | 0 | } |
848 | 0 | EVP_MD_CTX_free(mctx); |
849 | | |
850 | | /* Generate the pre-extract secret */ |
851 | 0 | if (!prov_tls13_hkdf_expand(md, prevsecret, prevsecretlen, |
852 | 0 | prefix, prefixlen, label, labellen, |
853 | 0 | hash, mdlen, preextractsec, mdlen)) |
854 | 0 | return 0; |
855 | 0 | prevsecret = preextractsec; |
856 | 0 | prevsecretlen = mdlen; |
857 | 0 | } |
858 | | |
859 | 0 | ret = HKDF_Extract(libctx, md, prevsecret, prevsecretlen, |
860 | 0 | insecret, insecretlen, out, outlen); |
861 | |
|
862 | 0 | if (prevsecret == preextractsec) |
863 | 0 | OPENSSL_cleanse(preextractsec, mdlen); |
864 | 0 | return ret; |
865 | 0 | } |
866 | | |
867 | | #ifdef FIPS_MODULE |
868 | | static int fips_tls1_3_digest_check_passed(KDF_HKDF *ctx, const EVP_MD *md) |
869 | | { |
870 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
871 | | /* |
872 | | * Perform digest check |
873 | | * |
874 | | * According to RFC 8446 appendix B.4, the valid hash functions are |
875 | | * specified in FIPS 180-4. However, it only lists SHA2-256 and SHA2-384 in |
876 | | * the table. ACVP also only lists the same set of hash functions. |
877 | | */ |
878 | | int digest_unapproved = !EVP_MD_is_a(md, SN_sha256) |
879 | | && !EVP_MD_is_a(md, SN_sha384); |
880 | | |
881 | | if (digest_unapproved) { |
882 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0, |
883 | | libctx, "TLS13 KDF", "Digest", |
884 | | ossl_fips_config_tls13_kdf_digest_check)) { |
885 | | ERR_raise(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED); |
886 | | return 0; |
887 | | } |
888 | | } |
889 | | return 1; |
890 | | } |
891 | | |
892 | | /* |
893 | | * Calculate the correct length of the secret key. |
894 | | * |
895 | | * RFC 8446: |
896 | | * If a given secret is not available, then the 0-value consisting of a |
897 | | * string of Hash.length bytes set to zeros is used. |
898 | | */ |
899 | | static size_t fips_tls1_3_key_size(KDF_HKDF *ctx) |
900 | | { |
901 | | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
902 | | size_t key_size = 0; |
903 | | |
904 | | if (ctx->key != NULL) |
905 | | key_size = ctx->key_len; |
906 | | else if (md != NULL) |
907 | | key_size = EVP_MD_size(md); |
908 | | |
909 | | return key_size; |
910 | | } |
911 | | |
912 | | static int fips_tls1_3_key_check_passed(KDF_HKDF *ctx) |
913 | | { |
914 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
915 | | int key_approved = ossl_kdf_check_key_size(fips_tls1_3_key_size(ctx)); |
916 | | |
917 | | if (!key_approved) { |
918 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE1, |
919 | | libctx, "TLS13 KDF", "Key size", |
920 | | ossl_fips_config_tls13_kdf_key_check)) { |
921 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
922 | | return 0; |
923 | | } |
924 | | } |
925 | | return 1; |
926 | | } |
927 | | #endif |
928 | | |
929 | | static int kdf_tls1_3_derive(void *vctx, unsigned char *key, size_t keylen, |
930 | | const OSSL_PARAM params[]) |
931 | 0 | { |
932 | 0 | KDF_HKDF *ctx = (KDF_HKDF *)vctx; |
933 | 0 | const EVP_MD *md; |
934 | |
|
935 | 0 | if (!ossl_prov_is_running() || !kdf_tls1_3_set_ctx_params(ctx, params)) |
936 | 0 | return 0; |
937 | | |
938 | 0 | md = ossl_prov_digest_md(&ctx->digest); |
939 | 0 | if (md == NULL) { |
940 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
941 | 0 | return 0; |
942 | 0 | } |
943 | | |
944 | 0 | switch (ctx->mode) { |
945 | 0 | default: |
946 | 0 | return 0; |
947 | | |
948 | 0 | case EVP_KDF_HKDF_MODE_EXTRACT_ONLY: |
949 | 0 | return prov_tls13_hkdf_generate_secret(PROV_LIBCTX_OF(ctx->provctx), |
950 | 0 | md, |
951 | 0 | ctx->salt, ctx->salt_len, |
952 | 0 | ctx->key, ctx->key_len, |
953 | 0 | ctx->prefix, ctx->prefix_len, |
954 | 0 | ctx->label, ctx->label_len, |
955 | 0 | key, keylen); |
956 | | |
957 | 0 | case EVP_KDF_HKDF_MODE_EXPAND_ONLY: |
958 | 0 | return prov_tls13_hkdf_expand(md, ctx->key, ctx->key_len, |
959 | 0 | ctx->prefix, ctx->prefix_len, |
960 | 0 | ctx->label, ctx->label_len, |
961 | 0 | ctx->data, ctx->data_len, |
962 | 0 | key, keylen); |
963 | 0 | } |
964 | 0 | } |
965 | | |
966 | | static int kdf_tls1_3_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
967 | 0 | { |
968 | 0 | struct hkdf_all_set_ctx_params_st p; |
969 | 0 | KDF_HKDF *ctx = vctx; |
970 | |
|
971 | 0 | if (ctx == NULL || !kdf_tls1_3_set_ctx_params_decoder(params, &p)) |
972 | 0 | return 0; |
973 | | |
974 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p.ind_d)) |
975 | 0 | return 0; |
976 | 0 | if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE1, p.ind_k)) |
977 | 0 | return 0; |
978 | | |
979 | 0 | if (!hkdf_common_set_ctx_params(ctx, &p)) |
980 | 0 | return 0; |
981 | | |
982 | 0 | if (ctx->mode == EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND) { |
983 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
984 | 0 | return 0; |
985 | 0 | } |
986 | | |
987 | 0 | if (p.prefix != NULL) { |
988 | 0 | OPENSSL_free(ctx->prefix); |
989 | 0 | ctx->prefix = NULL; |
990 | 0 | if (!OSSL_PARAM_get_octet_string(p.prefix, (void **)&ctx->prefix, 0, |
991 | 0 | &ctx->prefix_len)) |
992 | 0 | return 0; |
993 | 0 | } |
994 | | |
995 | 0 | if (p.label != NULL) { |
996 | 0 | OPENSSL_free(ctx->label); |
997 | 0 | ctx->label = NULL; |
998 | 0 | if (!OSSL_PARAM_get_octet_string(p.label, (void **)&ctx->label, 0, |
999 | 0 | &ctx->label_len)) |
1000 | 0 | return 0; |
1001 | 0 | } |
1002 | | |
1003 | 0 | if (p.data != NULL) { |
1004 | 0 | OPENSSL_clear_free(ctx->data, ctx->data_len); |
1005 | 0 | ctx->data = NULL; |
1006 | 0 | if (!OSSL_PARAM_get_octet_string(p.data, (void **)&ctx->data, 0, |
1007 | 0 | &ctx->data_len)) |
1008 | 0 | return 0; |
1009 | 0 | } |
1010 | | |
1011 | | #ifdef FIPS_MODULE |
1012 | | if (p.digest != NULL) { |
1013 | | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
1014 | | |
1015 | | if (!fips_tls1_3_digest_check_passed(ctx, md)) |
1016 | | return 0; |
1017 | | } |
1018 | | |
1019 | | if (p.key != NULL) |
1020 | | if (!fips_tls1_3_key_check_passed(ctx)) |
1021 | | return 0; |
1022 | | #endif |
1023 | | |
1024 | 0 | return 1; |
1025 | 0 | } |
1026 | | |
1027 | | static const OSSL_PARAM *kdf_tls1_3_settable_ctx_params(ossl_unused void *ctx, |
1028 | | ossl_unused void *provctx) |
1029 | 0 | { |
1030 | 0 | return kdf_tls1_3_set_ctx_params_list; |
1031 | 0 | } |
1032 | | |
1033 | | const OSSL_DISPATCH ossl_kdf_tls1_3_kdf_functions[] = { |
1034 | | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))kdf_hkdf_new }, |
1035 | | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))kdf_hkdf_dup }, |
1036 | | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))kdf_hkdf_free }, |
1037 | | { OSSL_FUNC_KDF_RESET, (void(*)(void))kdf_hkdf_reset }, |
1038 | | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))kdf_tls1_3_derive }, |
1039 | | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, |
1040 | | (void(*)(void))kdf_tls1_3_settable_ctx_params }, |
1041 | | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))kdf_tls1_3_set_ctx_params }, |
1042 | | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, |
1043 | | (void(*)(void))hkdf_gettable_ctx_params }, |
1044 | | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))hkdf_common_get_ctx_params }, |
1045 | | OSSL_DISPATCH_END |
1046 | | }; |