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.
 
 
 
 
 
 

104 line
4.2 KiB

  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. #include <stdlib.h>
  15. #include <string.h>
  16. #include <vector>
  17. #include <gtest/gtest.h>
  18. #include <openssl/err.h>
  19. #include <openssl/evp.h>
  20. #include "../test/file_test.h"
  21. #include "../test/test_util.h"
  22. static bool GetUint64(FileTest *t, uint64_t *out, const char *name) {
  23. std::string str;
  24. if (!t->GetAttribute(&str, name)) {
  25. return false;
  26. }
  27. char *endptr;
  28. *out = strtoull(str.data(), &endptr, 10);
  29. return !str.empty() && *endptr == '\0';
  30. }
  31. TEST(ScryptTest, TestVectors) {
  32. FileTestGTest("crypto/evp/scrypt_tests.txt", [](FileTest *t) {
  33. std::vector<uint8_t> password, salt, key;
  34. uint64_t N, r, p, max_mem = 0;
  35. ASSERT_TRUE(t->GetBytes(&password, "Password"));
  36. ASSERT_TRUE(t->GetBytes(&salt, "Salt"));
  37. ASSERT_TRUE(t->GetBytes(&key, "Key"));
  38. ASSERT_TRUE(GetUint64(t, &N, "N"));
  39. ASSERT_TRUE(GetUint64(t, &r, "r"));
  40. ASSERT_TRUE(GetUint64(t, &p, "p"));
  41. if (t->HasAttribute("MaxMemory")) {
  42. ASSERT_TRUE(GetUint64(t, &max_mem, "MaxMemory"));
  43. }
  44. std::vector<uint8_t> result(key.size());
  45. ASSERT_TRUE(EVP_PBE_scrypt(reinterpret_cast<const char *>(password.data()),
  46. password.size(), salt.data(), salt.size(), N, r,
  47. p, max_mem, result.data(), result.size()));
  48. EXPECT_EQ(Bytes(key), Bytes(result));
  49. });
  50. }
  51. TEST(ScryptTest, MemoryLimit) {
  52. static const char kPassword[] = "pleaseletmein";
  53. static const char kSalt[] = "SodiumChloride";
  54. // This test requires more than 1GB to run.
  55. uint8_t key[64];
  56. EXPECT_FALSE(EVP_PBE_scrypt(kPassword, strlen(kPassword),
  57. reinterpret_cast<const uint8_t *>(kSalt),
  58. strlen(kSalt), 1048576 /* N */, 8 /* r */,
  59. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  60. uint32_t err = ERR_get_error();
  61. EXPECT_EQ(ERR_LIB_EVP, ERR_GET_LIB(err));
  62. EXPECT_EQ(EVP_R_MEMORY_LIMIT_EXCEEDED, ERR_GET_REASON(err));
  63. }
  64. TEST(ScryptTest, InvalidParameters) {
  65. uint8_t key[64];
  66. // p and r are non-zero.
  67. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 0 /* r */,
  68. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  69. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 8 /* r */,
  70. 0 /* p */, 0 /* max_mem */, key, sizeof(key)));
  71. // N must be a power of 2 > 1.
  72. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 0 /* N */, 8 /* r */,
  73. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  74. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1 /* N */, 8 /* r */,
  75. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  76. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1023 /* N */, 8 /* r */,
  77. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  78. EXPECT_TRUE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1024 /* N */, 8 /* r */,
  79. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  80. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 1025 /* N */, 8 /* r */,
  81. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  82. // N must be below 2^(128 * r / 8).
  83. EXPECT_FALSE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 65536 /* N */, 1 /* r */,
  84. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  85. EXPECT_TRUE(EVP_PBE_scrypt(nullptr, 0, nullptr, 0, 32768 /* N */, 1 /* r */,
  86. 1 /* p */, 0 /* max_mem */, key, sizeof(key)));
  87. }