2
0

evp_kdf_test.c 51 KB

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