[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 | |
[email protected] | c9c251d | 2014-07-22 00:09:25 | [diff] [blame] | 7 | #include <string> |
| 8 | |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 9 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 11 | #include "base/pickle.h" |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 12 | #include "crypto/sha2.h" |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
| 15 | TEST(SecureHashTest, TestUpdate) { |
| 16 | // Example B.3 from FIPS 180-2: long message. |
| 17 | std::string input3(500000, 'a'); // 'a' repeated half a million times |
| 18 | int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c, |
| 19 | 0x99, 0x14, 0xfb, 0x92, |
| 20 | 0x81, 0xa1, 0xc7, 0xe2, |
| 21 | 0x84, 0xd7, 0x3e, 0x67, |
| 22 | 0xf1, 0x80, 0x9a, 0x48, |
| 23 | 0xa4, 0x97, 0x20, 0x0e, |
| 24 | 0x04, 0x6d, 0x39, 0xcc, |
| 25 | 0xc7, 0x11, 0x2c, 0xd0 }; |
| 26 | |
[email protected] | ea73fc7 | 2011-09-22 21:24:50 | [diff] [blame] | 27 | uint8 output3[crypto::kSHA256Length]; |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 28 | |
[email protected] | 4b559b4d | 2011-04-14 17:37:14 | [diff] [blame] | 29 | scoped_ptr<crypto::SecureHash> ctx(crypto::SecureHash::Create( |
| 30 | crypto::SecureHash::SHA256)); |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 31 | ctx->Update(input3.data(), input3.size()); |
| 32 | ctx->Update(input3.data(), input3.size()); |
| 33 | |
| 34 | ctx->Finish(output3, sizeof(output3)); |
[email protected] | ea73fc7 | 2011-09-22 21:24:50 | [diff] [blame] | 35 | for (size_t i = 0; i < crypto::kSHA256Length; i++) |
[email protected] | c322aa9 | 2011-01-27 11:21:07 | [diff] [blame] | 36 | EXPECT_EQ(expected3[i], static_cast<int>(output3[i])); |
| 37 | } |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 38 | |
| 39 | // Save the crypto state mid-stream, and create another instance with the |
| 40 | // saved state. Then feed the same data afterwards to both. |
| 41 | // When done, both should have the same hash value. |
| 42 | TEST(SecureHashTest, TestSerialization) { |
| 43 | std::string input1(10001, 'a'); // 'a' repeated 10001 times |
| 44 | std::string input2(10001, 'b'); // 'b' repeated 10001 times |
| 45 | std::string input3(10001, 'c'); // 'c' repeated 10001 times |
| 46 | std::string input4(10001, 'd'); // 'd' repeated 10001 times |
| 47 | std::string input5(10001, 'e'); // 'e' repeated 10001 times |
| 48 | |
| 49 | uint8 output1[crypto::kSHA256Length]; |
| 50 | uint8 output2[crypto::kSHA256Length]; |
| 51 | |
| 52 | scoped_ptr<crypto::SecureHash> ctx1(crypto::SecureHash::Create( |
| 53 | crypto::SecureHash::SHA256)); |
| 54 | scoped_ptr<crypto::SecureHash> ctx2(crypto::SecureHash::Create( |
| 55 | crypto::SecureHash::SHA256)); |
| 56 | Pickle pickle; |
| 57 | ctx1->Update(input1.data(), input1.size()); |
| 58 | ctx1->Update(input2.data(), input2.size()); |
| 59 | ctx1->Update(input3.data(), input3.size()); |
| 60 | |
| 61 | EXPECT_TRUE(ctx1->Serialize(&pickle)); |
| 62 | ctx1->Update(input4.data(), input4.size()); |
| 63 | ctx1->Update(input5.data(), input5.size()); |
| 64 | |
| 65 | ctx1->Finish(output1, sizeof(output1)); |
| 66 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 67 | PickleIterator data_iterator(pickle); |
| 68 | EXPECT_TRUE(ctx2->Deserialize(&data_iterator)); |
[email protected] | 02c28d22 | 2011-11-19 16:54:13 | [diff] [blame] | 69 | ctx2->Update(input4.data(), input4.size()); |
| 70 | ctx2->Update(input5.data(), input5.size()); |
| 71 | |
| 72 | ctx2->Finish(output2, sizeof(output2)); |
| 73 | |
| 74 | EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length)); |
| 75 | } |