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