scrypt_test.cc
Go to the documentation of this file.
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) {
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()));
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];
69  reinterpret_cast<const uint8_t *>(kSalt),
70  strlen(kSalt), 1048576 /* N */, 8 /* r */,
71  1 /* p */, 0 /* max_mem */, key, sizeof(key)));
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 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
ERR_LIB_EVP
@ ERR_LIB_EVP
Definition: err.h:297
evp.h
string.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
error_ref_leak.err
err
Definition: error_ref_leak.py:35
setup.name
name
Definition: setup.py:542
xds_manager.p
p
Definition: xds_manager.py:60
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ERR_get_error
#define ERR_get_error
Definition: boringssl_prefix_symbols.h:1419
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
FileTestGTest
void FileTestGTest(const char *path, std::function< void(FileTest *)> run_test)
Definition: file_test_gtest.cc:68
kPassword
static const char kPassword[]
Definition: pkcs12_test.cc:34
EVP_PBE_scrypt
#define EVP_PBE_scrypt
Definition: boringssl_prefix_symbols.h:1581
ERR_GET_REASON
#define ERR_GET_REASON(packed_error)
Definition: err.h:171
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
FileTest
Definition: file_test.h:90
ERR_GET_LIB
#define ERR_GET_LIB(packed_error)
Definition: err.h:166
err.h
key
const char * key
Definition: hpack_parser_table.cc:164
N
#define N
Definition: sync_test.cc:37
GetUint64
static bool GetUint64(FileTest *t, uint64_t *out, const char *name)
Definition: scrypt_test.cc:29
fix_build_deps.r
r
Definition: fix_build_deps.py:491
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
EVP_R_MEMORY_LIMIT_EXCEEDED
#define EVP_R_MEMORY_LIMIT_EXCEEDED
Definition: evp_errors.h:92
TEST
TEST(ScryptTest, TestVectors)
Definition: scrypt_test.cc:40


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:15