blob: 27d6d25508cb71b63a45234fd803db74a273b371 [file] [log] [blame]
[email protected]4b559b4d2011-04-14 17:37:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]4b559b4d2011-04-14 17:37:145#include "crypto/sha2.h"
initial.commitd7cae122008-07-26 21:49:386
avidd373b8b2015-12-21 21:34:437#include <stddef.h>
8#include <stdint.h>
9
initial.commitd7cae122008-07-26 21:49:3810#include "testing/gtest/include/gtest/gtest.h"
11
12TEST(Sha256Test, Test1) {
13 // Example B.1 from FIPS 180-2: one-block message.
14 std::string input1 = "abc";
15 int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
16 0x8f, 0x01, 0xcf, 0xea,
17 0x41, 0x41, 0x40, 0xde,
18 0x5d, 0xae, 0x22, 0x23,
19 0xb0, 0x03, 0x61, 0xa3,
20 0x96, 0x17, 0x7a, 0x9c,
21 0xb4, 0x10, 0xff, 0x61,
22 0xf2, 0x00, 0x15, 0xad };
23
avidd373b8b2015-12-21 21:34:4324 uint8_t output1[crypto::kSHA256Length];
[email protected]4b559b4d2011-04-14 17:37:1425 crypto::SHA256HashString(input1, output1, sizeof(output1));
[email protected]ea73fc72011-09-22 21:24:5026 for (size_t i = 0; i < crypto::kSHA256Length; i++)
initial.commitd7cae122008-07-26 21:49:3827 EXPECT_EQ(expected1[i], static_cast<int>(output1[i]));
28
avidd373b8b2015-12-21 21:34:4329 uint8_t output_truncated1[4]; // 4 bytes == 32 bits
[email protected]4b559b4d2011-04-14 17:37:1430 crypto::SHA256HashString(input1,
31 output_truncated1, sizeof(output_truncated1));
[email protected]44a1cbfa2008-08-15 01:05:1132 for (size_t i = 0; i < sizeof(output_truncated1); i++)
initial.commitd7cae122008-07-26 21:49:3833 EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i]));
34}
35
[email protected]6c683f02010-09-24 17:45:2136TEST(Sha256Test, Test1_String) {
37 // Same as the above, but using the wrapper that returns a std::string.
38 // Example B.1 from FIPS 180-2: one-block message.
39 std::string input1 = "abc";
40 int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
41 0x8f, 0x01, 0xcf, 0xea,
42 0x41, 0x41, 0x40, 0xde,
43 0x5d, 0xae, 0x22, 0x23,
44 0xb0, 0x03, 0x61, 0xa3,
45 0x96, 0x17, 0x7a, 0x9c,
46 0xb4, 0x10, 0xff, 0x61,
47 0xf2, 0x00, 0x15, 0xad };
48
[email protected]4b559b4d2011-04-14 17:37:1449 std::string output1 = crypto::SHA256HashString(input1);
[email protected]ea73fc72011-09-22 21:24:5050 ASSERT_EQ(crypto::kSHA256Length, output1.size());
51 for (size_t i = 0; i < crypto::kSHA256Length; i++)
avidd373b8b2015-12-21 21:34:4352 EXPECT_EQ(expected1[i], static_cast<uint8_t>(output1[i]));
[email protected]6c683f02010-09-24 17:45:2153}
54
initial.commitd7cae122008-07-26 21:49:3855TEST(Sha256Test, Test2) {
56 // Example B.2 from FIPS 180-2: multi-block message.
57 std::string input2 =
58 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
59 int expected2[] = { 0x24, 0x8d, 0x6a, 0x61,
60 0xd2, 0x06, 0x38, 0xb8,
61 0xe5, 0xc0, 0x26, 0x93,
62 0x0c, 0x3e, 0x60, 0x39,
63 0xa3, 0x3c, 0xe4, 0x59,
64 0x64, 0xff, 0x21, 0x67,
65 0xf6, 0xec, 0xed, 0xd4,
66 0x19, 0xdb, 0x06, 0xc1 };
67
avidd373b8b2015-12-21 21:34:4368 uint8_t output2[crypto::kSHA256Length];
[email protected]4b559b4d2011-04-14 17:37:1469 crypto::SHA256HashString(input2, output2, sizeof(output2));
[email protected]ea73fc72011-09-22 21:24:5070 for (size_t i = 0; i < crypto::kSHA256Length; i++)
initial.commitd7cae122008-07-26 21:49:3871 EXPECT_EQ(expected2[i], static_cast<int>(output2[i]));
72
avidd373b8b2015-12-21 21:34:4373 uint8_t output_truncated2[6];
[email protected]4b559b4d2011-04-14 17:37:1474 crypto::SHA256HashString(input2,
75 output_truncated2, sizeof(output_truncated2));
[email protected]44a1cbfa2008-08-15 01:05:1176 for (size_t i = 0; i < sizeof(output_truncated2); i++)
initial.commitd7cae122008-07-26 21:49:3877 EXPECT_EQ(expected2[i], static_cast<int>(output_truncated2[i]));
78}
79
80TEST(Sha256Test, Test3) {
81 // Example B.3 from FIPS 180-2: long message.
82 std::string input3(1000000, 'a'); // 'a' repeated a million times
83 int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c,
84 0x99, 0x14, 0xfb, 0x92,
85 0x81, 0xa1, 0xc7, 0xe2,
86 0x84, 0xd7, 0x3e, 0x67,
87 0xf1, 0x80, 0x9a, 0x48,
88 0xa4, 0x97, 0x20, 0x0e,
89 0x04, 0x6d, 0x39, 0xcc,
90 0xc7, 0x11, 0x2c, 0xd0 };
91
avidd373b8b2015-12-21 21:34:4392 uint8_t output3[crypto::kSHA256Length];
[email protected]4b559b4d2011-04-14 17:37:1493 crypto::SHA256HashString(input3, output3, sizeof(output3));
[email protected]ea73fc72011-09-22 21:24:5094 for (size_t i = 0; i < crypto::kSHA256Length; i++)
initial.commitd7cae122008-07-26 21:49:3895 EXPECT_EQ(expected3[i], static_cast<int>(output3[i]));
96
avidd373b8b2015-12-21 21:34:4397 uint8_t output_truncated3[12];
[email protected]4b559b4d2011-04-14 17:37:1498 crypto::SHA256HashString(input3,
99 output_truncated3, sizeof(output_truncated3));
[email protected]44a1cbfa2008-08-15 01:05:11100 for (size_t i = 0; i < sizeof(output_truncated3); i++)
initial.commitd7cae122008-07-26 21:49:38101 EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i]));
102}