pkey_meth_kdf_test.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2017-2020 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,
  34. (unsigned char *)"secret", 6) <= 0) {
  35. TEST_error("EVP_PKEY_CTX_set1_tls1_prf_secret");
  36. goto err;
  37. }
  38. if (EVP_PKEY_CTX_add1_tls1_prf_seed(pctx,
  39. (unsigned char *)"seed", 4) <= 0) {
  40. TEST_error("EVP_PKEY_CTX_add1_tls1_prf_seed");
  41. goto err;
  42. }
  43. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
  44. TEST_error("EVP_PKEY_derive");
  45. goto err;
  46. }
  47. {
  48. const unsigned char expected[sizeof(out)] = {
  49. 0x8e, 0x4d, 0x93, 0x25, 0x30, 0xd7, 0x65, 0xa0,
  50. 0xaa, 0xe9, 0x74, 0xc3, 0x04, 0x73, 0x5e, 0xcc
  51. };
  52. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  53. goto err;
  54. }
  55. }
  56. ret = 1;
  57. err:
  58. EVP_PKEY_CTX_free(pctx);
  59. return ret;
  60. }
  61. static int test_kdf_hkdf(void)
  62. {
  63. int ret = 0;
  64. EVP_PKEY_CTX *pctx;
  65. unsigned char out[10];
  66. size_t outlen = sizeof(out);
  67. if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)) == NULL) {
  68. TEST_error("EVP_PKEY_HKDF");
  69. goto err;
  70. }
  71. if (EVP_PKEY_derive_init(pctx) <= 0) {
  72. TEST_error("EVP_PKEY_derive_init");
  73. goto err;
  74. }
  75. if (EVP_PKEY_CTX_set_hkdf_md(pctx, EVP_sha256()) <= 0) {
  76. TEST_error("EVP_PKEY_CTX_set_hkdf_md");
  77. goto err;
  78. }
  79. if (EVP_PKEY_CTX_set1_hkdf_salt(pctx, (const unsigned char *)"salt", 4)
  80. <= 0) {
  81. TEST_error("EVP_PKEY_CTX_set1_hkdf_salt");
  82. goto err;
  83. }
  84. if (EVP_PKEY_CTX_set1_hkdf_key(pctx, (const unsigned char *)"secret", 6)
  85. <= 0) {
  86. TEST_error("EVP_PKEY_CTX_set1_hkdf_key");
  87. goto err;
  88. }
  89. if (EVP_PKEY_CTX_add1_hkdf_info(pctx, (const unsigned char *)"label", 5)
  90. <= 0) {
  91. TEST_error("EVP_PKEY_CTX_set1_hkdf_info");
  92. goto err;
  93. }
  94. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
  95. TEST_error("EVP_PKEY_derive");
  96. goto err;
  97. }
  98. {
  99. const unsigned char expected[sizeof(out)] = {
  100. 0x2a, 0xc4, 0x36, 0x9f, 0x52, 0x59, 0x96, 0xf8, 0xde, 0x13
  101. };
  102. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  103. goto err;
  104. }
  105. }
  106. ret = 1;
  107. err:
  108. EVP_PKEY_CTX_free(pctx);
  109. return ret;
  110. }
  111. #ifndef OPENSSL_NO_SCRYPT
  112. static int test_kdf_scrypt(void)
  113. {
  114. int ret = 0;
  115. EVP_PKEY_CTX *pctx;
  116. unsigned char out[64];
  117. size_t outlen = sizeof(out);
  118. if ((pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_SCRYPT, NULL)) == NULL) {
  119. TEST_error("EVP_PKEY_SCRYPT");
  120. goto err;
  121. }
  122. if (EVP_PKEY_derive_init(pctx) <= 0) {
  123. TEST_error("EVP_PKEY_derive_init");
  124. goto err;
  125. }
  126. if (EVP_PKEY_CTX_set1_pbe_pass(pctx, "password", 8) <= 0) {
  127. TEST_error("EVP_PKEY_CTX_set1_pbe_pass");
  128. goto err;
  129. }
  130. if (EVP_PKEY_CTX_set1_scrypt_salt(pctx, (unsigned char *)"NaCl", 4) <= 0) {
  131. TEST_error("EVP_PKEY_CTX_set1_scrypt_salt");
  132. goto err;
  133. }
  134. if (EVP_PKEY_CTX_set_scrypt_N(pctx, 1024) <= 0) {
  135. TEST_error("EVP_PKEY_CTX_set_scrypt_N");
  136. goto err;
  137. }
  138. if (EVP_PKEY_CTX_set_scrypt_r(pctx, 8) <= 0) {
  139. TEST_error("EVP_PKEY_CTX_set_scrypt_r");
  140. goto err;
  141. }
  142. if (EVP_PKEY_CTX_set_scrypt_p(pctx, 16) <= 0) {
  143. TEST_error("EVP_PKEY_CTX_set_scrypt_p");
  144. goto err;
  145. }
  146. if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 16) <= 0) {
  147. TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
  148. goto err;
  149. }
  150. if (EVP_PKEY_derive(pctx, out, &outlen) > 0) {
  151. TEST_error("EVP_PKEY_derive should have failed");
  152. goto err;
  153. }
  154. if (EVP_PKEY_CTX_set_scrypt_maxmem_bytes(pctx, 10 * 1024 * 1024) <= 0) {
  155. TEST_error("EVP_PKEY_CTX_set_maxmem_bytes");
  156. goto err;
  157. }
  158. if (EVP_PKEY_derive(pctx, out, &outlen) <= 0) {
  159. TEST_error("EVP_PKEY_derive");
  160. goto err;
  161. }
  162. {
  163. const unsigned char expected[sizeof(out)] = {
  164. 0xfd, 0xba, 0xbe, 0x1c, 0x9d, 0x34, 0x72, 0x00,
  165. 0x78, 0x56, 0xe7, 0x19, 0x0d, 0x01, 0xe9, 0xfe,
  166. 0x7c, 0x6a, 0xd7, 0xcb, 0xc8, 0x23, 0x78, 0x30,
  167. 0xe7, 0x73, 0x76, 0x63, 0x4b, 0x37, 0x31, 0x62,
  168. 0x2e, 0xaf, 0x30, 0xd9, 0x2e, 0x22, 0xa3, 0x88,
  169. 0x6f, 0xf1, 0x09, 0x27, 0x9d, 0x98, 0x30, 0xda,
  170. 0xc7, 0x27, 0xaf, 0xb9, 0x4a, 0x83, 0xee, 0x6d,
  171. 0x83, 0x60, 0xcb, 0xdf, 0xa2, 0xcc, 0x06, 0x40
  172. };
  173. if (!TEST_mem_eq(out, sizeof(out), expected, sizeof(expected))) {
  174. goto err;
  175. }
  176. }
  177. ret = 1;
  178. err:
  179. EVP_PKEY_CTX_free(pctx);
  180. return ret;
  181. }
  182. #endif
  183. int setup_tests(void)
  184. {
  185. ADD_TEST(test_kdf_tls1_prf);
  186. ADD_TEST(test_kdf_hkdf);
  187. #ifndef OPENSSL_NO_SCRYPT
  188. ADD_TEST(test_kdf_scrypt);
  189. #endif
  190. return 1;
  191. }