secure_hash_unittest.cc 6.5 KB

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