| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
| 17 |
| 18 | #include <vector> |
| 19 |
| 20 | #include <gtest/gtest.h> |
| 21 |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/evp.h> |
| 24 |
| 25 | #include "../test/file_test.h" |
| 26 | #include "../test/test_util.h" |
| 27 |
| 28 |
| 29 | static bool GetUint64(FileTest *t, uint64_t *out, const char *name) { |
| 30 | std::string str; |
| 31 | if (!t->GetAttribute(&str, name)) { |
| 32 | return false; |
| 33 | } |
| 34 |
| 35 | char *endptr; |
| 36 | *out = strtoull(str.data(), &endptr, 10); |
| 37 | return !str.empty() && *endptr == '\0'; |
| 38 | } |
| 39 |
| 40 | TEST(ScryptTest, TestVectors) { |
| 41 | FileTestGTest("crypto/evp/scrypt_tests.txt", [](FileTest *t) { |
| 42 | std::vector<uint8_t> password, salt, key; |
| 43 | uint64_t N, r, p, max_mem = 0; |
| 44 | ASSERT_TRUE(t->GetBytes(&password, "Password")); |
| 45 | ASSERT_TRUE(t->GetBytes(&salt, "Salt")); |
| 46 | ASSERT_TRUE(t->GetBytes(&key, "Key")); |
| 47 | ASSERT_TRUE(GetUint64(t, &N, "N")); |
| 48 | ASSERT_TRUE(GetUint64(t, &r, "r")); |
| 49 | ASSERT_TRUE(GetUint64(t, &p, "p")); |
| 50 | if (t->HasAttribute("MaxMemory")) { |
| 51 | ASSERT_TRUE(GetUint64(t, &max_mem, "MaxMemory")); |
| 52 | } |
| 53 |
| 54 | std::vector<uint8_t> result(key.size()); |
| 55 | ASSERT_TRUE(EVP_PBE_scrypt(reinterpret_cast<const char *>(password.data()), |
| 56 | password.size(), salt.data(), salt.size(), N, r, |
| 57 | p, max_mem, result.data(), result.size())); |
| 58 | EXPECT_EQ(Bytes(key), Bytes(result)); |
| 59 | }); |
| 60 | } |
| 61 |
| 62 | TEST(ScryptTest, MemoryLimit) { |
| 63 | static const char kPassword[] = "pleaseletmein"; |
| 64 | static const char kSalt[] = "SodiumChloride"; |
| 65 |
| 66 | // This test requires more than 1GB to run. |
| 67 | uint8_t key[64]; |
| 68 | EXPECT_FALSE(EVP_PBE_scrypt(kPassword, strlen(kPassword), |
| 69 | reinterpret_cast<const uint8_t *>(kSalt), |
| 70 | strlen(kSalt), 1048576 /* N */, 8 /* r */, |
| 71 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 72 | EXPECT_TRUE( |
| 73 | ErrorEquals(ERR_get_error(), ERR_LIB_EVP, EVP_R_MEMORY_LIMIT_EXCEEDED)); |
| 74 | } |
| 75 |
| 76 | TEST(ScryptTest, InvalidParameters) { |
| 77 | uint8_t key[64]; |
| 78 |
| 79 | // p and r are non-zero. |
| 80 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 0 /* r */, |
| 81 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 82 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 8 /* r */, |
| 83 | 0 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 84 |
| 85 | // N must be a power of 2 > 1. |
| 86 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 0 /* N */, 8 /* r */, |
| 87 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 88 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1 /* N */, 8 /* r */, |
| 89 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 90 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1023 /* N */, 8 /* r */, |
| 91 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 92 | EXPECT_TRUE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 8 /* r */, |
| 93 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 94 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1025 /* N */, 8 /* r */, |
| 95 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 96 |
| 97 | // N must be below 2^(128 * r / 8). |
| 98 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 65536 /* N */, 1 /* r */, |
| 99 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 100 | EXPECT_TRUE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 32768 /* N */, 1 /* r */, |
| 101 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 102 | } |
| 103 |