secure_hash_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 "crypto/sha2.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. TEST(SecureHashTest, TestUpdate) {
  12. // Example B.3 from FIPS 180-2: long message.
  13. std::string input3(500000, 'a'); // 'a' repeated half a million times
  14. const int kExpectedHashOfInput3[] = {
  15. 0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7,
  16. 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97,
  17. 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0};
  18. uint8_t output3[crypto::kSHA256Length];
  19. std::unique_ptr<crypto::SecureHash> ctx(
  20. crypto::SecureHash::Create(crypto::SecureHash::SHA256));
  21. ctx->Update(input3.data(), input3.size());
  22. ctx->Update(input3.data(), input3.size());
  23. ctx->Finish(output3, sizeof(output3));
  24. for (size_t i = 0; i < crypto::kSHA256Length; i++)
  25. EXPECT_EQ(kExpectedHashOfInput3[i], static_cast<int>(output3[i]));
  26. }
  27. TEST(SecureHashTest, TestClone) {
  28. std::string input1(10001, 'a'); // 'a' repeated 10001 times
  29. std::string input2(10001, 'd'); // 'd' repeated 10001 times
  30. const uint8_t kExpectedHashOfInput1[crypto::kSHA256Length] = {
  31. 0x0c, 0xab, 0x99, 0xa0, 0x58, 0x60, 0x0f, 0xfa, 0xad, 0x12, 0x92,
  32. 0xd0, 0xc5, 0x3c, 0x05, 0x48, 0xeb, 0xaf, 0x88, 0xdd, 0x1d, 0x01,
  33. 0x03, 0x03, 0x45, 0x70, 0x5f, 0x01, 0x8a, 0x81, 0x39, 0x09};
  34. const uint8_t kExpectedHashOfInput1And2[crypto::kSHA256Length] = {
  35. 0x4c, 0x8e, 0x26, 0x5a, 0xc3, 0x85, 0x1f, 0x1f, 0xa5, 0x04, 0x1c,
  36. 0xc7, 0x88, 0x53, 0x1c, 0xc7, 0x80, 0x47, 0x15, 0xfb, 0x47, 0xff,
  37. 0x72, 0xb1, 0x28, 0x37, 0xb0, 0x4d, 0x6e, 0x22, 0x2e, 0x4d};
  38. uint8_t output1[crypto::kSHA256Length];
  39. uint8_t output2[crypto::kSHA256Length];
  40. uint8_t output3[crypto::kSHA256Length];
  41. std::unique_ptr<crypto::SecureHash> ctx1(
  42. crypto::SecureHash::Create(crypto::SecureHash::SHA256));
  43. ctx1->Update(input1.data(), input1.size());
  44. std::unique_ptr<crypto::SecureHash> ctx2(ctx1->Clone());
  45. std::unique_ptr<crypto::SecureHash> ctx3(ctx2->Clone());
  46. // At this point, ctx1, ctx2, and ctx3 are all equivalent and represent the
  47. // state after hashing input1.
  48. // Updating ctx1 and ctx2 with input2 should produce equivalent results.
  49. ctx1->Update(input2.data(), input2.size());
  50. ctx1->Finish(output1, sizeof(output1));
  51. ctx2->Update(input2.data(), input2.size());
  52. ctx2->Finish(output2, sizeof(output2));
  53. EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length));
  54. EXPECT_EQ(0,
  55. memcmp(output1, kExpectedHashOfInput1And2, crypto::kSHA256Length));
  56. // Finish() ctx3, which should produce the hash of input1.
  57. ctx3->Finish(&output3, sizeof(output3));
  58. EXPECT_EQ(0, memcmp(output3, kExpectedHashOfInput1, crypto::kSHA256Length));
  59. }
  60. TEST(SecureHashTest, TestLength) {
  61. std::unique_ptr<crypto::SecureHash> ctx(
  62. crypto::SecureHash::Create(crypto::SecureHash::SHA256));
  63. EXPECT_EQ(crypto::kSHA256Length, ctx->GetHashLength());
  64. }
  65. TEST(SecureHashTest, Equality) {
  66. std::string input1(10001, 'a'); // 'a' repeated 10001 times
  67. std::string input2(10001, 'd'); // 'd' repeated 10001 times
  68. uint8_t output1[crypto::kSHA256Length];
  69. uint8_t output2[crypto::kSHA256Length];
  70. // Call Update() twice on input1 and input2.
  71. std::unique_ptr<crypto::SecureHash> ctx1(
  72. crypto::SecureHash::Create(crypto::SecureHash::SHA256));
  73. ctx1->Update(input1.data(), input1.size());
  74. ctx1->Update(input2.data(), input2.size());
  75. ctx1->Finish(output1, sizeof(output1));
  76. // Call Update() once one input1 + input2 (concatenation).
  77. std::unique_ptr<crypto::SecureHash> ctx2(
  78. crypto::SecureHash::Create(crypto::SecureHash::SHA256));
  79. std::string input3 = input1 + input2;
  80. ctx2->Update(input3.data(), input3.size());
  81. ctx2->Finish(output2, sizeof(output2));
  82. // The hash should be the same.
  83. EXPECT_EQ(0, memcmp(output1, output2, crypto::kSHA256Length));
  84. }