pkey_meth_kdf_test.c 5.2 KB

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