You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
6.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2012 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/message_digest.h"
  11. #include "rtc_base/string_encode.h"
  12. #include "test/gtest.h"
  13. namespace rtc {
  14. // Test vectors from RFC 1321.
  15. TEST(MessageDigestTest, TestMd5Digest) {
  16. // Test the string versions of the APIs.
  17. EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", ComputeDigest(DIGEST_MD5, ""));
  18. EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72",
  19. ComputeDigest(DIGEST_MD5, "abc"));
  20. EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b",
  21. ComputeDigest(DIGEST_MD5, "abcdefghijklmnopqrstuvwxyz"));
  22. // Test the raw buffer versions of the APIs; also check output buffer size.
  23. char output[16];
  24. EXPECT_EQ(sizeof(output),
  25. ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output)));
  26. EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72",
  27. hex_encode(output, sizeof(output)));
  28. EXPECT_EQ(0U,
  29. ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output) - 1));
  30. }
  31. // Test vectors from RFC 3174.
  32. TEST(MessageDigestTest, TestSha1Digest) {
  33. // Test the string versions of the APIs.
  34. EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709",
  35. ComputeDigest(DIGEST_SHA_1, ""));
  36. EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
  37. ComputeDigest(DIGEST_SHA_1, "abc"));
  38. EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
  39. ComputeDigest(
  40. DIGEST_SHA_1,
  41. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
  42. // Test the raw buffer versions of the APIs; also check output buffer size.
  43. char output[20];
  44. EXPECT_EQ(sizeof(output),
  45. ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output)));
  46. EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
  47. hex_encode(output, sizeof(output)));
  48. EXPECT_EQ(0U,
  49. ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output) - 1));
  50. }
  51. // Test that we fail properly if a bad digest algorithm is specified.
  52. TEST(MessageDigestTest, TestBadDigest) {
  53. std::string output;
  54. EXPECT_FALSE(ComputeDigest("sha-9000", "abc", &output));
  55. EXPECT_EQ("", ComputeDigest("sha-9000", "abc"));
  56. }
  57. // Test vectors from RFC 2202.
  58. TEST(MessageDigestTest, TestMd5Hmac) {
  59. // Test the string versions of the APIs.
  60. EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d",
  61. ComputeHmac(DIGEST_MD5, std::string(16, '\x0b'), "Hi There"));
  62. EXPECT_EQ("750c783e6ab0b503eaa86e310a5db738",
  63. ComputeHmac(DIGEST_MD5, "Jefe", "what do ya want for nothing?"));
  64. EXPECT_EQ("56be34521d144c88dbb8c733f0e8b3f6",
  65. ComputeHmac(DIGEST_MD5, std::string(16, '\xaa'),
  66. std::string(50, '\xdd')));
  67. EXPECT_EQ(
  68. "697eaf0aca3a3aea3a75164746ffaa79",
  69. ComputeHmac(DIGEST_MD5,
  70. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  71. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  72. std::string(50, '\xcd')));
  73. EXPECT_EQ(
  74. "56461ef2342edc00f9bab995690efd4c",
  75. ComputeHmac(DIGEST_MD5, std::string(16, '\x0c'), "Test With Truncation"));
  76. EXPECT_EQ(
  77. "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
  78. ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'),
  79. "Test Using Larger Than Block-Size Key - Hash Key First"));
  80. EXPECT_EQ("6f630fad67cda0ee1fb1f562db3aa53e",
  81. ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'),
  82. "Test Using Larger Than Block-Size Key and Larger "
  83. "Than One Block-Size Data"));
  84. // Test the raw buffer versions of the APIs; also check output buffer size.
  85. std::string key(16, '\x0b');
  86. std::string input("Hi There");
  87. char output[16];
  88. EXPECT_EQ(sizeof(output),
  89. ComputeHmac(DIGEST_MD5, key.c_str(), key.size(), input.c_str(),
  90. input.size(), output, sizeof(output)));
  91. EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d",
  92. hex_encode(output, sizeof(output)));
  93. EXPECT_EQ(0U, ComputeHmac(DIGEST_MD5, key.c_str(), key.size(), input.c_str(),
  94. input.size(), output, sizeof(output) - 1));
  95. }
  96. // Test vectors from RFC 2202.
  97. TEST(MessageDigestTest, TestSha1Hmac) {
  98. // Test the string versions of the APIs.
  99. EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00",
  100. ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0b'), "Hi There"));
  101. EXPECT_EQ("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
  102. ComputeHmac(DIGEST_SHA_1, "Jefe", "what do ya want for nothing?"));
  103. EXPECT_EQ("125d7342b9ac11cd91a39af48aa17b4f63f175d3",
  104. ComputeHmac(DIGEST_SHA_1, std::string(20, '\xaa'),
  105. std::string(50, '\xdd')));
  106. EXPECT_EQ(
  107. "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
  108. ComputeHmac(DIGEST_SHA_1,
  109. "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
  110. "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
  111. std::string(50, '\xcd')));
  112. EXPECT_EQ("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
  113. ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0c'),
  114. "Test With Truncation"));
  115. EXPECT_EQ(
  116. "aa4ae5e15272d00e95705637ce8a3b55ed402112",
  117. ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'),
  118. "Test Using Larger Than Block-Size Key - Hash Key First"));
  119. EXPECT_EQ("e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
  120. ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'),
  121. "Test Using Larger Than Block-Size Key and Larger "
  122. "Than One Block-Size Data"));
  123. // Test the raw buffer versions of the APIs; also check output buffer size.
  124. std::string key(20, '\x0b');
  125. std::string input("Hi There");
  126. char output[20];
  127. EXPECT_EQ(sizeof(output),
  128. ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(), input.c_str(),
  129. input.size(), output, sizeof(output)));
  130. EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00",
  131. hex_encode(output, sizeof(output)));
  132. EXPECT_EQ(0U,
  133. ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(), input.c_str(),
  134. input.size(), output, sizeof(output) - 1));
  135. }
  136. TEST(MessageDigestTest, TestBadHmac) {
  137. std::string output;
  138. EXPECT_FALSE(ComputeHmac("sha-9000", "key", "abc", &output));
  139. EXPECT_EQ("", ComputeHmac("sha-9000", "key", "abc"));
  140. }
  141. } // namespace rtc