evp_kdf_test.c 48 KB

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