evp_kdf_test.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 "testutil.h"
  16. static int test_kdf_tls1_prf(void)
  17. {
  18. int ret;
  19. EVP_KDF_CTX *kctx = NULL;
  20. const EVP_KDF *kdf;
  21. unsigned char out[16];
  22. static const unsigned char expected[sizeof(out)] = {
  23. 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
  24. 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
  25. };
  26. ret =
  27. TEST_ptr(kdf = EVP_get_kdfbyname(SN_tls1_prf))
  28. && TEST_ptr(kctx = EVP_KDF_CTX_new(kdf))
  29. && TEST_ptr_eq(EVP_KDF_CTX_kdf(kctx), kdf)
  30. && TEST_str_eq(EVP_KDF_name(kdf), SN_tls1_prf)
  31. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()), 0)
  32. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_TLS_SECRET,
  33. "secret", (size_t)6), 0)
  34. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED, "seed",
  35. (size_t)4), 0)
  36. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  37. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  38. EVP_KDF_CTX_free(kctx);
  39. return ret;
  40. }
  41. static int test_kdf_hkdf(void)
  42. {
  43. int ret;
  44. EVP_KDF_CTX *kctx;
  45. unsigned char out[10];
  46. static const unsigned char expected[sizeof(out)] = {
  47. 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
  48. };
  49. ret =
  50. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_HKDF))
  51. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()), 0)
  52. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "salt",
  53. (size_t)4), 0)
  54. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, "secret",
  55. (size_t)6), 0)
  56. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_HKDF_INFO, "label",
  57. (size_t)5), 0)
  58. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  59. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  60. EVP_KDF_CTX_free(kctx);
  61. return ret;
  62. }
  63. static int test_kdf_pbkdf2(void)
  64. {
  65. int ret;
  66. EVP_KDF_CTX *kctx;
  67. unsigned char out[25];
  68. size_t len = 0;
  69. const unsigned char expected[sizeof(out)] = {
  70. 0x34, 0x8c, 0x89, 0xdb, 0xcb, 0xd3, 0x2b, 0x2f,
  71. 0x32, 0xd8, 0x14, 0xb8, 0x11, 0x6e, 0x84, 0xcf,
  72. 0x2b, 0x17, 0x34, 0x7e, 0xbc, 0x18, 0x00, 0x18,
  73. 0x1c
  74. };
  75. if (sizeof(len) > 32)
  76. len = SIZE_MAX;
  77. ret = TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_PBKDF2))
  78. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS,
  79. "passwordPASSWORDpassword",
  80. (size_t)24), 0)
  81. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
  82. "saltSALTsaltSALTsaltSALTsaltSALTsalt",
  83. (size_t)36), 0)
  84. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 4096), 0)
  85. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()),
  86. 0)
  87. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
  88. 0), 0)
  89. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  90. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))
  91. /* A key length that is too small should fail */
  92. && TEST_int_eq(EVP_KDF_derive(kctx, out, 112 / 8 - 1), 0)
  93. /* A key length that is too large should fail */
  94. && (len == 0 || TEST_int_eq(EVP_KDF_derive(kctx, out, len), 0))
  95. /* Salt length less than 128 bits should fail */
  96. && TEST_int_eq(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
  97. "123456781234567",
  98. (size_t)15), 0)
  99. /* A small iteration count should fail */
  100. && TEST_int_eq(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 1), 0)
  101. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
  102. 1), 0)
  103. /* Small salts will pass if the "pkcs5" mode is enabled */
  104. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT,
  105. "123456781234567",
  106. (size_t)15), 0)
  107. /* A small iteration count will pass if "pkcs5" mode is enabled */
  108. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 1), 0)
  109. /*
  110. * If the "pkcs5" mode is disabled then the small salt and iter will
  111. * fail when the derive gets called.
  112. */
  113. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PBKDF2_PKCS5_MODE,
  114. 0), 0)
  115. && TEST_int_eq(EVP_KDF_derive(kctx, out, sizeof(out)), 0);
  116. EVP_KDF_CTX_free(kctx);
  117. return ret;
  118. }
  119. #ifndef OPENSSL_NO_SCRYPT
  120. static int test_kdf_scrypt(void)
  121. {
  122. int ret;
  123. EVP_KDF_CTX *kctx;
  124. unsigned char out[64];
  125. static const unsigned char expected[sizeof(out)] = {
  126. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  127. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  128. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  129. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  130. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  131. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  132. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  133. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  134. };
  135. ret =
  136. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SCRYPT))
  137. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, "password",
  138. (size_t)8), 0)
  139. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "NaCl",
  140. (size_t)4), 0)
  141. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_N,
  142. (uint64_t)1024), 0)
  143. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_R,
  144. (uint32_t)8), 0)
  145. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_P,
  146. (uint32_t)16), 0)
  147. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAXMEM_BYTES,
  148. (uint64_t)16), 0)
  149. /* failure test */
  150. && TEST_int_le(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  151. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAXMEM_BYTES,
  152. (uint64_t)(10 * 1024 * 1024)), 0)
  153. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  154. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  155. EVP_KDF_CTX_free(kctx);
  156. return ret;
  157. }
  158. #endif /* OPENSSL_NO_SCRYPT */
  159. static int test_kdf_ss_hash(void)
  160. {
  161. int ret;
  162. EVP_KDF_CTX *kctx = NULL;
  163. unsigned char out[14];
  164. static const unsigned char z[] = {
  165. 0x6d,0xbd,0xc2,0x3f,0x04,0x54,0x88,0xe4,0x06,0x27,0x57,0xb0,0x6b,0x9e,
  166. 0xba,0xe1,0x83,0xfc,0x5a,0x59,0x46,0xd8,0x0d,0xb9,0x3f,0xec,0x6f,0x62,
  167. 0xec,0x07,0xe3,0x72,0x7f,0x01,0x26,0xae,0xd1,0x2c,0xe4,0xb2,0x62,0xf4,
  168. 0x7d,0x48,0xd5,0x42,0x87,0xf8,0x1d,0x47,0x4c,0x7c,0x3b,0x18,0x50,0xe9
  169. };
  170. static const unsigned char other[] = {
  171. 0xa1,0xb2,0xc3,0xd4,0xe5,0x43,0x41,0x56,0x53,0x69,0x64,0x3c,0x83,0x2e,
  172. 0x98,0x49,0xdc,0xdb,0xa7,0x1e,0x9a,0x31,0x39,0xe6,0x06,0xe0,0x95,0xde,
  173. 0x3c,0x26,0x4a,0x66,0xe9,0x8a,0x16,0x58,0x54,0xcd,0x07,0x98,0x9b,0x1e,
  174. 0xe0,0xec,0x3f,0x8d,0xbe
  175. };
  176. static const unsigned char expected[sizeof(out)] = {
  177. 0xa4,0x62,0xde,0x16,0xa8,0x9d,0xe8,0x46,0x6e,0xf5,0x46,0x0b,0x47,0xb8
  178. };
  179. ret =
  180. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SS))
  181. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha224()), 0)
  182. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, z, sizeof(z)), 0)
  183. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSKDF_INFO, other,
  184. sizeof(other)), 0)
  185. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  186. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  187. EVP_KDF_CTX_free(kctx);
  188. return ret;
  189. }
  190. static int test_kdf_x963(void)
  191. {
  192. int ret;
  193. EVP_KDF_CTX *kctx = NULL;
  194. unsigned char out[1024 / 8];
  195. /*
  196. * Test data from https://csrc.nist.gov/CSRC/media/Projects/
  197. * Cryptographic-Algorithm-Validation-Program/documents/components/
  198. * 800-135testvectors/ansx963_2001.zip
  199. */
  200. static const unsigned char z[] = {
  201. 0x00, 0xaa, 0x5b, 0xb7, 0x9b, 0x33, 0xe3, 0x89, 0xfa, 0x58, 0xce, 0xad,
  202. 0xc0, 0x47, 0x19, 0x7f, 0x14, 0xe7, 0x37, 0x12, 0xf4, 0x52, 0xca, 0xa9,
  203. 0xfc, 0x4c, 0x9a, 0xdb, 0x36, 0x93, 0x48, 0xb8, 0x15, 0x07, 0x39, 0x2f,
  204. 0x1a, 0x86, 0xdd, 0xfd, 0xb7, 0xc4, 0xff, 0x82, 0x31, 0xc4, 0xbd, 0x0f,
  205. 0x44, 0xe4, 0x4a, 0x1b, 0x55, 0xb1, 0x40, 0x47, 0x47, 0xa9, 0xe2, 0xe7,
  206. 0x53, 0xf5, 0x5e, 0xf0, 0x5a, 0x2d
  207. };
  208. static const unsigned char shared[] = {
  209. 0xe3, 0xb5, 0xb4, 0xc1, 0xb0, 0xd5, 0xcf, 0x1d, 0x2b, 0x3a, 0x2f, 0x99,
  210. 0x37, 0x89, 0x5d, 0x31
  211. };
  212. static const unsigned char expected[sizeof(out)] = {
  213. 0x44, 0x63, 0xf8, 0x69, 0xf3, 0xcc, 0x18, 0x76, 0x9b, 0x52, 0x26, 0x4b,
  214. 0x01, 0x12, 0xb5, 0x85, 0x8f, 0x7a, 0xd3, 0x2a, 0x5a, 0x2d, 0x96, 0xd8,
  215. 0xcf, 0xfa, 0xbf, 0x7f, 0xa7, 0x33, 0x63, 0x3d, 0x6e, 0x4d, 0xd2, 0xa5,
  216. 0x99, 0xac, 0xce, 0xb3, 0xea, 0x54, 0xa6, 0x21, 0x7c, 0xe0, 0xb5, 0x0e,
  217. 0xef, 0x4f, 0x6b, 0x40, 0xa5, 0xc3, 0x02, 0x50, 0xa5, 0xa8, 0xee, 0xee,
  218. 0x20, 0x80, 0x02, 0x26, 0x70, 0x89, 0xdb, 0xf3, 0x51, 0xf3, 0xf5, 0x02,
  219. 0x2a, 0xa9, 0x63, 0x8b, 0xf1, 0xee, 0x41, 0x9d, 0xea, 0x9c, 0x4f, 0xf7,
  220. 0x45, 0xa2, 0x5a, 0xc2, 0x7b, 0xda, 0x33, 0xca, 0x08, 0xbd, 0x56, 0xdd,
  221. 0x1a, 0x59, 0xb4, 0x10, 0x6c, 0xf2, 0xdb, 0xbc, 0x0a, 0xb2, 0xaa, 0x8e,
  222. 0x2e, 0xfa, 0x7b, 0x17, 0x90, 0x2d, 0x34, 0x27, 0x69, 0x51, 0xce, 0xcc,
  223. 0xab, 0x87, 0xf9, 0x66, 0x1c, 0x3e, 0x88, 0x16
  224. };
  225. ret =
  226. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_X963))
  227. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha512()), 0)
  228. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, z, sizeof(z)), 0)
  229. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SHARED_INFO, shared,
  230. sizeof(shared)), 0)
  231. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  232. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  233. EVP_KDF_CTX_free(kctx);
  234. return ret;
  235. }
  236. static int test_kdf_ss_hmac(void)
  237. {
  238. int ret;
  239. EVP_KDF_CTX *kctx;
  240. const EVP_MAC *mac;
  241. unsigned char out[16];
  242. static const unsigned char z[] = {
  243. 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
  244. };
  245. static const unsigned char other[] = {
  246. 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
  247. };
  248. static const unsigned char salt[] = {
  249. 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
  250. 0x3f,0x89
  251. };
  252. static const unsigned char expected[sizeof(out)] = {
  253. 0x44,0xf6,0x76,0xe8,0x5c,0x1b,0x1a,0x8b,0xbc,0x3d,0x31,0x92,0x18,0x63,
  254. 0x1c,0xa3
  255. };
  256. ret =
  257. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SS))
  258. && TEST_ptr(mac = EVP_get_macbyname("HMAC"))
  259. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC, mac), 0)
  260. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()), 0)
  261. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, z, sizeof(z)), 0)
  262. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSKDF_INFO, other,
  263. sizeof(other)), 0)
  264. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, salt,
  265. sizeof(salt)), 0)
  266. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  267. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  268. EVP_KDF_CTX_free(kctx);
  269. return ret;
  270. }
  271. static int test_kdf_ss_kmac(void)
  272. {
  273. int ret;
  274. EVP_KDF_CTX *kctx;
  275. unsigned char out[64];
  276. const EVP_MAC *mac;
  277. static const unsigned char z[] = {
  278. 0xb7,0x4a,0x14,0x9a,0x16,0x15,0x46,0xf8,0xc2,0x0b,0x06,0xac,0x4e,0xd4
  279. };
  280. static const unsigned char other[] = {
  281. 0x34,0x8a,0x37,0xa2,0x7e,0xf1,0x28,0x2f,0x5f,0x02,0x0d,0xcc
  282. };
  283. static const unsigned char salt[] = {
  284. 0x36,0x38,0x27,0x1c,0xcd,0x68,0xa2,0x5d,0xc2,0x4e,0xcd,0xdd,0x39,0xef,
  285. 0x3f,0x89
  286. };
  287. static const unsigned char expected[sizeof(out)] = {
  288. 0xe9,0xc1,0x84,0x53,0xa0,0x62,0xb5,0x3b,0xdb,0xfc,0xbb,0x5a,0x34,0xbd,
  289. 0xb8,0xe5,0xe7,0x07,0xee,0xbb,0x5d,0xd1,0x34,0x42,0x43,0xd8,0xcf,0xc2,
  290. 0xc2,0xe6,0x33,0x2f,0x91,0xbd,0xa5,0x86,0xf3,0x7d,0xe4,0x8a,0x65,0xd4,
  291. 0xc5,0x14,0xfd,0xef,0xaa,0x1e,0x67,0x54,0xf3,0x73,0xd2,0x38,0xe1,0x95,
  292. 0xae,0x15,0x7e,0x1d,0xe8,0x14,0x98,0x03
  293. };
  294. ret =
  295. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SS))
  296. && TEST_ptr(mac = EVP_get_macbyname("KMAC128"))
  297. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC, mac), 0)
  298. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, z,
  299. sizeof(z)), 0)
  300. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSKDF_INFO, other,
  301. sizeof(other)), 0)
  302. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, salt,
  303. sizeof(salt)), 0)
  304. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAC_SIZE,
  305. (size_t)20), 0)
  306. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  307. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  308. EVP_KDF_CTX_free(kctx);
  309. return ret;
  310. }
  311. static int test_kdf_sshkdf(void)
  312. {
  313. int ret;
  314. EVP_KDF_CTX *kctx;
  315. unsigned char out[8];
  316. /* Test data from NIST CAVS 14.1 test vectors */
  317. static const unsigned char key[] = {
  318. 0x00, 0x00, 0x00, 0x81, 0x00, 0x87, 0x5c, 0x55, 0x1c, 0xef, 0x52, 0x6a,
  319. 0x4a, 0x8b, 0xe1, 0xa7, 0xdf, 0x27, 0xe9, 0xed, 0x35, 0x4b, 0xac, 0x9a,
  320. 0xfb, 0x71, 0xf5, 0x3d, 0xba, 0xe9, 0x05, 0x67, 0x9d, 0x14, 0xf9, 0xfa,
  321. 0xf2, 0x46, 0x9c, 0x53, 0x45, 0x7c, 0xf8, 0x0a, 0x36, 0x6b, 0xe2, 0x78,
  322. 0x96, 0x5b, 0xa6, 0x25, 0x52, 0x76, 0xca, 0x2d, 0x9f, 0x4a, 0x97, 0xd2,
  323. 0x71, 0xf7, 0x1e, 0x50, 0xd8, 0xa9, 0xec, 0x46, 0x25, 0x3a, 0x6a, 0x90,
  324. 0x6a, 0xc2, 0xc5, 0xe4, 0xf4, 0x8b, 0x27, 0xa6, 0x3c, 0xe0, 0x8d, 0x80,
  325. 0x39, 0x0a, 0x49, 0x2a, 0xa4, 0x3b, 0xad, 0x9d, 0x88, 0x2c, 0xca, 0xc2,
  326. 0x3d, 0xac, 0x88, 0xbc, 0xad, 0xa4, 0xb4, 0xd4, 0x26, 0xa3, 0x62, 0x08,
  327. 0x3d, 0xab, 0x65, 0x69, 0xc5, 0x4c, 0x22, 0x4d, 0xd2, 0xd8, 0x76, 0x43,
  328. 0xaa, 0x22, 0x76, 0x93, 0xe1, 0x41, 0xad, 0x16, 0x30, 0xce, 0x13, 0x14,
  329. 0x4e
  330. };
  331. static const unsigned char xcghash[] = {
  332. 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
  333. 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
  334. 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
  335. };
  336. static const unsigned char sessid[] = {
  337. 0x0e, 0x68, 0x3f, 0xc8, 0xa9, 0xed, 0x7c, 0x2f, 0xf0, 0x2d, 0xef, 0x23,
  338. 0xb2, 0x74, 0x5e, 0xbc, 0x99, 0xb2, 0x67, 0xda, 0xa8, 0x6a, 0x4a, 0xa7,
  339. 0x69, 0x72, 0x39, 0x08, 0x82, 0x53, 0xf6, 0x42
  340. };
  341. static const unsigned char expected[sizeof(out)] = {
  342. 0x41, 0xff, 0x2e, 0xad, 0x16, 0x83, 0xf1, 0xe6
  343. };
  344. ret =
  345. TEST_ptr(kctx = EVP_KDF_CTX_new_id(EVP_KDF_SSHKDF))
  346. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()), 0)
  347. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, key,
  348. sizeof(key)), 0)
  349. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSHKDF_XCGHASH,
  350. xcghash, sizeof(xcghash)), 0)
  351. && TEST_int_gt(EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSHKDF_SESSION_ID,
  352. sessid, sizeof(sessid)), 0)
  353. && TEST_int_gt(
  354. EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SSHKDF_TYPE,
  355. (int)EVP_KDF_SSHKDF_TYPE_INITIAL_IV_CLI_TO_SRV), 0)
  356. && TEST_int_gt(EVP_KDF_derive(kctx, out, sizeof(out)), 0)
  357. && TEST_mem_eq(out, sizeof(out), expected, sizeof(expected));
  358. EVP_KDF_CTX_free(kctx);
  359. return ret;
  360. }
  361. static int test_kdf_get_kdf(void)
  362. {
  363. const EVP_KDF *kdf1, *kdf2;
  364. ASN1_OBJECT *obj;
  365. return
  366. TEST_ptr(obj = OBJ_nid2obj(NID_id_pbkdf2))
  367. && TEST_ptr(kdf1 = EVP_get_kdfbyname(LN_id_pbkdf2))
  368. && TEST_ptr(kdf2 = EVP_get_kdfbyobj(obj))
  369. && TEST_ptr_eq(kdf1, kdf2)
  370. && TEST_ptr(kdf1 = EVP_get_kdfbyname(SN_tls1_prf))
  371. && TEST_ptr(kdf2 = EVP_get_kdfbyname(LN_tls1_prf))
  372. && TEST_ptr_eq(kdf1, kdf2)
  373. && TEST_ptr(kdf2 = EVP_get_kdfbynid(NID_tls1_prf))
  374. && TEST_ptr_eq(kdf1, kdf2);
  375. }
  376. int setup_tests(void)
  377. {
  378. ADD_TEST(test_kdf_get_kdf);
  379. ADD_TEST(test_kdf_tls1_prf);
  380. ADD_TEST(test_kdf_hkdf);
  381. ADD_TEST(test_kdf_pbkdf2);
  382. #ifndef OPENSSL_NO_SCRYPT
  383. ADD_TEST(test_kdf_scrypt);
  384. #endif
  385. ADD_TEST(test_kdf_ss_hash);
  386. ADD_TEST(test_kdf_ss_hmac);
  387. ADD_TEST(test_kdf_ss_kmac);
  388. ADD_TEST(test_kdf_sshkdf);
  389. ADD_TEST(test_kdf_x963);
  390. return 1;
  391. }