[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [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 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 5 | #include "crypto/secure_hash.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 6 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 10 | #include <memory> |
[email protected] | c9c251d | 2014-07-22 00:09:25 | [diff] [blame] | 11 | #include <string> |
| 12 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 13 | #include "crypto/sha2.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | TEST(SecureHashTest, TestUpdate) { |
| 17 | // Example B.3 from FIPS 180-2: long message. |
| 18 | std::string input3(500000, 'a'); // 'a' repeated half a million times |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 19 | const int kExpectedHashOfInput3[] = { |
| 20 | 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, |
| 21 | 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, |
| 22 | 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0}; |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 23 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 24 | uint8_t output3[crypto::kSHA256Length]; |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 25 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 26 | std::unique_ptr<crypto::SecureHash> ctx( |
| 27 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 28 | ctx->Update(input3.data(), input3.size()); |
| 29 | ctx->Update(input3.data(), input3.size()); |
| 30 | |
| 31 | ctx->Finish(output3, sizeof(output3)); |
[email protected] | ea73fc7 | 2011-09-22 21:24:50 | [diff] [blame] | 32 | for (size_t i = 0; i < crypto::kSHA256Length; i++) |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 33 | EXPECT_EQ(kExpectedHashOfInput3[i], static_cast<int>(output3[i])); |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 34 | } |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 35 | |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 36 | TEST(SecureHashTest, TestClone) { |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 37 | std::string input1(10001, 'a'); // 'a' repeated 10001 times |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 38 | std::string input2(10001, 'd'); // 'd' repeated 10001 times |
| 39 | |
| 40 | const uint8_t kExpectedHashOfInput1[crypto::kSHA256Length] = { |
| 41 | 0x0c, 0xab, 0x99, 0xa0, 0x58, 0x60, 0x0f, 0xfa, 0xad, 0x12, 0x92, |
| 42 | 0xd0, 0xc5, 0x3c, 0x05, 0x48, 0xeb, 0xaf, 0x88, 0xdd, 0x1d, 0x01, |
| 43 | 0x03, 0x03, 0x45, 0x70, 0x5f, 0x01, 0x8a, 0x81, 0x39, 0x09}; |
| 44 | const uint8_t kExpectedHashOfInput1And2[crypto::kSHA256Length] = { |
| 45 | 0x4c, 0x8e, 0x26, 0x5a, 0xc3, 0x85, 0x1f, 0x1f, 0xa5, 0x04, 0x1c, |
| 46 | 0xc7, 0x88, 0x53, 0x1c, 0xc7, 0x80, 0x47, 0x15, 0xfb, 0x47, 0xff, |
| 47 | 0x72, 0xb1, 0x28, 0x37, 0xb0, 0x4d, 0x6e, 0x22, 0x2e, 0x4d}; |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 48 | |
avi | dd373b8b | 2015-12-21 21:34:43 | [diff] [blame] | 49 | uint8_t output1[crypto::kSHA256Length]; |
| 50 | uint8_t output2[crypto::kSHA256Length]; |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 51 | uint8_t output3[crypto::kSHA256Length]; |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 52 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 53 | std::unique_ptr<crypto::SecureHash> ctx1( |
| 54 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 55 | ctx1->Update(input1.data(), input1.size()); |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 56 | |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 57 | std::unique_ptr<crypto::SecureHash> ctx2(ctx1->Clone()); |
| 58 | std::unique_ptr<crypto::SecureHash> ctx3(ctx2->Clone()); |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 59 | // At this point, ctx1, ctx2, and ctx3 are all equivalent and represent the |
| 60 | // state after hashing input1. |
| 61 | |
| 62 | // Updating ctx1 and ctx2 with input2 should produce equivalent results. |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 63 | ctx1->Update(input2.data(), input2.size()); |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 64 | ctx1->Finish(output1, sizeof(output1)); |
| 65 | |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 66 | ctx2->Update(input2.data(), input2.size()); |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 67 | ctx2->Finish(output2, sizeof(output2)); |
| 68 | |
| 69 | EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length)); |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 70 | EXPECT_EQ(0, |
| 71 | memcmp(output1, kExpectedHashOfInput1And2, crypto::kSHA256Length)); |
| 72 | |
| 73 | // Finish() ctx3, which should produce the hash of input1. |
| 74 | ctx3->Finish(&output3, sizeof(output3)); |
| 75 | EXPECT_EQ(0, memcmp(output3, kExpectedHashOfInput1, crypto::kSHA256Length)); |
| 76 | } |
| 77 | |
| 78 | TEST(SecureHashTest, TestLength) { |
thakis | d1a1847 | 2016-04-08 22:30:41 | [diff] [blame] | 79 | std::unique_ptr<crypto::SecureHash> ctx( |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 80 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 81 | EXPECT_EQ(crypto::kSHA256Length, ctx->GetHashLength()); |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 82 | } |
Xiaohan Wang | bb6c0b8 | 2017-10-24 01:18:01 | [diff] [blame] | 83 | |
| 84 | TEST(SecureHashTest, Equality) { |
| 85 | std::string input1(10001, 'a'); // 'a' repeated 10001 times |
| 86 | std::string input2(10001, 'd'); // 'd' repeated 10001 times |
| 87 | |
| 88 | uint8_t output1[crypto::kSHA256Length]; |
| 89 | uint8_t output2[crypto::kSHA256Length]; |
| 90 | |
| 91 | // Call Update() twice on input1 and input2. |
| 92 | std::unique_ptr<crypto::SecureHash> ctx1( |
| 93 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 94 | ctx1->Update(input1.data(), input1.size()); |
| 95 | ctx1->Update(input2.data(), input2.size()); |
| 96 | ctx1->Finish(output1, sizeof(output1)); |
| 97 | |
| 98 | // Call Update() once one input1 + input2 (concatenation). |
| 99 | std::unique_ptr<crypto::SecureHash> ctx2( |
| 100 | crypto::SecureHash::Create(crypto::SecureHash::SHA256)); |
| 101 | std::string input3 = input1 + input2; |
| 102 | ctx2->Update(input3.data(), input3.size()); |
| 103 | ctx2->Finish(output2, sizeof(output2)); |
| 104 | |
| 105 | // The hash should be the same. |
| 106 | EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length)); |
| 107 | } |