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