You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

221 lines
7.7 KiB

  1. /* Copyright (c) 2015, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <openssl/crypto.h>
  17. #include <openssl/digest.h>
  18. #include <openssl/err.h>
  19. #include <openssl/evp.h>
  20. // Prints out the data buffer as a sequence of hex bytes.
  21. static void PrintDataHex(const void *data, size_t len) {
  22. for (size_t i = 0; i < len; ++i) {
  23. fprintf(stderr, "%02x", (int)((const uint8_t *)data)[i]);
  24. }
  25. }
  26. // Helper for testing that PBKDF2 derives the expected key from the given
  27. // inputs. Returns 1 on success, 0 otherwise.
  28. static bool TestPBKDF2(const void *password, size_t password_len,
  29. const void *salt, size_t salt_len, unsigned iterations,
  30. const EVP_MD *digest, size_t key_len,
  31. const uint8_t *expected_key) {
  32. uint8_t key[64];
  33. if (key_len > sizeof(key)) {
  34. fprintf(stderr, "Output buffer is not large enough.\n");
  35. return false;
  36. }
  37. if (!PKCS5_PBKDF2_HMAC((const char *)password, password_len,
  38. (const uint8_t *)salt, salt_len, iterations, digest,
  39. key_len, key)) {
  40. fprintf(stderr, "Call to PKCS5_PBKDF2_HMAC failed\n");
  41. ERR_print_errors_fp(stderr);
  42. return false;
  43. }
  44. if (memcmp(key, expected_key, key_len) != 0) {
  45. fprintf(stderr, "Resulting key material does not match expectation\n");
  46. fprintf(stderr, "Expected:\n ");
  47. PrintDataHex(expected_key, key_len);
  48. fprintf(stderr, "\nActual:\n ");
  49. PrintDataHex(key, key_len);
  50. fprintf(stderr, "\n");
  51. return false;
  52. }
  53. return true;
  54. }
  55. // Tests deriving a key using an empty password (specified both as NULL and as
  56. // non-NULL). Note that NULL has special meaning to HMAC initialization.
  57. static bool TestEmptyPassword() {
  58. const uint8_t kKey[] = {0xa3, 0x3d, 0xdd, 0xc3, 0x04, 0x78, 0x18,
  59. 0x55, 0x15, 0x31, 0x1f, 0x87, 0x52, 0x89,
  60. 0x5d, 0x36, 0xea, 0x43, 0x63, 0xa2};
  61. if (!TestPBKDF2(NULL, 0, "salt", 4, 1, EVP_sha1(), sizeof(kKey), kKey) ||
  62. !TestPBKDF2("", 0, "salt", 4, 1, EVP_sha1(), sizeof(kKey), kKey)) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. // Tests deriving a key using an empty salt. Note that the expectation was
  68. // generated using OpenSSL itself, and hence is not verified.
  69. static bool TestEmptySalt() {
  70. const uint8_t kKey[] = {0x8b, 0xc2, 0xf9, 0x16, 0x7a, 0x81, 0xcd, 0xcf,
  71. 0xad, 0x12, 0x35, 0xcd, 0x90, 0x47, 0xf1, 0x13,
  72. 0x62, 0x71, 0xc1, 0xf9, 0x78, 0xfc, 0xfc, 0xb3,
  73. 0x5e, 0x22, 0xdb, 0xea, 0xfa, 0x46, 0x34, 0xf6};
  74. if (!TestPBKDF2("password", 8, NULL, 0, 2, EVP_sha256(), sizeof(kKey),
  75. kKey) ||
  76. !TestPBKDF2("password", 8, "", 0, 2, EVP_sha256(), sizeof(kKey), kKey)) {
  77. return false;
  78. }
  79. return true;
  80. }
  81. // Exercises test vectors taken from https://tools.ietf.org/html/rfc6070.
  82. // Note that each of these test vectors uses SHA-1 as the digest.
  83. static bool TestRFC6070Vectors() {
  84. const uint8_t kKey1[] = {0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e,
  85. 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60,
  86. 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6};
  87. const uint8_t kKey2[] = {0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f,
  88. 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d,
  89. 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57};
  90. const uint8_t kKey3[] = {0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d,
  91. 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3};
  92. if (!TestPBKDF2("password", 8, "salt", 4, 1, EVP_sha1(), sizeof(kKey1),
  93. kKey1) ||
  94. !TestPBKDF2("password", 8, "salt", 4, 2, EVP_sha1(), sizeof(kKey2),
  95. kKey2) ||
  96. !TestPBKDF2("pass\0word", 9, "sa\0lt", 5, 4096, EVP_sha1(),
  97. sizeof(kKey3), kKey3)) {
  98. return false;
  99. }
  100. return true;
  101. }
  102. // Tests key derivation using SHA-2 digests.
  103. static bool TestSHA2() {
  104. // This test was taken from:
  105. // http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors.
  106. const uint8_t kKey1[] = {0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3,
  107. 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0,
  108. 0x2a, 0x30, 0x3f, 0x8e, 0xf3, 0xc2, 0x51, 0xdf,
  109. 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43};
  110. // This test was taken from:
  111. // http://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors.
  112. const uint8_t kKey2[] = {
  113. 0x8c, 0x05, 0x11, 0xf4, 0xc6, 0xe5, 0x97, 0xc6, 0xac, 0x63, 0x15,
  114. 0xd8, 0xf0, 0x36, 0x2e, 0x22, 0x5f, 0x3c, 0x50, 0x14, 0x95, 0xba,
  115. 0x23, 0xb8, 0x68, 0xc0, 0x05, 0x17, 0x4d, 0xc4, 0xee, 0x71, 0x11,
  116. 0x5b, 0x59, 0xf9, 0xe6, 0x0c, 0xd9, 0x53, 0x2f, 0xa3, 0x3e, 0x0f,
  117. 0x75, 0xae, 0xfe, 0x30, 0x22, 0x5c, 0x58, 0x3a, 0x18, 0x6c, 0xd8,
  118. 0x2b, 0xd4, 0xda, 0xea, 0x97, 0x24, 0xa3, 0xd3, 0xb8};
  119. if (!TestPBKDF2("password", 8, "salt", 4, 2, EVP_sha256(), sizeof(kKey1),
  120. kKey1) ||
  121. !TestPBKDF2("passwordPASSWORDpassword", 24,
  122. "saltSALTsaltSALTsaltSALTsaltSALTsalt", 36, 4096,
  123. EVP_sha512(), sizeof(kKey2), kKey2)) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. // Tests key derivation using iterations=0.
  129. //
  130. // RFC 2898 defines the iteration count (c) as a "positive integer". So doing a
  131. // key derivation with iterations=0 is ill-defined and should result in a
  132. // failure.
  133. static bool TestZeroIterations() {
  134. static const char kPassword[] = "password";
  135. const size_t password_len = strlen(kPassword);
  136. static const uint8_t kSalt[] = {1, 2, 3, 4};
  137. const size_t salt_len = sizeof(kSalt);
  138. const EVP_MD *digest = EVP_sha1();
  139. uint8_t key[10] = {0};
  140. const size_t key_len = sizeof(key);
  141. // Verify that calling with iterations=1 works.
  142. if (!PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len,
  143. 1 /* iterations */, digest, key_len, key)) {
  144. fprintf(stderr, "PBKDF2 failed with iterations=1\n");
  145. return false;
  146. }
  147. // Flip the first key byte (so can later test if it got set).
  148. const uint8_t expected_first_byte = key[0];
  149. key[0] = ~key[0];
  150. // However calling it with iterations=0 fails.
  151. if (PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len,
  152. 0 /* iterations */, digest, key_len, key)) {
  153. fprintf(stderr, "PBKDF2 returned zero with iterations=0\n");
  154. return false;
  155. }
  156. // For backwards compatibility, the iterations == 0 case still fills in
  157. // the out key.
  158. return key[0] == expected_first_byte;
  159. }
  160. int main(void) {
  161. CRYPTO_library_init();
  162. if (!TestEmptyPassword()) {
  163. fprintf(stderr, "TestEmptyPassword failed\n");
  164. return 1;
  165. }
  166. if (!TestEmptySalt()) {
  167. fprintf(stderr, "TestEmptySalt failed\n");
  168. return 1;
  169. }
  170. if (!TestRFC6070Vectors()) {
  171. fprintf(stderr, "TestRFC6070Vectors failed\n");
  172. return 1;
  173. }
  174. if (!TestSHA2()) {
  175. fprintf(stderr, "TestSHA2 failed\n");
  176. return 1;
  177. }
  178. if (!TestZeroIterations()) {
  179. fprintf(stderr, "TestZeroIterations failed\n");
  180. return 1;
  181. }
  182. printf("PASS\n");
  183. ERR_free_strings();
  184. return 0;
  185. }