1 // Copyright 2019 The Chromium Authors. All rights reserved.
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 "util/crypto/secure_hash.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <memory>
11 #include <string>
12 #include <vector>
13
14 #include "gmock/gmock.h"
15 #include "gtest/gtest.h"
16 #include "openssl/evp.h"
17 #include "openssl/sha.h"
18
19 namespace openscreen {
TEST(SecureHashTest,TestUpdate)20 TEST(SecureHashTest, TestUpdate) {
21 // Example B.3 from FIPS 180-2: long message.
22 std::string input3(500000, 'a'); // 'a' repeated half a million times
23 const uint8_t kExpectedHashOfInput3[] = {
24 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
25 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
26 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0};
27
28 SecureHash ctx(EVP_sha256());
29 std::vector<uint8_t> output3(ctx.GetHashLength());
30 ctx.Update(input3);
31 ctx.Update(input3);
32 ctx.Finish(output3.data());
33 EXPECT_THAT(output3, testing::ElementsAreArray(kExpectedHashOfInput3));
34 }
35
TEST(SecureHashTest,TestCopyable)36 TEST(SecureHashTest, TestCopyable) {
37 std::string input1(10001, 'a'); // 'a' repeated 10001 times
38 std::string input2(10001, 'd'); // 'd' repeated 10001 times
39
40 const uint8_t kExpectedHashOfInput1[SHA256_DIGEST_LENGTH] = {
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[SHA256_DIGEST_LENGTH] = {
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};
48
49 SecureHash ctx1(EVP_sha256());
50 std::vector<uint8_t> output1(ctx1.GetHashLength());
51 ctx1.Update(input1);
52
53 SecureHash ctx2 = ctx1;
54 std::vector<uint8_t> output2(ctx2.GetHashLength());
55
56 SecureHash ctx3 = ctx1;
57 std::vector<uint8_t> output3(ctx3.GetHashLength());
58
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.
63 ctx1.Update(input2);
64 ctx1.Finish(output1.data());
65
66 ctx2.Update(input2);
67 ctx2.Finish(output2.data());
68
69 EXPECT_THAT(output1, testing::ElementsAreArray(output2));
70 EXPECT_THAT(output1, testing::ElementsAreArray(kExpectedHashOfInput1And2));
71
72 // Finish() ctx3, which should produce the hash of input1.
73 ctx3.Finish(output3.data());
74 EXPECT_THAT(output3, testing::ElementsAreArray(kExpectedHashOfInput1));
75 }
76
TEST(SecureHashTest,TestLength)77 TEST(SecureHashTest, TestLength) {
78 SecureHash ctx(EVP_sha256());
79 EXPECT_EQ(static_cast<size_t>(SHA256_DIGEST_LENGTH), ctx.GetHashLength());
80 }
81
TEST(SecureHashTest,Equality)82 TEST(SecureHashTest, Equality) {
83 std::string input1(10001, 'a'); // 'a' repeated 10001 times
84 std::string input2(10001, 'd'); // 'd' repeated 10001 times
85
86 // Call Update() twice on input1 and input2.
87 SecureHash ctx1(EVP_sha256());
88 std::vector<uint8_t> output1(ctx1.GetHashLength());
89 ctx1.Update(input1);
90 ctx1.Update(input2);
91 ctx1.Finish(output1.data());
92
93 // Call Update() once one input1 + input2 (concatenation).
94 SecureHash ctx2(EVP_sha256());
95 std::vector<uint8_t> output2(ctx2.GetHashLength());
96 std::string input3 = input1 + input2;
97 ctx2.Update(input3);
98 ctx2.Finish(output2.data());
99
100 // The hash should be the same.
101 EXPECT_THAT(output1, testing::ElementsAreArray(output2));
102 }
103 } // namespace openscreen
104