1 /*
2 * Copyright 2018-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2018-2020, 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 /* Tests of the EVP_KDF_CTX APIs */
12
13 #include <stdio.h>
14 #include <string.h>
15
16 #include <openssl/evp.h>
17 #include <openssl/kdf.h>
18 #include <openssl/core_names.h>
19 #include "internal/numbers.h"
20 #include "internal/sizes.h"
21 #include "testutil.h"
22
23
get_kdfbyname_libctx(OSSL_LIB_CTX * libctx,const char * name)24 static EVP_KDF_CTX *get_kdfbyname_libctx(OSSL_LIB_CTX *libctx, const char *name)
25 {
26 EVP_KDF *kdf = EVP_KDF_fetch(libctx, name, NULL);
27 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
28
29 EVP_KDF_free(kdf);
30 return kctx;
31 }
32
get_kdfbyname(const char * name)33 static EVP_KDF_CTX *get_kdfbyname(const char *name)
34 {
35 return get_kdfbyname_libctx(NULL, name);
36 }
37
construct_tls1_prf_params(const char * digest,const char * secret,const char * seed)38 static OSSL_PARAM *construct_tls1_prf_params(const char *digest, const char *secret,
39 const char *seed)
40 {
41 OSSL_PARAM *params = OPENSSL_malloc_array(4, sizeof(OSSL_PARAM));
42 OSSL_PARAM *p = params;
43
44 if (params == NULL)
45 return NULL;
46
47 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
48 (char *)digest, 0);
49 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
50 (unsigned char *)secret,
51 strlen(secret));
52 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
53 (unsigned char *)seed,
54 strlen(seed));
55 *p = OSSL_PARAM_construct_end();
56
57 return params;
58 }
59
test_kdf_tls1_prf(void)60 static int test_kdf_tls1_prf(void)
61 {
62 int ret;
63 EVP_KDF_CTX *kctx = NULL;
64 unsigned char out[16];
65 OSSL_PARAM *params;
66 static const unsigned char expected[sizeof(out)] = {
67 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
68 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
69 };
70
71 params = construct_tls1_prf_params("sha256", "secret", "seed");
72
73 ret = TEST_ptr(params)
74 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
75 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
76 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
77
78 EVP_KDF_CTX_free(kctx);
79 OPENSSL_free(params);
80 return ret;
81 }
82
test_kdf_tls1_prf_invalid_digest(void)83 static int test_kdf_tls1_prf_invalid_digest(void)
84 {
85 int ret;
86 EVP_KDF_CTX *kctx = NULL;
87 OSSL_PARAM *params;
88
89 params = construct_tls1_prf_params("blah", "secret", "seed");
90
91 ret = TEST_ptr(params)
92 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
93 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
94
95 EVP_KDF_CTX_free(kctx);
96 OPENSSL_free(params);
97 return ret;
98 }
99
test_kdf_tls1_prf_zero_output_size(void)100 static int test_kdf_tls1_prf_zero_output_size(void)
101 {
102 int ret;
103 EVP_KDF_CTX *kctx = NULL;
104 unsigned char out[16];
105 OSSL_PARAM *params;
106
107 params = construct_tls1_prf_params("sha256", "secret", "seed");
108
109 /* Negative test - derive should fail */
110 ret = TEST_ptr(params)
111 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
112 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
113 && TEST_int_eq(EVP_KDF_derive(kctx, out, 0, NULL), 0);
114
115 EVP_KDF_CTX_free(kctx);
116 OPENSSL_free(params);
117 return ret;
118 }
119
test_kdf_tls1_prf_empty_secret(void)120 static int test_kdf_tls1_prf_empty_secret(void)
121 {
122 int ret;
123 EVP_KDF_CTX *kctx = NULL;
124 unsigned char out[16];
125 OSSL_PARAM *params;
126
127 params = construct_tls1_prf_params("sha256", "", "seed");
128
129 ret = TEST_ptr(params)
130 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
131 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
132
133 EVP_KDF_CTX_free(kctx);
134 OPENSSL_free(params);
135 return ret;
136 }
137
test_kdf_tls1_prf_1byte_secret(void)138 static int test_kdf_tls1_prf_1byte_secret(void)
139 {
140 int ret;
141 EVP_KDF_CTX *kctx = NULL;
142 unsigned char out[16];
143 OSSL_PARAM *params;
144
145 params = construct_tls1_prf_params("sha256", "1", "seed");
146
147 ret = TEST_ptr(params)
148 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
149 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
150
151 EVP_KDF_CTX_free(kctx);
152 OPENSSL_free(params);
153 return ret;
154 }
155
test_kdf_tls1_prf_empty_seed(void)156 static int test_kdf_tls1_prf_empty_seed(void)
157 {
158 int ret;
159 EVP_KDF_CTX *kctx = NULL;
160 unsigned char out[16];
161 OSSL_PARAM *params;
162
163 params = construct_tls1_prf_params("sha256", "secret", "");
164
165 /* Negative test - derive should fail */
166 ret = TEST_ptr(params)
167 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
168 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
169 && TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0);
170
171 EVP_KDF_CTX_free(kctx);
172 OPENSSL_free(params);
173 return ret;
174 }
175
test_kdf_tls1_prf_1byte_seed(void)176 static int test_kdf_tls1_prf_1byte_seed(void)
177 {
178 int ret;
179 EVP_KDF_CTX *kctx = NULL;
180 unsigned char out[16];
181 OSSL_PARAM *params;
182
183 params = construct_tls1_prf_params("sha256", "secret", "1");
184
185 ret = TEST_ptr(params)
186 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
187 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
188
189 EVP_KDF_CTX_free(kctx);
190 OPENSSL_free(params);
191 return ret;
192 }
193
construct_hkdf_params(char * digest,char * key,size_t keylen,char * salt,char * info)194 static OSSL_PARAM *construct_hkdf_params(char *digest, char *key,
195 size_t keylen, char *salt, char *info)
196 {
197 OSSL_PARAM *params = OPENSSL_malloc_array(5, sizeof(OSSL_PARAM));
198 OSSL_PARAM *p = params;
199
200 if (params == NULL)
201 return NULL;
202
203 if (digest != NULL)
204 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
205 digest, 0);
206 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
207 salt, strlen(salt));
208 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
209 (unsigned char *)key, keylen);
210 if (info != NULL)
211 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
212 info, strlen(info));
213 else
214 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE,
215 "EXTRACT_ONLY", 0);
216 *p = OSSL_PARAM_construct_end();
217
218 return params;
219 }
220
test_kdf_hkdf(void)221 static int test_kdf_hkdf(void)
222 {
223 int ret;
224 EVP_KDF_CTX *kctx = NULL;
225 unsigned char out[10];
226 OSSL_PARAM *params;
227 static const unsigned char expected[sizeof(out)] = {
228 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
229 };
230
231 params = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
232
233 ret = TEST_ptr(params)
234 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
235 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
236 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
237
238 EVP_KDF_CTX_free(kctx);
239 OPENSSL_free(params);
240 return ret;
241 }
242
do_kdf_hkdf_gettables(int extract_only,int has_digest)243 static int do_kdf_hkdf_gettables(int extract_only, int has_digest)
244 {
245 int ret = 0;
246 size_t sz = 0;
247 OSSL_PARAM *params;
248 OSSL_PARAM params_get[5];
249 char digest[OSSL_MAX_NAME_SIZE];
250 char mode_utf8[OSSL_MAX_NAME_SIZE];
251 int mode_int = -1;
252 char salt[16] = { 0 };
253 char info[16] = { 0 };
254 const OSSL_PARAM *gettables, *p;
255 EVP_KDF_CTX *kctx = NULL;
256
257 if (!TEST_ptr(params = construct_hkdf_params(
258 has_digest ? "sha256" : NULL,
259 "secret", 6, "salt",
260 extract_only ? NULL : "label"))
261 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
262 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)))
263 goto err;
264
265 /* Check OSSL_KDF_PARAM_SIZE is gettable */
266 if (!TEST_ptr(gettables = EVP_KDF_CTX_gettable_params(kctx))
267 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettables, OSSL_KDF_PARAM_SIZE))
268 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettables, OSSL_KDF_PARAM_MODE))
269 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettables, OSSL_KDF_PARAM_DIGEST))
270 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettables, OSSL_KDF_PARAM_SALT))
271 || !TEST_ptr(p = OSSL_PARAM_locate_const(gettables, OSSL_KDF_PARAM_INFO)))
272 goto err;
273
274 /* Get OSSL_KDF_PARAM_SIZE as a size_t */
275 params_get[0] = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_SIZE, &sz);
276 params_get[1] = OSSL_PARAM_construct_end();
277 if (has_digest) {
278 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
279 || !TEST_size_t_eq(sz, extract_only ? SHA256_DIGEST_LENGTH : SIZE_MAX))
280 goto err;
281 } else {
282 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 0))
283 goto err;
284 }
285
286 params_get[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, digest,
287 sizeof(digest));
288 params_get[1] = OSSL_PARAM_construct_end();
289 if (has_digest) {
290 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
291 || !TEST_str_eq(digest, "SHA2-256"))
292 goto err;
293 } else {
294 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 0))
295 goto err;
296 }
297
298 params_get[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode_int);
299 params_get[1] = OSSL_PARAM_construct_end();
300 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
301 || !TEST_int_eq(mode_int, extract_only ? EVP_KDF_HKDF_MODE_EXTRACT_ONLY :
302 EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND))
303 goto err;
304
305 params_get[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, mode_utf8,
306 sizeof(mode_utf8));
307 params_get[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt, sizeof(salt));
308 params_get[2] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, info, sizeof(info));
309 params_get[3] = OSSL_PARAM_construct_end();
310 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
311 || !TEST_str_eq(mode_utf8, extract_only ? "EXTRACT_ONLY" : "EXTRACT_AND_EXPAND")
312 || !TEST_int_eq(mode_int, extract_only ? EVP_KDF_HKDF_MODE_EXTRACT_ONLY :
313 EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND)
314 || !TEST_str_eq(info, extract_only ? "" : "label")
315 || !TEST_str_eq(salt, "salt"))
316 goto err;
317
318 /* Get params returns 1 if an unsupported parameter is requested */
319 params_get[0] = OSSL_PARAM_construct_end();
320 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1))
321 goto err;
322 ret = 1;
323 err:
324 EVP_KDF_CTX_free(kctx);
325 OPENSSL_free(params);
326 return ret;
327 }
328
test_kdf_hkdf_gettables(void)329 static int test_kdf_hkdf_gettables(void)
330 {
331 return do_kdf_hkdf_gettables(0, 1);
332 }
333
test_kdf_hkdf_gettables_extractonly(void)334 static int test_kdf_hkdf_gettables_extractonly(void)
335 {
336 return do_kdf_hkdf_gettables(1, 1);
337 }
338
test_kdf_hkdf_gettables_no_digest(void)339 static int test_kdf_hkdf_gettables_no_digest(void)
340 {
341 return do_kdf_hkdf_gettables(1, 0);
342 }
343
test_kdf_hkdf_invalid_digest(void)344 static int test_kdf_hkdf_invalid_digest(void)
345 {
346 int ret;
347 EVP_KDF_CTX *kctx = NULL;
348 OSSL_PARAM *params;
349
350 params = construct_hkdf_params("blah", "secret", 6, "salt", "label");
351
352 ret = TEST_ptr(params)
353 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
354 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
355
356 EVP_KDF_CTX_free(kctx);
357 OPENSSL_free(params);
358 return ret;
359 }
360
kdf_hkdf_fixed_digest_change_digest(const char * kdf_name,char * bad_digest)361 static int kdf_hkdf_fixed_digest_change_digest(const char *kdf_name, char *bad_digest)
362 {
363 int ret;
364 EVP_KDF_CTX *kctx = NULL;
365 char digestname[OSSL_MAX_NAME_SIZE];
366 OSSL_PARAM params_get[2];
367 OSSL_PARAM *params_set;
368
369 params_get[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
370 digestname, sizeof(digestname));
371 params_get[1] = OSSL_PARAM_construct_end();
372
373 params_set = construct_hkdf_params(bad_digest, "secret", 6, "salt", "label");
374
375 /* In a fixed-digest KDF it is not allowed to set the same digest nor change it */
376 ret = TEST_ptr(params_get)
377 && TEST_ptr(kctx = get_kdfbyname(kdf_name))
378 && TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
379 && TEST_false(EVP_KDF_CTX_set_params(kctx, params_get))
380 && TEST_false(EVP_KDF_CTX_set_params(kctx, params_set));
381
382 EVP_KDF_CTX_free(kctx);
383 OPENSSL_free(params_set);
384 return ret;
385 }
386
test_kdf_hkdf_fixed_digest_change_digest(void)387 static int test_kdf_hkdf_fixed_digest_change_digest(void)
388 {
389 return kdf_hkdf_fixed_digest_change_digest(OSSL_KDF_NAME_HKDF_SHA256, "sha512")
390 && kdf_hkdf_fixed_digest_change_digest(OSSL_KDF_NAME_HKDF_SHA384, "sha512")
391 && kdf_hkdf_fixed_digest_change_digest(OSSL_KDF_NAME_HKDF_SHA512, "sha256");
392 }
393
kdf_hkdf_fixed_digest_preserve_digest(const char * kdf_name,const char * expected_digest,char * bad_digest)394 static int kdf_hkdf_fixed_digest_preserve_digest(const char *kdf_name, const char *expected_digest,
395 char *bad_digest)
396 {
397 int ret = 0;
398 EVP_KDF_CTX *kctx = NULL;
399 EVP_KDF_CTX *kctx_copy = NULL;
400 char digestname[OSSL_MAX_NAME_SIZE] = { 0 };
401 char salt[10] = { 0 };
402 char info[10] = { 0 };
403 OSSL_PARAM params_get[4];
404 OSSL_PARAM params_set[2];
405 OSSL_PARAM *params_init;
406
407 params_get[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
408 digestname, sizeof(digestname));
409 params_get[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
410 salt, sizeof(salt));
411 params_get[2] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
412 info, sizeof(info));
413 params_get[3] = OSSL_PARAM_construct_end();
414
415 params_init = construct_hkdf_params(NULL, "secret", 6, "salt", "label");
416
417 ret = TEST_ptr(kctx = get_kdfbyname(kdf_name))
418 && TEST_true(EVP_KDF_CTX_set_params(kctx, params_init));
419 if (!ret)
420 goto end;
421
422 params_set[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
423 bad_digest, strlen(bad_digest));
424 params_set[1] = OSSL_PARAM_construct_end();
425
426 /* Reset context and ensure it's still fixed-digest */
427 EVP_KDF_CTX_reset(kctx);
428 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
429 || !TEST_size_t_gt(params_get[0].return_size, 0)
430 || !TEST_str_eq(digestname, expected_digest)
431 || !TEST_size_t_eq(params_get[1].return_size, 0)
432 || !TEST_size_t_eq(params_get[2].return_size, 0)
433 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params_set)))
434 goto end;
435
436 /* Duplicate context and ensure it's still fixed-digest */
437 memset(digestname, 0, sizeof(digestname));
438 if (!TEST_ptr(kctx_copy = EVP_KDF_CTX_dup(kctx))
439 || !TEST_int_eq(EVP_KDF_CTX_get_params(kctx_copy, params_get), 1)
440 || !TEST_str_eq(digestname, expected_digest)
441 || !TEST_false(EVP_KDF_CTX_set_params(kctx_copy, params_set)))
442 goto end;
443
444 ret = 1;
445 end:
446 EVP_KDF_CTX_free(kctx);
447 EVP_KDF_CTX_free(kctx_copy);
448 OPENSSL_free(params_init);
449 return ret;
450 }
451
test_kdf_hkdf_fixed_digest_preserve_digest(void)452 static int test_kdf_hkdf_fixed_digest_preserve_digest(void)
453 {
454 return kdf_hkdf_fixed_digest_preserve_digest(OSSL_KDF_NAME_HKDF_SHA256, "SHA2-256", "SHA2-512")
455 && kdf_hkdf_fixed_digest_preserve_digest(OSSL_KDF_NAME_HKDF_SHA384, "SHA2-384", "SHA2-512")
456 && kdf_hkdf_fixed_digest_preserve_digest(OSSL_KDF_NAME_HKDF_SHA512, "SHA2-512", "SHA2-256");
457 }
458
test_kdf_hkdf_reset(void)459 static int test_kdf_hkdf_reset(void)
460 {
461 int ret = 0;
462 EVP_KDF_CTX *kctx = NULL;
463 OSSL_PARAM *params_set;
464 OSSL_PARAM params_get[3];
465 char digestname[OSSL_MAX_NAME_SIZE] = { 0 };
466 char salt[10] = { 0 };
467 char info[10] = { 0 };
468
469 params_set = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
470
471 if (!TEST_ptr(params_set)
472 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
473 || ! TEST_true(EVP_KDF_CTX_set_params(kctx, params_set)))
474 goto end;
475
476 /* Reset context and ensure it has been reset */
477 EVP_KDF_CTX_reset(kctx);
478
479 params_get[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
480 digestname, sizeof(digestname));
481 params_get[1] = OSSL_PARAM_construct_end();
482 /* Getting an unset digest results in an error */
483 if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params_get), 0))
484 goto end;
485
486 params_get[0] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
487 salt, sizeof(salt));
488 params_get[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
489 info, sizeof(info));
490 params_get[2] = OSSL_PARAM_construct_end();
491 /* Getting other unset params gives empty params */
492 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params_get), 1)
493 || !TEST_size_t_eq(params_get[0].return_size, 0)
494 || !TEST_size_t_eq(params_get[1].return_size, 0))
495 goto end;
496
497 ret = 1;
498 end:
499 EVP_KDF_CTX_free(kctx);
500 OPENSSL_free(params_set);
501 return ret;
502 }
503
test_kdf_hkdf_derive_set_params_fail(void)504 static int test_kdf_hkdf_derive_set_params_fail(void)
505 {
506 int ret = 0, i = 0;
507 EVP_KDF_CTX *kctx = NULL;
508 OSSL_PARAM params[2];
509 unsigned char out[10];
510
511 if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)))
512 goto end;
513 /*
514 * Set the wrong type for the digest so that it causes a failure
515 * inside kdf_hkdf_derive() when kdf_hkdf_set_ctx_params() is called
516 */
517 params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_DIGEST, &i);
518 params[1] = OSSL_PARAM_construct_end();
519 if (!TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), params), 0))
520 goto end;
521 ret = 1;
522 end:
523 EVP_KDF_CTX_free(kctx);
524 return ret;
525 }
526
test_kdf_hkdf_set_invalid_mode(void)527 static int test_kdf_hkdf_set_invalid_mode(void)
528 {
529 int ret = 0, bad_mode = 100;
530 EVP_KDF_CTX *kctx = NULL;
531 OSSL_PARAM params[2];
532
533 if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)))
534 goto end;
535 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE,
536 "BADMODE", 0);
537 params[1] = OSSL_PARAM_construct_end();
538 if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 0))
539 goto end;
540
541 params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &bad_mode);
542 if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 0))
543 goto end;
544
545 ret = 1;
546 end:
547 EVP_KDF_CTX_free(kctx);
548 return ret;
549 }
550
do_kdf_hkdf_set_invalid_param(const char * key,int type)551 static int do_kdf_hkdf_set_invalid_param(const char *key, int type)
552 {
553 int ret = 0;
554 EVP_KDF_CTX *kctx = NULL;
555 OSSL_PARAM params[2];
556 unsigned char buf[2];
557
558 if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF)))
559 goto end;
560 /* Set the wrong type for the key so that it causes a failure */
561 if (type == OSSL_PARAM_UTF8_STRING)
562 params[0] = OSSL_PARAM_construct_utf8_string(key, "BAD", 0);
563 else
564 params[0] = OSSL_PARAM_construct_octet_string(key, buf, sizeof(buf));
565 params[1] = OSSL_PARAM_construct_end();
566 if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 0))
567 goto end;
568
569 ret = 1;
570 end:
571 EVP_KDF_CTX_free(kctx);
572 return ret;
573 }
574
test_kdf_hkdf_set_ctx_param_fail(void)575 static int test_kdf_hkdf_set_ctx_param_fail(void)
576 {
577 return do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_MODE,
578 OSSL_PARAM_OCTET_STRING)
579 && do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_KEY,
580 OSSL_PARAM_UTF8_STRING)
581 && do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_SALT,
582 OSSL_PARAM_UTF8_STRING)
583 && do_kdf_hkdf_set_invalid_param(OSSL_KDF_PARAM_INFO,
584 OSSL_PARAM_UTF8_STRING);
585 }
586
test_kdf_hkdf_zero_output_size(void)587 static int test_kdf_hkdf_zero_output_size(void)
588 {
589 int ret;
590 EVP_KDF_CTX *kctx = NULL;
591 unsigned char out[10];
592 OSSL_PARAM *params;
593
594 params = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
595
596 /* Negative test - derive should fail */
597 ret = TEST_ptr(params)
598 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
599 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
600 && TEST_int_eq(EVP_KDF_derive(kctx, out, 0, NULL), 0);
601
602 EVP_KDF_CTX_free(kctx);
603 OPENSSL_free(params);
604 return ret;
605 }
606
test_kdf_hkdf_empty_key(void)607 static int test_kdf_hkdf_empty_key(void)
608 {
609 int ret;
610 EVP_KDF_CTX *kctx = NULL;
611 unsigned char out[10];
612 OSSL_PARAM *params;
613
614 params = construct_hkdf_params("sha256", "", 0, "salt", "label");
615
616 ret = TEST_ptr(params)
617 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
618 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
619
620 EVP_KDF_CTX_free(kctx);
621 OPENSSL_free(params);
622 return ret;
623 }
624
test_kdf_hkdf_1byte_key(void)625 static int test_kdf_hkdf_1byte_key(void)
626 {
627 int ret;
628 EVP_KDF_CTX *kctx = NULL;
629 unsigned char out[10];
630 OSSL_PARAM *params;
631
632 params = construct_hkdf_params("sha256", "1", 1, "salt", "label");
633
634 ret = TEST_ptr(params)
635 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
636 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
637
638 EVP_KDF_CTX_free(kctx);
639 OPENSSL_free(params);
640 return ret;
641 }
642
test_kdf_hkdf_empty_salt(void)643 static int test_kdf_hkdf_empty_salt(void)
644 {
645 int ret;
646 EVP_KDF_CTX *kctx = NULL;
647 unsigned char out[10];
648 OSSL_PARAM *params;
649
650 params = construct_hkdf_params("sha256", "secret", 6, "", "label");
651
652 ret = TEST_ptr(params)
653 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
654 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
655
656 EVP_KDF_CTX_free(kctx);
657 OPENSSL_free(params);
658 return ret;
659 }
660
construct_pbkdf1_params(char * pass,char * digest,char * salt,unsigned int * iter)661 static OSSL_PARAM *construct_pbkdf1_params(char *pass, char *digest, char *salt,
662 unsigned int *iter)
663 {
664 OSSL_PARAM *params = OPENSSL_malloc_array(5, sizeof(OSSL_PARAM));
665 OSSL_PARAM *p = params;
666
667 if (params == NULL)
668 return NULL;
669
670 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
671 (unsigned char *)pass, strlen(pass));
672 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
673 (unsigned char *)salt, strlen(salt));
674 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter);
675 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
676 digest, 0);
677 *p = OSSL_PARAM_construct_end();
678
679 return params;
680 }
681
test_kdf_pbkdf1(void)682 static int test_kdf_pbkdf1(void)
683 {
684 int ret = 0;
685 EVP_KDF_CTX *kctx = NULL;
686 unsigned char out[25];
687 unsigned int iterations = 4096;
688 OSSL_LIB_CTX *libctx = NULL;
689 OSSL_PARAM *params = NULL;
690 OSSL_PROVIDER *legacyprov = NULL;
691 OSSL_PROVIDER *defprov = NULL;
692 const unsigned char expected[sizeof(out)] = {
693 0xfb, 0x83, 0x4d, 0x36, 0x6d, 0xbc, 0x53, 0x87, 0x35, 0x1b, 0x34, 0x75,
694 0x95, 0x88, 0x32, 0x4f, 0x3e, 0x82, 0x81, 0x01, 0x21, 0x93, 0x64, 0x00,
695 0xcc
696 };
697
698 if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()))
699 goto err;
700
701 /* PBKDF1 only available in the legacy provider */
702 legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
703 if (legacyprov == NULL) {
704 OSSL_LIB_CTX_free(libctx);
705 return TEST_skip("PBKDF1 only available in legacy provider");
706 }
707
708 if (!TEST_ptr(defprov = OSSL_PROVIDER_load(libctx, "default")))
709 goto err;
710
711 params = construct_pbkdf1_params("passwordPASSWORDpassword", "sha256",
712 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
713 &iterations);
714
715 if (!TEST_ptr(params)
716 || !TEST_ptr(kctx = get_kdfbyname_libctx(libctx, OSSL_KDF_NAME_PBKDF1))
717 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
718 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)
719 || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)))
720 goto err;
721
722 ret = 1;
723 err:
724 EVP_KDF_CTX_free(kctx);
725 OPENSSL_free(params);
726 OSSL_PROVIDER_unload(defprov);
727 OSSL_PROVIDER_unload(legacyprov);
728 OSSL_LIB_CTX_free(libctx);
729 return ret;
730 }
731
test_kdf_pbkdf1_key_too_long(void)732 static int test_kdf_pbkdf1_key_too_long(void)
733 {
734 int ret = 0;
735 EVP_KDF_CTX *kctx = NULL;
736 unsigned char out[EVP_MAX_MD_SIZE + 1];
737 unsigned int iterations = 4096;
738 OSSL_LIB_CTX *libctx = NULL;
739 OSSL_PARAM *params = NULL;
740 OSSL_PROVIDER *legacyprov = NULL;
741 OSSL_PROVIDER *defprov = NULL;
742
743 if (!TEST_ptr(libctx = OSSL_LIB_CTX_new()))
744 goto err;
745
746 /* PBKDF1 only available in the legacy provider */
747 legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
748 if (legacyprov == NULL) {
749 OSSL_LIB_CTX_free(libctx);
750 return TEST_skip("PBKDF1 only available in legacy provider");
751 }
752
753 if (!TEST_ptr(defprov = OSSL_PROVIDER_load(libctx, "default")))
754 goto err;
755
756 params = construct_pbkdf1_params("passwordPASSWORDpassword", "sha256",
757 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
758 &iterations);
759
760 /*
761 * This is the same test sequence as test_kdf_pbkdf1, but we expect
762 * failure here as the requested key size is longer than the digest
763 * can provide
764 */
765 if (!TEST_ptr(params)
766 || !TEST_ptr(kctx = get_kdfbyname_libctx(libctx, OSSL_KDF_NAME_PBKDF1))
767 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
768 || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
769 goto err;
770
771 ret = 1;
772 err:
773 EVP_KDF_CTX_free(kctx);
774 OPENSSL_free(params);
775 OSSL_PROVIDER_unload(defprov);
776 OSSL_PROVIDER_unload(legacyprov);
777 OSSL_LIB_CTX_free(libctx);
778 return ret;
779 }
780
construct_pbkdf2_params(char * pass,char * digest,char * salt,unsigned int * iter,int * mode)781 static OSSL_PARAM *construct_pbkdf2_params(char *pass, char *digest, char *salt,
782 unsigned int *iter, int *mode)
783 {
784 OSSL_PARAM *params = OPENSSL_malloc_array(6, sizeof(OSSL_PARAM));
785 OSSL_PARAM *p = params;
786
787 if (params == NULL)
788 return NULL;
789
790 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
791 (unsigned char *)pass, strlen(pass));
792 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
793 (unsigned char *)salt, strlen(salt));
794 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter);
795 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
796 digest, 0);
797 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, mode);
798 *p = OSSL_PARAM_construct_end();
799
800 return params;
801 }
802
test_kdf_pbkdf2(void)803 static int test_kdf_pbkdf2(void)
804 {
805 int ret = 0;
806 EVP_KDF_CTX *kctx = NULL;
807 unsigned char out[25];
808 unsigned int iterations = 4096;
809 int mode = 0;
810 OSSL_PARAM *params;
811 const unsigned char expected[sizeof(out)] = {
812 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
813 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
814 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18,
815 0x1c
816 };
817
818 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
819 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
820 &iterations, &mode);
821
822 if (!TEST_ptr(params)
823 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
824 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
825 || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)))
826 goto err;
827
828 ret = 1;
829 err:
830 EVP_KDF_CTX_free(kctx);
831 OPENSSL_free(params);
832 return ret;
833 }
834
test_kdf_pbkdf2_small_output(void)835 static int test_kdf_pbkdf2_small_output(void)
836 {
837 int ret = 0;
838 EVP_KDF_CTX *kctx = NULL;
839 unsigned char out[25];
840 unsigned int iterations = 4096;
841 int mode = 0;
842 OSSL_PARAM *params;
843
844 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
845 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
846 &iterations, &mode);
847
848 if (!TEST_ptr(params)
849 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
850 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
851 /* A key length that is too small should fail */
852 || !TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1, NULL), 0))
853 goto err;
854
855 ret = 1;
856 err:
857 EVP_KDF_CTX_free(kctx);
858 OPENSSL_free(params);
859 return ret;
860 }
861
test_kdf_pbkdf2_large_output(void)862 static int test_kdf_pbkdf2_large_output(void)
863 {
864 int ret = 0;
865 EVP_KDF_CTX *kctx = NULL;
866 unsigned char out[25];
867 size_t len = 0;
868 unsigned int iterations = 4096;
869 int mode = 0;
870 OSSL_PARAM *params;
871
872 if (sizeof(len) > 32)
873 len = SIZE_MAX;
874
875 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
876 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
877 &iterations, &mode);
878
879 if (!TEST_ptr(params)
880 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
881 /* A key length that is too large should fail */
882 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
883 || (len != 0 && !TEST_int_eq(EVP_KDF_derive(kctx, out, len, NULL), 0)))
884 goto err;
885
886 ret = 1;
887 err:
888 EVP_KDF_CTX_free(kctx);
889 OPENSSL_free(params);
890 return ret;
891 }
892
test_kdf_pbkdf2_small_salt(void)893 static int test_kdf_pbkdf2_small_salt(void)
894 {
895 int ret = 0;
896 EVP_KDF_CTX *kctx = NULL;
897 unsigned int iterations = 4096;
898 int mode = 0;
899 OSSL_PARAM *params;
900
901 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
902 "saltSALT",
903 &iterations, &mode);
904
905 if (!TEST_ptr(params)
906 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
907 /* A salt that is too small should fail */
908 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
909 goto err;
910
911 ret = 1;
912 err:
913 EVP_KDF_CTX_free(kctx);
914 OPENSSL_free(params);
915 return ret;
916 }
917
test_kdf_pbkdf2_small_iterations(void)918 static int test_kdf_pbkdf2_small_iterations(void)
919 {
920 int ret = 0;
921 EVP_KDF_CTX *kctx = NULL;
922 unsigned int iterations = 1;
923 int mode = 0;
924 OSSL_PARAM *params;
925
926 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
927 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
928 &iterations, &mode);
929
930 if (!TEST_ptr(params)
931 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
932 /* An iteration count that is too small should fail */
933 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
934 goto err;
935
936 ret = 1;
937 err:
938 EVP_KDF_CTX_free(kctx);
939 OPENSSL_free(params);
940 return ret;
941 }
942
test_kdf_pbkdf2_small_salt_pkcs5(void)943 static int test_kdf_pbkdf2_small_salt_pkcs5(void)
944 {
945 int ret = 0;
946 EVP_KDF_CTX *kctx = NULL;
947 unsigned char out[25];
948 unsigned int iterations = 4096;
949 int mode = 1;
950 OSSL_PARAM *params;
951 OSSL_PARAM mode_params[2];
952
953 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
954 "saltSALT",
955 &iterations, &mode);
956
957 if (!TEST_ptr(params)
958 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
959 /* A salt that is too small should pass in pkcs5 mode */
960 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
961 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
962 goto err;
963
964 mode = 0;
965 mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
966 mode_params[1] = OSSL_PARAM_construct_end();
967
968 /* If the "pkcs5" mode is disabled then the derive will now fail */
969 if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params))
970 || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
971 goto err;
972
973 ret = 1;
974 err:
975 EVP_KDF_CTX_free(kctx);
976 OPENSSL_free(params);
977 return ret;
978 }
979
test_kdf_pbkdf2_small_iterations_pkcs5(void)980 static int test_kdf_pbkdf2_small_iterations_pkcs5(void)
981 {
982 int ret = 0;
983 EVP_KDF_CTX *kctx = NULL;
984 unsigned char out[25];
985 unsigned int iterations = 1;
986 int mode = 1;
987 OSSL_PARAM *params;
988 OSSL_PARAM mode_params[2];
989
990 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
991 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
992 &iterations, &mode);
993
994 if (!TEST_ptr(params)
995 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
996 /* An iteration count that is too small will pass in pkcs5 mode */
997 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
998 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
999 goto err;
1000
1001 mode = 0;
1002 mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
1003 mode_params[1] = OSSL_PARAM_construct_end();
1004
1005 /* If the "pkcs5" mode is disabled then the derive will now fail */
1006 if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params))
1007 || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
1008 goto err;
1009
1010 ret = 1;
1011 err:
1012 EVP_KDF_CTX_free(kctx);
1013 OPENSSL_free(params);
1014 return ret;
1015 }
1016
test_kdf_pbkdf2_invalid_digest(void)1017 static int test_kdf_pbkdf2_invalid_digest(void)
1018 {
1019 int ret = 0;
1020 EVP_KDF_CTX *kctx = NULL;
1021 unsigned int iterations = 4096;
1022 int mode = 0;
1023 OSSL_PARAM *params;
1024
1025 params = construct_pbkdf2_params("passwordPASSWORDpassword", "blah",
1026 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
1027 &iterations, &mode);
1028
1029 if (!TEST_ptr(params)
1030 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
1031 /* Unknown digest should fail */
1032 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
1033 goto err;
1034
1035 ret = 1;
1036 err:
1037 EVP_KDF_CTX_free(kctx);
1038 OPENSSL_free(params);
1039 return ret;
1040 }
1041
1042 #ifndef OPENSSL_NO_SCRYPT
test_kdf_scrypt(void)1043 static int test_kdf_scrypt(void)
1044 {
1045 int i, ret;
1046 EVP_KDF_CTX *kctx;
1047 OSSL_PARAM params[7], *p = params;
1048 unsigned char out[64];
1049 unsigned int nu = 1024, ru = 8, pu = 16, maxmem = 16;
1050 static const unsigned char expected[sizeof(out)] = {
1051 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
1052 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
1053 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
1054 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
1055 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
1056 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
1057 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
1058 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
1059 };
1060
1061 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
1062 (char *)"password", 8);
1063 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
1064 (char *)"NaCl", 4);
1065 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &nu);
1066 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &ru);
1067 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &pu);
1068 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_MAXMEM, &maxmem);
1069 *p = OSSL_PARAM_construct_end();
1070
1071 ret = TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SCRYPT));
1072 for (i = 0; ret && i < 2; ++i) {
1073 ret = ret
1074 && TEST_true(EVP_KDF_CTX_set_params(kctx, params));
1075 if (i == 0)
1076 ret = ret
1077 && TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)
1078 && TEST_true(OSSL_PARAM_set_uint(p - 1, 10 * 1024 * 1024))
1079 && TEST_true(EVP_KDF_CTX_set_params(kctx, p - 1));
1080 ret = ret
1081 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)
1082 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1083 if (i == 0)
1084 EVP_KDF_CTX_reset(kctx);
1085 }
1086
1087 EVP_KDF_CTX_free(kctx);
1088 return ret;
1089 }
1090 #endif /* OPENSSL_NO_SCRYPT */
1091
test_kdf_ss_hash(void)1092 static int test_kdf_ss_hash(void)
1093 {
1094 int ret;
1095 EVP_KDF_CTX *kctx;
1096 OSSL_PARAM params[4], *p = params;
1097 unsigned char out[14];
1098 static unsigned char z[] = {
1099 0x6d,0xbd,0xc2,0x3f,0x04,0x54,0x88,0xe4,0x06,0x27,0x57,0xb0,0x6b,0x9e,
1100 0xba,0xe1,0x83,0xfc,0x5a,0x59,0x46,0xd8,0x0d,0xb9,0x3f,0xec,0x6f,0x62,
1101 0xec,0x07,0xe3,0x72,0x7f,0x01,0x26,0xae,0xd1,0x2c,0xe4,0xb2,0x62,0xf4,
1102 0x7d,0x48,0xd5,0x42,0x87,0xf8,0x1d,0x47,0x4c,0x7c,0x3b,0x18,0x50,0xe9
1103 };
1104 static unsigned char other[] = {
1105 0xa1,0xb2,0xc3,0xd4,0xe5,0x43,0x41,0x56,0x53,0x69,0x64,0x3c,0x83,0x2e,
1106 0x98,0x49,0xdc,0xdb,0xa7,0x1e,0x9a,0x31,0x39,0xe6,0x06,0xe0,0x95,0xde,
1107 0x3c,0x26,0x4a,0x66,0xe9,0x8a,0x16,0x58,0x54,0xcd,0x07,0x98,0x9b,0x1e,
1108 0xe0,0xec,0x3f,0x8d,0xbe
1109 };
1110 static const unsigned char expected[sizeof(out)] = {
1111 0xa4,0x62,0xde,0x16,0xa8,0x9d,0xe8,0x46,0x6e,0xf5,0x46,0x0b,0x47,0xb8
1112 };
1113
1114 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
1115 (char *)"sha224", 0);
1116 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
1117 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
1118 sizeof(other));
1119 *p = OSSL_PARAM_construct_end();
1120
1121 ret =
1122 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
1123 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
1124 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1125
1126 EVP_KDF_CTX_free(kctx);
1127 return ret;
1128 }
1129
test_kdf_x963(void)1130 static int test_kdf_x963(void)
1131 {
1132 int ret;
1133 EVP_KDF_CTX *kctx;
1134 OSSL_PARAM params[4], *p = params;
1135 unsigned char out[1024 / 8];
1136 /*
1137 * Test data from https://csrc.nist.gov/CSRC/media/Projects/
1138 * Cryptographic-Algorithm-Validation-Program/documents/components/
1139 * 800-135testvectors/ansx963_2001.zip
1140 */
1141 static unsigned char z[] = {
1142 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89, 0xfa, 0x58, 0xce, 0xad,
1143 0xc0, 0x47, 0x19, 0x7f, 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
1144 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8, 0x15, 0x07, 0x39, 0x2f,
1145 0x1a, 0x86, 0xdd, 0xfd, 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
1146 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47, 0x47, 0xa9, 0xe2, 0xe7,
1147 0x53, 0xf5, 0x5e, 0xf0, 0x5a, 0x2d
1148 };
1149 static unsigned char shared[] = {
1150 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d, 0x2b, 0x3a, 0x2f, 0x99,
1151 0x37, 0x89, 0x5d, 0x31
1152 };
1153 static const unsigned char expected[sizeof(out)] = {
1154 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76, 0x9b, 0x52, 0x26, 0x4b,
1155 0x01, 0x12, 0xb5, 0x85, 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
1156 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d, 0x6e, 0x4d, 0xd2, 0xa5,
1157 0x99, 0xac, 0xce, 0xb3, 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
1158 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50, 0xa5, 0xa8, 0xee, 0xee,
1159 0x20, 0x80, 0x02, 0x26, 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
1160 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d, 0xea, 0x9c, 0x4f, 0xf7,
1161 0x45, 0xa2, 0x5a, 0xc2, 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
1162 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc, 0x0a, 0xb2, 0xaa, 0x8e,
1163 0x2e, 0xfa, 0x7b, 0x17, 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
1164 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
1165 };
1166
1167 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
1168 (char *)"sha512", 0);
1169 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
1170 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, shared,
1171 sizeof(shared));
1172 *p = OSSL_PARAM_construct_end();
1173
1174 ret =
1175 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X963KDF))
1176 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
1177 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1178
1179 EVP_KDF_CTX_free(kctx);
1180 return ret;
1181 }
1182
1183 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
1184 /*
1185 * KBKDF test vectors from RFC 6803 (Camellia Encryption for Kerberos 5)
1186 * section 10.
1187 */
test_kdf_kbkdf_6803_128(void)1188 static int test_kdf_kbkdf_6803_128(void)
1189 {
1190 int ret = 0, i, p;
1191 EVP_KDF_CTX *kctx;
1192 OSSL_PARAM params[7];
1193 static unsigned char input_key[] = {
1194 0x57, 0xD0, 0x29, 0x72, 0x98, 0xFF, 0xD9, 0xD3,
1195 0x5D, 0xE5, 0xA4, 0x7F, 0xB4, 0xBD, 0xE2, 0x4B,
1196 };
1197 static unsigned char constants[][5] = {
1198 { 0x00, 0x00, 0x00, 0x02, 0x99 },
1199 { 0x00, 0x00, 0x00, 0x02, 0xaa },
1200 { 0x00, 0x00, 0x00, 0x02, 0x55 },
1201 };
1202 static unsigned char outputs[][16] = {
1203 {0xD1, 0x55, 0x77, 0x5A, 0x20, 0x9D, 0x05, 0xF0,
1204 0x2B, 0x38, 0xD4, 0x2A, 0x38, 0x9E, 0x5A, 0x56},
1205 {0x64, 0xDF, 0x83, 0xF8, 0x5A, 0x53, 0x2F, 0x17,
1206 0x57, 0x7D, 0x8C, 0x37, 0x03, 0x57, 0x96, 0xAB},
1207 {0x3E, 0x4F, 0xBD, 0xF3, 0x0F, 0xB8, 0x25, 0x9C,
1208 0x42, 0x5C, 0xB6, 0xC9, 0x6F, 0x1F, 0x46, 0x35}
1209 };
1210 static unsigned char iv[16] = { 0 };
1211 unsigned char result[16] = { 0 };
1212
1213 for (i = 0; i < 3; i++) {
1214 p = 0;
1215 params[p++] = OSSL_PARAM_construct_utf8_string(
1216 OSSL_KDF_PARAM_CIPHER, "CAMELLIA-128-CBC", 0);
1217 params[p++] = OSSL_PARAM_construct_utf8_string(
1218 OSSL_KDF_PARAM_MAC, "CMAC", 0);
1219 params[p++] = OSSL_PARAM_construct_utf8_string(
1220 OSSL_KDF_PARAM_MODE, "FEEDBACK", 0);
1221 params[p++] = OSSL_PARAM_construct_octet_string(
1222 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
1223 params[p++] = OSSL_PARAM_construct_octet_string(
1224 OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i]));
1225 params[p++] = OSSL_PARAM_construct_octet_string(
1226 OSSL_KDF_PARAM_SEED, iv, sizeof(iv));
1227 params[p] = OSSL_PARAM_construct_end();
1228
1229 kctx = get_kdfbyname("KBKDF");
1230 ret = TEST_ptr(kctx)
1231 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result),
1232 params), 0)
1233 && TEST_mem_eq(result, sizeof(result), outputs[i],
1234 sizeof(outputs[i]));
1235 EVP_KDF_CTX_free(kctx);
1236 if (ret != 1)
1237 return ret;
1238 }
1239
1240 return ret;
1241 }
1242
test_kdf_kbkdf_6803_256(void)1243 static int test_kdf_kbkdf_6803_256(void)
1244 {
1245 int ret = 0, i, p;
1246 EVP_KDF_CTX *kctx;
1247 OSSL_PARAM params[7];
1248 static unsigned char input_key[] = {
1249 0xB9, 0xD6, 0x82, 0x8B, 0x20, 0x56, 0xB7, 0xBE,
1250 0x65, 0x6D, 0x88, 0xA1, 0x23, 0xB1, 0xFA, 0xC6,
1251 0x82, 0x14, 0xAC, 0x2B, 0x72, 0x7E, 0xCF, 0x5F,
1252 0x69, 0xAF, 0xE0, 0xC4, 0xDF, 0x2A, 0x6D, 0x2C,
1253 };
1254 static unsigned char constants[][5] = {
1255 { 0x00, 0x00, 0x00, 0x02, 0x99 },
1256 { 0x00, 0x00, 0x00, 0x02, 0xaa },
1257 { 0x00, 0x00, 0x00, 0x02, 0x55 },
1258 };
1259 static unsigned char outputs[][32] = {
1260 {0xE4, 0x67, 0xF9, 0xA9, 0x55, 0x2B, 0xC7, 0xD3,
1261 0x15, 0x5A, 0x62, 0x20, 0xAF, 0x9C, 0x19, 0x22,
1262 0x0E, 0xEE, 0xD4, 0xFF, 0x78, 0xB0, 0xD1, 0xE6,
1263 0xA1, 0x54, 0x49, 0x91, 0x46, 0x1A, 0x9E, 0x50,
1264 },
1265 {0x41, 0x2A, 0xEF, 0xC3, 0x62, 0xA7, 0x28, 0x5F,
1266 0xC3, 0x96, 0x6C, 0x6A, 0x51, 0x81, 0xE7, 0x60,
1267 0x5A, 0xE6, 0x75, 0x23, 0x5B, 0x6D, 0x54, 0x9F,
1268 0xBF, 0xC9, 0xAB, 0x66, 0x30, 0xA4, 0xC6, 0x04,
1269 },
1270 {0xFA, 0x62, 0x4F, 0xA0, 0xE5, 0x23, 0x99, 0x3F,
1271 0xA3, 0x88, 0xAE, 0xFD, 0xC6, 0x7E, 0x67, 0xEB,
1272 0xCD, 0x8C, 0x08, 0xE8, 0xA0, 0x24, 0x6B, 0x1D,
1273 0x73, 0xB0, 0xD1, 0xDD, 0x9F, 0xC5, 0x82, 0xB0,
1274 },
1275 };
1276 static unsigned char iv[16] = { 0 };
1277 unsigned char result[32] = { 0 };
1278
1279 for (i = 0; i < 3; i++) {
1280 p = 0;
1281 params[p++] = OSSL_PARAM_construct_utf8_string(
1282 OSSL_KDF_PARAM_CIPHER, "CAMELLIA-256-CBC", 0);
1283 params[p++] = OSSL_PARAM_construct_utf8_string(
1284 OSSL_KDF_PARAM_MAC, "CMAC", 0);
1285 params[p++] = OSSL_PARAM_construct_utf8_string(
1286 OSSL_KDF_PARAM_MODE, "FEEDBACK", 0);
1287 params[p++] = OSSL_PARAM_construct_octet_string(
1288 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
1289 params[p++] = OSSL_PARAM_construct_octet_string(
1290 OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i]));
1291 params[p++] = OSSL_PARAM_construct_octet_string(
1292 OSSL_KDF_PARAM_SEED, iv, sizeof(iv));
1293 params[p] = OSSL_PARAM_construct_end();
1294
1295 kctx = get_kdfbyname("KBKDF");
1296 ret = TEST_ptr(kctx)
1297 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result),
1298 params), 0)
1299 && TEST_mem_eq(result, sizeof(result), outputs[i],
1300 sizeof(outputs[i]));
1301 EVP_KDF_CTX_free(kctx);
1302 if (ret != 1)
1303 return ret;
1304 }
1305
1306 return ret;
1307 }
1308 #endif
1309
construct_kbkdf_params(char * digest,char * mac,unsigned char * key,size_t keylen,char * salt,char * info,int * r)1310 static OSSL_PARAM *construct_kbkdf_params(char *digest, char *mac, unsigned char *key,
1311 size_t keylen, char *salt, char *info, int *r)
1312 {
1313 OSSL_PARAM *params = OPENSSL_malloc_array(8, sizeof(OSSL_PARAM));
1314 OSSL_PARAM *p = params;
1315
1316 if (params == NULL)
1317 return NULL;
1318
1319 *p++ = OSSL_PARAM_construct_utf8_string(
1320 OSSL_KDF_PARAM_DIGEST, digest, 0);
1321 *p++ = OSSL_PARAM_construct_utf8_string(
1322 OSSL_KDF_PARAM_MAC, mac, 0);
1323 *p++ = OSSL_PARAM_construct_utf8_string(
1324 OSSL_KDF_PARAM_MODE, "COUNTER", 0);
1325 *p++ = OSSL_PARAM_construct_octet_string(
1326 OSSL_KDF_PARAM_KEY, key, keylen);
1327 *p++ = OSSL_PARAM_construct_octet_string(
1328 OSSL_KDF_PARAM_SALT, salt, strlen(salt));
1329 *p++ = OSSL_PARAM_construct_octet_string(
1330 OSSL_KDF_PARAM_INFO, info, strlen(info));
1331 *p++ = OSSL_PARAM_construct_int(
1332 OSSL_KDF_PARAM_KBKDF_R, r);
1333 *p = OSSL_PARAM_construct_end();
1334
1335 return params;
1336 }
1337
test_kdf_kbkdf_invalid_digest(void)1338 static int test_kdf_kbkdf_invalid_digest(void)
1339 {
1340 int ret;
1341 EVP_KDF_CTX *kctx;
1342 OSSL_PARAM *params;
1343
1344 static unsigned char key[] = {0x01};
1345 int r = 32;
1346
1347 params = construct_kbkdf_params("blah", "HMAC", key, 1, "prf", "test", &r);
1348 if (!TEST_ptr(params))
1349 return 0;
1350
1351 /* Negative test case - set_params should fail */
1352 kctx = get_kdfbyname("KBKDF");
1353 ret = TEST_ptr(kctx)
1354 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
1355
1356 EVP_KDF_CTX_free(kctx);
1357 OPENSSL_free(params);
1358 return ret;
1359 }
1360
test_kdf_kbkdf_invalid_mac(void)1361 static int test_kdf_kbkdf_invalid_mac(void)
1362 {
1363 int ret;
1364 EVP_KDF_CTX *kctx;
1365 OSSL_PARAM *params;
1366
1367 static unsigned char key[] = {0x01};
1368 int r = 32;
1369
1370 params = construct_kbkdf_params("sha256", "blah", key, 1, "prf", "test", &r);
1371 if (!TEST_ptr(params))
1372 return 0;
1373
1374 /* Negative test case - set_params should fail */
1375 kctx = get_kdfbyname("KBKDF");
1376 ret = TEST_ptr(kctx)
1377 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
1378
1379 EVP_KDF_CTX_free(kctx);
1380 OPENSSL_free(params);
1381 return ret;
1382 }
1383
test_kdf_kbkdf_invalid_r(void)1384 static int test_kdf_kbkdf_invalid_r(void)
1385 {
1386 int ret;
1387 EVP_KDF_CTX *kctx;
1388 OSSL_PARAM *params;
1389
1390 static unsigned char key[] = {0x01};
1391 int r = 31;
1392
1393 params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test", &r);
1394 if (!TEST_ptr(params))
1395 return 0;
1396
1397 /* Negative test case - derive should fail */
1398 kctx = get_kdfbyname("KBKDF");
1399 ret = TEST_ptr(kctx)
1400 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
1401
1402 EVP_KDF_CTX_free(kctx);
1403 OPENSSL_free(params);
1404 return ret;
1405 }
1406
1407
test_kdf_kbkdf_empty_key(void)1408 static int test_kdf_kbkdf_empty_key(void)
1409 {
1410 int ret;
1411 EVP_KDF_CTX *kctx;
1412 OSSL_PARAM *params;
1413
1414 static unsigned char key[] = {0x01};
1415 unsigned char result[32] = { 0 };
1416 int r = 32;
1417
1418 params = construct_kbkdf_params("sha256", "HMAC", key, 0, "prf", "test", &r);
1419 if (!TEST_ptr(params))
1420 return 0;
1421
1422 /* Negative test case - derive should fail */
1423 kctx = get_kdfbyname("KBKDF");
1424 ret = TEST_ptr(kctx)
1425 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
1426 && TEST_int_eq(EVP_KDF_derive(kctx, result, sizeof(result), NULL), 0);
1427
1428 EVP_KDF_CTX_free(kctx);
1429 OPENSSL_free(params);
1430 return ret;
1431 }
1432
test_kdf_kbkdf_1byte_key(void)1433 static int test_kdf_kbkdf_1byte_key(void)
1434 {
1435 int ret;
1436 EVP_KDF_CTX *kctx;
1437 OSSL_PARAM *params;
1438
1439 static unsigned char key[] = {0x01};
1440 unsigned char result[32] = { 0 };
1441 int r = 32;
1442
1443 params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test", &r);
1444 if (!TEST_ptr(params))
1445 return 0;
1446
1447 kctx = get_kdfbyname("KBKDF");
1448 ret = TEST_ptr(kctx)
1449 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0);
1450
1451 EVP_KDF_CTX_free(kctx);
1452 OPENSSL_free(params);
1453 return ret;
1454 }
1455
test_kdf_kbkdf_zero_output_size(void)1456 static int test_kdf_kbkdf_zero_output_size(void)
1457 {
1458 int ret;
1459 EVP_KDF_CTX *kctx;
1460 OSSL_PARAM *params;
1461
1462 static unsigned char key[] = {0x01};
1463 unsigned char result[32] = { 0 };
1464 int r = 32;
1465
1466 params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test", &r);
1467 if (!TEST_ptr(params))
1468 return 0;
1469
1470 /* Negative test case - derive should fail */
1471 kctx = get_kdfbyname("KBKDF");
1472 ret = TEST_ptr(kctx)
1473 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
1474 && TEST_int_eq(EVP_KDF_derive(kctx, result, 0, NULL), 0);
1475
1476 EVP_KDF_CTX_free(kctx);
1477 OPENSSL_free(params);
1478 return ret;
1479 }
1480
1481 /* Two test vectors from RFC 8009 (AES Encryption with HMAC-SHA2 for Kerberos
1482 * 5) appendix A. */
test_kdf_kbkdf_8009_prf1(void)1483 static int test_kdf_kbkdf_8009_prf1(void)
1484 {
1485 int ret, i = 0;
1486 EVP_KDF_CTX *kctx;
1487 OSSL_PARAM params[6];
1488 char *label = "prf", *digest = "sha256", *prf_input = "test",
1489 *mac = "HMAC";
1490 static unsigned char input_key[] = {
1491 0x37, 0x05, 0xD9, 0x60, 0x80, 0xC1, 0x77, 0x28,
1492 0xA0, 0xE8, 0x00, 0xEA, 0xB6, 0xE0, 0xD2, 0x3C,
1493 };
1494 static unsigned char output[] = {
1495 0x9D, 0x18, 0x86, 0x16, 0xF6, 0x38, 0x52, 0xFE,
1496 0x86, 0x91, 0x5B, 0xB8, 0x40, 0xB4, 0xA8, 0x86,
1497 0xFF, 0x3E, 0x6B, 0xB0, 0xF8, 0x19, 0xB4, 0x9B,
1498 0x89, 0x33, 0x93, 0xD3, 0x93, 0x85, 0x42, 0x95,
1499 };
1500 unsigned char result[sizeof(output)] = { 0 };
1501
1502 params[i++] = OSSL_PARAM_construct_utf8_string(
1503 OSSL_KDF_PARAM_DIGEST, digest, 0);
1504 params[i++] = OSSL_PARAM_construct_utf8_string(
1505 OSSL_KDF_PARAM_MAC, mac, 0);
1506 params[i++] = OSSL_PARAM_construct_octet_string(
1507 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
1508 params[i++] = OSSL_PARAM_construct_octet_string(
1509 OSSL_KDF_PARAM_SALT, label, strlen(label));
1510 params[i++] = OSSL_PARAM_construct_octet_string(
1511 OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
1512 params[i] = OSSL_PARAM_construct_end();
1513
1514 kctx = get_kdfbyname("KBKDF");
1515 ret = TEST_ptr(kctx)
1516 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
1517 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1518
1519 EVP_KDF_CTX_free(kctx);
1520 return ret;
1521 }
1522
test_kdf_kbkdf_8009_prf2(void)1523 static int test_kdf_kbkdf_8009_prf2(void)
1524 {
1525 int ret, i = 0;
1526 EVP_KDF_CTX *kctx;
1527 OSSL_PARAM params[6];
1528 char *label = "prf", *digest = "sha384", *prf_input = "test",
1529 *mac = "HMAC";
1530 static unsigned char input_key[] = {
1531 0x6D, 0x40, 0x4D, 0x37, 0xFA, 0xF7, 0x9F, 0x9D,
1532 0xF0, 0xD3, 0x35, 0x68, 0xD3, 0x20, 0x66, 0x98,
1533 0x00, 0xEB, 0x48, 0x36, 0x47, 0x2E, 0xA8, 0xA0,
1534 0x26, 0xD1, 0x6B, 0x71, 0x82, 0x46, 0x0C, 0x52,
1535 };
1536 static unsigned char output[] = {
1537 0x98, 0x01, 0xF6, 0x9A, 0x36, 0x8C, 0x2B, 0xF6,
1538 0x75, 0xE5, 0x95, 0x21, 0xE1, 0x77, 0xD9, 0xA0,
1539 0x7F, 0x67, 0xEF, 0xE1, 0xCF, 0xDE, 0x8D, 0x3C,
1540 0x8D, 0x6F, 0x6A, 0x02, 0x56, 0xE3, 0xB1, 0x7D,
1541 0xB3, 0xC1, 0xB6, 0x2A, 0xD1, 0xB8, 0x55, 0x33,
1542 0x60, 0xD1, 0x73, 0x67, 0xEB, 0x15, 0x14, 0xD2,
1543 };
1544 unsigned char result[sizeof(output)] = { 0 };
1545
1546 params[i++] = OSSL_PARAM_construct_utf8_string(
1547 OSSL_KDF_PARAM_DIGEST, digest, 0);
1548 params[i++] = OSSL_PARAM_construct_utf8_string(
1549 OSSL_KDF_PARAM_MAC, mac, 0);
1550 params[i++] = OSSL_PARAM_construct_octet_string(
1551 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
1552 params[i++] = OSSL_PARAM_construct_octet_string(
1553 OSSL_KDF_PARAM_SALT, label, strlen(label));
1554 params[i++] = OSSL_PARAM_construct_octet_string(
1555 OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
1556 params[i] = OSSL_PARAM_construct_end();
1557
1558 kctx = get_kdfbyname("KBKDF");
1559 ret = TEST_ptr(kctx)
1560 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
1561 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1562
1563 EVP_KDF_CTX_free(kctx);
1564 return ret;
1565 }
1566
1567 #if !defined(OPENSSL_NO_CMAC)
1568 /*
1569 * Test vector taken from
1570 * https://csrc.nist.gov/CSRC/media/Projects/
1571 * Cryptographic-Algorithm-Validation-Program/documents/KBKDF800-108/CounterMode.zip
1572 */
test_kdf_kbkdf_fixedinfo(void)1573 static int test_kdf_kbkdf_fixedinfo(void)
1574 {
1575 int ret;
1576 EVP_KDF_CTX *kctx;
1577 OSSL_PARAM params[8], *p = params;
1578 static char *cipher = "AES128";
1579 static char *mac = "CMAC";
1580 static char *mode = "COUNTER";
1581 int use_l = 0;
1582 int use_separator = 0;
1583
1584 static unsigned char input_key[] = {
1585 0xc1, 0x0b, 0x15, 0x2e, 0x8c, 0x97, 0xb7, 0x7e,
1586 0x18, 0x70, 0x4e, 0x0f, 0x0b, 0xd3, 0x83, 0x05,
1587 };
1588 static unsigned char fixed_input[] = {
1589 0x98, 0xcd, 0x4c, 0xbb, 0xbe, 0xbe, 0x15, 0xd1,
1590 0x7d, 0xc8, 0x6e, 0x6d, 0xba, 0xd8, 0x00, 0xa2,
1591 0xdc, 0xbd, 0x64, 0xf7, 0xc7, 0xad, 0x0e, 0x78,
1592 0xe9, 0xcf, 0x94, 0xff, 0xdb, 0xa8, 0x9d, 0x03,
1593 0xe9, 0x7e, 0xad, 0xf6, 0xc4, 0xf7, 0xb8, 0x06,
1594 0xca, 0xf5, 0x2a, 0xa3, 0x8f, 0x09, 0xd0, 0xeb,
1595 0x71, 0xd7, 0x1f, 0x49, 0x7b, 0xcc, 0x69, 0x06,
1596 0xb4, 0x8d, 0x36, 0xc4,
1597
1598 };
1599 static unsigned char output[] = {
1600 0x26, 0xfa, 0xf6, 0x19, 0x08, 0xad, 0x9e, 0xe8,
1601 0x81, 0xb8, 0x30, 0x5c, 0x22, 0x1d, 0xb5, 0x3f,
1602 };
1603 unsigned char result[sizeof(output)] = { 0 };
1604
1605 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER, cipher, 0);
1606 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, mac, 0);
1607 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, mode, 0);
1608 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, input_key,
1609 sizeof(input_key));
1610 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
1611 fixed_input, sizeof(fixed_input));
1612 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_L, &use_l);
1613 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR,
1614 &use_separator);
1615 *p = OSSL_PARAM_construct_end();
1616
1617 kctx = get_kdfbyname("KBKDF");
1618 ret = TEST_ptr(kctx)
1619 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
1620 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1621
1622 EVP_KDF_CTX_free(kctx);
1623 return ret;
1624 }
1625 #endif /* OPENSSL_NO_CMAC */
1626
test_kdf_kbkdf_kmac(void)1627 static int test_kdf_kbkdf_kmac(void)
1628 {
1629 int ret;
1630 EVP_KDF_CTX *kctx;
1631 OSSL_PARAM params[5], *p = params;
1632 static char *mac = "KMAC256";
1633
1634 static unsigned char input_key[] = {
1635 0xDD, 0x81, 0xEF, 0xC8, 0x2C, 0xDD, 0xEC, 0x51,
1636 0xC4, 0x09, 0xBD, 0x8C, 0xCB, 0xAF, 0x94, 0xF6,
1637 0x5F, 0xFA, 0x7B, 0x92, 0xF1, 0x11, 0xF9, 0x40,
1638 0x2B, 0x0D, 0x6A, 0xE0, 0x5E, 0x44, 0x92, 0x34,
1639 0xF0, 0x3B, 0xBA, 0xF5, 0x4F, 0xEF, 0x19, 0x45,
1640 0xDA
1641 };
1642 static unsigned char context[] = {
1643 0x81, 0xA1, 0xFE, 0x39, 0x91, 0xEE, 0x3F, 0xD3,
1644 0x90, 0x4E, 0x82, 0xE6, 0x13, 0x20, 0xEC, 0x6B,
1645 0x6E, 0x14, 0x0B, 0xBA, 0x95, 0x5D, 0x0B, 0x52,
1646 0x8E, 0x27, 0x67, 0xB3, 0xE0, 0x77, 0x05, 0x15,
1647 0xBD, 0x78, 0xF6, 0xE8, 0x8A, 0x7D, 0x9B, 0x08,
1648 0x20, 0x0F, 0xE9, 0x8D, 0xD6, 0x24, 0x67, 0xE2,
1649 0xCC, 0x6D, 0x42, 0xE6, 0x60, 0x50, 0x20, 0x77,
1650 0x89, 0x89, 0xB7, 0x2D, 0xF7, 0x5F, 0xE2, 0x79,
1651 0xDB, 0x58, 0x0B, 0x7B, 0x02, 0xB9, 0xD9, 0xB0,
1652 0xFA, 0x6B, 0x0B, 0xB6, 0xD4, 0x95, 0xDB, 0x46,
1653 0x55, 0x5F, 0x12, 0xC3, 0xF0, 0xE0, 0x6E, 0xC8,
1654 0xF4, 0xF8, 0xA1, 0x64, 0x2E, 0x96, 0x74, 0x2B,
1655 0xC6, 0xBD, 0x22, 0xB1, 0x6A, 0xBC, 0x41, 0xDF,
1656 0x30, 0x32, 0xC7, 0xCE, 0x18, 0x14, 0x70, 0x2A,
1657 0xED, 0xE5, 0xC4, 0x6B, 0x8A, 0xA8, 0x36, 0xFD,
1658 0x0A, 0x76, 0x38, 0x44, 0x98, 0x0A, 0xE3, 0xC2,
1659 0x3A, 0x24, 0xCB, 0x45, 0xBF, 0xC9, 0x2C, 0x19,
1660 0xCB, 0x9D, 0x6C, 0x27, 0xDE, 0x78, 0x3E, 0x2C,
1661 0x3D, 0x39, 0x6E, 0x11, 0x59, 0xAE, 0x4F, 0x91,
1662 0x03, 0xE2, 0x7B, 0x97, 0xD6, 0x0C, 0x7D, 0x9D,
1663 0x5A, 0xA5, 0x47, 0x57, 0x41, 0xAD, 0x64, 0x5B,
1664 0xF7, 0x1D, 0x1A, 0xDA, 0x3A, 0x39, 0xDF, 0x85,
1665 0x0D, 0x0F, 0x50, 0x15, 0xA7, 0x3D, 0x68, 0x81,
1666 0x7B, 0x0D, 0xF2, 0x24, 0x24, 0x23, 0x37, 0xE5,
1667 0x77, 0xA6, 0x61, 0xBE, 0xFE, 0x4B, 0x3B, 0x8E,
1668 0x4F, 0x15, 0x4F, 0xC1, 0x30, 0xCB, 0x9E, 0xF5,
1669 0x06, 0x9F, 0xBB, 0x0E, 0xF2, 0xF4, 0x43, 0xBB,
1670 0x64, 0x45, 0xA3, 0x7D, 0x3B, 0xB4, 0x70, 0x47,
1671 0xDF, 0x4A, 0xA5, 0xD9, 0x2F, 0xE6, 0x25, 0xC8,
1672 0x1D, 0x43, 0x0A, 0xEA, 0xF9, 0xCC, 0xC7, 0x1F,
1673 0x8A, 0x2D, 0xD8, 0x95, 0x6B, 0x16, 0x30, 0x1D,
1674 0x80, 0x90, 0xA4, 0x23, 0x14, 0x59, 0xD1, 0x5A,
1675 0x00, 0x48, 0x8D, 0xF7, 0xEA, 0x29, 0x23, 0xDF,
1676 0x35, 0x26, 0x25, 0x22, 0x12, 0xC4, 0x4C, 0x09,
1677 0x69, 0xB8, 0xD6, 0x0C, 0x0E, 0x71, 0x90, 0x6C,
1678 0x42, 0x90, 0x02, 0x53, 0xC5, 0x5A, 0xEF, 0x42,
1679 0x66, 0x1D, 0xAF, 0x45, 0xD5, 0x31, 0xD7, 0x61,
1680 0x3A, 0xE6, 0x06, 0xFB, 0x83, 0x72, 0xAD, 0x82,
1681 0xE3, 0x6A, 0x7E, 0x03, 0x9B, 0x37, 0x77, 0xAF,
1682 0x8D, 0x63, 0x28, 0xC2, 0x8A, 0x5E, 0xC6, 0x3B,
1683 0x22, 0xA8, 0x94, 0xC0, 0x46, 0x2F, 0x73, 0xE7,
1684 0xBB, 0x72, 0x44, 0x85, 0x20, 0x1D, 0xD0, 0x6A,
1685 0x52, 0x8C, 0xB1, 0x8B, 0x96, 0x11, 0xEB, 0xFB,
1686 0xDD, 0xF5, 0x74, 0x49, 0x19, 0x93, 0xD3, 0x7F,
1687 0x6C, 0x27, 0x19, 0x54, 0xDD, 0x00, 0x0F, 0x95,
1688 0xF6, 0x14, 0x15, 0x87, 0x32, 0x54, 0xA5, 0x02,
1689 0xAD, 0x41, 0x55, 0x5E, 0xDD, 0x32, 0x62, 0x3B,
1690 0xFC, 0x71, 0xC1, 0x56, 0xC4, 0x6A, 0xFC, 0xD0,
1691 0xF9, 0x77, 0xDA, 0xC5, 0x20, 0x7D, 0xAC, 0xA8,
1692 0xEB, 0x8F, 0xBE, 0xF9, 0x4D, 0xE8, 0x6D, 0x9E,
1693 0x4C, 0x39, 0xB3, 0x15, 0x63, 0xCD, 0xF6, 0x46,
1694 0xEC, 0x3A, 0xD2, 0x89, 0xA9, 0xFA, 0x24, 0xB4,
1695 0x0E, 0x62, 0x6F, 0x9F, 0xF3, 0xF1, 0x3C, 0x61,
1696 0x57, 0xB9, 0x2C, 0xD4, 0x78, 0x4F, 0x76, 0xCF,
1697 0xFB, 0x6A, 0x51, 0xE8, 0x1E, 0x0A, 0x33, 0x69,
1698 0x16, 0xCD, 0xB7, 0x5C, 0xDF, 0x03, 0x62, 0x17,
1699 0x63, 0x37, 0x49, 0xC3, 0xB7, 0x68, 0x09, 0x9E,
1700 0x22, 0xD2, 0x20, 0x96, 0x37, 0x0D, 0x13, 0xA4,
1701 0x96, 0xB1, 0x8D, 0x0B, 0x12, 0x87, 0xEB, 0x57,
1702 0x25, 0x27, 0x08, 0xFC, 0x90, 0x5E, 0x33, 0x77,
1703 0x50, 0x63, 0xE1, 0x8C, 0xF4, 0x0C, 0x80, 0x89,
1704 0x76, 0x63, 0x70, 0x0A, 0x61, 0x59, 0x90, 0x1F,
1705 0xC9, 0x47, 0xBA, 0x12, 0x7B, 0xB2, 0x7A, 0x44,
1706 0xC3, 0x3D, 0xD0, 0x38, 0xF1, 0x7F, 0x02, 0x92
1707 };
1708 static unsigned char label[] = {
1709 0xA5, 0xDE, 0x2A, 0x0A, 0xF0, 0xDA, 0x59, 0x04,
1710 0xCC, 0xFF, 0x50, 0xD3, 0xA5, 0xD2, 0xDE, 0xA3,
1711 0x33, 0xC0, 0x27, 0xED, 0xDC, 0x6A, 0x54, 0x54,
1712 0x95, 0x78, 0x74, 0x0D, 0xE7, 0xB7, 0x92, 0xD6,
1713 0x64, 0xD5, 0xFB, 0x1F, 0x0F, 0x87, 0xFD, 0x65,
1714 0x79, 0x8B, 0x81, 0x83, 0x95, 0x40, 0x7A, 0x19,
1715 0x8D, 0xCA, 0xE0, 0x4A, 0x93, 0xA8
1716 };
1717 static unsigned char output[] = {
1718 0xB5, 0x61, 0xE3, 0x7D, 0x06, 0xD5, 0x34, 0x80,
1719 0x74, 0x61, 0x16, 0x08, 0x6F, 0x89, 0x6F, 0xB1,
1720 0x43, 0xAF, 0x61, 0x28, 0x93, 0xD8, 0xDF, 0xF6,
1721 0xB6, 0x23, 0x43, 0x68, 0xE4, 0x84, 0xF3, 0xED,
1722 0x50, 0xB6, 0x81, 0x6D, 0x50, 0xF4, 0xAF, 0xF2,
1723 0xA5, 0x50, 0x7E, 0x25, 0xBF, 0x05, 0xBE, 0xE7,
1724 0x07, 0xB0, 0x95, 0xC3, 0x04, 0x38, 0xB4, 0xF9,
1725 0xC1, 0x1E, 0x96, 0x08, 0xF4, 0xC9, 0x05, 0x54,
1726 0x4A, 0xB6, 0x81, 0x92, 0x5B, 0x34, 0x8A, 0x45,
1727 0xDD, 0x7D, 0x98, 0x51, 0x1F, 0xD9, 0x90, 0x23,
1728 0x59, 0x97, 0xA2, 0x4E, 0x43, 0x49, 0xEB, 0x4E,
1729 0x86, 0xEC, 0x20, 0x3C, 0x31, 0xFF, 0x49, 0x55,
1730 0x49, 0xF5, 0xF5, 0x16, 0x79, 0xD9, 0x1C, 0x8E,
1731 0x6E, 0xB3, 0x1C, 0xAF, 0xC8, 0xAB, 0x3A, 0x5A,
1732 0xCE, 0xB1, 0xBD, 0x59, 0x69, 0xEE, 0xC0, 0x28,
1733 0x3E, 0x94, 0xD2, 0xCC, 0x91, 0x93, 0x73, 0x6A,
1734 0xD6, 0xB6, 0xC1, 0x42, 0x97, 0xB1, 0x13, 0xCF,
1735 0xF9, 0x55, 0x35, 0x50, 0xFC, 0x86, 0x75, 0x98,
1736 0x9F, 0xFC, 0x96, 0xB1, 0x43, 0x41, 0x8F, 0xFC,
1737 0x31, 0x09, 0x3B, 0x35, 0x22, 0x7B, 0x01, 0x96,
1738 0xA7, 0xF0, 0x78, 0x7B, 0x57, 0x00, 0xF2, 0xE5,
1739 0x92, 0x36, 0xCE, 0x64, 0xFD, 0x65, 0x09, 0xD8,
1740 0xBC, 0x5C, 0x82, 0x5C, 0x4C, 0x62, 0x5B, 0xCE,
1741 0x09, 0xB6, 0xCF, 0x4D, 0xAD, 0x8E, 0xDD, 0x96,
1742 0xB0, 0xCA, 0x52, 0xC1, 0xF4, 0x17, 0x0E, 0x2D,
1743 0x4E, 0xC3, 0xF9, 0x89, 0x1A, 0x24, 0x3D, 0x01,
1744 0xC8, 0x05, 0xBF, 0x7D, 0x2A, 0x46, 0xCD, 0x9A,
1745 0x66, 0xEE, 0x05, 0x78, 0x88, 0x2A, 0xEF, 0x37,
1746 0x9E, 0x72, 0x55, 0xDA, 0x82, 0x7A, 0x9B, 0xE8,
1747 0xF7, 0xA6, 0x74, 0xB8, 0x74, 0x39, 0x03, 0xE8,
1748 0xB9, 0x1F, 0x97, 0x78, 0xB9, 0xD9, 0x37, 0x16,
1749 0xFD, 0x2F, 0x31, 0xDE, 0xCC, 0x06, 0xD6, 0x5A,
1750 0xEB, 0xD1, 0xBB, 0x84, 0x30, 0x16, 0x81, 0xB0,
1751 0x7E, 0x04, 0x8C, 0x06, 0x67, 0xD1, 0x8A, 0x07,
1752 0x33, 0x76, 0x42, 0x8E, 0x87, 0xAB, 0x90, 0x6F,
1753 0x08, 0xED, 0x8D, 0xE8, 0xD0, 0x20, 0x00, 0x7E,
1754 0x3C, 0x4D, 0xA4, 0x40, 0x37, 0x13, 0x0F, 0x00,
1755 0x0C, 0xB7, 0x26, 0x03, 0x93, 0xD0, 0xBB, 0x08,
1756 0xD3, 0xCC, 0xA9, 0x28, 0xC2
1757 };
1758 unsigned char result[sizeof(output)] = { 0 };
1759
1760 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, mac, 0);
1761 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
1762 input_key, sizeof(input_key));
1763 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
1764 context, sizeof(context));
1765 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
1766 label, sizeof(label));
1767 *p = OSSL_PARAM_construct_end();
1768
1769 kctx = get_kdfbyname("KBKDF");
1770 ret = TEST_ptr(kctx)
1771 && TEST_size_t_eq(EVP_KDF_CTX_get_kdf_size(kctx), SIZE_MAX)
1772 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
1773 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1774
1775 EVP_KDF_CTX_free(kctx);
1776 return ret;
1777 }
1778
test_kdf_ss_hmac(void)1779 static int test_kdf_ss_hmac(void)
1780 {
1781 int ret;
1782 EVP_KDF_CTX *kctx;
1783 OSSL_PARAM params[6], *p = params;
1784 unsigned char out[16];
1785 static unsigned char z[] = {
1786 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
1787 };
1788 static unsigned char other[] = {
1789 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
1790 };
1791 static unsigned char salt[] = {
1792 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
1793 0x3f,0x89
1794 };
1795 static const unsigned char expected[sizeof(out)] = {
1796 0x44,0xf6,0x76,0xe8,0x5c,0x1b,0x1a,0x8b,0xbc,0x3d,0x31,0x92,0x18,0x63,
1797 0x1c,0xa3
1798 };
1799
1800 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
1801 (char *)OSSL_MAC_NAME_HMAC, 0);
1802 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
1803 (char *)"sha256", 0);
1804 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
1805 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
1806 sizeof(other));
1807 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt,
1808 sizeof(salt));
1809 *p = OSSL_PARAM_construct_end();
1810
1811 ret =
1812 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
1813 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
1814 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1815
1816 EVP_KDF_CTX_free(kctx);
1817 return ret;
1818 }
1819
test_kdf_ss_kmac(void)1820 static int test_kdf_ss_kmac(void)
1821 {
1822 int ret;
1823 EVP_KDF_CTX *kctx;
1824 OSSL_PARAM params[7], *p = params;
1825 unsigned char out[64];
1826 size_t mac_size = 20;
1827 static unsigned char z[] = {
1828 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
1829 };
1830 static unsigned char other[] = {
1831 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
1832 };
1833 static unsigned char salt[] = {
1834 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
1835 0x3f,0x89
1836 };
1837 static const unsigned char expected[sizeof(out)] = {
1838 0xe9,0xc1,0x84,0x53,0xa0,0x62,0xb5,0x3b,0xdb,0xfc,0xbb,0x5a,0x34,0xbd,
1839 0xb8,0xe5,0xe7,0x07,0xee,0xbb,0x5d,0xd1,0x34,0x42,0x43,0xd8,0xcf,0xc2,
1840 0xc2,0xe6,0x33,0x2f,0x91,0xbd,0xa5,0x86,0xf3,0x7d,0xe4,0x8a,0x65,0xd4,
1841 0xc5,0x14,0xfd,0xef,0xaa,0x1e,0x67,0x54,0xf3,0x73,0xd2,0x38,0xe1,0x95,
1842 0xae,0x15,0x7e,0x1d,0xe8,0x14,0x98,0x03
1843 };
1844
1845 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
1846 (char *)OSSL_MAC_NAME_KMAC128, 0);
1847 /* The digest parameter is not needed here and should be ignored */
1848 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
1849 (char *)"SHA256", 0);
1850 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
1851 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
1852 sizeof(other));
1853 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt,
1854 sizeof(salt));
1855 *p++ = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_MAC_SIZE, &mac_size);
1856 *p = OSSL_PARAM_construct_end();
1857
1858 ret =
1859 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
1860 && TEST_size_t_eq(EVP_KDF_CTX_get_kdf_size(kctx), 0)
1861 && TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1)
1862 /* The bug fix for KMAC returning SIZE_MAX was added in 3.0.8 */
1863 && (fips_provider_version_lt(NULL, 3, 0, 8)
1864 || TEST_size_t_eq(EVP_KDF_CTX_get_kdf_size(kctx), SIZE_MAX))
1865 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)
1866 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1867
1868 EVP_KDF_CTX_free(kctx);
1869 return ret;
1870 }
1871
test_kdf_sshkdf(void)1872 static int test_kdf_sshkdf(void)
1873 {
1874 int ret;
1875 EVP_KDF_CTX *kctx;
1876 OSSL_PARAM params[6], *p = params;
1877 char kdftype = EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV;
1878 unsigned char out[8];
1879 /* Test data from NIST CAVS 14.1 test vectors */
1880 static unsigned char key[] = {
1881 0x00, 0x00, 0x00, 0x81, 0x00, 0x87, 0x5c, 0x55, 0x1c, 0xef, 0x52, 0x6a,
1882 0x4a, 0x8b, 0xe1, 0xa7, 0xdf, 0x27, 0xe9, 0xed, 0x35, 0x4b, 0xac, 0x9a,
1883 0xfb, 0x71, 0xf5, 0x3d, 0xba, 0xe9, 0x05, 0x67, 0x9d, 0x14, 0xf9, 0xfa,
1884 0xf2, 0x46, 0x9c, 0x53, 0x45, 0x7c, 0xf8, 0x0a, 0x36, 0x6b, 0xe2, 0x78,
1885 0x96, 0x5b, 0xa6, 0x25, 0x52, 0x76, 0xca, 0x2d, 0x9f, 0x4a, 0x97, 0xd2,
1886 0x71, 0xf7, 0x1e, 0x50, 0xd8, 0xa9, 0xec, 0x46, 0x25, 0x3a, 0x6a, 0x90,
1887 0x6a, 0xc2, 0xc5, 0xe4, 0xf4, 0x8b, 0x27, 0xa6, 0x3c, 0xe0, 0x8d, 0x80,
1888 0x39, 0x0a, 0x49, 0x2a, 0xa4, 0x3b, 0xad, 0x9d, 0x88, 0x2c, 0xca, 0xc2,
1889 0x3d, 0xac, 0x88, 0xbc, 0xad, 0xa4, 0xb4, 0xd4, 0x26, 0xa3, 0x62, 0x08,
1890 0x3d, 0xab, 0x65, 0x69, 0xc5, 0x4c, 0x22, 0x4d, 0xd2, 0xd8, 0x76, 0x43,
1891 0xaa, 0x22, 0x76, 0x93, 0xe1, 0x41, 0xad, 0x16, 0x30, 0xce, 0x13, 0x14,
1892 0x4e
1893 };
1894 static unsigned char xcghash[] = {
1895 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
1896 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
1897 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
1898 };
1899 static unsigned char sessid[] = {
1900 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
1901 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
1902 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
1903 };
1904 static const unsigned char expected[sizeof(out)] = {
1905 0x41, 0xff, 0x2e, 0xad, 0x16, 0x83, 0xf1, 0xe6
1906 };
1907
1908 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
1909 (char *)"sha256", 0);
1910 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key,
1911 sizeof(key));
1912 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH,
1913 xcghash, sizeof(xcghash));
1914 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID,
1915 sessid, sizeof(sessid));
1916 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE,
1917 &kdftype, sizeof(kdftype));
1918 *p = OSSL_PARAM_construct_end();
1919
1920 ret =
1921 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSHKDF))
1922 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
1923 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1924
1925 EVP_KDF_CTX_free(kctx);
1926 return ret;
1927 }
1928
test_kdfs_same(EVP_KDF * kdf1,EVP_KDF * kdf2)1929 static int test_kdfs_same(EVP_KDF *kdf1, EVP_KDF *kdf2)
1930 {
1931 /* Fast path in case the two are the same algorithm pointer */
1932 if (kdf1 == kdf2)
1933 return 1;
1934 /*
1935 * Compare their names and providers instead.
1936 * This is necessary in a non-caching build (or a cache flush during fetch)
1937 * because without the algorithm in the cache, fetching it a second time
1938 * will result in a different pointer.
1939 */
1940 return TEST_ptr_eq(EVP_KDF_get0_provider(kdf1), EVP_KDF_get0_provider(kdf2))
1941 && TEST_str_eq(EVP_KDF_get0_name(kdf1), EVP_KDF_get0_name(kdf2));
1942 }
1943
test_kdf_get_kdf(void)1944 static int test_kdf_get_kdf(void)
1945 {
1946 EVP_KDF *kdf1 = NULL, *kdf2 = NULL;
1947 ASN1_OBJECT *obj;
1948 int ok = 1;
1949
1950 if (!TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2))
1951 || !TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_PBKDF2, NULL))
1952 || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(OBJ_obj2nid(obj)),
1953 NULL))
1954 || !test_kdfs_same(kdf1, kdf2))
1955 ok = 0;
1956 EVP_KDF_free(kdf1);
1957 kdf1 = NULL;
1958 EVP_KDF_free(kdf2);
1959 kdf2 = NULL;
1960
1961 if (!TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, SN_tls1_prf, NULL))
1962 || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, LN_tls1_prf, NULL))
1963 || !test_kdfs_same(kdf1, kdf2))
1964 ok = 0;
1965 /* kdf1 is reused below, so don't free it here */
1966 EVP_KDF_free(kdf2);
1967 kdf2 = NULL;
1968
1969 if (!TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(NID_tls1_prf), NULL))
1970 || !test_kdfs_same(kdf1, kdf2))
1971 ok = 0;
1972 EVP_KDF_free(kdf1);
1973 kdf1 = NULL;
1974 EVP_KDF_free(kdf2);
1975 kdf2 = NULL;
1976
1977 return ok;
1978 }
1979
1980 #if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES)
test_kdf_x942_asn1(void)1981 static int test_kdf_x942_asn1(void)
1982 {
1983 int ret;
1984 EVP_KDF_CTX *kctx = NULL;
1985 OSSL_PARAM params[4], *p = params;
1986 const char *cek_alg = SN_id_smime_alg_CMS3DESwrap;
1987 unsigned char out[24];
1988 /* RFC2631 Section 2.1.6 Test data */
1989 static unsigned char z[] = {
1990 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,
1991 0x0e,0x0f,0x10,0x11,0x12,0x13
1992 };
1993 static const unsigned char expected[sizeof(out)] = {
1994 0xa0,0x96,0x61,0x39,0x23,0x76,0xf7,0x04,
1995 0x4d,0x90,0x52,0xa3,0x97,0x88,0x32,0x46,
1996 0xb6,0x7f,0x5f,0x1e,0xf6,0x3e,0xb5,0xfb
1997 };
1998
1999 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
2000 (char *)"sha1", 0);
2001 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z,
2002 sizeof(z));
2003 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
2004 (char *)cek_alg, 0);
2005 *p = OSSL_PARAM_construct_end();
2006
2007 ret =
2008 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X942KDF_ASN1))
2009 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
2010 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
2011
2012 EVP_KDF_CTX_free(kctx);
2013 return ret;
2014 }
2015 #endif /* OPENSSL_NO_CMS */
2016
test_kdf_krb5kdf(void)2017 static int test_kdf_krb5kdf(void)
2018 {
2019 int ret;
2020 EVP_KDF_CTX *kctx;
2021 OSSL_PARAM params[4], *p = params;
2022 unsigned char out[16];
2023 static unsigned char key[] = {
2024 0x42, 0x26, 0x3C, 0x6E, 0x89, 0xF4, 0xFC, 0x28,
2025 0xB8, 0xDF, 0x68, 0xEE, 0x09, 0x79, 0x9F, 0x15
2026 };
2027 static unsigned char constant[] = {
2028 0x00, 0x00, 0x00, 0x02, 0x99
2029 };
2030 static const unsigned char expected[sizeof(out)] = {
2031 0x34, 0x28, 0x0A, 0x38, 0x2B, 0xC9, 0x27, 0x69,
2032 0xB2, 0xDA, 0x2F, 0x9E, 0xF0, 0x66, 0x85, 0x4B
2033 };
2034
2035 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER,
2036 (char *)"AES-128-CBC", 0);
2037 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key,
2038 sizeof(key));
2039 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_CONSTANT,
2040 constant, sizeof(constant));
2041 *p = OSSL_PARAM_construct_end();
2042
2043 ret =
2044 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_KRB5KDF))
2045 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
2046 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
2047
2048 EVP_KDF_CTX_free(kctx);
2049 return ret;
2050 }
2051
test_kdf_hmac_drbg_settables(void)2052 static int test_kdf_hmac_drbg_settables(void)
2053 {
2054 int ret = 0, i = 0, j = 0;
2055 EVP_KDF_CTX *kctx = NULL;
2056 const OSSL_PARAM *settableparams;
2057 OSSL_PARAM params[5];
2058 static const unsigned char ent[32] = { 0 };
2059 unsigned char out[32];
2060 char digestname[32];
2061 char macname[32];
2062
2063 /* Test there are settables */
2064 if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HMACDRBGKDF))
2065 || !TEST_ptr(settableparams = EVP_KDF_CTX_settable_params(kctx)))
2066 goto err;
2067
2068 /* Fail if no params have been set when doing a derive */
2069 if (!TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
2070 goto err;
2071
2072 /* Fail if we pass the wrong type for params */
2073 params[1] = OSSL_PARAM_construct_end();
2074 for (i = 0; settableparams[i].key != NULL; ++i) {
2075 /*
2076 * Skip "properties" and "engine" keys since they returns 1 unless
2077 * the digest is also set
2078 */
2079 if (OPENSSL_strcasecmp(settableparams[i].key,
2080 OSSL_KDF_PARAM_PROPERTIES) != 0
2081 && OPENSSL_strcasecmp(settableparams[i].key,
2082 OSSL_ALG_PARAM_ENGINE) != 0) {
2083 TEST_note("Testing set int into %s fails", settableparams[i].key);
2084 params[0] = OSSL_PARAM_construct_int(settableparams[i].key, &j);
2085 if (!TEST_int_le(EVP_KDF_CTX_set_params(kctx, params), 0))
2086 goto err;
2087 }
2088 }
2089 /* Test that we can set values multiple times */
2090 params[0] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY,
2091 (char *)ent, sizeof(ent));
2092 params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE,
2093 (char *)ent, sizeof(ent));
2094 params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, "SHA256",
2095 0);
2096 params[3] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES, "",
2097 0);
2098 params[4] = OSSL_PARAM_construct_end();
2099 if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1))
2100 goto err;
2101 if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1))
2102 goto err;
2103 /* Test we can retrieve values back */
2104 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
2105 digestname, sizeof(digestname));
2106 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC,
2107 macname, sizeof(macname));
2108 params[2] = OSSL_PARAM_construct_end();
2109 if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params), 1)
2110 || !TEST_mem_eq(digestname, params[0].return_size, "SHA2-256", 8)
2111 || !TEST_mem_eq(macname, params[1].return_size, "HMAC", 4))
2112 goto err;
2113
2114 /* Test the derive */
2115 if (!TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 1))
2116 goto err;
2117
2118 /* test that XOF digests are not allowed */
2119 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
2120 "shake256", 0);
2121 params[1] = OSSL_PARAM_construct_end();
2122 if (!TEST_int_le(EVP_KDF_CTX_set_params(kctx, params), 0))
2123 goto err;
2124
2125 ret = 1;
2126 err:
2127 EVP_KDF_CTX_free(kctx);
2128 return ret;
2129 }
2130
test_kdf_hmac_drbg_gettables(void)2131 static int test_kdf_hmac_drbg_gettables(void)
2132 {
2133 int ret = 0, i, j = 0;
2134 EVP_KDF_CTX *kctx = NULL;
2135 const OSSL_PARAM *gettableparams;
2136 OSSL_PARAM params[3];
2137 char buf[64];
2138
2139 /* Test there are gettables */
2140 if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HMACDRBGKDF))
2141 || !TEST_ptr(gettableparams = EVP_KDF_CTX_gettable_params(kctx)))
2142 goto err;
2143 /* Fail if we pass the wrong type for params */
2144 params[1] = OSSL_PARAM_construct_end();
2145 for (i = 0; gettableparams[i].key != NULL; ++i) {
2146 params[0] = OSSL_PARAM_construct_int(gettableparams[i].key, &j);
2147 if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params), 0))
2148 goto err;
2149 }
2150 /* fail to get params if they are not set yet */
2151 for (i = 0; gettableparams[i].key != NULL; ++i) {
2152 params[0] = OSSL_PARAM_construct_utf8_string(gettableparams[i].key,
2153 buf, sizeof(buf));
2154 if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params), 0))
2155 goto err;
2156 }
2157 ret = 1;
2158 err:
2159 EVP_KDF_CTX_free(kctx);
2160 return ret;
2161 }
2162
2163 /* Test that changing the KBKDF algorithm from KMAC to HMAC works correctly */
test_kbkdf_mac_change(void)2164 static int test_kbkdf_mac_change(void)
2165 {
2166 int ret = 0;
2167 EVP_KDF_CTX *kctx = NULL;
2168 OSSL_PARAM params[9], *p = params;
2169 /* Test data taken from the evptest corpus */
2170 int l = 0, sep = 0, r = 8;
2171 static /* const */ unsigned char key[] = {
2172 0x3e, 0xdc, 0x6b, 0x5b, 0x8f, 0x7a, 0xad, 0xbd,
2173 0x71, 0x37, 0x32, 0xb4, 0x82, 0xb8, 0xf9, 0x79,
2174 0x28, 0x6e, 0x1e, 0xa3, 0xb8, 0xf8, 0xf9, 0x9c,
2175 0x30, 0xc8, 0x84, 0xcf, 0xe3, 0x34, 0x9b, 0x83
2176 };
2177 static /* const */ unsigned char info[] = {
2178 0x98, 0xe9, 0x98, 0x8b, 0xb4, 0xcc, 0x8b, 0x34,
2179 0xd7, 0x92, 0x2e, 0x1c, 0x68, 0xad, 0x69, 0x2b,
2180 0xa2, 0xa1, 0xd9, 0xae, 0x15, 0x14, 0x95, 0x71,
2181 0x67, 0x5f, 0x17, 0xa7, 0x7a, 0xd4, 0x9e, 0x80,
2182 0xc8, 0xd2, 0xa8, 0x5e, 0x83, 0x1a, 0x26, 0x44,
2183 0x5b, 0x1f, 0x0f, 0xf4, 0x4d, 0x70, 0x84, 0xa1,
2184 0x72, 0x06, 0xb4, 0x89, 0x6c, 0x81, 0x12, 0xda,
2185 0xad, 0x18, 0x60, 0x5a
2186 };
2187 static const unsigned char output[] = {
2188 0x6c, 0x03, 0x76, 0x52, 0x99, 0x06, 0x74, 0xa0,
2189 0x78, 0x44, 0x73, 0x2d, 0x0a, 0xd9, 0x85, 0xf9
2190 };
2191 unsigned char out[sizeof(output)];
2192
2193 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
2194 OSSL_MAC_NAME_KMAC128, 0);
2195 params[1] = OSSL_PARAM_construct_end();
2196 if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_KBKDF))
2197 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params)))
2198 goto err;
2199
2200 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, "COUNTER", 0);
2201 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, "HMAC", 0);
2202 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST, "SHA256", 0);
2203 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_L, &l);
2204 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR, &sep);
2205 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_R, &r);
2206 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
2207 key, sizeof(key));
2208 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
2209 info, sizeof(info));
2210 *p = OSSL_PARAM_construct_end();
2211 if (!TEST_true(EVP_KDF_derive(kctx, out, sizeof(out), params))
2212 || !TEST_mem_eq(out, sizeof(out), output, sizeof(output)))
2213 goto err;
2214
2215 ret = 1;
2216 err:
2217 EVP_KDF_CTX_free(kctx);
2218 return ret;
2219 }
2220
setup_tests(void)2221 int setup_tests(void)
2222 {
2223 ADD_TEST(test_kdf_pbkdf1);
2224 ADD_TEST(test_kdf_pbkdf1_key_too_long);
2225 #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
2226 ADD_TEST(test_kdf_kbkdf_6803_128);
2227 ADD_TEST(test_kdf_kbkdf_6803_256);
2228 #endif
2229 ADD_TEST(test_kdf_kbkdf_invalid_digest);
2230 ADD_TEST(test_kdf_kbkdf_invalid_mac);
2231 ADD_TEST(test_kdf_kbkdf_invalid_r);
2232 ADD_TEST(test_kdf_kbkdf_zero_output_size);
2233 ADD_TEST(test_kdf_kbkdf_empty_key);
2234 ADD_TEST(test_kdf_kbkdf_1byte_key);
2235 ADD_TEST(test_kdf_kbkdf_8009_prf1);
2236 ADD_TEST(test_kdf_kbkdf_8009_prf2);
2237 #if !defined(OPENSSL_NO_CMAC)
2238 ADD_TEST(test_kdf_kbkdf_fixedinfo);
2239 #endif
2240 if (fips_provider_version_ge(NULL, 3, 1, 0))
2241 ADD_TEST(test_kdf_kbkdf_kmac);
2242 ADD_TEST(test_kdf_get_kdf);
2243 ADD_TEST(test_kdf_tls1_prf);
2244 ADD_TEST(test_kdf_tls1_prf_invalid_digest);
2245 ADD_TEST(test_kdf_tls1_prf_zero_output_size);
2246 ADD_TEST(test_kdf_tls1_prf_empty_secret);
2247 ADD_TEST(test_kdf_tls1_prf_1byte_secret);
2248 ADD_TEST(test_kdf_tls1_prf_empty_seed);
2249 ADD_TEST(test_kdf_tls1_prf_1byte_seed);
2250 ADD_TEST(test_kdf_hkdf);
2251 ADD_TEST(test_kdf_hkdf_invalid_digest);
2252 ADD_TEST(test_kdf_hkdf_fixed_digest_change_digest);
2253 ADD_TEST(test_kdf_hkdf_fixed_digest_preserve_digest);
2254 ADD_TEST(test_kdf_hkdf_reset);
2255 ADD_TEST(test_kdf_hkdf_zero_output_size);
2256 ADD_TEST(test_kdf_hkdf_empty_key);
2257 ADD_TEST(test_kdf_hkdf_1byte_key);
2258 ADD_TEST(test_kdf_hkdf_empty_salt);
2259 ADD_TEST(test_kdf_hkdf_gettables);
2260 ADD_TEST(test_kdf_hkdf_gettables_extractonly);
2261 ADD_TEST(test_kdf_hkdf_gettables_no_digest);
2262 ADD_TEST(test_kdf_hkdf_derive_set_params_fail);
2263 ADD_TEST(test_kdf_hkdf_set_invalid_mode);
2264 ADD_TEST(test_kdf_hkdf_set_ctx_param_fail);
2265 ADD_TEST(test_kdf_pbkdf2);
2266 ADD_TEST(test_kdf_pbkdf2_small_output);
2267 ADD_TEST(test_kdf_pbkdf2_large_output);
2268 ADD_TEST(test_kdf_pbkdf2_small_salt);
2269 ADD_TEST(test_kdf_pbkdf2_small_iterations);
2270 ADD_TEST(test_kdf_pbkdf2_small_salt_pkcs5);
2271 ADD_TEST(test_kdf_pbkdf2_small_iterations_pkcs5);
2272 ADD_TEST(test_kdf_pbkdf2_invalid_digest);
2273 #ifndef OPENSSL_NO_SCRYPT
2274 ADD_TEST(test_kdf_scrypt);
2275 #endif
2276 ADD_TEST(test_kdf_ss_hash);
2277 ADD_TEST(test_kdf_ss_hmac);
2278 ADD_TEST(test_kdf_ss_kmac);
2279 ADD_TEST(test_kdf_sshkdf);
2280 ADD_TEST(test_kdf_x963);
2281 #if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES)
2282 ADD_TEST(test_kdf_x942_asn1);
2283 #endif
2284 ADD_TEST(test_kdf_krb5kdf);
2285 ADD_TEST(test_kdf_hmac_drbg_settables);
2286 ADD_TEST(test_kdf_hmac_drbg_gettables);
2287 ADD_TEST(test_kbkdf_mac_change);
2288 return 1;
2289 }
2290