2
0

pkey_meth_kdf_test.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* Tests of the EVP_PKEY_CTX_set_* macro family */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <openssl/evp.h>
  13. #include <openssl/kdf.h>
  14. #include "testutil.h"
  15. static int test_kdf_tls1_prf(void)
  16. {
  17. int ret = 0;
  18. EVP_PKEY_CTX *pctx;
  19. unsigned char out[16];
  20. size_t outlen = sizeof(out);
  21. if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_TLS1_PRF, NULL)) == NULL) {
  22. TEST_error("EVP_PKEY_TLS1_PRF");
  23. goto err;
  24. }
  25. if (EVP_PKEY_derive_init(pctx) <= 0) {
  26. TEST_error("EVP_PKEY_derive_init");
  27. goto err;
  28. }
  29. if (EVP_PKEY_CTX_set_tls1_prf_md(pctx, EVP_sha256()) <= 0) {
  30. TEST_error("EVP_PKEY_CTX_set_tls1_prf_md");
  31. goto err;
  32. }
  33. if (EVP_PKEY_CTX_set1_tls1_prf_secret(pctx, "secret", 6) <= 0) {
  34. TEST_error("EVP_PKEY_CTX_set1_tls1_prf_secret");
  35. goto err;
  36. }
  37. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx, "seed", 4) <= 0) {
  38. TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed");
  39. goto err;
  40. }
  41. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
  42. TEST_error("EVP_PKEY_derive");
  43. goto err;
  44. }
  45. {
  46. const unsigned char expected[sizeof(out)] = {
  47. 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
  48. 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
  49. };
  50. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  51. goto err;
  52. }
  53. }
  54. ret = 1;
  55. err:
  56. EVP_PKEY_CTX_free(pctx);
  57. return ret;
  58. }
  59. static int test_kdf_hkdf(void)
  60. {
  61. int ret = 0;
  62. EVP_PKEY_CTX *pctx;
  63. unsigned char out[10];
  64. size_t outlen = sizeof(out);
  65. if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) {
  66. TEST_error("EVP_PKEY_HKDF");
  67. goto err;
  68. }
  69. if (EVP_PKEY_derive_init(pctx) <= 0) {
  70. TEST_error("EVP_PKEY_derive_init");
  71. goto err;
  72. }
  73. if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
  74. TEST_error("EVP_PKEY_CTX_set_hkdf_md");
  75. goto err;
  76. }
  77. if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, "salt", 4) <= 0) {
  78. TEST_error("EVP_PKEY_CTX_set1_hkdf_salt");
  79. goto err;
  80. }
  81. if (EVP_PKEY_CTX_set1_hkdf_key(pctx, "secret", 6) <= 0) {
  82. TEST_error("EVP_PKEY_CTX_set1_hkdf_key");
  83. goto err;
  84. }
  85. if (EVP_PKEY_CTX_add1_hkdf_info(pctx, "label", 5) <= 0) {
  86. TEST_error("EVP_PKEY_CTX_set1_hkdf_info");
  87. goto err;
  88. }
  89. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
  90. TEST_error("EVP_PKEY_derive");
  91. goto err;
  92. }
  93. {
  94. const unsigned char expected[sizeof(out)] = {
  95. 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
  96. };
  97. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  98. goto err;
  99. }
  100. }
  101. ret = 1;
  102. err:
  103. EVP_PKEY_CTX_free(pctx);
  104. return ret;
  105. }
  106. #ifndef OPENSSL_NO_SCRYPT
  107. static int test_kdf_scrypt(void)
  108. {
  109. int ret = 0;
  110. EVP_PKEY_CTX *pctx;
  111. unsigned char out[64];
  112. size_t outlen = sizeof(out);
  113. if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL)) == NULL) {
  114. TEST_error("EVP_PKEY_SCRYPT");
  115. goto err;
  116. }
  117. if (EVP_PKEY_derive_init(pctx) <= 0) {
  118. TEST_error("EVP_PKEY_derive_init");
  119. goto err;
  120. }
  121. if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) {
  122. TEST_error("EVP_PKEY_CTX_set1_pbe_pass");
  123. goto err;
  124. }
  125. if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, "NaCl", 4) <= 0) {
  126. TEST_error("EVP_PKEY_CTX_set1_scrypt_salt");
  127. goto err;
  128. }
  129. if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) {
  130. TEST_error("EVP_PKEY_CTX_set_scrypt_N");
  131. goto err;
  132. }
  133. if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) {
  134. TEST_error("EVP_PKEY_CTX_set_scrypt_r");
  135. goto err;
  136. }
  137. if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) {
  138. TEST_error("EVP_PKEY_CTX_set_scrypt_p");
  139. goto err;
  140. }
  141. if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) {
  142. TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
  143. goto err;
  144. }
  145. if (EVP_PKEY_derive(pctx, out, &outlen) > 0) {
  146. TEST_error("EVP_PKEY_derive should have failed");
  147. goto err;
  148. }
  149. if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) {
  150. TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
  151. goto err;
  152. }
  153. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
  154. TEST_error("EVP_PKEY_derive");
  155. goto err;
  156. }
  157. {
  158. const unsigned char expected[sizeof(out)] = {
  159. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  160. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  161. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  162. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  163. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  164. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  165. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  166. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  167. };
  168. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  169. goto err;
  170. }
  171. }
  172. ret = 1;
  173. err:
  174. EVP_PKEY_CTX_free(pctx);
  175. return ret;
  176. }
  177. #endif
  178. int setup_tests(void)
  179. {
  180. ADD_TEST(test_kdf_tls1_prf);
  181. ADD_TEST(test_kdf_hkdf);
  182. #ifndef OPENSSL_NO_SCRYPT
  183. ADD_TEST(test_kdf_scrypt);
  184. #endif
  185. return 1;
  186. }