henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/messagedigest.h" |
| 12 | #include "rtc_base/gunit.h" |
| 13 | #include "rtc_base/stringencode.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 14 | |
| 15 | namespace rtc { |
| 16 | |
| 17 | // Test vectors from RFC 1321. |
| 18 | TEST(MessageDigestTest, TestMd5Digest) { |
| 19 | // Test the string versions of the APIs. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 20 | EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", ComputeDigest(DIGEST_MD5, "")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 21 | EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 22 | ComputeDigest(DIGEST_MD5, "abc")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 23 | EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 24 | ComputeDigest(DIGEST_MD5, "abcdefghijklmnopqrstuvwxyz")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 25 | |
| 26 | // Test the raw buffer versions of the APIs; also check output buffer size. |
| 27 | char output[16]; |
| 28 | EXPECT_EQ(sizeof(output), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 30 | EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 31 | hex_encode(output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | EXPECT_EQ(0U, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 33 | ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output) - 1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // Test vectors from RFC 3174. |
| 37 | TEST(MessageDigestTest, TestSha1Digest) { |
| 38 | // Test the string versions of the APIs. |
| 39 | EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 40 | ComputeDigest(DIGEST_SHA_1, "")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 41 | EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 42 | ComputeDigest(DIGEST_SHA_1, "abc")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 43 | EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 44 | ComputeDigest( |
| 45 | DIGEST_SHA_1, |
| 46 | "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | |
| 48 | // Test the raw buffer versions of the APIs; also check output buffer size. |
| 49 | char output[20]; |
| 50 | EXPECT_EQ(sizeof(output), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 51 | ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 52 | EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 53 | hex_encode(output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 54 | EXPECT_EQ(0U, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output) - 1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Test that we fail properly if a bad digest algorithm is specified. |
| 59 | TEST(MessageDigestTest, TestBadDigest) { |
| 60 | std::string output; |
| 61 | EXPECT_FALSE(ComputeDigest("sha-9000", "abc", &output)); |
| 62 | EXPECT_EQ("", ComputeDigest("sha-9000", "abc")); |
| 63 | } |
| 64 | |
| 65 | // Test vectors from RFC 2202. |
| 66 | TEST(MessageDigestTest, TestMd5Hmac) { |
| 67 | // Test the string versions of the APIs. |
| 68 | EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 69 | ComputeHmac(DIGEST_MD5, std::string(16, '\x0b'), "Hi There")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 70 | EXPECT_EQ("750c783e6ab0b503eaa86e310a5db738", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 71 | ComputeHmac(DIGEST_MD5, "Jefe", "what do ya want for nothing?")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 72 | EXPECT_EQ("56be34521d144c88dbb8c733f0e8b3f6", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 73 | ComputeHmac(DIGEST_MD5, std::string(16, '\xaa'), |
| 74 | std::string(50, '\xdd'))); |
| 75 | EXPECT_EQ( |
| 76 | "697eaf0aca3a3aea3a75164746ffaa79", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 77 | ComputeHmac(DIGEST_MD5, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 78 | "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
| 79 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", |
| 80 | std::string(50, '\xcd'))); |
| 81 | EXPECT_EQ( |
| 82 | "56461ef2342edc00f9bab995690efd4c", |
| 83 | ComputeHmac(DIGEST_MD5, std::string(16, '\x0c'), "Test With Truncation")); |
| 84 | EXPECT_EQ( |
| 85 | "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 86 | ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 87 | "Test Using Larger Than Block-Size Key - Hash Key First")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 88 | EXPECT_EQ("6f630fad67cda0ee1fb1f562db3aa53e", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 89 | ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'), |
| 90 | "Test Using Larger Than Block-Size Key and Larger " |
| 91 | "Than One Block-Size Data")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 92 | |
| 93 | // Test the raw buffer versions of the APIs; also check output buffer size. |
| 94 | std::string key(16, '\x0b'); |
| 95 | std::string input("Hi There"); |
| 96 | char output[16]; |
| 97 | EXPECT_EQ(sizeof(output), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | ComputeHmac(DIGEST_MD5, key.c_str(), key.size(), input.c_str(), |
| 99 | input.size(), output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 100 | EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 101 | hex_encode(output, sizeof(output))); |
| 102 | EXPECT_EQ(0U, ComputeHmac(DIGEST_MD5, key.c_str(), key.size(), input.c_str(), |
| 103 | input.size(), output, sizeof(output) - 1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Test vectors from RFC 2202. |
| 107 | TEST(MessageDigestTest, TestSha1Hmac) { |
| 108 | // Test the string versions of the APIs. |
| 109 | EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 110 | ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0b'), "Hi There")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 111 | EXPECT_EQ("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 112 | ComputeHmac(DIGEST_SHA_1, "Jefe", "what do ya want for nothing?")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 113 | EXPECT_EQ("125d7342b9ac11cd91a39af48aa17b4f63f175d3", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | ComputeHmac(DIGEST_SHA_1, std::string(20, '\xaa'), |
| 115 | std::string(50, '\xdd'))); |
| 116 | EXPECT_EQ( |
| 117 | "4c9007f4026250c6bc8414f9bf50c86c2d7235da", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 118 | ComputeHmac(DIGEST_SHA_1, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 119 | "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" |
| 120 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", |
| 121 | std::string(50, '\xcd'))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 122 | EXPECT_EQ("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 123 | ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0c'), |
| 124 | "Test With Truncation")); |
| 125 | EXPECT_EQ( |
| 126 | "aa4ae5e15272d00e95705637ce8a3b55ed402112", |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 127 | ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 128 | "Test Using Larger Than Block-Size Key - Hash Key First")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 129 | EXPECT_EQ("e8e99d0f45237d786d6bbaa7965c7808bbff1a91", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 130 | ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'), |
| 131 | "Test Using Larger Than Block-Size Key and Larger " |
| 132 | "Than One Block-Size Data")); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 133 | |
| 134 | // Test the raw buffer versions of the APIs; also check output buffer size. |
| 135 | std::string key(20, '\x0b'); |
| 136 | std::string input("Hi There"); |
| 137 | char output[20]; |
| 138 | EXPECT_EQ(sizeof(output), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 139 | ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(), input.c_str(), |
| 140 | input.size(), output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 141 | EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00", |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 142 | hex_encode(output, sizeof(output))); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 143 | EXPECT_EQ(0U, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 144 | ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(), input.c_str(), |
| 145 | input.size(), output, sizeof(output) - 1)); |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | TEST(MessageDigestTest, TestBadHmac) { |
| 149 | std::string output; |
| 150 | EXPECT_FALSE(ComputeHmac("sha-9000", "key", "abc", &output)); |
| 151 | EXPECT_EQ("", ComputeHmac("sha-9000", "key", "abc")); |
| 152 | } |
| 153 | |
| 154 | } // namespace rtc |