• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 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 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "crypto/sha2.h"
11 
12 #include <stddef.h>
13 #include <stdint.h>
14 
15 #include "testing/gtest/include/gtest/gtest.h"
16 
TEST(Sha256Test,Empty)17 TEST(Sha256Test, Empty) {
18   const std::string empty;
19   std::string hash = crypto::SHA256HashString(empty);
20   int expected[] = {0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
21                     0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
22                     0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
23                     0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55};
24   ASSERT_EQ(hash.length(), crypto::kSHA256Length);
25   for (size_t i = 0; i < crypto::kSHA256Length; i++) {
26     EXPECT_EQ(expected[i], static_cast<uint8_t>(hash[i]));
27   }
28 }
29 
TEST(Sha256Test,Test1)30 TEST(Sha256Test, Test1) {
31   // Example B.1 from FIPS 180-2: one-block message.
32   std::string input1 = "abc";
33   int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
34                       0x8f, 0x01, 0xcf, 0xea,
35                       0x41, 0x41, 0x40, 0xde,
36                       0x5d, 0xae, 0x22, 0x23,
37                       0xb0, 0x03, 0x61, 0xa3,
38                       0x96, 0x17, 0x7a, 0x9c,
39                       0xb4, 0x10, 0xff, 0x61,
40                       0xf2, 0x00, 0x15, 0xad };
41 
42   uint8_t output1[crypto::kSHA256Length];
43   crypto::SHA256HashString(input1, output1, sizeof(output1));
44   for (size_t i = 0; i < crypto::kSHA256Length; i++)
45     EXPECT_EQ(expected1[i], static_cast<int>(output1[i]));
46 
47   uint8_t output_truncated1[4];  // 4 bytes == 32 bits
48   crypto::SHA256HashString(input1,
49                            output_truncated1, sizeof(output_truncated1));
50   for (size_t i = 0; i < sizeof(output_truncated1); i++)
51     EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i]));
52 }
53 
TEST(Sha256Test,Test1_String)54 TEST(Sha256Test, Test1_String) {
55   // Same as the above, but using the wrapper that returns a std::string.
56   // Example B.1 from FIPS 180-2: one-block message.
57   std::string input1 = "abc";
58   int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
59                       0x8f, 0x01, 0xcf, 0xea,
60                       0x41, 0x41, 0x40, 0xde,
61                       0x5d, 0xae, 0x22, 0x23,
62                       0xb0, 0x03, 0x61, 0xa3,
63                       0x96, 0x17, 0x7a, 0x9c,
64                       0xb4, 0x10, 0xff, 0x61,
65                       0xf2, 0x00, 0x15, 0xad };
66 
67   std::string output1 = crypto::SHA256HashString(input1);
68   ASSERT_EQ(crypto::kSHA256Length, output1.size());
69   for (size_t i = 0; i < crypto::kSHA256Length; i++)
70     EXPECT_EQ(expected1[i], static_cast<uint8_t>(output1[i]));
71 }
72 
TEST(Sha256Test,Test2)73 TEST(Sha256Test, Test2) {
74   // Example B.2 from FIPS 180-2: multi-block message.
75   std::string input2 =
76       "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
77   int expected2[] = { 0x24, 0x8d, 0x6a, 0x61,
78                       0xd2, 0x06, 0x38, 0xb8,
79                       0xe5, 0xc0, 0x26, 0x93,
80                       0x0c, 0x3e, 0x60, 0x39,
81                       0xa3, 0x3c, 0xe4, 0x59,
82                       0x64, 0xff, 0x21, 0x67,
83                       0xf6, 0xec, 0xed, 0xd4,
84                       0x19, 0xdb, 0x06, 0xc1 };
85 
86   uint8_t output2[crypto::kSHA256Length];
87   crypto::SHA256HashString(input2, output2, sizeof(output2));
88   for (size_t i = 0; i < crypto::kSHA256Length; i++)
89     EXPECT_EQ(expected2[i], static_cast<int>(output2[i]));
90 
91   uint8_t output_truncated2[6];
92   crypto::SHA256HashString(input2,
93                            output_truncated2, sizeof(output_truncated2));
94   for (size_t i = 0; i < sizeof(output_truncated2); i++)
95     EXPECT_EQ(expected2[i], static_cast<int>(output_truncated2[i]));
96 }
97 
TEST(Sha256Test,Test3)98 TEST(Sha256Test, Test3) {
99   // Example B.3 from FIPS 180-2: long message.
100   std::string input3(1000000, 'a');  // 'a' repeated a million times
101   int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c,
102                       0x99, 0x14, 0xfb, 0x92,
103                       0x81, 0xa1, 0xc7, 0xe2,
104                       0x84, 0xd7, 0x3e, 0x67,
105                       0xf1, 0x80, 0x9a, 0x48,
106                       0xa4, 0x97, 0x20, 0x0e,
107                       0x04, 0x6d, 0x39, 0xcc,
108                       0xc7, 0x11, 0x2c, 0xd0 };
109 
110   uint8_t output3[crypto::kSHA256Length];
111   crypto::SHA256HashString(input3, output3, sizeof(output3));
112   for (size_t i = 0; i < crypto::kSHA256Length; i++)
113     EXPECT_EQ(expected3[i], static_cast<int>(output3[i]));
114 
115   uint8_t output_truncated3[12];
116   crypto::SHA256HashString(input3,
117                            output_truncated3, sizeof(output_truncated3));
118   for (size_t i = 0; i < sizeof(output_truncated3); i++)
119     EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i]));
120 }
121