David Benjamin | b529253 | 2017-06-09 19:27:37 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2017, 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 | |
| 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 | uint32_t err = ERR_get_error(); |
| 73 | EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err)); |
| 74 | EXPECT_EQ(EVP_R_MEMORY_LIMIT_EXCEEDED, ERR_GET_REASON(err)); |
| 75 | } |
| 76 | |
| 77 | TEST(ScryptTest, InvalidParameters) { |
| 78 | uint8_t key[64]; |
| 79 | |
| 80 | // p and r are non-zero. |
| 81 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 0 /* r */, |
| 82 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 83 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 8 /* r */, |
| 84 | 0 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 85 | |
| 86 | // N must be a power of 2 > 1. |
| 87 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 0 /* N */, 8 /* r */, |
| 88 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 89 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1 /* N */, 8 /* r */, |
| 90 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 91 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1023 /* N */, 8 /* r */, |
| 92 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 93 | EXPECT_TRUE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 8 /* r */, |
| 94 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 95 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1025 /* N */, 8 /* r */, |
| 96 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 97 | |
| 98 | // N must be below 2^(128 * r / 8). |
| 99 | EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 65536 /* N */, 1 /* r */, |
| 100 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 101 | EXPECT_TRUE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 32768 /* N */, 1 /* r */, |
| 102 | 1 /* p */, 0 /* max_mem */, key, sizeof(key))); |
| 103 | } |