]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/evp_kdf_test.c
Add PBKDF1 to the legacy provider
[thirdparty/openssl.git] / test / evp_kdf_test.c
CommitLineData
5a285add 1/*
a28d06f3 2 * Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
4757a347 3 * Copyright (c) 2018-2020, Oracle and/or its affiliates. All rights reserved.
5a285add
DM
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>
81ff9eeb 18#include <openssl/core_names.h>
6251895c 19#include "internal/numbers.h"
5a285add
DM
20#include "testutil.h"
21
bf5739a0
P
22static EVP_KDF_CTX *get_kdfbyname(const char *name)
23{
24 EVP_KDF *kdf = EVP_KDF_fetch(NULL, name, NULL);
660c5344 25 EVP_KDF_CTX *kctx = EVP_KDF_CTX_new(kdf);
bf5739a0
P
26
27 EVP_KDF_free(kdf);
28 return kctx;
29}
30
1cae59d1
JS
31static OSSL_PARAM *construct_tls1_prf_params(const char *digest, const char *secret,
32 const char *seed)
33{
34 OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 4);
35 OSSL_PARAM *p = params;
36
0f183675
JS
37 if (params == NULL)
38 return NULL;
39
1cae59d1
JS
40 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
41 (char *)digest, 0);
42 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
43 (unsigned char *)secret,
44 strlen(secret));
45 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
46 (unsigned char *)seed,
47 strlen(seed));
48 *p = OSSL_PARAM_construct_end();
49
50 return params;
51}
52
5a285add
DM
53static int test_kdf_tls1_prf(void)
54{
a3c62426 55 int ret;
d2ba8123 56 EVP_KDF_CTX *kctx = NULL;
5a285add 57 unsigned char out[16];
1cae59d1 58 OSSL_PARAM *params;
8bbeaaa4 59 static const unsigned char expected[sizeof(out)] = {
a3c62426
SL
60 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
61 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
62 };
5a285add 63
1cae59d1 64 params = construct_tls1_prf_params("sha256", "secret", "seed");
bf5739a0 65
0f183675
JS
66 ret = TEST_ptr(params)
67 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
05cdec39 68 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
d2ba8123 69 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
5a285add 70
660c5344 71 EVP_KDF_CTX_free(kctx);
1cae59d1 72 OPENSSL_free(params);
5a285add
DM
73 return ret;
74}
75
1cae59d1
JS
76static int test_kdf_tls1_prf_invalid_digest(void)
77{
78 int ret;
79 EVP_KDF_CTX *kctx = NULL;
80 OSSL_PARAM *params;
81
82 params = construct_tls1_prf_params("blah", "secret", "seed");
83
0f183675
JS
84 ret = TEST_ptr(params)
85 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
1cae59d1
JS
86 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
87
88 EVP_KDF_CTX_free(kctx);
89 OPENSSL_free(params);
90 return ret;
91}
92
93static int test_kdf_tls1_prf_zero_output_size(void)
94{
95 int ret;
96 EVP_KDF_CTX *kctx = NULL;
97 unsigned char out[16];
98 OSSL_PARAM *params;
99
100 params = construct_tls1_prf_params("sha256", "secret", "seed");
101
102 /* Negative test - derive should fail */
0f183675
JS
103 ret = TEST_ptr(params)
104 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
1cae59d1 105 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 106 && TEST_int_eq(EVP_KDF_derive(kctx, out, 0, NULL), 0);
1cae59d1
JS
107
108 EVP_KDF_CTX_free(kctx);
109 OPENSSL_free(params);
110 return ret;
111}
112
113static int test_kdf_tls1_prf_empty_secret(void)
114{
115 int ret;
116 EVP_KDF_CTX *kctx = NULL;
117 unsigned char out[16];
118 OSSL_PARAM *params;
119
120 params = construct_tls1_prf_params("sha256", "", "seed");
121
0f183675
JS
122 ret = TEST_ptr(params)
123 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
05cdec39 124 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
1cae59d1
JS
125
126 EVP_KDF_CTX_free(kctx);
127 OPENSSL_free(params);
128 return ret;
129}
130
131static int test_kdf_tls1_prf_1byte_secret(void)
132{
133 int ret;
134 EVP_KDF_CTX *kctx = NULL;
135 unsigned char out[16];
136 OSSL_PARAM *params;
137
138 params = construct_tls1_prf_params("sha256", "1", "seed");
139
0f183675
JS
140 ret = TEST_ptr(params)
141 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
05cdec39 142 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
1cae59d1
JS
143
144 EVP_KDF_CTX_free(kctx);
145 OPENSSL_free(params);
146 return ret;
147}
148
149static int test_kdf_tls1_prf_empty_seed(void)
150{
151 int ret;
152 EVP_KDF_CTX *kctx = NULL;
153 unsigned char out[16];
154 OSSL_PARAM *params;
155
156 params = construct_tls1_prf_params("sha256", "secret", "");
157
158 /* Negative test - derive should fail */
0f183675
JS
159 ret = TEST_ptr(params)
160 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
1cae59d1 161 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 162 && TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0);
1cae59d1
JS
163
164 EVP_KDF_CTX_free(kctx);
165 OPENSSL_free(params);
166 return ret;
167}
168
169static int test_kdf_tls1_prf_1byte_seed(void)
170{
171 int ret;
172 EVP_KDF_CTX *kctx = NULL;
173 unsigned char out[16];
174 OSSL_PARAM *params;
175
176 params = construct_tls1_prf_params("sha256", "secret", "1");
177
0f183675
JS
178 ret = TEST_ptr(params)
179 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
05cdec39 180 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
1cae59d1
JS
181
182 EVP_KDF_CTX_free(kctx);
183 OPENSSL_free(params);
184 return ret;
185}
186
187static OSSL_PARAM *construct_hkdf_params(char *digest, char *key,
188 size_t keylen, char *salt, char *info)
189{
190 OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
191 OSSL_PARAM *p = params;
192
0f183675
JS
193 if (params == NULL)
194 return NULL;
195
1cae59d1
JS
196 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
197 digest, 0);
198 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
199 salt, strlen(salt));
200 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
201 (unsigned char *)key, keylen);
202 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
203 info, strlen(info));
204 *p = OSSL_PARAM_construct_end();
205
206 return params;
207}
208
5a285add
DM
209static int test_kdf_hkdf(void)
210{
a3c62426 211 int ret;
0f183675 212 EVP_KDF_CTX *kctx = NULL;
5a285add 213 unsigned char out[10];
1cae59d1 214 OSSL_PARAM *params;
8bbeaaa4 215 static const unsigned char expected[sizeof(out)] = {
a3c62426
SL
216 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
217 };
5a285add 218
1cae59d1 219 params = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
bf5739a0 220
0f183675
JS
221 ret = TEST_ptr(params)
222 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
05cdec39 223 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
d2ba8123 224 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
5a285add 225
660c5344 226 EVP_KDF_CTX_free(kctx);
1cae59d1
JS
227 OPENSSL_free(params);
228 return ret;
229}
230
231static int test_kdf_hkdf_invalid_digest(void)
232{
233 int ret;
0f183675 234 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
235 OSSL_PARAM *params;
236
237 params = construct_hkdf_params("blah", "secret", 6, "salt", "label");
238
0f183675
JS
239 ret = TEST_ptr(params)
240 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
1cae59d1
JS
241 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
242
243 EVP_KDF_CTX_free(kctx);
244 OPENSSL_free(params);
245 return ret;
246}
247
248static int test_kdf_hkdf_zero_output_size(void)
249{
250 int ret;
0f183675 251 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
252 unsigned char out[10];
253 OSSL_PARAM *params;
254
255 params = construct_hkdf_params("sha256", "secret", 6, "salt", "label");
256
257 /* Negative test - derive should fail */
0f183675
JS
258 ret = TEST_ptr(params)
259 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
1cae59d1 260 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 261 && TEST_int_eq(EVP_KDF_derive(kctx, out, 0, NULL), 0);
1cae59d1
JS
262
263 EVP_KDF_CTX_free(kctx);
264 OPENSSL_free(params);
265 return ret;
266}
267
268static int test_kdf_hkdf_empty_key(void)
269{
270 int ret;
0f183675 271 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
272 unsigned char out[10];
273 OSSL_PARAM *params;
274
275 params = construct_hkdf_params("sha256", "", 0, "salt", "label");
276
0f183675
JS
277 ret = TEST_ptr(params)
278 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
05cdec39 279 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
1cae59d1
JS
280
281 EVP_KDF_CTX_free(kctx);
282 OPENSSL_free(params);
283 return ret;
284}
285
286static int test_kdf_hkdf_1byte_key(void)
287{
288 int ret;
0f183675 289 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
290 unsigned char out[10];
291 OSSL_PARAM *params;
292
293 params = construct_hkdf_params("sha256", "1", 1, "salt", "label");
294
0f183675
JS
295 ret = TEST_ptr(params)
296 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
05cdec39 297 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
1cae59d1
JS
298
299 EVP_KDF_CTX_free(kctx);
300 OPENSSL_free(params);
301 return ret;
302}
303
304static int test_kdf_hkdf_empty_salt(void)
305{
306 int ret;
0f183675 307 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
308 unsigned char out[10];
309 OSSL_PARAM *params;
310
311 params = construct_hkdf_params("sha256", "secret", 6, "", "label");
312
0f183675
JS
313 ret = TEST_ptr(params)
314 && TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
05cdec39 315 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0);
1cae59d1
JS
316
317 EVP_KDF_CTX_free(kctx);
318 OPENSSL_free(params);
5a285add
DM
319 return ret;
320}
321
0f183675
JS
322static OSSL_PARAM *construct_pbkdf1_params(char *pass, char *digest, char *salt,
323 unsigned int *iter)
324{
325 OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 5);
326 OSSL_PARAM *p = params;
327
328 if (params == NULL)
329 return NULL;
330
331 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
332 (unsigned char *)pass, strlen(pass));
333 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
334 (unsigned char *)salt, strlen(salt));
335 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter);
336 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
337 digest, 0);
338 *p = OSSL_PARAM_construct_end();
339
340 return params;
341}
342
343static int test_kdf_pbkdf1(void)
344{
345 int ret = 0;
346 EVP_KDF_CTX *kctx = NULL;
347 unsigned char out[25];
348 unsigned int iterations = 4096;
349 OSSL_PARAM *params;
350 OSSL_PROVIDER *prov = NULL;
351 const unsigned char expected[sizeof(out)] = {
352 0xfb, 0x83, 0x4d, 0x36, 0x6d, 0xbc, 0x53, 0x87, 0x35, 0x1b, 0x34, 0x75,
353 0x95, 0x88, 0x32, 0x4f, 0x3e, 0x82, 0x81, 0x01, 0x21, 0x93, 0x64, 0x00,
354 0xcc
355 };
356
357 /* PBKDF1 only available in the legacy provider */
358 prov = OSSL_PROVIDER_load(NULL, "legacy");
359 if (prov == NULL)
360 return TEST_skip("PBKDF1 only available in legacy provider");
361
362 params = construct_pbkdf1_params("passwordPASSWORDpassword", "sha256",
363 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
364 &iterations);
365
366 if (!TEST_ptr(params)
367 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF1))
368 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
369 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)
370 || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)))
371 goto err;
372
373 ret = 1;
374err:
375 EVP_KDF_CTX_free(kctx);
376 OPENSSL_free(params);
377 OSSL_PROVIDER_unload(prov);
378 return ret;
379}
380
1cae59d1
JS
381static OSSL_PARAM *construct_pbkdf2_params(char *pass, char *digest, char *salt,
382 unsigned int *iter, int *mode)
383{
384 OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 6);
385 OSSL_PARAM *p = params;
386
0f183675
JS
387 if (params == NULL)
388 return NULL;
389
1cae59d1
JS
390 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
391 (unsigned char *)pass, strlen(pass));
392 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
393 (unsigned char *)salt, strlen(salt));
394 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, iter);
395 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
396 digest, 0);
397 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, mode);
398 *p = OSSL_PARAM_construct_end();
399
400 return params;
401}
402
5a285add
DM
403static int test_kdf_pbkdf2(void)
404{
bf5739a0 405 int ret = 0;
0f183675 406 EVP_KDF_CTX *kctx = NULL;
f0efeea2 407 unsigned char out[25];
bf5739a0
P
408 unsigned int iterations = 4096;
409 int mode = 0;
1cae59d1 410 OSSL_PARAM *params;
f0efeea2
SL
411 const unsigned char expected[sizeof(out)] = {
412 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
413 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
414 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18,
415 0x1c
a3c62426 416 };
5a285add 417
1cae59d1
JS
418 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
419 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
420 &iterations, &mode);
bf5739a0 421
0f183675
JS
422 if (!TEST_ptr(params)
423 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
05cdec39 424 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
1cae59d1
JS
425 || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected)))
426 goto err;
427
428 ret = 1;
429err:
430 EVP_KDF_CTX_free(kctx);
431 OPENSSL_free(params);
432 return ret;
433}
434
435static int test_kdf_pbkdf2_small_output(void)
436{
437 int ret = 0;
0f183675 438 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
439 unsigned char out[25];
440 unsigned int iterations = 4096;
441 int mode = 0;
442 OSSL_PARAM *params;
443
444 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
445 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
446 &iterations, &mode);
447
0f183675
JS
448 if (!TEST_ptr(params)
449 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
660c5344 450 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
bf5739a0 451 /* A key length that is too small should fail */
05cdec39 452 || !TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1, NULL), 0))
1cae59d1
JS
453 goto err;
454
455 ret = 1;
456err:
457 EVP_KDF_CTX_free(kctx);
458 OPENSSL_free(params);
459 return ret;
460}
461
462static int test_kdf_pbkdf2_large_output(void)
463{
464 int ret = 0;
0f183675 465 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
466 unsigned char out[25];
467 size_t len = 0;
468 unsigned int iterations = 4096;
469 int mode = 0;
470 OSSL_PARAM *params;
471
472 if (sizeof(len) > 32)
473 len = SIZE_MAX;
474
475 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
476 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
477 &iterations, &mode);
478
0f183675
JS
479 if (!TEST_ptr(params)
480 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
bf5739a0 481 /* A key length that is too large should fail */
05cdec39
P
482 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
483 || (len != 0 && !TEST_int_eq(EVP_KDF_derive(kctx, out, len, NULL), 0)))
bf5739a0 484 goto err;
1cae59d1 485
bf5739a0
P
486 ret = 1;
487err:
660c5344 488 EVP_KDF_CTX_free(kctx);
1cae59d1
JS
489 OPENSSL_free(params);
490 return ret;
491}
492
493static int test_kdf_pbkdf2_small_salt(void)
494{
495 int ret = 0;
0f183675 496 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
497 unsigned int iterations = 4096;
498 int mode = 0;
499 OSSL_PARAM *params;
500
501 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
502 "saltSALT",
503 &iterations, &mode);
504
0f183675
JS
505 if (!TEST_ptr(params)
506 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
1cae59d1
JS
507 /* A salt that is too small should fail */
508 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
509 goto err;
510
511 ret = 1;
512err:
513 EVP_KDF_CTX_free(kctx);
514 OPENSSL_free(params);
515 return ret;
516}
517
518static int test_kdf_pbkdf2_small_iterations(void)
519{
520 int ret = 0;
0f183675 521 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
522 unsigned int iterations = 1;
523 int mode = 0;
524 OSSL_PARAM *params;
525
526 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
527 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
528 &iterations, &mode);
529
0f183675
JS
530 if (!TEST_ptr(params)
531 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
1cae59d1
JS
532 /* An iteration count that is too small should fail */
533 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
534 goto err;
535
536 ret = 1;
537err:
538 EVP_KDF_CTX_free(kctx);
539 OPENSSL_free(params);
540 return ret;
541}
542
543static int test_kdf_pbkdf2_small_salt_pkcs5(void)
544{
545 int ret = 0;
0f183675 546 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
547 unsigned char out[25];
548 unsigned int iterations = 4096;
549 int mode = 1;
550 OSSL_PARAM *params;
551 OSSL_PARAM mode_params[2];
552
553 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
554 "saltSALT",
555 &iterations, &mode);
556
0f183675
JS
557 if (!TEST_ptr(params)
558 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
1cae59d1
JS
559 /* A salt that is too small should pass in pkcs5 mode */
560 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 561 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
1cae59d1
JS
562 goto err;
563
564 mode = 0;
565 mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
566 mode_params[1] = OSSL_PARAM_construct_end();
567
568 /* If the "pkcs5" mode is disabled then the derive will now fail */
569 if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params))
05cdec39 570 || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
1cae59d1
JS
571 goto err;
572
573 ret = 1;
574err:
575 EVP_KDF_CTX_free(kctx);
576 OPENSSL_free(params);
577 return ret;
578}
579
580static int test_kdf_pbkdf2_small_iterations_pkcs5(void)
581{
582 int ret = 0;
0f183675 583 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
584 unsigned char out[25];
585 unsigned int iterations = 1;
586 int mode = 1;
587 OSSL_PARAM *params;
588 OSSL_PARAM mode_params[2];
589
590 params = construct_pbkdf2_params("passwordPASSWORDpassword", "sha256",
591 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
592 &iterations, &mode);
593
0f183675
JS
594 if (!TEST_ptr(params)
595 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
1cae59d1
JS
596 /* An iteration count that is too small will pass in pkcs5 mode */
597 || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 598 || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
1cae59d1
JS
599 goto err;
600
601 mode = 0;
602 mode_params[0] = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
603 mode_params[1] = OSSL_PARAM_construct_end();
604
605 /* If the "pkcs5" mode is disabled then the derive will now fail */
606 if (!TEST_true(EVP_KDF_CTX_set_params(kctx, mode_params))
05cdec39 607 || !TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
1cae59d1
JS
608 goto err;
609
610 ret = 1;
611err:
612 EVP_KDF_CTX_free(kctx);
613 OPENSSL_free(params);
614 return ret;
615}
616
617static int test_kdf_pbkdf2_invalid_digest(void)
618{
619 int ret = 0;
0f183675 620 EVP_KDF_CTX *kctx = NULL;
1cae59d1
JS
621 unsigned int iterations = 4096;
622 int mode = 0;
623 OSSL_PARAM *params;
624
625 params = construct_pbkdf2_params("passwordPASSWORDpassword", "blah",
626 "saltSALTsaltSALTsaltSALTsaltSALTsalt",
627 &iterations, &mode);
628
0f183675
JS
629 if (!TEST_ptr(params)
630 || !TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
1cae59d1
JS
631 /* Unknown digest should fail */
632 || !TEST_false(EVP_KDF_CTX_set_params(kctx, params)))
633 goto err;
634
635 ret = 1;
636err:
637 EVP_KDF_CTX_free(kctx);
638 OPENSSL_free(params);
5a285add
DM
639 return ret;
640}
641
642#ifndef OPENSSL_NO_SCRYPT
643static int test_kdf_scrypt(void)
644{
a3c62426 645 int ret;
5a285add 646 EVP_KDF_CTX *kctx;
bf5739a0 647 OSSL_PARAM params[7], *p = params;
5a285add 648 unsigned char out[64];
bf5739a0 649 unsigned int nu = 1024, ru = 8, pu = 16, maxmem = 16;
8bbeaaa4 650 static const unsigned char expected[sizeof(out)] = {
a3c62426
SL
651 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
652 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
653 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
654 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
655 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
656 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
657 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
658 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
659 };
5a285add 660
bf5739a0
P
661 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
662 (char *)"password", 8);
663 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
664 (char *)"NaCl", 4);
665 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &nu);
666 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &ru);
667 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &pu);
668 *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_MAXMEM, &maxmem);
669 *p = OSSL_PARAM_construct_end();
670
d2ba8123 671 ret =
27e27cd7 672 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SCRYPT))
660c5344 673 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
bf5739a0 674 /* failure test *//*
05cdec39 675 && TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)*/
bf5739a0 676 && TEST_true(OSSL_PARAM_set_uint(p - 1, 10 * 1024 * 1024))
660c5344 677 && TEST_true(EVP_KDF_CTX_set_params(kctx, p - 1))
05cdec39 678 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0)
d2ba8123 679 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
5a285add 680
660c5344 681 EVP_KDF_CTX_free(kctx);
5a285add
DM
682 return ret;
683}
a3c62426 684#endif /* OPENSSL_NO_SCRYPT */
5a285add 685
9537fe57
SL
686static int test_kdf_ss_hash(void)
687{
a3c62426 688 int ret;
bf5739a0
P
689 EVP_KDF_CTX *kctx;
690 OSSL_PARAM params[4], *p = params;
8bbeaaa4 691 unsigned char out[14];
bf5739a0 692 static unsigned char z[] = {
9537fe57
SL
693 0x6d,0xbd,0xc2,0x3f,0x04,0x54,0x88,0xe4,0x06,0x27,0x57,0xb0,0x6b,0x9e,
694 0xba,0xe1,0x83,0xfc,0x5a,0x59,0x46,0xd8,0x0d,0xb9,0x3f,0xec,0x6f,0x62,
695 0xec,0x07,0xe3,0x72,0x7f,0x01,0x26,0xae,0xd1,0x2c,0xe4,0xb2,0x62,0xf4,
696 0x7d,0x48,0xd5,0x42,0x87,0xf8,0x1d,0x47,0x4c,0x7c,0x3b,0x18,0x50,0xe9
697 };
bf5739a0 698 static unsigned char other[] = {
9537fe57
SL
699 0xa1,0xb2,0xc3,0xd4,0xe5,0x43,0x41,0x56,0x53,0x69,0x64,0x3c,0x83,0x2e,
700 0x98,0x49,0xdc,0xdb,0xa7,0x1e,0x9a,0x31,0x39,0xe6,0x06,0xe0,0x95,0xde,
701 0x3c,0x26,0x4a,0x66,0xe9,0x8a,0x16,0x58,0x54,0xcd,0x07,0x98,0x9b,0x1e,
702 0xe0,0xec,0x3f,0x8d,0xbe
703 };
8bbeaaa4 704 static const unsigned char expected[sizeof(out)] = {
9537fe57
SL
705 0xa4,0x62,0xde,0x16,0xa8,0x9d,0xe8,0x46,0x6e,0xf5,0x46,0x0b,0x47,0xb8
706 };
9537fe57 707
bf5739a0 708 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
3262300a 709 (char *)"sha224", 0);
bf5739a0
P
710 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
711 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
712 sizeof(other));
713 *p = OSSL_PARAM_construct_end();
714
d2ba8123 715 ret =
27e27cd7 716 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
05cdec39 717 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
d2ba8123 718 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
9537fe57 719
660c5344 720 EVP_KDF_CTX_free(kctx);
a3c62426 721 return ret;
9537fe57
SL
722}
723
8bbeaaa4
SL
724static int test_kdf_x963(void)
725{
726 int ret;
bf5739a0
P
727 EVP_KDF_CTX *kctx;
728 OSSL_PARAM params[4], *p = params;
8bbeaaa4
SL
729 unsigned char out[1024 / 8];
730 /*
731 * Test data from https://csrc.nist.gov/CSRC/media/Projects/
732 * Cryptographic-Algorithm-Validation-Program/documents/components/
733 * 800-135testvectors/ansx963_2001.zip
734 */
bf5739a0 735 static unsigned char z[] = {
8bbeaaa4
SL
736 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89, 0xfa, 0x58, 0xce, 0xad,
737 0xc0, 0x47, 0x19, 0x7f, 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
738 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8, 0x15, 0x07, 0x39, 0x2f,
739 0x1a, 0x86, 0xdd, 0xfd, 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
740 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47, 0x47, 0xa9, 0xe2, 0xe7,
741 0x53, 0xf5, 0x5e, 0xf0, 0x5a, 0x2d
742 };
bf5739a0 743 static unsigned char shared[] = {
8bbeaaa4
SL
744 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d, 0x2b, 0x3a, 0x2f, 0x99,
745 0x37, 0x89, 0x5d, 0x31
746 };
747 static const unsigned char expected[sizeof(out)] = {
748 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76, 0x9b, 0x52, 0x26, 0x4b,
749 0x01, 0x12, 0xb5, 0x85, 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
750 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d, 0x6e, 0x4d, 0xd2, 0xa5,
751 0x99, 0xac, 0xce, 0xb3, 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
752 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50, 0xa5, 0xa8, 0xee, 0xee,
753 0x20, 0x80, 0x02, 0x26, 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
754 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d, 0xea, 0x9c, 0x4f, 0xf7,
755 0x45, 0xa2, 0x5a, 0xc2, 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
756 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc, 0x0a, 0xb2, 0xaa, 0x8e,
757 0x2e, 0xfa, 0x7b, 0x17, 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
758 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
759 };
760
bf5739a0 761 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
3262300a 762 (char *)"sha512", 0);
bf5739a0
P
763 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
764 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, shared,
765 sizeof(shared));
766 *p = OSSL_PARAM_construct_end();
767
8bbeaaa4 768 ret =
27e27cd7 769 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X963KDF))
05cdec39 770 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
8bbeaaa4
SL
771 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
772
660c5344 773 EVP_KDF_CTX_free(kctx);
8bbeaaa4
SL
774 return ret;
775}
776
726ad13c 777#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
f6dead1b
RH
778/*
779 * KBKDF test vectors from RFC 6803 (Camellia Encryption for Kerberos 5)
780 * section 10.
781 */
782static int test_kdf_kbkdf_6803_128(void)
783{
784 int ret = 0, i, p;
785 EVP_KDF_CTX *kctx;
786 OSSL_PARAM params[7];
787 static unsigned char input_key[] = {
788 0x57, 0xD0, 0x29, 0x72, 0x98, 0xFF, 0xD9, 0xD3,
789 0x5D, 0xE5, 0xA4, 0x7F, 0xB4, 0xBD, 0xE2, 0x4B,
790 };
791 static unsigned char constants[][5] = {
792 { 0x00, 0x00, 0x00, 0x02, 0x99 },
793 { 0x00, 0x00, 0x00, 0x02, 0xaa },
794 { 0x00, 0x00, 0x00, 0x02, 0x55 },
795 };
796 static unsigned char outputs[][16] = {
797 {0xD1, 0x55, 0x77, 0x5A, 0x20, 0x9D, 0x05, 0xF0,
798 0x2B, 0x38, 0xD4, 0x2A, 0x38, 0x9E, 0x5A, 0x56},
799 {0x64, 0xDF, 0x83, 0xF8, 0x5A, 0x53, 0x2F, 0x17,
800 0x57, 0x7D, 0x8C, 0x37, 0x03, 0x57, 0x96, 0xAB},
801 {0x3E, 0x4F, 0xBD, 0xF3, 0x0F, 0xB8, 0x25, 0x9C,
802 0x42, 0x5C, 0xB6, 0xC9, 0x6F, 0x1F, 0x46, 0x35}
803 };
804 static unsigned char iv[16] = { 0 };
805 unsigned char result[16] = { 0 };
806
807 for (i = 0; i < 3; i++) {
808 p = 0;
809 params[p++] = OSSL_PARAM_construct_utf8_string(
810 OSSL_KDF_PARAM_CIPHER, "CAMELLIA-128-CBC", 0);
811 params[p++] = OSSL_PARAM_construct_utf8_string(
812 OSSL_KDF_PARAM_MAC, "CMAC", 0);
813 params[p++] = OSSL_PARAM_construct_utf8_string(
814 OSSL_KDF_PARAM_MODE, "FEEDBACK", 0);
815 params[p++] = OSSL_PARAM_construct_octet_string(
816 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
817 params[p++] = OSSL_PARAM_construct_octet_string(
818 OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i]));
819 params[p++] = OSSL_PARAM_construct_octet_string(
820 OSSL_KDF_PARAM_SEED, iv, sizeof(iv));
821 params[p] = OSSL_PARAM_construct_end();
822
823 kctx = get_kdfbyname("KBKDF");
824 ret = TEST_ptr(kctx)
05cdec39
P
825 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result),
826 params), 0)
f6dead1b
RH
827 && TEST_mem_eq(result, sizeof(result), outputs[i],
828 sizeof(outputs[i]));
660c5344 829 EVP_KDF_CTX_free(kctx);
f6dead1b
RH
830 if (ret != 1)
831 return ret;
832 }
833
834 return ret;
835}
836
837static int test_kdf_kbkdf_6803_256(void)
838{
839 int ret = 0, i, p;
840 EVP_KDF_CTX *kctx;
841 OSSL_PARAM params[7];
842 static unsigned char input_key[] = {
843 0xB9, 0xD6, 0x82, 0x8B, 0x20, 0x56, 0xB7, 0xBE,
844 0x65, 0x6D, 0x88, 0xA1, 0x23, 0xB1, 0xFA, 0xC6,
845 0x82, 0x14, 0xAC, 0x2B, 0x72, 0x7E, 0xCF, 0x5F,
846 0x69, 0xAF, 0xE0, 0xC4, 0xDF, 0x2A, 0x6D, 0x2C,
847 };
848 static unsigned char constants[][5] = {
849 { 0x00, 0x00, 0x00, 0x02, 0x99 },
850 { 0x00, 0x00, 0x00, 0x02, 0xaa },
851 { 0x00, 0x00, 0x00, 0x02, 0x55 },
852 };
853 static unsigned char outputs[][32] = {
854 {0xE4, 0x67, 0xF9, 0xA9, 0x55, 0x2B, 0xC7, 0xD3,
855 0x15, 0x5A, 0x62, 0x20, 0xAF, 0x9C, 0x19, 0x22,
856 0x0E, 0xEE, 0xD4, 0xFF, 0x78, 0xB0, 0xD1, 0xE6,
857 0xA1, 0x54, 0x49, 0x91, 0x46, 0x1A, 0x9E, 0x50,
858 },
859 {0x41, 0x2A, 0xEF, 0xC3, 0x62, 0xA7, 0x28, 0x5F,
860 0xC3, 0x96, 0x6C, 0x6A, 0x51, 0x81, 0xE7, 0x60,
861 0x5A, 0xE6, 0x75, 0x23, 0x5B, 0x6D, 0x54, 0x9F,
862 0xBF, 0xC9, 0xAB, 0x66, 0x30, 0xA4, 0xC6, 0x04,
863 },
864 {0xFA, 0x62, 0x4F, 0xA0, 0xE5, 0x23, 0x99, 0x3F,
865 0xA3, 0x88, 0xAE, 0xFD, 0xC6, 0x7E, 0x67, 0xEB,
866 0xCD, 0x8C, 0x08, 0xE8, 0xA0, 0x24, 0x6B, 0x1D,
867 0x73, 0xB0, 0xD1, 0xDD, 0x9F, 0xC5, 0x82, 0xB0,
868 },
869 };
870 static unsigned char iv[16] = { 0 };
871 unsigned char result[32] = { 0 };
872
873 for (i = 0; i < 3; i++) {
874 p = 0;
875 params[p++] = OSSL_PARAM_construct_utf8_string(
876 OSSL_KDF_PARAM_CIPHER, "CAMELLIA-256-CBC", 0);
877 params[p++] = OSSL_PARAM_construct_utf8_string(
878 OSSL_KDF_PARAM_MAC, "CMAC", 0);
879 params[p++] = OSSL_PARAM_construct_utf8_string(
880 OSSL_KDF_PARAM_MODE, "FEEDBACK", 0);
881 params[p++] = OSSL_PARAM_construct_octet_string(
882 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
883 params[p++] = OSSL_PARAM_construct_octet_string(
884 OSSL_KDF_PARAM_SALT, constants[i], sizeof(constants[i]));
885 params[p++] = OSSL_PARAM_construct_octet_string(
886 OSSL_KDF_PARAM_SEED, iv, sizeof(iv));
887 params[p] = OSSL_PARAM_construct_end();
888
889 kctx = get_kdfbyname("KBKDF");
890 ret = TEST_ptr(kctx)
05cdec39
P
891 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result),
892 params), 0)
f6dead1b
RH
893 && TEST_mem_eq(result, sizeof(result), outputs[i],
894 sizeof(outputs[i]));
660c5344 895 EVP_KDF_CTX_free(kctx);
f6dead1b
RH
896 if (ret != 1)
897 return ret;
898 }
899
900 return ret;
901}
726ad13c 902#endif
f6dead1b 903
1cae59d1
JS
904static OSSL_PARAM *construct_kbkdf_params(char *digest, char *mac, unsigned char *key,
905 size_t keylen, char *salt, char *info)
906{
907 OSSL_PARAM *params = OPENSSL_malloc(sizeof(OSSL_PARAM) * 7);
908 OSSL_PARAM *p = params;
909
0f183675
JS
910 if (params == NULL)
911 return NULL;
912
1cae59d1
JS
913 *p++ = OSSL_PARAM_construct_utf8_string(
914 OSSL_KDF_PARAM_DIGEST, digest, 0);
915 *p++ = OSSL_PARAM_construct_utf8_string(
916 OSSL_KDF_PARAM_MAC, mac, 0);
917 *p++ = OSSL_PARAM_construct_utf8_string(
918 OSSL_KDF_PARAM_MODE, "COUNTER", 0);
919 *p++ = OSSL_PARAM_construct_octet_string(
920 OSSL_KDF_PARAM_KEY, key, keylen);
921 *p++ = OSSL_PARAM_construct_octet_string(
922 OSSL_KDF_PARAM_SALT, salt, strlen(salt));
923 *p++ = OSSL_PARAM_construct_octet_string(
924 OSSL_KDF_PARAM_INFO, info, strlen(info));
925 *p = OSSL_PARAM_construct_end();
926
927 return params;
928}
929
930static int test_kdf_kbkdf_invalid_digest(void)
931{
932 int ret;
933 EVP_KDF_CTX *kctx;
934 OSSL_PARAM *params;
935
936 static unsigned char key[] = {0x01};
937
938 params = construct_kbkdf_params("blah", "HMAC", key, 1, "prf", "test");
0f183675
JS
939 if (!TEST_ptr(params))
940 return 0;
1cae59d1
JS
941
942 /* Negative test case - set_params should fail */
943 kctx = get_kdfbyname("KBKDF");
944 ret = TEST_ptr(kctx)
945 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
946
947 EVP_KDF_CTX_free(kctx);
948 OPENSSL_free(params);
949 return ret;
950}
951
952static int test_kdf_kbkdf_invalid_mac(void)
953{
954 int ret;
955 EVP_KDF_CTX *kctx;
956 OSSL_PARAM *params;
957
958 static unsigned char key[] = {0x01};
959
960 params = construct_kbkdf_params("sha256", "blah", key, 1, "prf", "test");
0f183675
JS
961 if (!TEST_ptr(params))
962 return 0;
1cae59d1
JS
963
964 /* Negative test case - set_params should fail */
965 kctx = get_kdfbyname("KBKDF");
966 ret = TEST_ptr(kctx)
967 && TEST_false(EVP_KDF_CTX_set_params(kctx, params));
968
969 EVP_KDF_CTX_free(kctx);
970 OPENSSL_free(params);
971 return ret;
972}
973
974static int test_kdf_kbkdf_empty_key(void)
975{
976 int ret;
977 EVP_KDF_CTX *kctx;
978 OSSL_PARAM *params;
979
980 static unsigned char key[] = {0x01};
981 unsigned char result[32] = { 0 };
982
983 params = construct_kbkdf_params("sha256", "HMAC", key, 0, "prf", "test");
0f183675
JS
984 if (!TEST_ptr(params))
985 return 0;
1cae59d1
JS
986
987 /* Negative test case - derive should fail */
988 kctx = get_kdfbyname("KBKDF");
989 ret = TEST_ptr(kctx)
990 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 991 && TEST_int_eq(EVP_KDF_derive(kctx, result, sizeof(result), NULL), 0);
1cae59d1
JS
992
993 EVP_KDF_CTX_free(kctx);
994 OPENSSL_free(params);
995 return ret;
996}
997
998static int test_kdf_kbkdf_1byte_key(void)
999{
1000 int ret;
1001 EVP_KDF_CTX *kctx;
1002 OSSL_PARAM *params;
1003
1004 static unsigned char key[] = {0x01};
1005 unsigned char result[32] = { 0 };
1006
1007 params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test");
0f183675
JS
1008 if (!TEST_ptr(params))
1009 return 0;
1cae59d1
JS
1010
1011 kctx = get_kdfbyname("KBKDF");
1012 ret = TEST_ptr(kctx)
05cdec39 1013 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0);
1cae59d1
JS
1014
1015 EVP_KDF_CTX_free(kctx);
1016 OPENSSL_free(params);
1017 return ret;
1018}
1019
1020static int test_kdf_kbkdf_zero_output_size(void)
1021{
1022 int ret;
1023 EVP_KDF_CTX *kctx;
1024 OSSL_PARAM *params;
1025
1026 static unsigned char key[] = {0x01};
1027 unsigned char result[32] = { 0 };
1028
1029 params = construct_kbkdf_params("sha256", "HMAC", key, 1, "prf", "test");
0f183675
JS
1030 if (!TEST_ptr(params))
1031 return 0;
1cae59d1
JS
1032
1033 /* Negative test case - derive should fail */
1034 kctx = get_kdfbyname("KBKDF");
1035 ret = TEST_ptr(kctx)
1036 && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
05cdec39 1037 && TEST_int_eq(EVP_KDF_derive(kctx, result, 0, NULL), 0);
1cae59d1
JS
1038
1039 EVP_KDF_CTX_free(kctx);
1040 OPENSSL_free(params);
1041 return ret;
1042}
1043
a39bc440
RH
1044/* Two test vectors from RFC 8009 (AES Encryption with HMAC-SHA2 for Kerberos
1045 * 5) appendix A. */
1046static int test_kdf_kbkdf_8009_prf1(void)
1047{
1048 int ret, i = 0;
1049 EVP_KDF_CTX *kctx;
1050 OSSL_PARAM params[6];
1051 char *label = "prf", *digest = "sha256", *prf_input = "test",
1052 *mac = "HMAC";
1053 static unsigned char input_key[] = {
1054 0x37, 0x05, 0xD9, 0x60, 0x80, 0xC1, 0x77, 0x28,
1055 0xA0, 0xE8, 0x00, 0xEA, 0xB6, 0xE0, 0xD2, 0x3C,
1056 };
1057 static unsigned char output[] = {
1058 0x9D, 0x18, 0x86, 0x16, 0xF6, 0x38, 0x52, 0xFE,
1059 0x86, 0x91, 0x5B, 0xB8, 0x40, 0xB4, 0xA8, 0x86,
1060 0xFF, 0x3E, 0x6B, 0xB0, 0xF8, 0x19, 0xB4, 0x9B,
1061 0x89, 0x33, 0x93, 0xD3, 0x93, 0x85, 0x42, 0x95,
1062 };
1063 unsigned char result[sizeof(output)] = { 0 };
1064
1065 params[i++] = OSSL_PARAM_construct_utf8_string(
1cae59d1 1066 OSSL_KDF_PARAM_DIGEST, digest, 0);
a39bc440 1067 params[i++] = OSSL_PARAM_construct_utf8_string(
1cae59d1 1068 OSSL_KDF_PARAM_MAC, mac, 0);
a39bc440
RH
1069 params[i++] = OSSL_PARAM_construct_octet_string(
1070 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
1071 params[i++] = OSSL_PARAM_construct_octet_string(
1072 OSSL_KDF_PARAM_SALT, label, strlen(label));
1073 params[i++] = OSSL_PARAM_construct_octet_string(
1074 OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
1075 params[i] = OSSL_PARAM_construct_end();
1076
1077 kctx = get_kdfbyname("KBKDF");
1078 ret = TEST_ptr(kctx)
05cdec39 1079 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
a39bc440
RH
1080 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1081
660c5344 1082 EVP_KDF_CTX_free(kctx);
a39bc440
RH
1083 return ret;
1084}
1085
1086static int test_kdf_kbkdf_8009_prf2(void)
1087{
1088 int ret, i = 0;
1089 EVP_KDF_CTX *kctx;
1090 OSSL_PARAM params[6];
1091 char *label = "prf", *digest = "sha384", *prf_input = "test",
1092 *mac = "HMAC";
1093 static unsigned char input_key[] = {
1094 0x6D, 0x40, 0x4D, 0x37, 0xFA, 0xF7, 0x9F, 0x9D,
1095 0xF0, 0xD3, 0x35, 0x68, 0xD3, 0x20, 0x66, 0x98,
1096 0x00, 0xEB, 0x48, 0x36, 0x47, 0x2E, 0xA8, 0xA0,
1097 0x26, 0xD1, 0x6B, 0x71, 0x82, 0x46, 0x0C, 0x52,
1098 };
1099 static unsigned char output[] = {
1100 0x98, 0x01, 0xF6, 0x9A, 0x36, 0x8C, 0x2B, 0xF6,
1101 0x75, 0xE5, 0x95, 0x21, 0xE1, 0x77, 0xD9, 0xA0,
1102 0x7F, 0x67, 0xEF, 0xE1, 0xCF, 0xDE, 0x8D, 0x3C,
1103 0x8D, 0x6F, 0x6A, 0x02, 0x56, 0xE3, 0xB1, 0x7D,
1104 0xB3, 0xC1, 0xB6, 0x2A, 0xD1, 0xB8, 0x55, 0x33,
1105 0x60, 0xD1, 0x73, 0x67, 0xEB, 0x15, 0x14, 0xD2,
1106 };
1107 unsigned char result[sizeof(output)] = { 0 };
1108
1109 params[i++] = OSSL_PARAM_construct_utf8_string(
1cae59d1 1110 OSSL_KDF_PARAM_DIGEST, digest, 0);
a39bc440 1111 params[i++] = OSSL_PARAM_construct_utf8_string(
1cae59d1 1112 OSSL_KDF_PARAM_MAC, mac, 0);
a39bc440
RH
1113 params[i++] = OSSL_PARAM_construct_octet_string(
1114 OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
1115 params[i++] = OSSL_PARAM_construct_octet_string(
1116 OSSL_KDF_PARAM_SALT, label, strlen(label));
1117 params[i++] = OSSL_PARAM_construct_octet_string(
1118 OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
1119 params[i] = OSSL_PARAM_construct_end();
1120
1121 kctx = get_kdfbyname("KBKDF");
1122 ret = TEST_ptr(kctx)
05cdec39 1123 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
a39bc440
RH
1124 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1125
660c5344 1126 EVP_KDF_CTX_free(kctx);
a39bc440
RH
1127 return ret;
1128}
1129
4757a347
SL
1130#if !defined(OPENSSL_NO_CMAC)
1131/*
1132 * Test vector taken from
1133 * https://csrc.nist.gov/CSRC/media/Projects/
1134 * Cryptographic-Algorithm-Validation-Program/documents/KBKDF800-108/CounterMode.zip
1135 * Note: Only 32 bit counter is supported ([RLEN=32_BITS])
1136 */
1137static int test_kdf_kbkdf_fixedinfo(void)
1138{
1139 int ret;
1140 EVP_KDF_CTX *kctx;
1141 OSSL_PARAM params[8], *p = params;
1142 static char *cipher = "AES128";
1143 static char *mac = "CMAC";
1144 static char *mode = "COUNTER";
1145 int use_l = 0;
1146 int use_separator = 0;
1147
1148 static unsigned char input_key[] = {
1149 0xc1, 0x0b, 0x15, 0x2e, 0x8c, 0x97, 0xb7, 0x7e,
1150 0x18, 0x70, 0x4e, 0x0f, 0x0b, 0xd3, 0x83, 0x05,
1151 };
1152 static unsigned char fixed_input[] = {
1153 0x98, 0xcd, 0x4c, 0xbb, 0xbe, 0xbe, 0x15, 0xd1,
1154 0x7d, 0xc8, 0x6e, 0x6d, 0xba, 0xd8, 0x00, 0xa2,
1155 0xdc, 0xbd, 0x64, 0xf7, 0xc7, 0xad, 0x0e, 0x78,
1156 0xe9, 0xcf, 0x94, 0xff, 0xdb, 0xa8, 0x9d, 0x03,
1157 0xe9, 0x7e, 0xad, 0xf6, 0xc4, 0xf7, 0xb8, 0x06,
1158 0xca, 0xf5, 0x2a, 0xa3, 0x8f, 0x09, 0xd0, 0xeb,
1159 0x71, 0xd7, 0x1f, 0x49, 0x7b, 0xcc, 0x69, 0x06,
1160 0xb4, 0x8d, 0x36, 0xc4,
1161
1162 };
1163 static unsigned char output[] = {
1164 0x26, 0xfa, 0xf6, 0x19, 0x08, 0xad, 0x9e, 0xe8,
1165 0x81, 0xb8, 0x30, 0x5c, 0x22, 0x1d, 0xb5, 0x3f,
1166 };
1167 unsigned char result[sizeof(output)] = { 0 };
1168
1169 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER, cipher, 0);
1170 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC, mac, 0);
1171 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MODE, mode, 0);
1172 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, input_key,
1173 sizeof(input_key));
1174 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
1175 fixed_input, sizeof(fixed_input));
1176 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_L, &use_l);
1177 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_KBKDF_USE_SEPARATOR,
1178 &use_separator);
1179 *p = OSSL_PARAM_construct_end();
1180
1181 kctx = get_kdfbyname("KBKDF");
1182 ret = TEST_ptr(kctx)
05cdec39 1183 && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result), params), 0)
4757a347
SL
1184 && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
1185
1186 EVP_KDF_CTX_free(kctx);
1187 return ret;
1188}
1189#endif /* OPENSSL_NO_CMAC */
1190
9537fe57
SL
1191static int test_kdf_ss_hmac(void)
1192{
a3c62426 1193 int ret;
9537fe57 1194 EVP_KDF_CTX *kctx;
bf5739a0 1195 OSSL_PARAM params[6], *p = params;
8bbeaaa4 1196 unsigned char out[16];
bf5739a0 1197 static unsigned char z[] = {
9537fe57
SL
1198 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
1199 };
bf5739a0 1200 static unsigned char other[] = {
9537fe57
SL
1201 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
1202 };
bf5739a0 1203 static unsigned char salt[] = {
9537fe57
SL
1204 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
1205 0x3f,0x89
1206 };
8bbeaaa4 1207 static const unsigned char expected[sizeof(out)] = {
9537fe57
SL
1208 0x44,0xf6,0x76,0xe8,0x5c,0x1b,0x1a,0x8b,0xbc,0x3d,0x31,0x92,0x18,0x63,
1209 0x1c,0xa3
1210 };
9537fe57 1211
bf5739a0 1212 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
3262300a 1213 (char *)OSSL_MAC_NAME_HMAC, 0);
bf5739a0 1214 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
3262300a 1215 (char *)"sha256", 0);
bf5739a0
P
1216 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
1217 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
1218 sizeof(other));
1219 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt,
1220 sizeof(salt));
1221 *p = OSSL_PARAM_construct_end();
1222
d2ba8123 1223 ret =
27e27cd7 1224 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
05cdec39 1225 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
d2ba8123 1226 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
9537fe57 1227
660c5344 1228 EVP_KDF_CTX_free(kctx);
a3c62426 1229 return ret;
9537fe57
SL
1230}
1231
1232static int test_kdf_ss_kmac(void)
1233{
a3c62426 1234 int ret;
9537fe57 1235 EVP_KDF_CTX *kctx;
bf5739a0 1236 OSSL_PARAM params[6], *p = params;
9537fe57 1237 unsigned char out[64];
bf5739a0
P
1238 size_t mac_size = 20;
1239 static unsigned char z[] = {
9537fe57
SL
1240 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
1241 };
bf5739a0 1242 static unsigned char other[] = {
9537fe57
SL
1243 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
1244 };
bf5739a0 1245 static unsigned char salt[] = {
9537fe57
SL
1246 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
1247 0x3f,0x89
1248 };
8bbeaaa4 1249 static const unsigned char expected[sizeof(out)] = {
9537fe57
SL
1250 0xe9,0xc1,0x84,0x53,0xa0,0x62,0xb5,0x3b,0xdb,0xfc,0xbb,0x5a,0x34,0xbd,
1251 0xb8,0xe5,0xe7,0x07,0xee,0xbb,0x5d,0xd1,0x34,0x42,0x43,0xd8,0xcf,0xc2,
1252 0xc2,0xe6,0x33,0x2f,0x91,0xbd,0xa5,0x86,0xf3,0x7d,0xe4,0x8a,0x65,0xd4,
1253 0xc5,0x14,0xfd,0xef,0xaa,0x1e,0x67,0x54,0xf3,0x73,0xd2,0x38,0xe1,0x95,
1254 0xae,0x15,0x7e,0x1d,0xe8,0x14,0x98,0x03
1255 };
1256
bf5739a0 1257 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
3262300a 1258 (char *)OSSL_MAC_NAME_KMAC128, 0);
bf5739a0
P
1259 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
1260 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
1261 sizeof(other));
1262 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt,
1263 sizeof(salt));
1264 *p++ = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_MAC_SIZE, &mac_size);
1265 *p = OSSL_PARAM_construct_end();
1266
d2ba8123 1267 ret =
27e27cd7 1268 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
05cdec39 1269 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
d2ba8123 1270 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
9537fe57 1271
660c5344 1272 EVP_KDF_CTX_free(kctx);
a3c62426 1273 return ret;
9537fe57
SL
1274}
1275
87d9955e
SS
1276static int test_kdf_sshkdf(void)
1277{
1278 int ret;
1279 EVP_KDF_CTX *kctx;
bf5739a0 1280 OSSL_PARAM params[6], *p = params;
a8eb71ad 1281 char kdftype = EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV;
87d9955e
SS
1282 unsigned char out[8];
1283 /* Test data from NIST CAVS 14.1 test vectors */
bf5739a0 1284 static unsigned char key[] = {
87d9955e
SS
1285 0x00, 0x00, 0x00, 0x81, 0x00, 0x87, 0x5c, 0x55, 0x1c, 0xef, 0x52, 0x6a,
1286 0x4a, 0x8b, 0xe1, 0xa7, 0xdf, 0x27, 0xe9, 0xed, 0x35, 0x4b, 0xac, 0x9a,
1287 0xfb, 0x71, 0xf5, 0x3d, 0xba, 0xe9, 0x05, 0x67, 0x9d, 0x14, 0xf9, 0xfa,
1288 0xf2, 0x46, 0x9c, 0x53, 0x45, 0x7c, 0xf8, 0x0a, 0x36, 0x6b, 0xe2, 0x78,
1289 0x96, 0x5b, 0xa6, 0x25, 0x52, 0x76, 0xca, 0x2d, 0x9f, 0x4a, 0x97, 0xd2,
1290 0x71, 0xf7, 0x1e, 0x50, 0xd8, 0xa9, 0xec, 0x46, 0x25, 0x3a, 0x6a, 0x90,
1291 0x6a, 0xc2, 0xc5, 0xe4, 0xf4, 0x8b, 0x27, 0xa6, 0x3c, 0xe0, 0x8d, 0x80,
1292 0x39, 0x0a, 0x49, 0x2a, 0xa4, 0x3b, 0xad, 0x9d, 0x88, 0x2c, 0xca, 0xc2,
1293 0x3d, 0xac, 0x88, 0xbc, 0xad, 0xa4, 0xb4, 0xd4, 0x26, 0xa3, 0x62, 0x08,
1294 0x3d, 0xab, 0x65, 0x69, 0xc5, 0x4c, 0x22, 0x4d, 0xd2, 0xd8, 0x76, 0x43,
1295 0xaa, 0x22, 0x76, 0x93, 0xe1, 0x41, 0xad, 0x16, 0x30, 0xce, 0x13, 0x14,
1296 0x4e
1297 };
bf5739a0 1298 static unsigned char xcghash[] = {
87d9955e
SS
1299 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
1300 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
1301 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
1302 };
bf5739a0 1303 static unsigned char sessid[] = {
87d9955e
SS
1304 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
1305 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
1306 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
1307 };
8bbeaaa4 1308 static const unsigned char expected[sizeof(out)] = {
87d9955e
SS
1309 0x41, 0xff, 0x2e, 0xad, 0x16, 0x83, 0xf1, 0xe6
1310 };
1311
bf5739a0 1312 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
3262300a 1313 (char *)"sha256", 0);
bf5739a0
P
1314 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key,
1315 sizeof(key));
1316 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH,
1317 xcghash, sizeof(xcghash));
1318 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID,
1319 sessid, sizeof(sessid));
1320 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE,
a8eb71ad 1321 &kdftype, sizeof(kdftype));
bf5739a0
P
1322 *p = OSSL_PARAM_construct_end();
1323
d2ba8123 1324 ret =
27e27cd7 1325 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSHKDF))
05cdec39 1326 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
d2ba8123 1327 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
87d9955e 1328
660c5344 1329 EVP_KDF_CTX_free(kctx);
87d9955e
SS
1330 return ret;
1331}
1332
b5873b31
P
1333static int test_kdfs_same( EVP_KDF *kdf1, EVP_KDF *kdf2)
1334{
1335 /* Fast path in case the two are the same algorithm pointer */
1336 if (kdf1 == kdf2)
1337 return 1;
1338 /*
1339 * Compare their names and providers instead.
1340 * This is necessary in a non-caching build (or a cache flush during fetch)
1341 * because without the algorithm in the cache, fetching it a second time
1342 * will result in a different pointer.
1343 */
1344 return TEST_ptr_eq(EVP_KDF_provider(kdf1), EVP_KDF_provider(kdf2))
1345 && TEST_str_eq(EVP_KDF_name(kdf1), EVP_KDF_name(kdf2));
1346}
1347
d2ba8123
SL
1348static int test_kdf_get_kdf(void)
1349{
ff756eed 1350 EVP_KDF *kdf1 = NULL, *kdf2 = NULL;
d2ba8123 1351 ASN1_OBJECT *obj;
ff756eed
RL
1352 int ok = 1;
1353
1354 if (!TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2))
27e27cd7 1355 || !TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_PBKDF2, NULL))
ff756eed
RL
1356 || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(OBJ_obj2nid(obj)),
1357 NULL))
b5873b31 1358 || !test_kdfs_same(kdf1, kdf2))
ff756eed
RL
1359 ok = 0;
1360 EVP_KDF_free(kdf1);
1361 kdf1 = NULL;
1362 EVP_KDF_free(kdf2);
1363 kdf2 = NULL;
1364
1365 if (!TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, SN_tls1_prf, NULL))
1366 || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, LN_tls1_prf, NULL))
b5873b31 1367 || !test_kdfs_same(kdf1, kdf2))
ff756eed
RL
1368 ok = 0;
1369 /* kdf1 is re-used below, so don't free it here */
1370 EVP_KDF_free(kdf2);
1371 kdf2 = NULL;
1372
1373 if (!TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(NID_tls1_prf), NULL))
b5873b31 1374 || !test_kdfs_same(kdf1, kdf2))
ff756eed
RL
1375 ok = 0;
1376 EVP_KDF_free(kdf1);
1377 kdf1 = NULL;
1378 EVP_KDF_free(kdf2);
1379 kdf2 = NULL;
1380
1381 return ok;
d2ba8123
SL
1382}
1383
e98a182e 1384#if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES)
1aec7716
SL
1385static int test_kdf_x942_asn1(void)
1386{
1387 int ret;
1388 EVP_KDF_CTX *kctx = NULL;
bf5739a0
P
1389 OSSL_PARAM params[4], *p = params;
1390 const char *cek_alg = SN_id_smime_alg_CMS3DESwrap;
1aec7716
SL
1391 unsigned char out[24];
1392 /* RFC2631 Section 2.1.6 Test data */
bf5739a0 1393 static unsigned char z[] = {
1aec7716
SL
1394 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,
1395 0x0e,0x0f,0x10,0x11,0x12,0x13
1396 };
1397 static const unsigned char expected[sizeof(out)] = {
1398 0xa0,0x96,0x61,0x39,0x23,0x76,0xf7,0x04,
1399 0x4d,0x90,0x52,0xa3,0x97,0x88,0x32,0x46,
1400 0xb6,0x7f,0x5f,0x1e,0xf6,0x3e,0xb5,0xfb
1401 };
1402
bf5739a0 1403 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
1cae59d1 1404 (char *)"sha1", 0);
bf5739a0
P
1405 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z,
1406 sizeof(z));
1407 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
1cae59d1 1408 (char *)cek_alg, 0);
bf5739a0
P
1409 *p = OSSL_PARAM_construct_end();
1410
1aec7716 1411 ret =
89cccbea 1412 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X942KDF_ASN1))
05cdec39 1413 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
1aec7716
SL
1414 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1415
660c5344 1416 EVP_KDF_CTX_free(kctx);
1aec7716
SL
1417 return ret;
1418}
1419#endif /* OPENSSL_NO_CMS */
1420
33f54da3
SS
1421static int test_kdf_krb5kdf(void)
1422{
1423 int ret;
1424 EVP_KDF_CTX *kctx;
1425 OSSL_PARAM params[4], *p = params;
1426 unsigned char out[16];
1427 static unsigned char key[] = {
1428 0x42, 0x26, 0x3C, 0x6E, 0x89, 0xF4, 0xFC, 0x28,
1429 0xB8, 0xDF, 0x68, 0xEE, 0x09, 0x79, 0x9F, 0x15
1430 };
1431 static unsigned char constant[] = {
1432 0x00, 0x00, 0x00, 0x02, 0x99
1433 };
1434 static const unsigned char expected[sizeof(out)] = {
1435 0x34, 0x28, 0x0A, 0x38, 0x2B, 0xC9, 0x27, 0x69,
1436 0xB2, 0xDA, 0x2F, 0x9E, 0xF0, 0x66, 0x85, 0x4B
1437 };
1438
1439 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CIPHER,
3262300a 1440 (char *)"AES-128-CBC", 0);
33f54da3
SS
1441 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key,
1442 sizeof(key));
1443 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_CONSTANT,
1444 constant, sizeof(constant));
1445 *p = OSSL_PARAM_construct_end();
1446
1447 ret =
1448 TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_KRB5KDF))
05cdec39 1449 && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out), params), 0)
33f54da3
SS
1450 && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
1451
660c5344 1452 EVP_KDF_CTX_free(kctx);
33f54da3
SS
1453 return ret;
1454}
1455
5a285add
DM
1456int setup_tests(void)
1457{
726ad13c 1458#if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
f6dead1b
RH
1459 ADD_TEST(test_kdf_kbkdf_6803_128);
1460 ADD_TEST(test_kdf_kbkdf_6803_256);
726ad13c 1461#endif
1cae59d1
JS
1462 ADD_TEST(test_kdf_kbkdf_invalid_digest);
1463 ADD_TEST(test_kdf_kbkdf_invalid_mac);
1464 ADD_TEST(test_kdf_kbkdf_zero_output_size);
1465 ADD_TEST(test_kdf_kbkdf_empty_key);
1466 ADD_TEST(test_kdf_kbkdf_1byte_key);
a39bc440
RH
1467 ADD_TEST(test_kdf_kbkdf_8009_prf1);
1468 ADD_TEST(test_kdf_kbkdf_8009_prf2);
4757a347
SL
1469#if !defined(OPENSSL_NO_CMAC)
1470 ADD_TEST(test_kdf_kbkdf_fixedinfo);
1471#endif
d2ba8123 1472 ADD_TEST(test_kdf_get_kdf);
5a285add 1473 ADD_TEST(test_kdf_tls1_prf);
1cae59d1
JS
1474 ADD_TEST(test_kdf_tls1_prf_invalid_digest);
1475 ADD_TEST(test_kdf_tls1_prf_zero_output_size);
1476 ADD_TEST(test_kdf_tls1_prf_empty_secret);
1477 ADD_TEST(test_kdf_tls1_prf_1byte_secret);
1478 ADD_TEST(test_kdf_tls1_prf_empty_seed);
1479 ADD_TEST(test_kdf_tls1_prf_1byte_seed);
5a285add 1480 ADD_TEST(test_kdf_hkdf);
1cae59d1
JS
1481 ADD_TEST(test_kdf_hkdf_invalid_digest);
1482 ADD_TEST(test_kdf_hkdf_zero_output_size);
1483 ADD_TEST(test_kdf_hkdf_empty_key);
1484 ADD_TEST(test_kdf_hkdf_1byte_key);
1485 ADD_TEST(test_kdf_hkdf_empty_salt);
0f183675 1486 ADD_TEST(test_kdf_pbkdf1);
5a285add 1487 ADD_TEST(test_kdf_pbkdf2);
1cae59d1
JS
1488 ADD_TEST(test_kdf_pbkdf2_small_output);
1489 ADD_TEST(test_kdf_pbkdf2_large_output);
1490 ADD_TEST(test_kdf_pbkdf2_small_salt);
1491 ADD_TEST(test_kdf_pbkdf2_small_iterations);
1492 ADD_TEST(test_kdf_pbkdf2_small_salt_pkcs5);
1493 ADD_TEST(test_kdf_pbkdf2_small_iterations_pkcs5);
1494 ADD_TEST(test_kdf_pbkdf2_invalid_digest);
5a285add
DM
1495#ifndef OPENSSL_NO_SCRYPT
1496 ADD_TEST(test_kdf_scrypt);
1497#endif
9537fe57
SL
1498 ADD_TEST(test_kdf_ss_hash);
1499 ADD_TEST(test_kdf_ss_hmac);
1500 ADD_TEST(test_kdf_ss_kmac);
87d9955e 1501 ADD_TEST(test_kdf_sshkdf);
8bbeaaa4 1502 ADD_TEST(test_kdf_x963);
e98a182e 1503#if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES)
1aec7716
SL
1504 ADD_TEST(test_kdf_x942_asn1);
1505#endif
33f54da3 1506 ADD_TEST(test_kdf_krb5kdf);
5a285add
DM
1507 return 1;
1508}