2
0

evp_kdf_test.c 57 KB

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