]>
Commit | Line | Data |
---|---|---|
9537fe57 SL |
1 | /* |
2 | * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. | |
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> | |
9537fe57 | 45 | #include "internal/cryptlib.h" |
e3405a4a | 46 | #include "internal/numbers.h" |
9537fe57 | 47 | #include "internal/evp_int.h" |
e3405a4a P |
48 | #include "internal/provider_ctx.h" |
49 | #include "internal/providercommonerr.h" | |
50 | #include "internal/provider_algs.h" | |
7e149b39 | 51 | #include "internal/provider_util.h" |
9537fe57 | 52 | |
e3405a4a P |
53 | typedef struct { |
54 | void *provctx; | |
776796e8 | 55 | EVP_MAC *mac; /* H(x) = HMAC_hash OR H(x) = KMAC */ |
7e149b39 | 56 | PROV_DIGEST digest; |
9537fe57 SL |
57 | unsigned char *secret; |
58 | size_t secret_len; | |
59 | unsigned char *info; | |
60 | size_t info_len; | |
61 | unsigned char *salt; | |
62 | size_t salt_len; | |
63 | size_t out_len; /* optional KMAC parameter */ | |
e3405a4a | 64 | } KDF_SSKDF; |
9537fe57 SL |
65 | |
66 | #define SSKDF_MAX_INLEN (1<<30) | |
67 | #define SSKDF_KMAC128_DEFAULT_SALT_SIZE (168 - 4) | |
68 | #define SSKDF_KMAC256_DEFAULT_SALT_SIZE (136 - 4) | |
69 | ||
70 | /* KMAC uses a Customisation string of 'KDF' */ | |
71 | static const unsigned char kmac_custom_str[] = { 0x4B, 0x44, 0x46 }; | |
72 | ||
e3405a4a P |
73 | static OSSL_OP_kdf_newctx_fn sskdf_new; |
74 | static OSSL_OP_kdf_freectx_fn sskdf_free; | |
75 | static OSSL_OP_kdf_reset_fn sskdf_reset; | |
76 | static OSSL_OP_kdf_derive_fn sskdf_derive; | |
77 | static OSSL_OP_kdf_derive_fn x963kdf_derive; | |
78 | static OSSL_OP_kdf_settable_ctx_params_fn sskdf_settable_ctx_params; | |
79 | static OSSL_OP_kdf_set_ctx_params_fn sskdf_set_ctx_params; | |
80 | static OSSL_OP_kdf_gettable_ctx_params_fn sskdf_gettable_ctx_params; | |
81 | static OSSL_OP_kdf_get_ctx_params_fn sskdf_get_ctx_params; | |
82 | ||
9537fe57 SL |
83 | /* |
84 | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final | |
85 | * Section 4. One-Step Key Derivation using H(x) = hash(x) | |
8bbeaaa4 SL |
86 | * Note: X9.63 also uses this code with the only difference being that the |
87 | * counter is appended to the secret 'z'. | |
88 | * i.e. | |
89 | * result[i] = Hash(counter || z || info) for One Step OR | |
90 | * result[i] = Hash(z || counter || info) for X9.63. | |
9537fe57 SL |
91 | */ |
92 | static int SSKDF_hash_kdm(const EVP_MD *kdf_md, | |
93 | const unsigned char *z, size_t z_len, | |
94 | const unsigned char *info, size_t info_len, | |
8bbeaaa4 | 95 | unsigned int append_ctr, |
9537fe57 SL |
96 | unsigned char *derived_key, size_t derived_key_len) |
97 | { | |
98 | int ret = 0, hlen; | |
99 | size_t counter, out_len, len = derived_key_len; | |
100 | unsigned char c[4]; | |
101 | unsigned char mac[EVP_MAX_MD_SIZE]; | |
102 | unsigned char *out = derived_key; | |
103 | EVP_MD_CTX *ctx = NULL, *ctx_init = NULL; | |
104 | ||
105 | if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN | |
106 | || derived_key_len > SSKDF_MAX_INLEN | |
107 | || derived_key_len == 0) | |
108 | return 0; | |
109 | ||
110 | hlen = EVP_MD_size(kdf_md); | |
111 | if (hlen <= 0) | |
112 | return 0; | |
113 | out_len = (size_t)hlen; | |
114 | ||
115 | ctx = EVP_MD_CTX_create(); | |
116 | ctx_init = EVP_MD_CTX_create(); | |
117 | if (ctx == NULL || ctx_init == NULL) | |
118 | goto end; | |
119 | ||
120 | if (!EVP_DigestInit(ctx_init, kdf_md)) | |
121 | goto end; | |
122 | ||
123 | for (counter = 1;; counter++) { | |
124 | c[0] = (unsigned char)((counter >> 24) & 0xff); | |
125 | c[1] = (unsigned char)((counter >> 16) & 0xff); | |
126 | c[2] = (unsigned char)((counter >> 8) & 0xff); | |
127 | c[3] = (unsigned char)(counter & 0xff); | |
128 | ||
129 | if (!(EVP_MD_CTX_copy_ex(ctx, ctx_init) | |
8bbeaaa4 | 130 | && (append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
9537fe57 | 131 | && EVP_DigestUpdate(ctx, z, z_len) |
8bbeaaa4 | 132 | && (!append_ctr || EVP_DigestUpdate(ctx, c, sizeof(c))) |
9537fe57 SL |
133 | && EVP_DigestUpdate(ctx, info, info_len))) |
134 | goto end; | |
135 | if (len >= out_len) { | |
136 | if (!EVP_DigestFinal_ex(ctx, out, NULL)) | |
137 | goto end; | |
138 | out += out_len; | |
139 | len -= out_len; | |
140 | if (len == 0) | |
141 | break; | |
142 | } else { | |
143 | if (!EVP_DigestFinal_ex(ctx, mac, NULL)) | |
144 | goto end; | |
145 | memcpy(out, mac, len); | |
146 | break; | |
147 | } | |
148 | } | |
149 | ret = 1; | |
150 | end: | |
151 | EVP_MD_CTX_destroy(ctx); | |
152 | EVP_MD_CTX_destroy(ctx_init); | |
153 | OPENSSL_cleanse(mac, sizeof(mac)); | |
154 | return ret; | |
155 | } | |
156 | ||
157 | static int kmac_init(EVP_MAC_CTX *ctx, const unsigned char *custom, | |
158 | size_t custom_len, size_t kmac_out_len, | |
159 | size_t derived_key_len, unsigned char **out) | |
160 | { | |
776796e8 RL |
161 | OSSL_PARAM params[2]; |
162 | ||
9537fe57 SL |
163 | /* Only KMAC has custom data - so return if not KMAC */ |
164 | if (custom == NULL) | |
165 | return 1; | |
166 | ||
776796e8 RL |
167 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, |
168 | (void *)custom, custom_len); | |
169 | params[1] = OSSL_PARAM_construct_end(); | |
170 | ||
171 | if (!EVP_MAC_CTX_set_params(ctx, params)) | |
9537fe57 SL |
172 | return 0; |
173 | ||
174 | /* By default only do one iteration if kmac_out_len is not specified */ | |
175 | if (kmac_out_len == 0) | |
176 | kmac_out_len = derived_key_len; | |
177 | /* otherwise check the size is valid */ | |
178 | else if (!(kmac_out_len == derived_key_len | |
179 | || kmac_out_len == 20 | |
180 | || kmac_out_len == 28 | |
181 | || kmac_out_len == 32 | |
182 | || kmac_out_len == 48 | |
183 | || kmac_out_len == 64)) | |
184 | return 0; | |
185 | ||
703170d4 | 186 | params[0] = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_SIZE, |
776796e8 RL |
187 | &kmac_out_len); |
188 | ||
189 | if (EVP_MAC_CTX_set_params(ctx, params) <= 0) | |
9537fe57 SL |
190 | return 0; |
191 | ||
192 | /* | |
193 | * For kmac the output buffer can be larger than EVP_MAX_MD_SIZE: so | |
194 | * alloc a buffer for this case. | |
195 | */ | |
196 | if (kmac_out_len > EVP_MAX_MD_SIZE) { | |
197 | *out = OPENSSL_zalloc(kmac_out_len); | |
198 | if (*out == NULL) | |
199 | return 0; | |
200 | } | |
201 | return 1; | |
202 | } | |
203 | ||
204 | /* | |
205 | * Refer to https://csrc.nist.gov/publications/detail/sp/800-56c/rev-1/final | |
206 | * Section 4. One-Step Key Derivation using MAC: i.e either | |
207 | * H(x) = HMAC-hash(salt, x) OR | |
208 | * H(x) = KMAC#(salt, x, outbits, CustomString='KDF') | |
209 | */ | |
776796e8 | 210 | static int SSKDF_mac_kdm(EVP_MAC *kdf_mac, const EVP_MD *hmac_md, |
9537fe57 SL |
211 | const unsigned char *kmac_custom, |
212 | size_t kmac_custom_len, size_t kmac_out_len, | |
213 | const unsigned char *salt, size_t salt_len, | |
214 | const unsigned char *z, size_t z_len, | |
215 | const unsigned char *info, size_t info_len, | |
216 | unsigned char *derived_key, size_t derived_key_len) | |
217 | { | |
218 | int ret = 0; | |
219 | size_t counter, out_len, len; | |
220 | unsigned char c[4]; | |
221 | unsigned char mac_buf[EVP_MAX_MD_SIZE]; | |
222 | unsigned char *out = derived_key; | |
223 | EVP_MAC_CTX *ctx = NULL, *ctx_init = NULL; | |
224 | unsigned char *mac = mac_buf, *kmac_buffer = NULL; | |
776796e8 RL |
225 | OSSL_PARAM params[3]; |
226 | size_t params_n = 0; | |
9537fe57 SL |
227 | |
228 | if (z_len > SSKDF_MAX_INLEN || info_len > SSKDF_MAX_INLEN | |
229 | || derived_key_len > SSKDF_MAX_INLEN | |
230 | || derived_key_len == 0) | |
231 | return 0; | |
232 | ||
9537fe57 | 233 | ctx_init = EVP_MAC_CTX_new(kdf_mac); |
be5fc053 | 234 | if (ctx_init == NULL) |
9537fe57 | 235 | goto end; |
9537fe57 | 236 | |
776796e8 RL |
237 | if (hmac_md != NULL) { |
238 | const char *mdname = EVP_MD_name(hmac_md); | |
239 | params[params_n++] = | |
703170d4 | 240 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, |
7f588d20 | 241 | (char *)mdname, 0); |
776796e8 RL |
242 | } |
243 | params[params_n++] = | |
244 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, (void *)salt, | |
245 | salt_len); | |
246 | params[params_n] = OSSL_PARAM_construct_end(); | |
247 | ||
248 | if (!EVP_MAC_CTX_set_params(ctx_init, params)) | |
9537fe57 SL |
249 | goto end; |
250 | ||
251 | if (!kmac_init(ctx_init, kmac_custom, kmac_custom_len, kmac_out_len, | |
252 | derived_key_len, &kmac_buffer)) | |
253 | goto end; | |
254 | if (kmac_buffer != NULL) | |
255 | mac = kmac_buffer; | |
256 | ||
257 | if (!EVP_MAC_init(ctx_init)) | |
258 | goto end; | |
259 | ||
260 | out_len = EVP_MAC_size(ctx_init); /* output size */ | |
261 | if (out_len <= 0) | |
262 | goto end; | |
263 | len = derived_key_len; | |
264 | ||
265 | for (counter = 1;; counter++) { | |
266 | c[0] = (unsigned char)((counter >> 24) & 0xff); | |
267 | c[1] = (unsigned char)((counter >> 16) & 0xff); | |
268 | c[2] = (unsigned char)((counter >> 8) & 0xff); | |
269 | c[3] = (unsigned char)(counter & 0xff); | |
270 | ||
be5fc053 KR |
271 | ctx = EVP_MAC_CTX_dup(ctx_init); |
272 | if (!(ctx != NULL | |
9537fe57 SL |
273 | && EVP_MAC_update(ctx, c, sizeof(c)) |
274 | && EVP_MAC_update(ctx, z, z_len) | |
275 | && EVP_MAC_update(ctx, info, info_len))) | |
276 | goto end; | |
277 | if (len >= out_len) { | |
776796e8 | 278 | if (!EVP_MAC_final(ctx, out, NULL, len)) |
9537fe57 SL |
279 | goto end; |
280 | out += out_len; | |
281 | len -= out_len; | |
282 | if (len == 0) | |
283 | break; | |
284 | } else { | |
776796e8 | 285 | if (!EVP_MAC_final(ctx, mac, NULL, len)) |
9537fe57 SL |
286 | goto end; |
287 | memcpy(out, mac, len); | |
288 | break; | |
289 | } | |
be5fc053 KR |
290 | EVP_MAC_CTX_free(ctx); |
291 | ctx = NULL; | |
9537fe57 SL |
292 | } |
293 | ret = 1; | |
294 | end: | |
a3c62426 SL |
295 | if (kmac_buffer != NULL) |
296 | OPENSSL_clear_free(kmac_buffer, kmac_out_len); | |
297 | else | |
298 | OPENSSL_cleanse(mac_buf, sizeof(mac_buf)); | |
299 | ||
9537fe57 SL |
300 | EVP_MAC_CTX_free(ctx); |
301 | EVP_MAC_CTX_free(ctx_init); | |
9537fe57 SL |
302 | return ret; |
303 | } | |
304 | ||
e3405a4a | 305 | static void *sskdf_new(void *provctx) |
9537fe57 | 306 | { |
e3405a4a | 307 | KDF_SSKDF *ctx; |
9537fe57 | 308 | |
e3405a4a P |
309 | if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) |
310 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
311 | ctx->provctx = provctx; | |
312 | return ctx; | |
9537fe57 SL |
313 | } |
314 | ||
e3405a4a | 315 | static void sskdf_reset(void *vctx) |
9537fe57 | 316 | { |
e3405a4a | 317 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
9537fe57 | 318 | |
7e149b39 | 319 | ossl_prov_digest_reset(&ctx->digest); |
df2f8af4 | 320 | EVP_MAC_free(ctx->mac); |
e3405a4a P |
321 | OPENSSL_clear_free(ctx->secret, ctx->secret_len); |
322 | OPENSSL_clear_free(ctx->info, ctx->info_len); | |
323 | OPENSSL_clear_free(ctx->salt, ctx->salt_len); | |
e3405a4a | 324 | memset(ctx, 0, sizeof(*ctx)); |
9537fe57 SL |
325 | } |
326 | ||
e3405a4a | 327 | static void sskdf_free(void *vctx) |
9537fe57 | 328 | { |
e3405a4a | 329 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
9537fe57 | 330 | |
e3405a4a | 331 | sskdf_reset(ctx); |
e3405a4a | 332 | OPENSSL_free(ctx); |
9537fe57 SL |
333 | } |
334 | ||
e3405a4a P |
335 | static int sskdf_set_buffer(unsigned char **out, size_t *out_len, |
336 | const OSSL_PARAM *p) | |
9537fe57 | 337 | { |
e3405a4a | 338 | if (p->data == NULL || p->data_size == 0) |
9537fe57 | 339 | return 1; |
e3405a4a P |
340 | OPENSSL_free(*out); |
341 | *out = NULL; | |
342 | return OSSL_PARAM_get_octet_string(p, (void **)out, 0, out_len); | |
9537fe57 SL |
343 | } |
344 | ||
e3405a4a | 345 | static size_t sskdf_size(KDF_SSKDF *ctx) |
9537fe57 SL |
346 | { |
347 | int len; | |
7e149b39 | 348 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
9537fe57 | 349 | |
7e149b39 | 350 | if (md == NULL) { |
e3405a4a | 351 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
9537fe57 SL |
352 | return 0; |
353 | } | |
7e149b39 | 354 | len = EVP_MD_size(md); |
9537fe57 SL |
355 | return (len <= 0) ? 0 : (size_t)len; |
356 | } | |
357 | ||
e3405a4a | 358 | static int sskdf_derive(void *vctx, unsigned char *key, size_t keylen) |
9537fe57 | 359 | { |
e3405a4a | 360 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
7e149b39 | 361 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
e3405a4a P |
362 | |
363 | if (ctx->secret == NULL) { | |
364 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); | |
9537fe57 SL |
365 | return 0; |
366 | } | |
367 | ||
e3405a4a | 368 | if (ctx->mac != NULL) { |
9537fe57 SL |
369 | /* H(x) = KMAC or H(x) = HMAC */ |
370 | int ret; | |
371 | const unsigned char *custom = NULL; | |
372 | size_t custom_len = 0; | |
9537fe57 SL |
373 | int default_salt_len; |
374 | ||
776796e8 RL |
375 | /* |
376 | * TODO(3.0) investigate the necessity to have all these controls. | |
377 | * Why does KMAC require a salt length that's shorter than the MD | |
378 | * block size? | |
379 | */ | |
7cfa1717 | 380 | if (EVP_MAC_is_a(ctx->mac, OSSL_MAC_NAME_HMAC)) { |
9537fe57 | 381 | /* H(x) = HMAC(x, salt, hash) */ |
7e149b39 | 382 | if (md == NULL) { |
e3405a4a | 383 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
9537fe57 SL |
384 | return 0; |
385 | } | |
7e149b39 | 386 | default_salt_len = EVP_MD_block_size(md); |
9537fe57 SL |
387 | if (default_salt_len <= 0) |
388 | return 0; | |
7cfa1717 RL |
389 | } else if (EVP_MAC_is_a(ctx->mac, OSSL_MAC_NAME_KMAC128) |
390 | || EVP_MAC_is_a(ctx->mac, OSSL_MAC_NAME_KMAC256)) { | |
9537fe57 SL |
391 | /* H(x) = KMACzzz(x, salt, custom) */ |
392 | custom = kmac_custom_str; | |
393 | custom_len = sizeof(kmac_custom_str); | |
7cfa1717 | 394 | if (EVP_MAC_is_a(ctx->mac, OSSL_MAC_NAME_KMAC128)) |
9537fe57 SL |
395 | default_salt_len = SSKDF_KMAC128_DEFAULT_SALT_SIZE; |
396 | else | |
397 | default_salt_len = SSKDF_KMAC256_DEFAULT_SALT_SIZE; | |
398 | } else { | |
e3405a4a | 399 | ERR_raise(ERR_LIB_PROV, PROV_R_UNSUPPORTED_MAC_TYPE); |
9537fe57 SL |
400 | return 0; |
401 | } | |
402 | /* If no salt is set then use a default_salt of zeros */ | |
e3405a4a P |
403 | if (ctx->salt == NULL || ctx->salt_len <= 0) { |
404 | ctx->salt = OPENSSL_zalloc(default_salt_len); | |
405 | if (ctx->salt == NULL) { | |
406 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
9537fe57 SL |
407 | return 0; |
408 | } | |
e3405a4a | 409 | ctx->salt_len = default_salt_len; |
9537fe57 | 410 | } |
7e149b39 | 411 | ret = SSKDF_mac_kdm(ctx->mac, md, |
e3405a4a P |
412 | custom, custom_len, ctx->out_len, |
413 | ctx->salt, ctx->salt_len, | |
414 | ctx->secret, ctx->secret_len, | |
415 | ctx->info, ctx->info_len, key, keylen); | |
9537fe57 SL |
416 | return ret; |
417 | } else { | |
418 | /* H(x) = hash */ | |
7e149b39 | 419 | if (md == NULL) { |
e3405a4a | 420 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
9537fe57 SL |
421 | return 0; |
422 | } | |
7e149b39 | 423 | return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
e3405a4a | 424 | ctx->info, ctx->info_len, 0, key, keylen); |
8bbeaaa4 SL |
425 | } |
426 | } | |
427 | ||
e3405a4a | 428 | static int x963kdf_derive(void *vctx, unsigned char *key, size_t keylen) |
8bbeaaa4 | 429 | { |
e3405a4a | 430 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; |
7e149b39 | 431 | const EVP_MD *md = ossl_prov_digest_md(&ctx->digest); |
e3405a4a P |
432 | |
433 | if (ctx->secret == NULL) { | |
434 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_SECRET); | |
8bbeaaa4 SL |
435 | return 0; |
436 | } | |
437 | ||
e3405a4a P |
438 | if (ctx->mac != NULL) { |
439 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_SUPPORTED); | |
8bbeaaa4 SL |
440 | return 0; |
441 | } else { | |
442 | /* H(x) = hash */ | |
7e149b39 | 443 | if (md == NULL) { |
e3405a4a P |
444 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST); |
445 | return 0; | |
446 | } | |
7e149b39 | 447 | return SSKDF_hash_kdm(md, ctx->secret, ctx->secret_len, |
e3405a4a P |
448 | ctx->info, ctx->info_len, 1, key, keylen); |
449 | } | |
450 | } | |
451 | ||
452 | static int sskdf_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
453 | { | |
454 | const OSSL_PARAM *p; | |
455 | KDF_SSKDF *ctx = vctx; | |
7e149b39 | 456 | OPENSSL_CTX *provctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx); |
e3405a4a P |
457 | EVP_MAC *mac; |
458 | size_t sz; | |
459 | const char *properties = NULL; | |
460 | ||
7e149b39 P |
461 | if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx)) |
462 | return 0; | |
e3405a4a P |
463 | |
464 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MAC)) != NULL) { | |
465 | EVP_MAC_free(ctx->mac); | |
466 | ctx->mac = NULL; | |
467 | ||
468 | mac = EVP_MAC_fetch(PROV_LIBRARY_CONTEXT_OF(ctx->provctx), p->data, | |
469 | properties); | |
470 | if (mac == NULL) | |
471 | return 0; | |
472 | EVP_MAC_free(ctx->mac); | |
473 | ctx->mac = mac; | |
474 | } | |
475 | ||
476 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SECRET)) != NULL | |
477 | || (p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_KEY)) != NULL) | |
478 | if (!sskdf_set_buffer(&ctx->secret, &ctx->secret_len, p)) | |
479 | return 0; | |
480 | ||
481 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_INFO)) != NULL) | |
482 | if (!sskdf_set_buffer(&ctx->info, &ctx->info_len, p)) | |
483 | return 0; | |
484 | ||
485 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_SALT)) != NULL) | |
486 | if (!sskdf_set_buffer(&ctx->salt, &ctx->salt_len, p)) | |
487 | return 0; | |
488 | ||
489 | if ((p = OSSL_PARAM_locate_const(params, OSSL_KDF_PARAM_MAC_SIZE)) | |
490 | != NULL) { | |
491 | if (!OSSL_PARAM_get_size_t(p, &sz) || sz == 0) | |
492 | return 0; | |
493 | ctx->out_len = sz; | |
9537fe57 | 494 | } |
e3405a4a P |
495 | return 1; |
496 | } | |
497 | ||
498 | static const OSSL_PARAM *sskdf_settable_ctx_params(void) | |
499 | { | |
500 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
501 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SECRET, NULL, 0), | |
502 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_KEY, NULL, 0), | |
503 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_INFO, NULL, 0), | |
504 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_PROPERTIES, NULL, 0), | |
505 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST, NULL, 0), | |
506 | OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_MAC, NULL, 0), | |
507 | OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT, NULL, 0), | |
508 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_MAC_SIZE, NULL), | |
509 | OSSL_PARAM_END | |
510 | }; | |
511 | return known_settable_ctx_params; | |
512 | } | |
513 | ||
514 | static int sskdf_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
515 | { | |
516 | KDF_SSKDF *ctx = (KDF_SSKDF *)vctx; | |
517 | OSSL_PARAM *p; | |
518 | ||
519 | if ((p = OSSL_PARAM_locate(params, OSSL_KDF_PARAM_SIZE)) != NULL) | |
520 | return OSSL_PARAM_set_size_t(p, sskdf_size(ctx)); | |
521 | return -2; | |
522 | } | |
523 | ||
524 | static const OSSL_PARAM *sskdf_gettable_ctx_params(void) | |
525 | { | |
526 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
527 | OSSL_PARAM_size_t(OSSL_KDF_PARAM_SIZE, NULL), | |
528 | OSSL_PARAM_END | |
529 | }; | |
530 | return known_gettable_ctx_params; | |
9537fe57 SL |
531 | } |
532 | ||
e3405a4a P |
533 | const OSSL_DISPATCH kdf_sskdf_functions[] = { |
534 | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new }, | |
535 | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free }, | |
536 | { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset }, | |
537 | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))sskdf_derive }, | |
538 | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | |
539 | (void(*)(void))sskdf_settable_ctx_params }, | |
540 | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params }, | |
541 | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | |
542 | (void(*)(void))sskdf_gettable_ctx_params }, | |
543 | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params }, | |
544 | { 0, NULL } | |
9537fe57 | 545 | }; |
8bbeaaa4 | 546 | |
e3405a4a P |
547 | const OSSL_DISPATCH kdf_x963_kdf_functions[] = { |
548 | { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))sskdf_new }, | |
549 | { OSSL_FUNC_KDF_FREECTX, (void(*)(void))sskdf_free }, | |
550 | { OSSL_FUNC_KDF_RESET, (void(*)(void))sskdf_reset }, | |
551 | { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x963kdf_derive }, | |
552 | { OSSL_FUNC_KDF_SETTABLE_CTX_PARAMS, | |
553 | (void(*)(void))sskdf_settable_ctx_params }, | |
554 | { OSSL_FUNC_KDF_SET_CTX_PARAMS, (void(*)(void))sskdf_set_ctx_params }, | |
555 | { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS, | |
556 | (void(*)(void))sskdf_gettable_ctx_params }, | |
557 | { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))sskdf_get_ctx_params }, | |
558 | { 0, NULL } | |
8bbeaaa4 | 559 | }; |