evp_kdf_test.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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. static int test_kdf_hmac_drbg_settables(void)
  1420. {
  1421. int ret = 0, i = 0, j = 0;
  1422. EVP_KDF_CTX *kctx = NULL;
  1423. const OSSL_PARAM *settableparams;
  1424. OSSL_PARAM params[5];
  1425. static const unsigned char ent[32] = { 0 };
  1426. unsigned char out[32];
  1427. char digestname[32];
  1428. char macname[32];
  1429. EVP_MD *shake256 = NULL;
  1430. /* Test there are settables */
  1431. if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HMACDRBGKDF))
  1432. || !TEST_ptr(settableparams = EVP_KDF_CTX_settable_params(kctx)))
  1433. goto err;
  1434. /* Fail if no params have been set when doing a derive */
  1435. if (!TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 0))
  1436. goto err;
  1437. /* Fail if we pass the wrong type for params */
  1438. params[1] = OSSL_PARAM_construct_end();
  1439. for (i = 0; settableparams[i].key != NULL; ++i) {
  1440. /* Skip "properties" key since it returns 1 unless the digest is also set */
  1441. if (OPENSSL_strcasecmp(settableparams[i].key,
  1442. OSSL_KDF_PARAM_PROPERTIES) != 0) {
  1443. TEST_note("Testing set int into %s fails", settableparams[i].key);
  1444. params[0] = OSSL_PARAM_construct_int(settableparams[i].key, &j);
  1445. if (!TEST_int_le(EVP_KDF_CTX_set_params(kctx, params), 0))
  1446. goto err;
  1447. }
  1448. }
  1449. /* Test that we can set values multiple times */
  1450. params[0] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_ENTROPY,
  1451. (char *)ent, sizeof(ent));
  1452. params[1] = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_HMACDRBG_NONCE,
  1453. (char *)ent, sizeof(ent));
  1454. params[2] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST, "SHA256",
  1455. 0);
  1456. params[3] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES, "",
  1457. 0);
  1458. params[4] = OSSL_PARAM_construct_end();
  1459. if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1))
  1460. goto err;
  1461. if (!TEST_int_eq(EVP_KDF_CTX_set_params(kctx, params), 1))
  1462. goto err;
  1463. /* Test we can retrieve values back */
  1464. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
  1465. digestname, sizeof(digestname));
  1466. params[1] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_MAC,
  1467. macname, sizeof(macname));
  1468. params[2] = OSSL_PARAM_construct_end();
  1469. if (!TEST_int_eq(EVP_KDF_CTX_get_params(kctx, params), 1)
  1470. || !TEST_mem_eq(digestname, params[0].return_size, "SHA2-256", 8)
  1471. || !TEST_mem_eq(macname, params[1].return_size, "HMAC", 4))
  1472. goto err;
  1473. /* Test the derive */
  1474. if (!TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out), NULL), 1))
  1475. goto err;
  1476. /* test that XOF digests are not allowed */
  1477. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
  1478. "shake256", 0);
  1479. params[1] = OSSL_PARAM_construct_end();
  1480. if (!TEST_int_le(EVP_KDF_CTX_set_params(kctx, params), 0))
  1481. goto err;
  1482. ret = 1;
  1483. err:
  1484. EVP_MD_free(shake256);
  1485. EVP_KDF_CTX_free(kctx);
  1486. return ret;
  1487. }
  1488. static int test_kdf_hmac_drbg_gettables(void)
  1489. {
  1490. int ret = 0, i, j = 0;
  1491. EVP_KDF_CTX *kctx = NULL;
  1492. const OSSL_PARAM *gettableparams;
  1493. OSSL_PARAM params[3];
  1494. char buf[64];
  1495. /* Test there are gettables */
  1496. if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HMACDRBGKDF))
  1497. || !TEST_ptr(gettableparams = EVP_KDF_CTX_gettable_params(kctx)))
  1498. goto err;
  1499. /* Fail if we pass the wrong type for params */
  1500. params[1] = OSSL_PARAM_construct_end();
  1501. for (i = 0; gettableparams[i].key != NULL; ++i) {
  1502. params[0] = OSSL_PARAM_construct_int(gettableparams[i].key, &j);
  1503. if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params), 0))
  1504. goto err;
  1505. }
  1506. /* fail to get params if they are not set yet */
  1507. for (i = 0; gettableparams[i].key != NULL; ++i) {
  1508. params[0] = OSSL_PARAM_construct_utf8_string(gettableparams[i].key,
  1509. buf, sizeof(buf));
  1510. if (!TEST_int_le(EVP_KDF_CTX_get_params(kctx, params), 0))
  1511. goto err;
  1512. }
  1513. ret = 1;
  1514. err:
  1515. EVP_KDF_CTX_free(kctx);
  1516. return ret;
  1517. }
  1518. int setup_tests(void)
  1519. {
  1520. ADD_TEST(test_kdf_pbkdf1);
  1521. #if !defined(OPENSSL_NO_CMAC) && !defined(OPENSSL_NO_CAMELLIA)
  1522. ADD_TEST(test_kdf_kbkdf_6803_128);
  1523. ADD_TEST(test_kdf_kbkdf_6803_256);
  1524. #endif
  1525. ADD_TEST(test_kdf_kbkdf_invalid_digest);
  1526. ADD_TEST(test_kdf_kbkdf_invalid_mac);
  1527. ADD_TEST(test_kdf_kbkdf_invalid_r);
  1528. ADD_TEST(test_kdf_kbkdf_zero_output_size);
  1529. ADD_TEST(test_kdf_kbkdf_empty_key);
  1530. ADD_TEST(test_kdf_kbkdf_1byte_key);
  1531. ADD_TEST(test_kdf_kbkdf_8009_prf1);
  1532. ADD_TEST(test_kdf_kbkdf_8009_prf2);
  1533. #if !defined(OPENSSL_NO_CMAC)
  1534. ADD_TEST(test_kdf_kbkdf_fixedinfo);
  1535. #endif
  1536. ADD_TEST(test_kdf_get_kdf);
  1537. ADD_TEST(test_kdf_tls1_prf);
  1538. ADD_TEST(test_kdf_tls1_prf_invalid_digest);
  1539. ADD_TEST(test_kdf_tls1_prf_zero_output_size);
  1540. ADD_TEST(test_kdf_tls1_prf_empty_secret);
  1541. ADD_TEST(test_kdf_tls1_prf_1byte_secret);
  1542. ADD_TEST(test_kdf_tls1_prf_empty_seed);
  1543. ADD_TEST(test_kdf_tls1_prf_1byte_seed);
  1544. ADD_TEST(test_kdf_hkdf);
  1545. ADD_TEST(test_kdf_hkdf_invalid_digest);
  1546. ADD_TEST(test_kdf_hkdf_zero_output_size);
  1547. ADD_TEST(test_kdf_hkdf_empty_key);
  1548. ADD_TEST(test_kdf_hkdf_1byte_key);
  1549. ADD_TEST(test_kdf_hkdf_empty_salt);
  1550. ADD_TEST(test_kdf_hkdf_gettables);
  1551. ADD_TEST(test_kdf_hkdf_gettables_expandonly);
  1552. ADD_TEST(test_kdf_hkdf_gettables_no_digest);
  1553. ADD_TEST(test_kdf_hkdf_derive_set_params_fail);
  1554. ADD_TEST(test_kdf_hkdf_set_invalid_mode);
  1555. ADD_TEST(test_kdf_hkdf_set_ctx_param_fail);
  1556. ADD_TEST(test_kdf_pbkdf2);
  1557. ADD_TEST(test_kdf_pbkdf2_small_output);
  1558. ADD_TEST(test_kdf_pbkdf2_large_output);
  1559. ADD_TEST(test_kdf_pbkdf2_small_salt);
  1560. ADD_TEST(test_kdf_pbkdf2_small_iterations);
  1561. ADD_TEST(test_kdf_pbkdf2_small_salt_pkcs5);
  1562. ADD_TEST(test_kdf_pbkdf2_small_iterations_pkcs5);
  1563. ADD_TEST(test_kdf_pbkdf2_invalid_digest);
  1564. #ifndef OPENSSL_NO_SCRYPT
  1565. ADD_TEST(test_kdf_scrypt);
  1566. #endif
  1567. ADD_TEST(test_kdf_ss_hash);
  1568. ADD_TEST(test_kdf_ss_hmac);
  1569. ADD_TEST(test_kdf_ss_kmac);
  1570. ADD_TEST(test_kdf_sshkdf);
  1571. ADD_TEST(test_kdf_x963);
  1572. #if !defined(OPENSSL_NO_CMS) && !defined(OPENSSL_NO_DES)
  1573. ADD_TEST(test_kdf_x942_asn1);
  1574. #endif
  1575. ADD_TEST(test_kdf_krb5kdf);
  1576. ADD_TEST(test_kdf_hmac_drbg_settables);
  1577. ADD_TEST(test_kdf_hmac_drbg_gettables);
  1578. return 1;
  1579. }