[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / crypto / secure_hash_unittest.cc
1 // Copyright 2012 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 #include "crypto/secure_hash.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <memory>
11 #include <string>
12 #include <utility>
13
14 #include "crypto/sha2.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/boringssl/src/include/openssl/sha.h"
17
18 class SecureHashTest : public testing::Test,
19                        public testing::WithParamInterface<
20                            std::pair<crypto::SecureHash::Algorithm, uint64_t>> {
21  public:
22   SecureHashTest()
23       : algorithm_(GetParam().first), hash_length_(GetParam().second) {}
24
25  protected:
26   crypto::SecureHash::Algorithm algorithm_;
27   const uint64_t hash_length_;
28 };
29
30 TEST_P(SecureHashTest, TestUpdateSHA256) {
31   std::string input3;
32   std::vector<uint8_t> expected_hash_of_input_3;
33
34   switch (algorithm_) {
35     case crypto::SecureHash::SHA256:
36       // Example B.3 from FIPS 180-2: long message.
37       input3 = std::string(500000, 'a');  // 'a' repeated half a million times
38       expected_hash_of_input_3 = {
39           0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
40           0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
41           0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0};
42       break;
43     case crypto::SecureHash::SHA512:
44       // Example C.3 from FIPS 180-2: long message.
45       input3 = std::string(500000, 'a');  // 'a' repeated half a million times
46       expected_hash_of_input_3 = {
47           0xe7, 0x18, 0x48, 0x3d, 0x0c, 0xe7, 0x69, 0x64, 0x4e, 0x2e, 0x42,
48           0xc7, 0xbc, 0x15, 0xb4, 0x63, 0x8e, 0x1f, 0x98, 0xb1, 0x3b, 0x20,
49           0x44, 0x28, 0x56, 0x32, 0xa8, 0x03, 0xaf, 0xa9, 0x73, 0xeb, 0xde,
50           0x0f, 0xf2, 0x44, 0x87, 0x7e, 0xa6, 0x0a, 0x4c, 0xb0, 0x43, 0x2c,
51           0xe5, 0x77, 0xc3, 0x1b, 0xeb, 0x00, 0x9c, 0x5c, 0x2c, 0x49, 0xaa,
52           0x2e, 0x4e, 0xad, 0xb2, 0x17, 0xad, 0x8c, 0xc0, 0x9b};
53       break;
54   }
55
56   uint8_t output3[hash_length_];
57
58   std::unique_ptr<crypto::SecureHash> ctx(
59       crypto::SecureHash::Create(algorithm_));
60   ctx->Update(input3.data(), input3.size());
61   ctx->Update(input3.data(), input3.size());
62
63   ctx->Finish(output3, sizeof(output3));
64   for (size_t i = 0; i < hash_length_; i++)
65     EXPECT_EQ(expected_hash_of_input_3[i], static_cast<int>(output3[i]));
66 }
67
68 TEST_P(SecureHashTest, TestClone) {
69   std::string input1(10001, 'a');  // 'a' repeated 10001 times
70   std::string input2(10001, 'd');  // 'd' repeated 10001 times
71
72   std::vector<uint8_t> expected_hash_of_input_1;
73   std::vector<uint8_t> expected_hash_of_input_1_and_2;
74
75   switch (algorithm_) {
76     case crypto::SecureHash::SHA256:
77       expected_hash_of_input_1 = {
78           0x0c, 0xab, 0x99, 0xa0, 0x58, 0x60, 0x0f, 0xfa, 0xad, 0x12, 0x92,
79           0xd0, 0xc5, 0x3c, 0x05, 0x48, 0xeb, 0xaf, 0x88, 0xdd, 0x1d, 0x01,
80           0x03, 0x03, 0x45, 0x70, 0x5f, 0x01, 0x8a, 0x81, 0x39, 0x09};
81       expected_hash_of_input_1_and_2 = {
82           0x4c, 0x8e, 0x26, 0x5a, 0xc3, 0x85, 0x1f, 0x1f, 0xa5, 0x04, 0x1c,
83           0xc7, 0x88, 0x53, 0x1c, 0xc7, 0x80, 0x47, 0x15, 0xfb, 0x47, 0xff,
84           0x72, 0xb1, 0x28, 0x37, 0xb0, 0x4d, 0x6e, 0x22, 0x2e, 0x4d};
85       break;
86     case crypto::SecureHash::SHA512:
87       expected_hash_of_input_1 = {
88           0xea, 0x03, 0xb2, 0x23, 0x32, 0x29, 0xc8, 0x87, 0x86, 0x33, 0xa3,
89           0x70, 0xc7, 0xb2, 0x40, 0xea, 0xef, 0xd9, 0x55, 0xe2, 0xb3, 0x79,
90           0xd6, 0xb3, 0x3f, 0x5e, 0xff, 0x89, 0xfd, 0x86, 0x7b, 0x10, 0xe2,
91           0xc1, 0x3b, 0x2f, 0xf5, 0x29, 0x80, 0xa0, 0xb0, 0xf9, 0xcf, 0x47,
92           0xa7, 0xff, 0x73, 0xac, 0xd2, 0x66, 0x9e, 0x53, 0x78, 0x9f, 0xc6,
93           0x07, 0x7a, 0xb7, 0x09, 0x1f, 0xa4, 0x3b, 0x18, 0x00};
94       expected_hash_of_input_1_and_2 = {
95           0x41, 0x6d, 0x46, 0x8d, 0x8a, 0x84, 0x3d, 0xf9, 0x43, 0xac, 0xe6,
96           0x4d, 0x5b, 0x60, 0xd7, 0x1a, 0xb1, 0xe6, 0x2d, 0xd3, 0xe6, 0x97,
97           0xaf, 0x6f, 0x34, 0x97, 0x8f, 0x01, 0xd4, 0x15, 0x06, 0xfa, 0x69,
98           0x48, 0x0e, 0x24, 0x0d, 0x98, 0x84, 0x76, 0xd2, 0x95, 0x4c, 0x16,
99           0x02, 0xfd, 0x71, 0xd4, 0x25, 0xb3, 0x8f, 0xf2, 0x60, 0xa3, 0x0e,
100           0xdb, 0xe9, 0x87, 0x32, 0xfc, 0xf3, 0x2d, 0x0a, 0x28};
101       break;
102   }
103
104   uint8_t output1[hash_length_];
105   uint8_t output2[hash_length_];
106   uint8_t output3[hash_length_];
107
108   std::unique_ptr<crypto::SecureHash> ctx1(
109       crypto::SecureHash::Create(algorithm_));
110   ctx1->Update(input1.data(), input1.size());
111
112   std::unique_ptr<crypto::SecureHash> ctx2(ctx1->Clone());
113   std::unique_ptr<crypto::SecureHash> ctx3(ctx2->Clone());
114   // At this point, ctx1, ctx2, and ctx3 are all equivalent and represent the
115   // state after hashing input1.
116
117   // Updating ctx1 and ctx2 with input2 should produce equivalent results.
118   ctx1->Update(input2.data(), input2.size());
119   ctx1->Finish(output1, sizeof(output1));
120
121   ctx2->Update(input2.data(), input2.size());
122   ctx2->Finish(output2, sizeof(output2));
123
124   EXPECT_EQ(0, memcmp(output1, output2, hash_length_));
125   EXPECT_EQ(
126       0, memcmp(output1, expected_hash_of_input_1_and_2.data(), hash_length_));
127
128   // Finish() ctx3, which should produce the hash of input1.
129   ctx3->Finish(&output3, sizeof(output3));
130   EXPECT_EQ(0, memcmp(output3, expected_hash_of_input_1.data(), hash_length_));
131 }
132
133 TEST_P(SecureHashTest, TestLength) {
134   std::unique_ptr<crypto::SecureHash> ctx(
135       crypto::SecureHash::Create(algorithm_));
136   EXPECT_EQ(hash_length_, ctx->GetHashLength());
137 }
138
139 TEST_P(SecureHashTest, Equality) {
140   std::string input1(10001, 'a');  // 'a' repeated 10001 times
141   std::string input2(10001, 'd');  // 'd' repeated 10001 times
142
143   uint8_t output1[hash_length_];
144   uint8_t output2[hash_length_];
145
146   // Call Update() twice on input1 and input2.
147   std::unique_ptr<crypto::SecureHash> ctx1(
148       crypto::SecureHash::Create(algorithm_));
149   ctx1->Update(input1.data(), input1.size());
150   ctx1->Update(input2.data(), input2.size());
151   ctx1->Finish(output1, sizeof(output1));
152
153   // Call Update() once one input1 + input2 (concatenation).
154   std::unique_ptr<crypto::SecureHash> ctx2(
155       crypto::SecureHash::Create(algorithm_));
156   std::string input3 = input1 + input2;
157   ctx2->Update(input3.data(), input3.size());
158   ctx2->Finish(output2, sizeof(output2));
159
160   // The hash should be the same.
161   EXPECT_EQ(0, memcmp(output1, output2, hash_length_));
162 }
163
164 INSTANTIATE_TEST_SUITE_P(
165     All,
166     SecureHashTest,
167     testing::Values(
168         std::make_pair(crypto::SecureHash::SHA256, SHA256_DIGEST_LENGTH),
169         std::make_pair(crypto::SecureHash::SHA512, SHA512_DIGEST_LENGTH)));