Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 1 | // Copyright 2011 The Chromium OS Authors. All rights reserved. |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 9e5b683 | 2015-10-14 15:57:14 -0700 | [diff] [blame] | 5 | #include "third_party/chromium/crypto/sha2.h" |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 6 | |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 7 | #include <gtest/gtest.h> |
| 8 | |
| 9 | namespace weave { |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 10 | |
| 11 | TEST(Sha256Test, Test1) { |
| 12 | // Example B.1 from FIPS 180-2: one-block message. |
| 13 | std::string input1 = "abc"; |
| 14 | int expected1[] = { 0xba, 0x78, 0x16, 0xbf, |
| 15 | 0x8f, 0x01, 0xcf, 0xea, |
| 16 | 0x41, 0x41, 0x40, 0xde, |
| 17 | 0x5d, 0xae, 0x22, 0x23, |
| 18 | 0xb0, 0x03, 0x61, 0xa3, |
| 19 | 0x96, 0x17, 0x7a, 0x9c, |
| 20 | 0xb4, 0x10, 0xff, 0x61, |
| 21 | 0xf2, 0x00, 0x15, 0xad }; |
| 22 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 23 | uint8_t output1[crypto::kSHA256Length]; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 24 | crypto::SHA256HashString(input1, output1, sizeof(output1)); |
| 25 | for (size_t i = 0; i < crypto::kSHA256Length; i++) |
| 26 | EXPECT_EQ(expected1[i], static_cast<int>(output1[i])); |
| 27 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 28 | uint8_t output_truncated1[4]; // 4 bytes == 32 bits |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 29 | crypto::SHA256HashString(input1, |
| 30 | output_truncated1, sizeof(output_truncated1)); |
| 31 | for (size_t i = 0; i < sizeof(output_truncated1); i++) |
| 32 | EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i])); |
| 33 | } |
| 34 | |
| 35 | TEST(Sha256Test, Test1_String) { |
| 36 | // Same as the above, but using the wrapper that returns a std::string. |
| 37 | // Example B.1 from FIPS 180-2: one-block message. |
| 38 | std::string input1 = "abc"; |
| 39 | int expected1[] = { 0xba, 0x78, 0x16, 0xbf, |
| 40 | 0x8f, 0x01, 0xcf, 0xea, |
| 41 | 0x41, 0x41, 0x40, 0xde, |
| 42 | 0x5d, 0xae, 0x22, 0x23, |
| 43 | 0xb0, 0x03, 0x61, 0xa3, |
| 44 | 0x96, 0x17, 0x7a, 0x9c, |
| 45 | 0xb4, 0x10, 0xff, 0x61, |
| 46 | 0xf2, 0x00, 0x15, 0xad }; |
| 47 | |
| 48 | std::string output1 = crypto::SHA256HashString(input1); |
| 49 | ASSERT_EQ(crypto::kSHA256Length, output1.size()); |
| 50 | for (size_t i = 0; i < crypto::kSHA256Length; i++) |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 51 | EXPECT_EQ(expected1[i], static_cast<uint8_t>(output1[i])); |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | TEST(Sha256Test, Test2) { |
| 55 | // Example B.2 from FIPS 180-2: multi-block message. |
| 56 | std::string input2 = |
| 57 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; |
| 58 | int expected2[] = { 0x24, 0x8d, 0x6a, 0x61, |
| 59 | 0xd2, 0x06, 0x38, 0xb8, |
| 60 | 0xe5, 0xc0, 0x26, 0x93, |
| 61 | 0x0c, 0x3e, 0x60, 0x39, |
| 62 | 0xa3, 0x3c, 0xe4, 0x59, |
| 63 | 0x64, 0xff, 0x21, 0x67, |
| 64 | 0xf6, 0xec, 0xed, 0xd4, |
| 65 | 0x19, 0xdb, 0x06, 0xc1 }; |
| 66 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 67 | uint8_t output2[crypto::kSHA256Length]; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 68 | crypto::SHA256HashString(input2, output2, sizeof(output2)); |
| 69 | for (size_t i = 0; i < crypto::kSHA256Length; i++) |
| 70 | EXPECT_EQ(expected2[i], static_cast<int>(output2[i])); |
| 71 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 72 | uint8_t output_truncated2[6]; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 73 | crypto::SHA256HashString(input2, |
| 74 | output_truncated2, sizeof(output_truncated2)); |
| 75 | for (size_t i = 0; i < sizeof(output_truncated2); i++) |
| 76 | EXPECT_EQ(expected2[i], static_cast<int>(output_truncated2[i])); |
| 77 | } |
| 78 | |
| 79 | TEST(Sha256Test, Test3) { |
| 80 | // Example B.3 from FIPS 180-2: long message. |
| 81 | std::string input3(1000000, 'a'); // 'a' repeated a million times |
| 82 | int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c, |
| 83 | 0x99, 0x14, 0xfb, 0x92, |
| 84 | 0x81, 0xa1, 0xc7, 0xe2, |
| 85 | 0x84, 0xd7, 0x3e, 0x67, |
| 86 | 0xf1, 0x80, 0x9a, 0x48, |
| 87 | 0xa4, 0x97, 0x20, 0x0e, |
| 88 | 0x04, 0x6d, 0x39, 0xcc, |
| 89 | 0xc7, 0x11, 0x2c, 0xd0 }; |
| 90 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 91 | uint8_t output3[crypto::kSHA256Length]; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 92 | crypto::SHA256HashString(input3, output3, sizeof(output3)); |
| 93 | for (size_t i = 0; i < crypto::kSHA256Length; i++) |
| 94 | EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); |
| 95 | |
Alex Vakulenko | 674f0eb | 2016-01-20 08:10:48 -0800 | [diff] [blame] | 96 | uint8_t output_truncated3[12]; |
Vitaly Buka | 6ca6a23 | 2015-08-06 17:32:43 -0700 | [diff] [blame] | 97 | crypto::SHA256HashString(input3, |
| 98 | output_truncated3, sizeof(output_truncated3)); |
| 99 | for (size_t i = 0; i < sizeof(output_truncated3); i++) |
| 100 | EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i])); |
| 101 | } |
Vitaly Buka | 9ba72a8 | 2015-08-06 17:36:17 -0700 | [diff] [blame] | 102 | |
| 103 | } // namespace weave |