]>
Commit | Line | Data |
---|---|---|
9537fe57 | 1 | /* |
da1c088f | 2 | * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved. |
9537fe57 SL |
3 | * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
4 | * | |
5 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
6 | * this file except in compliance with the License. You can obtain a copy | |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
9 | */ | |
10 | ||
11 | /* | |
12 | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final | |
13 | * Section 4.1. | |
14 | * | |
15 | * The Single Step KDF algorithm is given by: | |
16 | * | |
17 | * Result(0) = empty bit string (i.e., the null string). | |
18 | * For i = 1 to reps, do the following: | |
19 | * Increment counter by 1. | |
7a228c39 | 20 | * Result(i) = Result(i - 1) || H(counter || Z || FixedInfo). |
9537fe57 SL |
21 | * DKM = LeftmostBits(Result(reps), L)) |
22 | * | |
23 | * NOTES: | |
24 | * Z is a shared secret required to produce the derived key material. | |
25 | * counter is a 4 byte buffer. | |
26 | * FixedInfo is a bit string containing context specific data. | |
27 | * DKM is the output derived key material. | |
28 | * L is the required size of the DKM. | |
29 | * reps = [L / H_outputBits] | |
30 | * H(x) is the auxiliary function that can be either a hash, HMAC or KMAC. | |
31 | * H_outputBits is the length of the output of the auxiliary function H(x). | |
32 | * | |
33 | * Currently there is not a comprehensive list of test vectors for this | |
34 | * algorithm, especially for H(x) = HMAC and H(x) = KMAC. | |
35 | * Test vectors for H(x) = Hash are indirectly used by CAVS KAS tests. | |
36 | */ | |
37 | #include <stdlib.h> | |
38 | #include <stdarg.h> | |
39 | #include <string.h> | |
40 | #include <openssl/hmac.h> | |
41 | #include <openssl/evp.h> | |
42 | #include <openssl/kdf.h> | |
776796e8 RL |
43 | #include <openssl/core_names.h> |
44 | #include <openssl/params.h> | |
2741128e | 45 | #include <openssl/proverr.h> |
9537fe57 | 46 | #include "internal/cryptlib.h" |
e3405a4a | 47 | #include "internal/numbers.h" |
25f2138b | 48 | #include "crypto/evp.h" |
ddd21319 | 49 | #include "prov/provider_ctx.h" |
2b9e4e95 | 50 | #include "prov/providercommon.h" |
af3e7e1b | 51 | #include "prov/implementations.h" |
ddd21319 | 52 | #include "prov/provider_util.h" |
345b42be | 53 | #include "internal/params.h" |
9537fe57 | 54 | |
e3405a4a P |
55 | typedef struct { |
56 | void *provctx; | |
d3386f77 RL |
57 | EVP_MAC_CTX *macctx; /* H(x) = HMAC_hash OR H(x) = KMAC */ |
58 | PROV_DIGEST digest; /* H(x) = hash(x) */ | |
9537fe57 SL |
59 | unsigned char *secret; |
60 | size_t secret_len; | |
61 | unsigned char *info; | |
62 | size_t info_len; | |
63 | unsigned char *salt; | |
64 | size_t salt_len; | |
65 | size_t out_len; /* optional KMAC parameter */ | |
e8add4d3 | 66 | int is_kmac; |
e3405a4a | 67 | } KDF_SSKDF; |
9537fe57 SL |
68 | |
69 | #define SSKDF_MAX_INLEN (1<<30) | |
70 | #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4) | |
71 | #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4) | |
72 | ||
73 | /* KMAC uses a Customisation string of 'KDF' */ | |
74 | static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 }; | |
75 | ||
363b1e5d | 76 | static OSSL_FUNC_kdf_newctx_fn sskdf_new; |
2722eece | 77 | static OSSL_FUNC_kdf_dupctx_fn sskdf_dup; |
363b1e5d DMSP |
78 | static OSSL_FUNC_kdf_freectx_fn sskdf_free; |
79 | static OSSL_FUNC_kdf_reset_fn sskdf_reset; | |
80 | static OSSL_FUNC_kdf_derive_fn sskdf_derive; | |
81 | static OSSL_FUNC_kdf_derive_fn x963kdf_derive; | |
82 | static OSSL_FUNC_kdf_settable_ctx_params_fn sskdf_settable_ctx_params; | |
83 | static OSSL_FUNC_kdf_set_ctx_params_fn sskdf_set_ctx_params; | |
84 | static OSSL_FUNC_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params; | |
85 | static OSSL_FUNC_kdf_get_ctx_params_fn sskdf_get_ctx_params; | |
e3405a4a | 86 | |
9537fe57 SL |
87 | /* |
88 | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final | |
89 | * Section 4. One-Step Key Derivation using H(x) = hash(x) | |
8bbeaaa4 SL |
90 | * Note: X9.63 also uses this code with the only difference being that the |
91 | * counter is appended to the secret 'z'. | |
92 | * i.e. | |
93 | * result[i] = Hash(counter || z || info) for One Step OR | |
94 | * result[i] = Hash(z || counter || info) for X9.63. | |
9537fe57 SL |
95 | */ |
96 | static int SSKDF_hash_kdm(const EVP_MD *kdf_md, | |
97 | const unsigned char *z, size_t z_len, | |
98 | const unsigned char *info, size_t info_len, | |
8bbeaaa4 | 99 | unsigned int append_ctr, |
9537fe57 SL |
100 | unsigned char *derived_key, size_t derived_key_len) |
101 | { | |
102 | int ret = 0, hlen; | |
103 | size_t counter, out_len, len = derived_key_len; | |
104 | unsigned char c[4]; | |
105 | unsigned char mac[EVP_MAX_MD_SIZE]; | |
106 | unsigned char *out = derived_key; | |
107 | EVP_MD_CTX *ctx = NULL, *ctx_init = NULL; | |
108 | ||
109 | if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN | |
110 | || derived_key_len > SSKDF_MAX_INLEN | |
111 | || derived_key_len == 0) | |
112 | return 0; | |
113 | ||
ed576acd | 114 | hlen = EVP_MD_get_size(kdf_md); |
9537fe57 SL |
115 | if (hlen <= 0) |
116 | return 0; | |
117 | out_len = (size_t)hlen; | |
118 | ||
119 | ctx = EVP_MD_CTX_create(); | |
120 | ctx_init = EVP_MD_CTX_create(); | |
121 | if (ctx == NULL || ctx_init == NULL) | |
122 | goto end; | |
123 | ||
124 | if (!EVP_DigestInit(ctx_init, kdf_md)) | |
125 | goto end; | |
126 | ||
127 | for (counter = 1;; counter++) { | |
128 | c[0] = (unsigned char)((counter >> 24) & 0xff); | |
129 | c[1] = (unsigned char)((counter >> 16) & 0xff); | |
130 | c[2] = (unsigned char)((counter >> 8) & 0xff); | |
131 | c[3] = (unsigned char)(counter & 0xff); | |
132 | ||
133 | if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init) | |
8bbeaaa4 | 134 | && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
9537fe57 | 135 | && EVP_DigestUpdate(ctx, z, z_len) |
8bbeaaa4 | 136 | && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
9537fe57 SL |
137 | && EVP_DigestUpdate(ctx, info, info_len))) |
138 | goto end; | |
139 | if (len >= out_len) { | |
140 | if (!EVP_DigestFinal_ex(ctx, out, NULL)) | |
141 | goto end; | |
142 | out += out_len; | |
143 | len -= out_len; | |
144 | if (len == 0) | |
145 | break; | |
146 | } else { | |
147 | if (!EVP_DigestFinal_ex(ctx, mac, NULL)) | |
148 | goto end; | |
149 | memcpy(out, mac, len); | |
150 | break; | |
151 | } | |
152 | } | |
153 | ret = 1; | |
154 | end: | |
155 | EVP_MD_CTX_destroy(ctx); | |
156 | EVP_MD_CTX_destroy(ctx_init); | |
157 | OPENSSL_cleanse(mac, sizeof(mac)); | |
158 | return ret; | |
159 | } | |
160 | ||
161 | static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, | |
162 | size_t custom_len, size_t kmac_out_len, | |
163 | size_t derived_key_len, unsigned char **out) | |
164 | { | |
776796e8 RL |
165 | OSSL_PARAM params[2]; |
166 | ||
9537fe57 SL |
167 | /* Only KMAC has custom data - so return if not KMAC */ |
168 | if (custom == NULL) | |
169 | return 1; | |
170 | ||
776796e8 RL |
171 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, |
172 | (void *)custom, custom_len); | |
173 | params[1] = OSSL_PARAM_construct_end(); | |
174 | ||
865adf97 | 175 | if (!EVP_MAC_CTX_set_params(ctx, params)) |
9537fe57 SL |
176 | return 0; |
177 | ||
178 | /* By default only do one iteration if kmac_out_len is not specified */ | |
179 | if (kmac_out_len == 0) | |
180 | kmac_out_len = derived_key_len; | |
181 | /* otherwise check the size is valid */ | |
182 | else if (!(kmac_out_len == derived_key_len | |
183 | || kmac_out_len == 20 | |
184 | || kmac_out_len == 28 | |
185 | || kmac_out_len == 32 | |
186 | || kmac_out_len == 48 | |
187 | || kmac_out_len == 64)) | |
188 | return 0; | |
189 | ||
703170d4 | 190 | params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, |
776796e8 RL |
191 | &kmac_out_len); |
192 | ||
865adf97 | 193 | if (EVP_MAC_CTX_set_params(ctx, params) <= 0) |
9537fe57 SL |
194 | return 0; |
195 | ||
196 | /* | |
197 | * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so | |
198 | * alloc a buffer for this case. | |
199 | */ | |
200 | if (kmac_out_len > EVP_MAX_MD_SIZE) { | |
201 | *out = OPENSSL_zalloc(kmac_out_len); | |
202 | if (*out == NULL) | |
203 | return 0; | |
204 | } | |
205 | return 1; | |
206 | } | |
207 | ||
208 | /* | |
209 | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final | |
210 | * Section 4. One-Step Key Derivation using MAC: i.e either | |
211 | * H(x) = HMAC-hash(salt, x) OR | |
212 | * H(x) = KMAC#(salt, x, outbits, CustomString='KDF') | |
213 | */ | |
d3386f77 | 214 | static int SSKDF_mac_kdm(EVP_MAC_CTX *ctx_init, |
9537fe57 SL |
215 | const unsigned char *kmac_custom, |
216 | size_t kmac_custom_len, size_t kmac_out_len, | |
217 | const unsigned char *salt, size_t salt_len, | |
218 | const unsigned char *z, size_t z_len, | |
219 | const unsigned char *info, size_t info_len, | |
220 | unsigned char *derived_key, size_t derived_key_len) | |
221 | { | |
222 | int ret = 0; | |
223 | size_t counter, out_len, len; | |
224 | unsigned char c[4]; | |
225 | unsigned char mac_buf[EVP_MAX_MD_SIZE]; | |
226 | unsigned char *out = derived_key; | |
d3386f77 | 227 | EVP_MAC_CTX *ctx = NULL; |
9537fe57 SL |
228 | unsigned char *mac = mac_buf, *kmac_buffer = NULL; |
229 | ||
230 | if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN | |
231 | || derived_key_len > SSKDF_MAX_INLEN | |
232 | || derived_key_len == 0) | |
233 | return 0; | |
234 | ||
9537fe57 SL |
235 | if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len, |
236 | derived_key_len, &kmac_buffer)) | |
237 | goto end; | |
238 | if (kmac_buffer != NULL) | |
239 | mac = kmac_buffer; | |
240 | ||
2524ec1a | 241 | if (!EVP_MAC_init(ctx_init, salt, salt_len, NULL)) |
9537fe57 SL |
242 | goto end; |
243 | ||
90a2576b | 244 | out_len = EVP_MAC_CTX_get_mac_size(ctx_init); /* output size */ |
7be8ba54 | 245 | if (out_len <= 0 || (mac == mac_buf && out_len > sizeof(mac_buf))) |
9537fe57 SL |
246 | goto end; |
247 | len = derived_key_len; | |
248 | ||
249 | for (counter = 1;; counter++) { | |
250 | c[0] = (unsigned char)((counter >> 24) & 0xff); | |
251 | c[1] = (unsigned char)((counter >> 16) & 0xff); | |
252 | c[2] = (unsigned char)((counter >> 8) & 0xff); | |
253 | c[3] = (unsigned char)(counter & 0xff); | |
254 | ||
865adf97 | 255 | ctx = EVP_MAC_CTX_dup(ctx_init); |
be5fc053 | 256 | if (!(ctx != NULL |
9537fe57 SL |
257 | && EVP_MAC_update(ctx, c, sizeof(c)) |
258 | && EVP_MAC_update(ctx, z, z_len) | |
259 | && EVP_MAC_update(ctx, info, info_len))) | |
260 | goto end; | |
261 | if (len >= out_len) { | |
776796e8 | 262 | if (!EVP_MAC_final(ctx, out, NULL, len)) |
9537fe57 SL |
263 | goto end; |
264 | out += out_len; | |
265 | len -= out_len; | |
266 | if (len == 0) | |
267 | break; | |
268 | } else { | |
7be8ba54 | 269 | if (!EVP_MAC_final(ctx, mac, NULL, out_len)) |
9537fe57 SL |
270 | goto end; |
271 | memcpy(out, mac, len); | |
272 | break; | |
273 | } | |
865adf97 | 274 | EVP_MAC_CTX_free(ctx); |
be5fc053 | 275 | ctx = NULL; |
9537fe57 SL |
276 | } |
277 | ret = 1; | |
278 | end: | |
a3c62426 SL |
279 | if (kmac_buffer != NULL) |
280 | OPENSSL_clear_free(kmac_buffer, kmac_out_len); | |
281 | else | |
282 | OPENSSL_cleanse(mac_buf, sizeof(mac_buf)); | |
283 | ||
865adf97 | 284 | EVP_MAC_CTX_free(ctx); |
9537fe57 SL |
285 | return ret; |
286 | } | |
287 | ||
e3405a4a | 288 | static void *sskdf_new(void *provctx) |
9537fe57 | 289 | { |
e3405a4a | 290 | KDF_SSKDF *ctx; |
9537fe57 | 291 | |
2b9e4e95 P |
292 | if (!ossl_prov_is_running()) |
293 | return NULL; | |
294 | ||
e077455e RL |
295 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) != NULL) |
296 | ctx->provctx = provctx; | |
e3405a4a | 297 | return ctx; |
9537fe57 SL |
298 | } |
299 | ||
e3405a4a | 300 | static void sskdf_reset(void *vctx) |
9537fe57 | 301 | { |
e3405a4a | 302 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
0577959c | 303 | void *provctx = ctx->provctx; |
9537fe57 | 304 | |
865adf97 | 305 | EVP_MAC_CTX_free(ctx->macctx); |
7e149b39 | 306 | ossl_prov_digest_reset(&ctx->digest); |
e3405a4a P |
307 | OPENSSL_clear_free(ctx->secret, ctx->secret_len); |
308 | OPENSSL_clear_free(ctx->info, ctx->info_len); | |
309 | OPENSSL_clear_free(ctx->salt, ctx->salt_len); | |
e3405a4a | 310 | memset(ctx, 0, sizeof(*ctx)); |
0577959c | 311 | ctx->provctx = provctx; |
9537fe57 SL |
312 | } |
313 | ||
e3405a4a | 314 | static void sskdf_free(void *vctx) |
9537fe57 | 315 | { |
e3405a4a | 316 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
9537fe57 | 317 | |
3c659415 P |
318 | if (ctx != NULL) { |
319 | sskdf_reset(ctx); | |
320 | OPENSSL_free(ctx); | |
321 | } | |
9537fe57 SL |
322 | } |
323 | ||
2722eece P |
324 | static void *sskdf_dup(void *vctx) |
325 | { | |
326 | const KDF_SSKDF *src = (const KDF_SSKDF *)vctx; | |
327 | KDF_SSKDF *dest; | |
328 | ||
329 | dest = sskdf_new(src->provctx); | |
330 | if (dest != NULL) { | |
331 | if (src->macctx != NULL) { | |
332 | dest->macctx = EVP_MAC_CTX_dup(src->macctx); | |
333 | if (dest->macctx == NULL) | |
334 | goto err; | |
335 | } | |
336 | if (!ossl_prov_memdup(src->info, src->info_len, | |
337 | &dest->info, &dest->info_len) | |
338 | || !ossl_prov_memdup(src->salt, src->salt_len, | |
339 | &dest->salt , &dest->salt_len) | |
340 | || !ossl_prov_memdup(src->secret, src->secret_len, | |
341 | &dest->secret, &dest->secret_len) | |
342 | || !ossl_prov_digest_copy(&dest->digest, &src->digest)) | |
343 | goto err; | |
344 | dest->out_len = src->out_len; | |
e8add4d3 | 345 | dest->is_kmac = src->is_kmac; |
2722eece P |
346 | } |
347 | return dest; | |
348 | ||
349 | err: | |
350 | sskdf_free(dest); | |
351 | return NULL; | |
352 | } | |
353 | ||
e3405a4a | 354 | static size_t sskdf_size(KDF_SSKDF *ctx) |
9537fe57 SL |
355 | { |
356 | int len; | |
e8add4d3 | 357 | const EVP_MD *md = NULL; |
9537fe57 | 358 | |
e8add4d3 | 359 | if (ctx->is_kmac) |
360 | return SIZE_MAX; | |
361 | ||
362 | md = ossl_prov_digest_md(&ctx->digest); | |
7e149b39 | 363 | if (md == NULL) { |
e3405a4a | 364 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
9537fe57 SL |
365 | return 0; |
366 | } | |
ed576acd | 367 | len = EVP_MD_get_size(md); |
9537fe57 SL |
368 | return (len <= 0) ? 0 : (size_t)len; |
369 | } | |
370 | ||
3469b388 P |
371 | static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen, |
372 | const OSSL_PARAM params[]) | |
9537fe57 | 373 | { |
e3405a4a | 374 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
2b9e4e95 | 375 | const EVP_MD *md; |
e3405a4a | 376 | |
3469b388 | 377 | if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params)) |
2b9e4e95 | 378 | return 0; |
e3405a4a P |
379 | if (ctx->secret == NULL) { |
380 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); | |
9537fe57 SL |
381 | return 0; |
382 | } | |
2b9e4e95 | 383 | md = ossl_prov_digest_md(&ctx->digest); |
9537fe57 | 384 | |
d3386f77 | 385 | if (ctx->macctx != NULL) { |
9537fe57 SL |
386 | /* H(x) = KMAC or H(x) = HMAC */ |
387 | int ret; | |
388 | const unsigned char *custom = NULL; | |
389 | size_t custom_len = 0; | |
9537fe57 | 390 | int default_salt_len; |
ed576acd | 391 | EVP_MAC *mac = EVP_MAC_CTX_get0_mac(ctx->macctx); |
9537fe57 | 392 | |
d3386f77 | 393 | if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_HMAC)) { |
9537fe57 | 394 | /* H(x) = HMAC(x, salt, hash) */ |
7e149b39 | 395 | if (md == NULL) { |
e3405a4a | 396 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
9537fe57 SL |
397 | return 0; |
398 | } | |
ed576acd | 399 | default_salt_len = EVP_MD_get_size(md); |
9537fe57 SL |
400 | if (default_salt_len <= 0) |
401 | return 0; | |
e8add4d3 | 402 | } else if (ctx->is_kmac) { |
9537fe57 SL |
403 | /* H(x) = KMACzzz(x, salt, custom) */ |
404 | custom = kmac_custom_str; | |
405 | custom_len = sizeof(kmac_custom_str); | |
d3386f77 | 406 | if (EVP_MAC_is_a(mac, OSSL_MAC_NAME_KMAC128)) |
9537fe57 SL |
407 | default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE; |
408 | else | |
409 | default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE; | |
410 | } else { | |
e3405a4a | 411 | ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE); |
9537fe57 SL |
412 | return 0; |
413 | } | |
414 | /* If no salt is set then use a default_salt of zeros */ | |
e3405a4a P |
415 | if (ctx->salt == NULL || ctx->salt_len <= 0) { |
416 | ctx->salt = OPENSSL_zalloc(default_salt_len); | |
e077455e | 417 | if (ctx->salt == NULL) |
9537fe57 | 418 | return 0; |
e3405a4a | 419 | ctx->salt_len = default_salt_len; |
9537fe57 | 420 | } |
d3386f77 | 421 | ret = SSKDF_mac_kdm(ctx->macctx, |
e3405a4a P |
422 | custom, custom_len, ctx->out_len, |
423 | ctx->salt, ctx->salt_len, | |
424 | ctx->secret, ctx->secret_len, | |
425 | ctx->info, ctx->info_len, key, keylen); | |
9537fe57 SL |
426 | return ret; |
427 | } else { | |
428 | /* H(x) = hash */ | |
7e149b39 | 429 | if (md == NULL) { |
e3405a4a | 430 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
9537fe57 SL |
431 | return 0; |
432 | } | |
7e149b39 | 433 | return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
e3405a4a | 434 | ctx->info, ctx->info_len, 0, key, keylen); |
8bbeaaa4 SL |
435 | } |
436 | } | |
437 | ||
3469b388 P |
438 | static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen, |
439 | const OSSL_PARAM params[]) | |
8bbeaaa4 | 440 | { |
e3405a4a | 441 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
2b9e4e95 P |
442 | const EVP_MD *md; |
443 | ||
3469b388 | 444 | if (!ossl_prov_is_running() || !sskdf_set_ctx_params(ctx, params)) |
2b9e4e95 | 445 | return 0; |
e3405a4a P |
446 | |
447 | if (ctx->secret == NULL) { | |
448 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); | |
8bbeaaa4 SL |
449 | return 0; |
450 | } | |
451 | ||
d3386f77 | 452 | if (ctx->macctx != NULL) { |
e3405a4a | 453 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED); |
8bbeaaa4 | 454 | return 0; |
e3405a4a | 455 | } |
d3386f77 RL |
456 | |
457 | /* H(x) = hash */ | |
2b9e4e95 | 458 | md = ossl_prov_digest_md(&ctx->digest); |
d3386f77 RL |
459 | if (md == NULL) { |
460 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); | |
461 | return 0; | |
462 | } | |
463 | ||
464 | return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, | |
465 | ctx->info, ctx->info_len, 1, key, keylen); | |
e3405a4a P |
466 | } |
467 | ||
468 | static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
469 | { | |
470 | const OSSL_PARAM *p; | |
471 | KDF_SSKDF *ctx = vctx; | |
a829b735 | 472 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
e3405a4a | 473 | size_t sz; |
345b42be | 474 | int r; |
e3405a4a | 475 | |
c983a0e5 P |
476 | if (params == NULL) |
477 | return 1; | |
478 | ||
d3386f77 RL |
479 | if (!ossl_prov_macctx_load_from_params(&ctx->macctx, params, |
480 | NULL, NULL, NULL, libctx)) | |
481 | return 0; | |
345b42be P |
482 | if (ctx->macctx != NULL) { |
483 | if (EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx), | |
484 | OSSL_MAC_NAME_KMAC128) | |
485 | || EVP_MAC_is_a(EVP_MAC_CTX_get0_mac(ctx->macctx), | |
486 | OSSL_MAC_NAME_KMAC256)) { | |
487 | ctx->is_kmac = 1; | |
488 | } | |
489 | } | |
e8add4d3 | 490 | |
345b42be P |
491 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, libctx)) |
492 | return 0; | |
e3405a4a | 493 | |
345b42be P |
494 | r = ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SECRET, |
495 | &ctx->secret, &ctx->secret_len); | |
496 | if (r == -1) | |
497 | r = ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_KEY, | |
498 | &ctx->secret, &ctx->secret_len); | |
499 | if (r == 0) | |
500 | return 0; | |
e3405a4a | 501 | |
345b42be P |
502 | if (ossl_param_get1_concat_octet_string(params, OSSL_KDF_PARAM_INFO, |
503 | &ctx->info, &ctx->info_len, 0) == 0) | |
504 | return 0; | |
e3405a4a | 505 | |
345b42be P |
506 | if (ossl_param_get1_octet_string(params, OSSL_KDF_PARAM_SALT, |
507 | &ctx->salt, &ctx->salt_len) == 0) | |
e3405a4a P |
508 | return 0; |
509 | ||
510 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MAC_SIZE)) | |
511 | != NULL) { | |
512 | if (!OSSL_PARAM_get_size_t(p, &sz) || sz == 0) | |
513 | return 0; | |
514 | ctx->out_len = sz; | |
9537fe57 | 515 | } |
e3405a4a P |
516 | return 1; |
517 | } | |
518 | ||
1e8e5c60 P |
519 | static const OSSL_PARAM *sskdf_settable_ctx_params(ossl_unused void *ctx, |
520 | ossl_unused void *provctx) | |
e3405a4a P |
521 | { |
522 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
523 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0), | |
524 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), | |
525 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0), | |
526 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), | |
527 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), | |
528 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0), | |
529 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), | |
530 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_MAC_SIZE, NULL), | |
531 | OSSL_PARAM_END | |
532 | }; | |
533 | return known_settable_ctx_params; | |
534 | } | |
535 | ||
536 | static int sskdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
537 | { | |
538 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; | |
539 | OSSL_PARAM *p; | |
540 | ||
541 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) | |
542 | return OSSL_PARAM_set_size_t(p, sskdf_size(ctx)); | |
543 | return -2; | |
544 | } | |
545 | ||
1e8e5c60 P |
546 | static const OSSL_PARAM *sskdf_gettable_ctx_params(ossl_unused void *ctx, |
547 | ossl_unused void *provctx) | |
e3405a4a P |
548 | { |
549 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
550 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), | |
551 | OSSL_PARAM_END | |
552 | }; | |
553 | return known_gettable_ctx_params; | |
9537fe57 SL |
554 | } |
555 | ||
1be63951 | 556 | const OSSL_DISPATCH ossl_kdf_sskdf_functions[] = { |
e3405a4a | 557 | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new }, |
2722eece | 558 | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))sskdf_dup }, |
e3405a4a P |
559 | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free }, |
560 | { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset }, | |
561 | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))sskdf_derive }, | |
562 | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | |
563 | (void(*)(void))sskdf_settable_ctx_params }, | |
564 | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params }, | |
565 | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | |
566 | (void(*)(void))sskdf_gettable_ctx_params }, | |
567 | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params }, | |
1e6bd31e | 568 | OSSL_DISPATCH_END |
9537fe57 | 569 | }; |
8bbeaaa4 | 570 | |
1be63951 | 571 | const OSSL_DISPATCH ossl_kdf_x963_kdf_functions[] = { |
e3405a4a | 572 | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new }, |
2722eece | 573 | { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))sskdf_dup }, |
e3405a4a P |
574 | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free }, |
575 | { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset }, | |
576 | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x963kdf_derive }, | |
577 | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | |
578 | (void(*)(void))sskdf_settable_ctx_params }, | |
579 | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params }, | |
580 | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | |
581 | (void(*)(void))sskdf_gettable_ctx_params }, | |
582 | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params }, | |
1e6bd31e | 583 | OSSL_DISPATCH_END |
8bbeaaa4 | 584 | }; |