1 /*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "talk/base/gunit.h"
29 #include "talk/base/messagedigest.h"
30 #include "talk/base/stringencode.h"
31
32 namespace talk_base {
33
34 // Test vectors from RFC 1321.
TEST(MessageDigestTest,TestMd5Digest)35 TEST(MessageDigestTest, TestMd5Digest) {
36 // Test the string versions of the APIs.
37 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e",
38 ComputeDigest(DIGEST_MD5, ""));
39 EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72",
40 ComputeDigest(DIGEST_MD5, "abc"));
41 EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b",
42 ComputeDigest(DIGEST_MD5, "abcdefghijklmnopqrstuvwxyz"));
43
44 // Test the raw buffer versions of the APIs; also check output buffer size.
45 char output[16];
46 EXPECT_EQ(sizeof(output),
47 ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output)));
48 EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72",
49 hex_encode(output, sizeof(output)));
50 EXPECT_EQ(0U,
51 ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output) - 1));
52 }
53
54 // Test vectors from RFC 3174.
TEST(MessageDigestTest,TestSha1Digest)55 TEST(MessageDigestTest, TestSha1Digest) {
56 // Test the string versions of the APIs.
57 EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709",
58 ComputeDigest(DIGEST_SHA_1, ""));
59 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
60 ComputeDigest(DIGEST_SHA_1, "abc"));
61 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
62 ComputeDigest(DIGEST_SHA_1,
63 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
64
65 // Test the raw buffer versions of the APIs; also check output buffer size.
66 char output[20];
67 EXPECT_EQ(sizeof(output),
68 ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output)));
69 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
70 hex_encode(output, sizeof(output)));
71 EXPECT_EQ(0U,
72 ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output) - 1));
73 }
74
75 // Test that we fail properly if a bad digest algorithm is specified.
TEST(MessageDigestTest,TestBadDigest)76 TEST(MessageDigestTest, TestBadDigest) {
77 std::string output;
78 EXPECT_FALSE(ComputeDigest("sha-9000", "abc", &output));
79 EXPECT_EQ("", ComputeDigest("sha-9000", "abc"));
80 }
81
82 // Test vectors from RFC 2202.
TEST(MessageDigestTest,TestMd5Hmac)83 TEST(MessageDigestTest, TestMd5Hmac) {
84 // Test the string versions of the APIs.
85 EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d",
86 ComputeHmac(DIGEST_MD5, std::string(16, '\x0b'), "Hi There"));
87 EXPECT_EQ("750c783e6ab0b503eaa86e310a5db738",
88 ComputeHmac(DIGEST_MD5, "Jefe", "what do ya want for nothing?"));
89 EXPECT_EQ("56be34521d144c88dbb8c733f0e8b3f6",
90 ComputeHmac(DIGEST_MD5, std::string(16, '\xaa'),
91 std::string(50, '\xdd')));
92 EXPECT_EQ("697eaf0aca3a3aea3a75164746ffaa79",
93 ComputeHmac(DIGEST_MD5,
94 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
95 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
96 std::string(50, '\xcd')));
97 EXPECT_EQ("56461ef2342edc00f9bab995690efd4c",
98 ComputeHmac(DIGEST_MD5, std::string(16, '\x0c'),
99 "Test With Truncation"));
100 EXPECT_EQ("6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
101 ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'),
102 "Test Using Larger Than Block-Size Key - Hash Key First"));
103 EXPECT_EQ("6f630fad67cda0ee1fb1f562db3aa53e",
104 ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'),
105 "Test Using Larger Than Block-Size Key and Larger "
106 "Than One Block-Size Data"));
107
108 // Test the raw buffer versions of the APIs; also check output buffer size.
109 std::string key(16, '\x0b');
110 std::string input("Hi There");
111 char output[16];
112 EXPECT_EQ(sizeof(output),
113 ComputeHmac(DIGEST_MD5, key.c_str(), key.size(),
114 input.c_str(), input.size(), output, sizeof(output)));
115 EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d",
116 hex_encode(output, sizeof(output)));
117 EXPECT_EQ(0U,
118 ComputeHmac(DIGEST_MD5, key.c_str(), key.size(),
119 input.c_str(), input.size(), output, sizeof(output) - 1));
120 }
121
122 // Test vectors from RFC 2202.
TEST(MessageDigestTest,TestSha1Hmac)123 TEST(MessageDigestTest, TestSha1Hmac) {
124 // Test the string versions of the APIs.
125 EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00",
126 ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0b'), "Hi There"));
127 EXPECT_EQ("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
128 ComputeHmac(DIGEST_SHA_1, "Jefe", "what do ya want for nothing?"));
129 EXPECT_EQ("125d7342b9ac11cd91a39af48aa17b4f63f175d3",
130 ComputeHmac(DIGEST_SHA_1, std::string(20, '\xaa'),
131 std::string(50, '\xdd')));
132 EXPECT_EQ("4c9007f4026250c6bc8414f9bf50c86c2d7235da",
133 ComputeHmac(DIGEST_SHA_1,
134 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
135 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
136 std::string(50, '\xcd')));
137 EXPECT_EQ("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
138 ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0c'),
139 "Test With Truncation"));
140 EXPECT_EQ("aa4ae5e15272d00e95705637ce8a3b55ed402112",
141 ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'),
142 "Test Using Larger Than Block-Size Key - Hash Key First"));
143 EXPECT_EQ("e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
144 ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'),
145 "Test Using Larger Than Block-Size Key and Larger "
146 "Than One Block-Size Data"));
147
148 // Test the raw buffer versions of the APIs; also check output buffer size.
149 std::string key(20, '\x0b');
150 std::string input("Hi There");
151 char output[20];
152 EXPECT_EQ(sizeof(output),
153 ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(),
154 input.c_str(), input.size(), output, sizeof(output)));
155 EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00",
156 hex_encode(output, sizeof(output)));
157 EXPECT_EQ(0U,
158 ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(),
159 input.c_str(), input.size(), output, sizeof(output) - 1));
160 }
161
TEST(MessageDigestTest,TestBadHmac)162 TEST(MessageDigestTest, TestBadHmac) {
163 std::string output;
164 EXPECT_FALSE(ComputeHmac("sha-9000", "key", "abc", &output));
165 EXPECT_EQ("", ComputeHmac("sha-9000", "key", "abc"));
166 }
167
168 } // namespace talk_base
169