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