• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "crypto/secure_hash.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <memory>
11 #include <string>
12 #include <utility>
13 
14 #include "base/types/fixed_array.h"
15 #include "crypto/sha2.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/boringssl/src/include/openssl/sha.h"
18 
19 class SecureHashTest : public testing::Test,
20                        public testing::WithParamInterface<
21                            std::pair<crypto::SecureHash::Algorithm, uint64_t>> {
22  public:
SecureHashTest()23   SecureHashTest()
24       : algorithm_(GetParam().first), hash_length_(GetParam().second) {}
25 
26  protected:
27   crypto::SecureHash::Algorithm algorithm_;
28   const uint64_t hash_length_;
29 };
30 
TEST_P(SecureHashTest,TestUpdateSHA256)31 TEST_P(SecureHashTest, TestUpdateSHA256) {
32   std::string input3;
33   std::vector<uint8_t> expected_hash_of_input_3;
34 
35   switch (algorithm_) {
36     case crypto::SecureHash::SHA256:
37       // Example B.3 from FIPS 180-2: long message.
38       input3 = std::string(500000, 'a');  // 'a' repeated half a million times
39       expected_hash_of_input_3 = {
40           0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
41           0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
42           0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0};
43       break;
44     case crypto::SecureHash::SHA512:
45       // Example C.3 from FIPS 180-2: long message.
46       input3 = std::string(500000, 'a');  // 'a' repeated half a million times
47       expected_hash_of_input_3 = {
48           0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, 0x4e, 0x2e, 0x42,
49           0xc7, 0xbc, 0x15, 0xb4, 0x63, 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20,
50           0x44, 0x28, 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb, 0xde,
51           0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a, 0x4c, 0xb0, 0x43, 0x2c,
52           0xe5, 0x77, 0xc3, 0x1b, 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa,
53           0x2e, 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b};
54       break;
55   }
56 
57   base::FixedArray<uint8_t> output3(hash_length_);
58 
59   std::unique_ptr<crypto::SecureHash> ctx(
60       crypto::SecureHash::Create(algorithm_));
61   ctx->Update(input3.data(), input3.size());
62   ctx->Update(input3.data(), input3.size());
63 
64   ctx->Finish(output3.data(), output3.size());
65   for (size_t i = 0; i < hash_length_; i++)
66     EXPECT_EQ(expected_hash_of_input_3[i], static_cast<int>(output3[i]));
67 }
68 
TEST_P(SecureHashTest,TestClone)69 TEST_P(SecureHashTest, TestClone) {
70   std::string input1(10001, 'a');  // 'a' repeated 10001 times
71   std::string input2(10001, 'd');  // 'd' repeated 10001 times
72 
73   std::vector<uint8_t> expected_hash_of_input_1;
74   std::vector<uint8_t> expected_hash_of_input_1_and_2;
75 
76   switch (algorithm_) {
77     case crypto::SecureHash::SHA256:
78       expected_hash_of_input_1 = {
79           0x0c, 0xab, 0x99, 0xa0, 0x58, 0x60, 0x0f, 0xfa, 0xad, 0x12, 0x92,
80           0xd0, 0xc5, 0x3c, 0x05, 0x48, 0xeb, 0xaf, 0x88, 0xdd, 0x1d, 0x01,
81           0x03, 0x03, 0x45, 0x70, 0x5f, 0x01, 0x8a, 0x81, 0x39, 0x09};
82       expected_hash_of_input_1_and_2 = {
83           0x4c, 0x8e, 0x26, 0x5a, 0xc3, 0x85, 0x1f, 0x1f, 0xa5, 0x04, 0x1c,
84           0xc7, 0x88, 0x53, 0x1c, 0xc7, 0x80, 0x47, 0x15, 0xfb, 0x47, 0xff,
85           0x72, 0xb1, 0x28, 0x37, 0xb0, 0x4d, 0x6e, 0x22, 0x2e, 0x4d};
86       break;
87     case crypto::SecureHash::SHA512:
88       expected_hash_of_input_1 = {
89           0xea, 0x03, 0xb2, 0x23, 0x32, 0x29, 0xc8, 0x87, 0x86, 0x33, 0xa3,
90           0x70, 0xc7, 0xb2, 0x40, 0xea, 0xef, 0xd9, 0x55, 0xe2, 0xb3, 0x79,
91           0xd6, 0xb3, 0x3f, 0x5e, 0xff, 0x89, 0xfd, 0x86, 0x7b, 0x10, 0xe2,
92           0xc1, 0x3b, 0x2f, 0xf5, 0x29, 0x80, 0xa0, 0xb0, 0xf9, 0xcf, 0x47,
93           0xa7, 0xff, 0x73, 0xac, 0xd2, 0x66, 0x9e, 0x53, 0x78, 0x9f, 0xc6,
94           0x07, 0x7a, 0xb7, 0x09, 0x1f, 0xa4, 0x3b, 0x18, 0x00};
95       expected_hash_of_input_1_and_2 = {
96           0x41, 0x6d, 0x46, 0x8d, 0x8a, 0x84, 0x3d, 0xf9, 0x43, 0xac, 0xe6,
97           0x4d, 0x5b, 0x60, 0xd7, 0x1a, 0xb1, 0xe6, 0x2d, 0xd3, 0xe6, 0x97,
98           0xaf, 0x6f, 0x34, 0x97, 0x8f, 0x01, 0xd4, 0x15, 0x06, 0xfa, 0x69,
99           0x48, 0x0e, 0x24, 0x0d, 0x98, 0x84, 0x76, 0xd2, 0x95, 0x4c, 0x16,
100           0x02, 0xfd, 0x71, 0xd4, 0x25, 0xb3, 0x8f, 0xf2, 0x60, 0xa3, 0x0e,
101           0xdb, 0xe9, 0x87, 0x32, 0xfc, 0xf3, 0x2d, 0x0a, 0x28};
102       break;
103   }
104 
105   base::FixedArray<uint8_t> output1(hash_length_);
106   base::FixedArray<uint8_t> output2(hash_length_);
107   base::FixedArray<uint8_t> output3(hash_length_);
108 
109   std::unique_ptr<crypto::SecureHash> ctx1(
110       crypto::SecureHash::Create(algorithm_));
111   ctx1->Update(input1.data(), input1.size());
112 
113   std::unique_ptr<crypto::SecureHash> ctx2(ctx1->Clone());
114   std::unique_ptr<crypto::SecureHash> ctx3(ctx2->Clone());
115   // At this point, ctx1, ctx2, and ctx3 are all equivalent and represent the
116   // state after hashing input1.
117 
118   // Updating ctx1 and ctx2 with input2 should produce equivalent results.
119   ctx1->Update(input2.data(), input2.size());
120   ctx1->Finish(output1.data(), output1.size());
121 
122   ctx2->Update(input2.data(), input2.size());
123   ctx2->Finish(output2.data(), output2.size());
124 
125   EXPECT_EQ(0, memcmp(output1.data(), output2.data(), hash_length_));
126   EXPECT_EQ(0, memcmp(output1.data(), expected_hash_of_input_1_and_2.data(),
127                       hash_length_));
128 
129   // Finish() ctx3, which should produce the hash of input1.
130   ctx3->Finish(output3.data(), output3.size());
131   EXPECT_EQ(
132       0, memcmp(output3.data(), expected_hash_of_input_1.data(), hash_length_));
133 }
134 
TEST_P(SecureHashTest,TestLength)135 TEST_P(SecureHashTest, TestLength) {
136   std::unique_ptr<crypto::SecureHash> ctx(
137       crypto::SecureHash::Create(algorithm_));
138   EXPECT_EQ(hash_length_, ctx->GetHashLength());
139 }
140 
TEST_P(SecureHashTest,Equality)141 TEST_P(SecureHashTest, Equality) {
142   std::string input1(10001, 'a');  // 'a' repeated 10001 times
143   std::string input2(10001, 'd');  // 'd' repeated 10001 times
144 
145   base::FixedArray<uint8_t> output1(hash_length_);
146   base::FixedArray<uint8_t> output2(hash_length_);
147 
148   // Call Update() twice on input1 and input2.
149   std::unique_ptr<crypto::SecureHash> ctx1(
150       crypto::SecureHash::Create(algorithm_));
151   ctx1->Update(input1.data(), input1.size());
152   ctx1->Update(input2.data(), input2.size());
153   ctx1->Finish(output1.data(), output1.size());
154 
155   // Call Update() once one input1 + input2 (concatenation).
156   std::unique_ptr<crypto::SecureHash> ctx2(
157       crypto::SecureHash::Create(algorithm_));
158   std::string input3 = input1 + input2;
159   ctx2->Update(input3.data(), input3.size());
160   ctx2->Finish(output2.data(), output2.size());
161 
162   // The hash should be the same.
163   EXPECT_EQ(0, memcmp(output1.data(), output2.data(), hash_length_));
164 }
165 
166 INSTANTIATE_TEST_SUITE_P(
167     All,
168     SecureHashTest,
169     testing::Values(
170         std::make_pair(crypto::SecureHash::SHA256, SHA256_DIGEST_LENGTH),
171         std::make_pair(crypto::SecureHash::SHA512, SHA512_DIGEST_LENGTH)));
172