Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 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 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 15 | #include <gtest/gtest.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 16 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 17 | #include <openssl/digest.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 18 | #include <openssl/evp.h> |
| 19 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 20 | #include "../internal.h" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 21 | #include "../test/test_util.h" |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 22 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 23 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 24 | // Tests deriving a key using an empty password (specified both as NULL and as |
| 25 | // non-NULL). Note that NULL has special meaning to HMAC initialization. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 26 | TEST(PBKDFTest, EmptyPassword) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 27 | const uint8_t kKey[] = {0xa3, 0x3d, 0xdd, 0xc3, 0x04, 0x78, 0x18, |
| 28 | 0x55, 0x15, 0x31, 0x1f, 0x87, 0x52, 0x89, |
| 29 | 0x5d, 0x36, 0xea, 0x43, 0x63, 0xa2}; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 30 | uint8_t key[sizeof(kKey)]; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 31 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 32 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC(NULL, 0, (const uint8_t *)"salt", 4, 1, |
| 33 | EVP_sha1(), sizeof(kKey), key)); |
| 34 | EXPECT_EQ(Bytes(kKey), Bytes(key)); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 35 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 36 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("", 0, (const uint8_t *)"salt", 4, 1, |
| 37 | EVP_sha1(), sizeof(kKey), key)); |
| 38 | EXPECT_EQ(Bytes(kKey), Bytes(key)); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | // Tests deriving a key using an empty salt. Note that the expectation was |
| 42 | // generated using OpenSSL itself, and hence is not verified. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 43 | TEST(PBKDFTest, EmptySalt) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 44 | const uint8_t kKey[] = {0x8b, 0xc2, 0xf9, 0x16, 0x7a, 0x81, 0xcd, 0xcf, |
| 45 | 0xad, 0x12, 0x35, 0xcd, 0x90, 0x47, 0xf1, 0x13, |
| 46 | 0x62, 0x71, 0xc1, 0xf9, 0x78, 0xfc, 0xfc, 0xb3, |
| 47 | 0x5e, 0x22, 0xdb, 0xea, 0xfa, 0x46, 0x34, 0xf6}; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 48 | uint8_t key[sizeof(kKey)]; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 49 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 50 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, NULL, 0, 2, EVP_sha256(), |
| 51 | sizeof(kKey), key)); |
| 52 | EXPECT_EQ(Bytes(kKey), Bytes(key)); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 53 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 54 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"", 0, 2, |
| 55 | EVP_sha256(), sizeof(kKey), key)); |
| 56 | EXPECT_EQ(Bytes(kKey), Bytes(key)); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // Exercises test vectors taken from https://tools.ietf.org/html/rfc6070. |
| 60 | // Note that each of these test vectors uses SHA-1 as the digest. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 61 | TEST(PBKDFTest, RFC6070Vectors) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 62 | const uint8_t kKey1[] = {0x0c, 0x60, 0xc8, 0x0f, 0x96, 0x1f, 0x0e, |
| 63 | 0x71, 0xf3, 0xa9, 0xb5, 0x24, 0xaf, 0x60, |
| 64 | 0x12, 0x06, 0x2f, 0xe0, 0x37, 0xa6}; |
| 65 | const uint8_t kKey2[] = {0xea, 0x6c, 0x01, 0x4d, 0xc7, 0x2d, 0x6f, |
| 66 | 0x8c, 0xcd, 0x1e, 0xd9, 0x2a, 0xce, 0x1d, |
| 67 | 0x41, 0xf0, 0xd8, 0xde, 0x89, 0x57}; |
| 68 | const uint8_t kKey3[] = {0x56, 0xfa, 0x6a, 0xa7, 0x55, 0x48, 0x09, 0x9d, |
| 69 | 0xcc, 0x37, 0xd7, 0xf0, 0x34, 0x25, 0xe0, 0xc3}; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 70 | uint8_t key[sizeof(kKey1)]; |
| 71 | static_assert(sizeof(key) >= sizeof(kKey2), "output too small"); |
| 72 | static_assert(sizeof(key) >= sizeof(kKey3), "output too small"); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 73 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 74 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"salt", 4, 1, |
| 75 | EVP_sha1(), sizeof(kKey1), key)); |
| 76 | EXPECT_EQ(Bytes(kKey1), Bytes(key, sizeof(kKey1))); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 77 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 78 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"salt", 4, 2, |
| 79 | EVP_sha1(), sizeof(kKey2), key)); |
| 80 | EXPECT_EQ(Bytes(kKey2), Bytes(key, sizeof(kKey2))); |
| 81 | |
| 82 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("pass\0word", 9, (const uint8_t *)"sa\0lt", 5, |
| 83 | 4096, EVP_sha1(), sizeof(kKey3), key)); |
| 84 | EXPECT_EQ(Bytes(kKey3), Bytes(key, sizeof(kKey3))); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Tests key derivation using SHA-2 digests. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 88 | TEST(PBKDFTest, SHA2) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 89 | // This test was taken from: |
| 90 | // http://stackoverflow.com/questions/5130513/pbkdf2-hmac-sha2-test-vectors. |
| 91 | const uint8_t kKey1[] = {0xae, 0x4d, 0x0c, 0x95, 0xaf, 0x6b, 0x46, 0xd3, |
| 92 | 0x2d, 0x0a, 0xdf, 0xf9, 0x28, 0xf0, 0x6d, 0xd0, |
| 93 | 0x2a, 0x30, 0x3f, 0x8e, 0xf3, 0xc2, 0x51, 0xdf, |
| 94 | 0xd6, 0xe2, 0xd8, 0x5a, 0x95, 0x47, 0x4c, 0x43}; |
| 95 | |
| 96 | // This test was taken from: |
| 97 | // http://stackoverflow.com/questions/15593184/pbkdf2-hmac-sha-512-test-vectors. |
| 98 | const uint8_t kKey2[] = { |
| 99 | 0x8c, 0x05, 0x11, 0xf4, 0xc6, 0xe5, 0x97, 0xc6, 0xac, 0x63, 0x15, |
| 100 | 0xd8, 0xf0, 0x36, 0x2e, 0x22, 0x5f, 0x3c, 0x50, 0x14, 0x95, 0xba, |
| 101 | 0x23, 0xb8, 0x68, 0xc0, 0x05, 0x17, 0x4d, 0xc4, 0xee, 0x71, 0x11, |
| 102 | 0x5b, 0x59, 0xf9, 0xe6, 0x0c, 0xd9, 0x53, 0x2f, 0xa3, 0x3e, 0x0f, |
| 103 | 0x75, 0xae, 0xfe, 0x30, 0x22, 0x5c, 0x58, 0x3a, 0x18, 0x6c, 0xd8, |
| 104 | 0x2b, 0xd4, 0xda, 0xea, 0x97, 0x24, 0xa3, 0xd3, 0xb8}; |
| 105 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 106 | uint8_t key[sizeof(kKey2)]; |
| 107 | static_assert(sizeof(key) >= sizeof(kKey1), "output too small"); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 108 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 109 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC("password", 8, (const uint8_t *)"salt", 4, 2, |
| 110 | EVP_sha256(), sizeof(kKey1), key)); |
| 111 | EXPECT_EQ(Bytes(kKey1), Bytes(key, sizeof(kKey1))); |
| 112 | |
| 113 | ASSERT_TRUE( |
| 114 | PKCS5_PBKDF2_HMAC("passwordPASSWORDpassword", 24, |
| 115 | (const uint8_t *)"saltSALTsaltSALTsaltSALTsaltSALTsalt", |
| 116 | 36, 4096, EVP_sha512(), sizeof(kKey2), key)); |
| 117 | EXPECT_EQ(Bytes(kKey2), Bytes(key, sizeof(kKey2))); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 120 | // Tests key derivation using iterations=0. |
| 121 | // |
| 122 | // RFC 2898 defines the iteration count (c) as a "positive integer". So doing a |
| 123 | // key derivation with iterations=0 is ill-defined and should result in a |
| 124 | // failure. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 125 | TEST(PBKDFTest, ZeroIterations) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 126 | static const char kPassword[] = "password"; |
| 127 | const size_t password_len = strlen(kPassword); |
| 128 | static const uint8_t kSalt[] = {1, 2, 3, 4}; |
| 129 | const size_t salt_len = sizeof(kSalt); |
| 130 | const EVP_MD *digest = EVP_sha1(); |
| 131 | |
| 132 | uint8_t key[10] = {0}; |
| 133 | const size_t key_len = sizeof(key); |
| 134 | |
| 135 | // Verify that calling with iterations=1 works. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 136 | ASSERT_TRUE(PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len, |
| 137 | 1 /* iterations */, digest, key_len, key)); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 138 | |
| 139 | // Flip the first key byte (so can later test if it got set). |
| 140 | const uint8_t expected_first_byte = key[0]; |
| 141 | key[0] = ~key[0]; |
| 142 | |
| 143 | // However calling it with iterations=0 fails. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 144 | ASSERT_FALSE(PKCS5_PBKDF2_HMAC(kPassword, password_len, kSalt, salt_len, |
| 145 | 0 /* iterations */, digest, key_len, key)); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 146 | |
| 147 | // For backwards compatibility, the iterations == 0 case still fills in |
| 148 | // the out key. |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 149 | EXPECT_EQ(expected_first_byte, key[0]); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 150 | } |