sha2_unittest.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2011 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/sha2.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. TEST(Sha256Test, Test1) {
  9. // Example B.1 from FIPS 180-2: one-block message.
  10. std::string input1 = "abc";
  11. int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
  12. 0x8f, 0x01, 0xcf, 0xea,
  13. 0x41, 0x41, 0x40, 0xde,
  14. 0x5d, 0xae, 0x22, 0x23,
  15. 0xb0, 0x03, 0x61, 0xa3,
  16. 0x96, 0x17, 0x7a, 0x9c,
  17. 0xb4, 0x10, 0xff, 0x61,
  18. 0xf2, 0x00, 0x15, 0xad };
  19. uint8_t output1[crypto::kSHA256Length];
  20. crypto::SHA256HashString(input1, output1, sizeof(output1));
  21. for (size_t i = 0; i < crypto::kSHA256Length; i++)
  22. EXPECT_EQ(expected1[i], static_cast<int>(output1[i]));
  23. uint8_t output_truncated1[4]; // 4 bytes == 32 bits
  24. crypto::SHA256HashString(input1,
  25. output_truncated1, sizeof(output_truncated1));
  26. for (size_t i = 0; i < sizeof(output_truncated1); i++)
  27. EXPECT_EQ(expected1[i], static_cast<int>(output_truncated1[i]));
  28. }
  29. TEST(Sha256Test, Test1_String) {
  30. // Same as the above, but using the wrapper that returns a std::string.
  31. // Example B.1 from FIPS 180-2: one-block message.
  32. std::string input1 = "abc";
  33. int expected1[] = { 0xba, 0x78, 0x16, 0xbf,
  34. 0x8f, 0x01, 0xcf, 0xea,
  35. 0x41, 0x41, 0x40, 0xde,
  36. 0x5d, 0xae, 0x22, 0x23,
  37. 0xb0, 0x03, 0x61, 0xa3,
  38. 0x96, 0x17, 0x7a, 0x9c,
  39. 0xb4, 0x10, 0xff, 0x61,
  40. 0xf2, 0x00, 0x15, 0xad };
  41. std::string output1 = crypto::SHA256HashString(input1);
  42. ASSERT_EQ(crypto::kSHA256Length, output1.size());
  43. for (size_t i = 0; i < crypto::kSHA256Length; i++)
  44. EXPECT_EQ(expected1[i], static_cast<uint8_t>(output1[i]));
  45. }
  46. TEST(Sha256Test, Test2) {
  47. // Example B.2 from FIPS 180-2: multi-block message.
  48. std::string input2 =
  49. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  50. int expected2[] = { 0x24, 0x8d, 0x6a, 0x61,
  51. 0xd2, 0x06, 0x38, 0xb8,
  52. 0xe5, 0xc0, 0x26, 0x93,
  53. 0x0c, 0x3e, 0x60, 0x39,
  54. 0xa3, 0x3c, 0xe4, 0x59,
  55. 0x64, 0xff, 0x21, 0x67,
  56. 0xf6, 0xec, 0xed, 0xd4,
  57. 0x19, 0xdb, 0x06, 0xc1 };
  58. uint8_t output2[crypto::kSHA256Length];
  59. crypto::SHA256HashString(input2, output2, sizeof(output2));
  60. for (size_t i = 0; i < crypto::kSHA256Length; i++)
  61. EXPECT_EQ(expected2[i], static_cast<int>(output2[i]));
  62. uint8_t output_truncated2[6];
  63. crypto::SHA256HashString(input2,
  64. output_truncated2, sizeof(output_truncated2));
  65. for (size_t i = 0; i < sizeof(output_truncated2); i++)
  66. EXPECT_EQ(expected2[i], static_cast<int>(output_truncated2[i]));
  67. }
  68. TEST(Sha256Test, Test3) {
  69. // Example B.3 from FIPS 180-2: long message.
  70. std::string input3(1000000, 'a'); // 'a' repeated a million times
  71. int expected3[] = { 0xcd, 0xc7, 0x6e, 0x5c,
  72. 0x99, 0x14, 0xfb, 0x92,
  73. 0x81, 0xa1, 0xc7, 0xe2,
  74. 0x84, 0xd7, 0x3e, 0x67,
  75. 0xf1, 0x80, 0x9a, 0x48,
  76. 0xa4, 0x97, 0x20, 0x0e,
  77. 0x04, 0x6d, 0x39, 0xcc,
  78. 0xc7, 0x11, 0x2c, 0xd0 };
  79. uint8_t output3[crypto::kSHA256Length];
  80. crypto::SHA256HashString(input3, output3, sizeof(output3));
  81. for (size_t i = 0; i < crypto::kSHA256Length; i++)
  82. EXPECT_EQ(expected3[i], static_cast<int>(output3[i]));
  83. uint8_t output_truncated3[12];
  84. crypto::SHA256HashString(input3,
  85. output_truncated3, sizeof(output_truncated3));
  86. for (size_t i = 0; i < sizeof(output_truncated3); i++)
  87. EXPECT_EQ(expected3[i], static_cast<int>(output_truncated3[i]));
  88. }