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