evp_kdf_test.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 = 0;
  19. EVP_KDF_CTX *kctx;
  20. unsigned char out[16];
  21. if ((kctx = EVP_KDF_CTX_new_id(EVP_KDF_TLS1_PRF)) == NULL) {
  22. TEST_error("EVP_KDF_TLS1_PRF");
  23. goto err;
  24. }
  25. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()) <= 0) {
  26. TEST_error("EVP_KDF_CTRL_SET_MD");
  27. goto err;
  28. }
  29. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_TLS_SECRET,
  30. "secret", (size_t)6) <= 0) {
  31. TEST_error("EVP_KDF_CTRL_SET_TLS_SECRET");
  32. goto err;
  33. }
  34. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_TLS_SEED, "seed", (size_t)4) <= 0) {
  35. TEST_error("EVP_KDF_CTRL_ADD_TLS_SEED");
  36. goto err;
  37. }
  38. if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0) {
  39. TEST_error("EVP_KDF_derive");
  40. goto err;
  41. }
  42. {
  43. const unsigned char expected[sizeof(out)] = {
  44. 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
  45. 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
  46. };
  47. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  48. goto err;
  49. }
  50. }
  51. ret = 1;
  52. err:
  53. EVP_KDF_CTX_free(kctx);
  54. return ret;
  55. }
  56. static int test_kdf_hkdf(void)
  57. {
  58. int ret = 0;
  59. EVP_KDF_CTX *kctx;
  60. unsigned char out[10];
  61. if ((kctx = EVP_KDF_CTX_new_id(EVP_KDF_HKDF)) == NULL) {
  62. TEST_error("EVP_KDF_HKDF");
  63. goto err;
  64. }
  65. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()) <= 0) {
  66. TEST_error("EVP_KDF_CTRL_SET_MD");
  67. goto err;
  68. }
  69. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "salt", (size_t)4) <= 0) {
  70. TEST_error("EVP_KDF_CTRL_SET_SALT");
  71. goto err;
  72. }
  73. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_KEY, "secret", (size_t)6) <= 0) {
  74. TEST_error("EVP_KDF_CTRL_SET_KEY");
  75. goto err;
  76. }
  77. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_ADD_HKDF_INFO,
  78. "label", (size_t)5) <= 0) {
  79. TEST_error("EVP_KDF_CTRL_ADD_HKDF_INFO");
  80. goto err;
  81. }
  82. if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0) {
  83. TEST_error("EVP_KDF_derive");
  84. goto err;
  85. }
  86. {
  87. const unsigned char expected[sizeof(out)] = {
  88. 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
  89. };
  90. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  91. goto err;
  92. }
  93. }
  94. ret = 1;
  95. err:
  96. EVP_KDF_CTX_free(kctx);
  97. return ret;
  98. }
  99. static int test_kdf_pbkdf2(void)
  100. {
  101. int ret = 0;
  102. EVP_KDF_CTX *kctx;
  103. unsigned char out[32];
  104. if ((kctx = EVP_KDF_CTX_new_id(EVP_KDF_PBKDF2)) == NULL) {
  105. TEST_error("EVP_KDF_PBKDF2");
  106. goto err;
  107. }
  108. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, "password", (size_t)8) <= 0) {
  109. TEST_error("EVP_KDF_CTRL_SET_PASS");
  110. goto err;
  111. }
  112. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "salt", (size_t)4) <= 0) {
  113. TEST_error("EVP_KDF_CTRL_SET_SALT");
  114. goto err;
  115. }
  116. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_ITER, 2) <= 0) {
  117. TEST_error("EVP_KDF_CTRL_SET_ITER");
  118. goto err;
  119. }
  120. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MD, EVP_sha256()) <= 0) {
  121. TEST_error("EVP_KDF_CTRL_SET_MD");
  122. goto err;
  123. }
  124. if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0) {
  125. TEST_error("EVP_KDF_derive");
  126. goto err;
  127. }
  128. {
  129. const unsigned char expected[sizeof(out)] = {
  130. 0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
  131. 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0,
  132. 0x2a, 0x30, 0x3f, 0x8e, 0xf3, 0xc2, 0x51, 0xdf,
  133. 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43
  134. };
  135. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  136. goto err;
  137. }
  138. }
  139. ret = 1;
  140. err:
  141. EVP_KDF_CTX_free(kctx);
  142. return ret;
  143. }
  144. #ifndef OPENSSL_NO_SCRYPT
  145. static int test_kdf_scrypt(void)
  146. {
  147. int ret = 0;
  148. EVP_KDF_CTX *kctx;
  149. unsigned char out[64];
  150. if ((kctx = EVP_KDF_CTX_new_id(EVP_KDF_SCRYPT)) == NULL) {
  151. TEST_error("EVP_KDF_SCRYPT");
  152. goto err;
  153. }
  154. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_PASS, "password", (size_t)8) <= 0) {
  155. TEST_error("EVP_KDF_CTRL_SET_PASS");
  156. goto err;
  157. }
  158. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SALT, "NaCl", (size_t)4) <= 0) {
  159. TEST_error("EVP_KDF_CTRL_SET_SALT");
  160. goto err;
  161. }
  162. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_N, (uint64_t)1024) <= 0) {
  163. TEST_error("EVP_KDF_CTRL_SET_SCRYPT_N");
  164. goto err;
  165. }
  166. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_R, (uint32_t)8) <= 0) {
  167. TEST_error("EVP_KDF_CTRL_SET_SCRYPT_R");
  168. goto err;
  169. }
  170. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_SCRYPT_P, (uint32_t)16) <= 0) {
  171. TEST_error("EVP_KDF_CTRL_SET_SCRYPT_P");
  172. goto err;
  173. }
  174. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAXMEM_BYTES, (uint64_t)16) <= 0) {
  175. TEST_error("EVP_KDF_CTRL_SET_MAXMEM_BYTES");
  176. goto err;
  177. }
  178. if (EVP_KDF_derive(kctx, out, sizeof(out)) > 0) {
  179. TEST_error("EVP_KDF_derive should have failed");
  180. goto err;
  181. }
  182. if (EVP_KDF_ctrl(kctx, EVP_KDF_CTRL_SET_MAXMEM_BYTES,
  183. (uint64_t)(10 * 1024 * 1024)) <= 0) {
  184. TEST_error("EVP_KDF_CTRL_SET_MAXMEM_BYTES");
  185. goto err;
  186. }
  187. if (EVP_KDF_derive(kctx, out, sizeof(out)) <= 0) {
  188. TEST_error("EVP_KDF_derive");
  189. goto err;
  190. }
  191. {
  192. const unsigned char expected[sizeof(out)] = {
  193. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  194. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  195. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  196. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  197. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  198. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  199. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  200. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  201. };
  202. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  203. goto err;
  204. }
  205. }
  206. ret = 1;
  207. err:
  208. EVP_KDF_CTX_free(kctx);
  209. return ret;
  210. }
  211. #endif
  212. int setup_tests(void)
  213. {
  214. ADD_TEST(test_kdf_tls1_prf);
  215. ADD_TEST(test_kdf_hkdf);
  216. ADD_TEST(test_kdf_pbkdf2);
  217. #ifndef OPENSSL_NO_SCRYPT
  218. ADD_TEST(test_kdf_scrypt);
  219. #endif
  220. return 1;
  221. }