evp_kdf_test.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * Copyright 2018-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2018-2019, 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 int test_kdf_tls1_prf(void)
  25. {
  26. int ret;
  27. EVP_KDF_CTX *kctx = NULL;
  28. unsigned char out[16];
  29. OSSL_PARAM params[4], *p = params;
  30. static const unsigned char expected[sizeof(out)] = {
  31. 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
  32. 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
  33. };
  34. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  35. (char *)"sha256", sizeof("sha256"));
  36. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
  37. (unsigned char *)"secret",
  38. (size_t)6);
  39. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
  40. (unsigned char *)"seed",
  41. (size_t)4);
  42. *p = OSSL_PARAM_construct_end();
  43. ret =
  44. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_TLS1_PRF))
  45. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  46. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  47. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  48. EVP_KDF_CTX_free(kctx);
  49. return ret;
  50. }
  51. static int test_kdf_hkdf(void)
  52. {
  53. int ret;
  54. EVP_KDF_CTX *kctx;
  55. unsigned char out[10];
  56. OSSL_PARAM params[5], *p = params;
  57. static const unsigned char expected[sizeof(out)] = {
  58. 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
  59. };
  60. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  61. (char *)"sha256", sizeof("sha256"));
  62. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  63. (unsigned char *)"salt", 4);
  64. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
  65. (unsigned char *)"secret", 6);
  66. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
  67. (unsigned char *)"label", 5);
  68. *p = OSSL_PARAM_construct_end();
  69. ret =
  70. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_HKDF))
  71. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  72. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  73. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  74. EVP_KDF_CTX_free(kctx);
  75. return ret;
  76. }
  77. static int test_kdf_pbkdf2(void)
  78. {
  79. int ret = 0;
  80. EVP_KDF_CTX *kctx;
  81. unsigned char out[25];
  82. size_t len = 0;
  83. unsigned int iterations = 4096;
  84. int mode = 0;
  85. OSSL_PARAM params[6], *p = params;
  86. const unsigned char expected[sizeof(out)] = {
  87. 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
  88. 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
  89. 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18,
  90. 0x1c
  91. };
  92. if (sizeof(len) > 32)
  93. len = SIZE_MAX;
  94. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  95. (unsigned char *)
  96. "passwordPASSWORDpassword", 24);
  97. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  98. (unsigned char *)
  99. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  100. 36);
  101. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_ITER, &iterations);
  102. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  103. (char *)"sha256", 7);
  104. *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_PKCS5, &mode);
  105. *p = OSSL_PARAM_construct_end();
  106. if (!TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_PBKDF2))
  107. || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  108. || !TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  109. || !TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))
  110. || !TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  111. /* A key length that is too small should fail */
  112. || !TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1), 0)
  113. /* A key length that is too large should fail */
  114. || (len != 0 && !TEST_int_eq(EVP_KDF_derive(kctx, out, len), 0)))
  115. goto err;
  116. #if 0
  117. /* TODO */
  118. /* Salt length less than 128 bits should fail */
  119. || TEST_int_eq(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
  120. "123456781234567",
  121. (size_t)15), 0)
  122. /* A small iteration count should fail */
  123. || TEST_int_eq(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 1), 0)
  124. || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
  125. 1), 0)
  126. /* Small salts will pass if the "pkcs5" mode is enabled */
  127. || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
  128. "123456781234567",
  129. (size_t)15), 0)
  130. /* A small iteration count will pass if "pkcs5" mode is enabled */
  131. || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 1), 0)
  132. /*
  133. * If the "pkcs5" mode is disabled then the small salt and iter will
  134. * fail when the derive gets called.
  135. */
  136. || TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
  137. 0), 0)
  138. || TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
  139. #endif
  140. ret = 1;
  141. err:
  142. EVP_KDF_CTX_free(kctx);
  143. return ret;
  144. }
  145. #ifndef OPENSSL_NO_SCRYPT
  146. static int test_kdf_scrypt(void)
  147. {
  148. int ret;
  149. EVP_KDF_CTX *kctx;
  150. OSSL_PARAM params[7], *p = params;
  151. unsigned char out[64];
  152. unsigned int nu = 1024, ru = 8, pu = 16, maxmem = 16;
  153. static const unsigned char expected[sizeof(out)] = {
  154. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  155. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  156. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  157. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  158. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  159. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  160. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  161. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  162. };
  163. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PASSWORD,
  164. (char *)"password", 8);
  165. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
  166. (char *)"NaCl", 4);
  167. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_N, &nu);
  168. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_R, &ru);
  169. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_P, &pu);
  170. *p++ = OSSL_PARAM_construct_uint(OSSL_KDF_PARAM_SCRYPT_MAXMEM, &maxmem);
  171. *p = OSSL_PARAM_construct_end();
  172. ret =
  173. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SCRYPT))
  174. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  175. /* failure test *//*
  176. && TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out)), 0)*/
  177. && TEST_true(OSSL_PARAM_set_uint(p - 1, 10 * 1024 * 1024))
  178. && TEST_true(EVP_KDF_CTX_set_params(kctx, p - 1))
  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. return ret;
  183. }
  184. #endif /* OPENSSL_NO_SCRYPT */
  185. static int test_kdf_ss_hash(void)
  186. {
  187. int ret;
  188. EVP_KDF_CTX *kctx;
  189. OSSL_PARAM params[4], *p = params;
  190. unsigned char out[14];
  191. static unsigned char z[] = {
  192. 0x6d,0xbd,0xc2,0x3f,0x04,0x54,0x88,0xe4,0x06,0x27,0x57,0xb0,0x6b,0x9e,
  193. 0xba,0xe1,0x83,0xfc,0x5a,0x59,0x46,0xd8,0x0d,0xb9,0x3f,0xec,0x6f,0x62,
  194. 0xec,0x07,0xe3,0x72,0x7f,0x01,0x26,0xae,0xd1,0x2c,0xe4,0xb2,0x62,0xf4,
  195. 0x7d,0x48,0xd5,0x42,0x87,0xf8,0x1d,0x47,0x4c,0x7c,0x3b,0x18,0x50,0xe9
  196. };
  197. static unsigned char other[] = {
  198. 0xa1,0xb2,0xc3,0xd4,0xe5,0x43,0x41,0x56,0x53,0x69,0x64,0x3c,0x83,0x2e,
  199. 0x98,0x49,0xdc,0xdb,0xa7,0x1e,0x9a,0x31,0x39,0xe6,0x06,0xe0,0x95,0xde,
  200. 0x3c,0x26,0x4a,0x66,0xe9,0x8a,0x16,0x58,0x54,0xcd,0x07,0x98,0x9b,0x1e,
  201. 0xe0,0xec,0x3f,0x8d,0xbe
  202. };
  203. static const unsigned char expected[sizeof(out)] = {
  204. 0xa4,0x62,0xde,0x16,0xa8,0x9d,0xe8,0x46,0x6e,0xf5,0x46,0x0b,0x47,0xb8
  205. };
  206. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  207. (char *)"sha224", sizeof("sha224"));
  208. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
  209. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
  210. sizeof(other));
  211. *p = OSSL_PARAM_construct_end();
  212. ret =
  213. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
  214. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  215. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  216. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  217. EVP_KDF_CTX_free(kctx);
  218. return ret;
  219. }
  220. static int test_kdf_x963(void)
  221. {
  222. int ret;
  223. EVP_KDF_CTX *kctx;
  224. OSSL_PARAM params[4], *p = params;
  225. unsigned char out[1024 / 8];
  226. /*
  227. * Test data from https://csrc.nist.gov/CSRC/media/Projects/
  228. * Cryptographic-Algorithm-Validation-Program/documents/components/
  229. * 800-135testvectors/ansx963_2001.zip
  230. */
  231. static unsigned char z[] = {
  232. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89, 0xfa, 0x58, 0xce, 0xad,
  233. 0xc0, 0x47, 0x19, 0x7f, 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  234. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8, 0x15, 0x07, 0x39, 0x2f,
  235. 0x1a, 0x86, 0xdd, 0xfd, 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  236. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47, 0x47, 0xa9, 0xe2, 0xe7,
  237. 0x53, 0xf5, 0x5e, 0xf0, 0x5a, 0x2d
  238. };
  239. static unsigned char shared[] = {
  240. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d, 0x2b, 0x3a, 0x2f, 0x99,
  241. 0x37, 0x89, 0x5d, 0x31
  242. };
  243. static const unsigned char expected[sizeof(out)] = {
  244. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76, 0x9b, 0x52, 0x26, 0x4b,
  245. 0x01, 0x12, 0xb5, 0x85, 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  246. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d, 0x6e, 0x4d, 0xd2, 0xa5,
  247. 0x99, 0xac, 0xce, 0xb3, 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  248. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50, 0xa5, 0xa8, 0xee, 0xee,
  249. 0x20, 0x80, 0x02, 0x26, 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  250. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d, 0xea, 0x9c, 0x4f, 0xf7,
  251. 0x45, 0xa2, 0x5a, 0xc2, 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  252. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc, 0x0a, 0xb2, 0xaa, 0x8e,
  253. 0x2e, 0xfa, 0x7b, 0x17, 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  254. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  255. };
  256. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  257. (char *)"sha512", sizeof("sha512"));
  258. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
  259. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, shared,
  260. sizeof(shared));
  261. *p = OSSL_PARAM_construct_end();
  262. ret =
  263. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X963KDF))
  264. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  265. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  266. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  267. EVP_KDF_CTX_free(kctx);
  268. return ret;
  269. }
  270. /* Two test vectors from RFC 8009 (AES Encryption with HMAC-SHA2 for Kerberos
  271. * 5) appendix A. */
  272. static int test_kdf_kbkdf_8009_prf1(void)
  273. {
  274. int ret, i = 0;
  275. EVP_KDF_CTX *kctx;
  276. OSSL_PARAM params[6];
  277. char *label = "prf", *digest = "sha256", *prf_input = "test",
  278. *mac = "HMAC";
  279. static unsigned char input_key[] = {
  280. 0x37, 0x05, 0xD9, 0x60, 0x80, 0xC1, 0x77, 0x28,
  281. 0xA0, 0xE8, 0x00, 0xEA, 0xB6, 0xE0, 0xD2, 0x3C,
  282. };
  283. static unsigned char output[] = {
  284. 0x9D, 0x18, 0x86, 0x16, 0xF6, 0x38, 0x52, 0xFE,
  285. 0x86, 0x91, 0x5B, 0xB8, 0x40, 0xB4, 0xA8, 0x86,
  286. 0xFF, 0x3E, 0x6B, 0xB0, 0xF8, 0x19, 0xB4, 0x9B,
  287. 0x89, 0x33, 0x93, 0xD3, 0x93, 0x85, 0x42, 0x95,
  288. };
  289. unsigned char result[sizeof(output)] = { 0 };
  290. params[i++] = OSSL_PARAM_construct_utf8_string(
  291. OSSL_KDF_PARAM_DIGEST, digest, strlen(digest) + 1);
  292. params[i++] = OSSL_PARAM_construct_utf8_string(
  293. OSSL_KDF_PARAM_MAC, mac, strlen(mac) + 1);
  294. params[i++] = OSSL_PARAM_construct_octet_string(
  295. OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
  296. params[i++] = OSSL_PARAM_construct_octet_string(
  297. OSSL_KDF_PARAM_SALT, label, strlen(label));
  298. params[i++] = OSSL_PARAM_construct_octet_string(
  299. OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
  300. params[i] = OSSL_PARAM_construct_end();
  301. kctx = get_kdfbyname("KBKDF");
  302. ret = TEST_ptr(kctx)
  303. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  304. && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
  305. && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
  306. EVP_KDF_CTX_free(kctx);
  307. return ret;
  308. }
  309. static int test_kdf_kbkdf_8009_prf2(void)
  310. {
  311. int ret, i = 0;
  312. EVP_KDF_CTX *kctx;
  313. OSSL_PARAM params[6];
  314. char *label = "prf", *digest = "sha384", *prf_input = "test",
  315. *mac = "HMAC";
  316. static unsigned char input_key[] = {
  317. 0x6D, 0x40, 0x4D, 0x37, 0xFA, 0xF7, 0x9F, 0x9D,
  318. 0xF0, 0xD3, 0x35, 0x68, 0xD3, 0x20, 0x66, 0x98,
  319. 0x00, 0xEB, 0x48, 0x36, 0x47, 0x2E, 0xA8, 0xA0,
  320. 0x26, 0xD1, 0x6B, 0x71, 0x82, 0x46, 0x0C, 0x52,
  321. };
  322. static unsigned char output[] = {
  323. 0x98, 0x01, 0xF6, 0x9A, 0x36, 0x8C, 0x2B, 0xF6,
  324. 0x75, 0xE5, 0x95, 0x21, 0xE1, 0x77, 0xD9, 0xA0,
  325. 0x7F, 0x67, 0xEF, 0xE1, 0xCF, 0xDE, 0x8D, 0x3C,
  326. 0x8D, 0x6F, 0x6A, 0x02, 0x56, 0xE3, 0xB1, 0x7D,
  327. 0xB3, 0xC1, 0xB6, 0x2A, 0xD1, 0xB8, 0x55, 0x33,
  328. 0x60, 0xD1, 0x73, 0x67, 0xEB, 0x15, 0x14, 0xD2,
  329. };
  330. unsigned char result[sizeof(output)] = { 0 };
  331. params[i++] = OSSL_PARAM_construct_utf8_string(
  332. OSSL_KDF_PARAM_DIGEST, digest, strlen(digest) + 1);
  333. params[i++] = OSSL_PARAM_construct_utf8_string(
  334. OSSL_KDF_PARAM_MAC, mac, strlen(mac) + 1);
  335. params[i++] = OSSL_PARAM_construct_octet_string(
  336. OSSL_KDF_PARAM_KEY, input_key, sizeof(input_key));
  337. params[i++] = OSSL_PARAM_construct_octet_string(
  338. OSSL_KDF_PARAM_SALT, label, strlen(label));
  339. params[i++] = OSSL_PARAM_construct_octet_string(
  340. OSSL_KDF_PARAM_INFO, prf_input, strlen(prf_input));
  341. params[i] = OSSL_PARAM_construct_end();
  342. kctx = get_kdfbyname("KBKDF");
  343. ret = TEST_ptr(kctx)
  344. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  345. && TEST_int_gt(EVP_KDF_derive(kctx, result, sizeof(result)), 0)
  346. && TEST_mem_eq(result, sizeof(result), output, sizeof(output));
  347. EVP_KDF_CTX_free(kctx);
  348. return ret;
  349. }
  350. static int test_kdf_ss_hmac(void)
  351. {
  352. int ret;
  353. EVP_KDF_CTX *kctx;
  354. OSSL_PARAM params[6], *p = params;
  355. unsigned char out[16];
  356. static unsigned char z[] = {
  357. 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
  358. };
  359. static unsigned char other[] = {
  360. 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
  361. };
  362. static unsigned char salt[] = {
  363. 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
  364. 0x3f,0x89
  365. };
  366. static const unsigned char expected[sizeof(out)] = {
  367. 0x44,0xf6,0x76,0xe8,0x5c,0x1b,0x1a,0x8b,0xbc,0x3d,0x31,0x92,0x18,0x63,
  368. 0x1c,0xa3
  369. };
  370. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
  371. (char *)OSSL_MAC_NAME_HMAC,
  372. sizeof(OSSL_MAC_NAME_HMAC));
  373. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  374. (char *)"sha256", sizeof("sha256"));
  375. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
  376. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
  377. sizeof(other));
  378. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt,
  379. sizeof(salt));
  380. *p = OSSL_PARAM_construct_end();
  381. ret =
  382. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
  383. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  384. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  385. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  386. EVP_KDF_CTX_free(kctx);
  387. return ret;
  388. }
  389. static int test_kdf_ss_kmac(void)
  390. {
  391. int ret;
  392. EVP_KDF_CTX *kctx;
  393. OSSL_PARAM params[6], *p = params;
  394. unsigned char out[64];
  395. size_t mac_size = 20;
  396. static unsigned char z[] = {
  397. 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
  398. };
  399. static unsigned char other[] = {
  400. 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
  401. };
  402. static unsigned char salt[] = {
  403. 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
  404. 0x3f,0x89
  405. };
  406. static const unsigned char expected[sizeof(out)] = {
  407. 0xe9,0xc1,0x84,0x53,0xa0,0x62,0xb5,0x3b,0xdb,0xfc,0xbb,0x5a,0x34,0xbd,
  408. 0xb8,0xe5,0xe7,0x07,0xee,0xbb,0x5d,0xd1,0x34,0x42,0x43,0xd8,0xcf,0xc2,
  409. 0xc2,0xe6,0x33,0x2f,0x91,0xbd,0xa5,0x86,0xf3,0x7d,0xe4,0x8a,0x65,0xd4,
  410. 0xc5,0x14,0xfd,0xef,0xaa,0x1e,0x67,0x54,0xf3,0x73,0xd2,0x38,0xe1,0x95,
  411. 0xae,0x15,0x7e,0x1d,0xe8,0x14,0x98,0x03
  412. };
  413. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_MAC,
  414. (char *)OSSL_MAC_NAME_KMAC128,
  415. sizeof(OSSL_MAC_NAME_KMAC128));
  416. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z, sizeof(z));
  417. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO, other,
  418. sizeof(other));
  419. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT, salt,
  420. sizeof(salt));
  421. *p++ = OSSL_PARAM_construct_size_t(OSSL_KDF_PARAM_MAC_SIZE, &mac_size);
  422. *p = OSSL_PARAM_construct_end();
  423. ret =
  424. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSKDF))
  425. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  426. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  427. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  428. EVP_KDF_CTX_free(kctx);
  429. return ret;
  430. }
  431. static int test_kdf_sshkdf(void)
  432. {
  433. int ret;
  434. EVP_KDF_CTX *kctx;
  435. OSSL_PARAM params[6], *p = params;
  436. char kdftype = EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV;
  437. unsigned char out[8];
  438. /* Test data from NIST CAVS 14.1 test vectors */
  439. static unsigned char key[] = {
  440. 0x00, 0x00, 0x00, 0x81, 0x00, 0x87, 0x5c, 0x55, 0x1c, 0xef, 0x52, 0x6a,
  441. 0x4a, 0x8b, 0xe1, 0xa7, 0xdf, 0x27, 0xe9, 0xed, 0x35, 0x4b, 0xac, 0x9a,
  442. 0xfb, 0x71, 0xf5, 0x3d, 0xba, 0xe9, 0x05, 0x67, 0x9d, 0x14, 0xf9, 0xfa,
  443. 0xf2, 0x46, 0x9c, 0x53, 0x45, 0x7c, 0xf8, 0x0a, 0x36, 0x6b, 0xe2, 0x78,
  444. 0x96, 0x5b, 0xa6, 0x25, 0x52, 0x76, 0xca, 0x2d, 0x9f, 0x4a, 0x97, 0xd2,
  445. 0x71, 0xf7, 0x1e, 0x50, 0xd8, 0xa9, 0xec, 0x46, 0x25, 0x3a, 0x6a, 0x90,
  446. 0x6a, 0xc2, 0xc5, 0xe4, 0xf4, 0x8b, 0x27, 0xa6, 0x3c, 0xe0, 0x8d, 0x80,
  447. 0x39, 0x0a, 0x49, 0x2a, 0xa4, 0x3b, 0xad, 0x9d, 0x88, 0x2c, 0xca, 0xc2,
  448. 0x3d, 0xac, 0x88, 0xbc, 0xad, 0xa4, 0xb4, 0xd4, 0x26, 0xa3, 0x62, 0x08,
  449. 0x3d, 0xab, 0x65, 0x69, 0xc5, 0x4c, 0x22, 0x4d, 0xd2, 0xd8, 0x76, 0x43,
  450. 0xaa, 0x22, 0x76, 0x93, 0xe1, 0x41, 0xad, 0x16, 0x30, 0xce, 0x13, 0x14,
  451. 0x4e
  452. };
  453. static unsigned char xcghash[] = {
  454. 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
  455. 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
  456. 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
  457. };
  458. static unsigned char sessid[] = {
  459. 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
  460. 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
  461. 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
  462. };
  463. static const unsigned char expected[sizeof(out)] = {
  464. 0x41, 0xff, 0x2e, 0xad, 0x16, 0x83, 0xf1, 0xe6
  465. };
  466. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  467. (char *)"sha256", sizeof("sha256"));
  468. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, key,
  469. sizeof(key));
  470. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_XCGHASH,
  471. xcghash, sizeof(xcghash));
  472. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SSHKDF_SESSION_ID,
  473. sessid, sizeof(sessid));
  474. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_SSHKDF_TYPE,
  475. &kdftype, sizeof(kdftype));
  476. *p = OSSL_PARAM_construct_end();
  477. ret =
  478. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_SSHKDF))
  479. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  480. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  481. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  482. EVP_KDF_CTX_free(kctx);
  483. return ret;
  484. }
  485. static int test_kdf_get_kdf(void)
  486. {
  487. EVP_KDF *kdf1 = NULL, *kdf2 = NULL;
  488. ASN1_OBJECT *obj;
  489. int ok = 1;
  490. if (!TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2))
  491. || !TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_PBKDF2, NULL))
  492. || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(OBJ_obj2nid(obj)),
  493. NULL))
  494. || !TEST_ptr_eq(kdf1, kdf2))
  495. ok = 0;
  496. EVP_KDF_free(kdf1);
  497. kdf1 = NULL;
  498. EVP_KDF_free(kdf2);
  499. kdf2 = NULL;
  500. if (!TEST_ptr(kdf1 = EVP_KDF_fetch(NULL, SN_tls1_prf, NULL))
  501. || !TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, LN_tls1_prf, NULL))
  502. || !TEST_ptr_eq(kdf1, kdf2))
  503. ok = 0;
  504. /* kdf1 is re-used below, so don't free it here */
  505. EVP_KDF_free(kdf2);
  506. kdf2 = NULL;
  507. if (!TEST_ptr(kdf2 = EVP_KDF_fetch(NULL, OBJ_nid2sn(NID_tls1_prf), NULL))
  508. || !TEST_ptr_eq(kdf1, kdf2))
  509. ok = 0;
  510. EVP_KDF_free(kdf1);
  511. kdf1 = NULL;
  512. EVP_KDF_free(kdf2);
  513. kdf2 = NULL;
  514. return ok;
  515. }
  516. #ifndef OPENSSL_NO_CMS
  517. static int test_kdf_x942_asn1(void)
  518. {
  519. int ret;
  520. EVP_KDF_CTX *kctx = NULL;
  521. OSSL_PARAM params[4], *p = params;
  522. const char *cek_alg = SN_id_smime_alg_CMS3DESwrap;
  523. unsigned char out[24];
  524. /* RFC2631 Section 2.1.6 Test data */
  525. static unsigned char z[] = {
  526. 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,
  527. 0x0e,0x0f,0x10,0x11,0x12,0x13
  528. };
  529. static const unsigned char expected[sizeof(out)] = {
  530. 0xa0,0x96,0x61,0x39,0x23,0x76,0xf7,0x04,
  531. 0x4d,0x90,0x52,0xa3,0x97,0x88,0x32,0x46,
  532. 0xb6,0x7f,0x5f,0x1e,0xf6,0x3e,0xb5,0xfb
  533. };
  534. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
  535. (char *)"sha1", sizeof("sha1"));
  536. *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY, z,
  537. sizeof(z));
  538. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_CEK_ALG,
  539. (char *)cek_alg,
  540. strlen(cek_alg) + 1);
  541. *p = OSSL_PARAM_construct_end();
  542. ret =
  543. TEST_ptr(kctx = get_kdfbyname(OSSL_KDF_NAME_X942KDF))
  544. && TEST_true(EVP_KDF_CTX_set_params(kctx, params))
  545. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  546. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  547. EVP_KDF_CTX_free(kctx);
  548. return ret;
  549. }
  550. #endif /* OPENSSL_NO_CMS */
  551. int setup_tests(void)
  552. {
  553. ADD_TEST(test_kdf_kbkdf_8009_prf1);
  554. ADD_TEST(test_kdf_kbkdf_8009_prf2);
  555. ADD_TEST(test_kdf_get_kdf);
  556. ADD_TEST(test_kdf_tls1_prf);
  557. ADD_TEST(test_kdf_hkdf);
  558. ADD_TEST(test_kdf_pbkdf2);
  559. #ifndef OPENSSL_NO_SCRYPT
  560. ADD_TEST(test_kdf_scrypt);
  561. #endif
  562. ADD_TEST(test_kdf_ss_hash);
  563. ADD_TEST(test_kdf_ss_hmac);
  564. ADD_TEST(test_kdf_ss_kmac);
  565. ADD_TEST(test_kdf_sshkdf);
  566. ADD_TEST(test_kdf_x963);
  567. #ifndef OPENSSL_NO_CMS
  568. ADD_TEST(test_kdf_x942_asn1);
  569. #endif
  570. return 1;
  571. }