blob: 85e9bbde53a91eefb87757d3f5b551939397df6e [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
Steve Anton10542f22019-01-11 09:11:00 -080011#include "rtc_base/message_digest.h"
Yves Gerey3e707812018-11-28 16:47:49 +010012
Steve Anton10542f22019-01-11 09:11:00 -080013#include "rtc_base/string_encode.h"
Yves Gerey3e707812018-11-28 16:47:49 +010014#include "test/gtest.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
16namespace rtc {
17
18// Test vectors from RFC 1321.
19TEST(MessageDigestTest, TestMd5Digest) {
20 // Test the string versions of the APIs.
Yves Gerey665174f2018-06-19 15:03:05 +020021 EXPECT_EQ("d41d8cd98f00b204e9800998ecf8427e", ComputeDigest(DIGEST_MD5, ""));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022 EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72",
Yves Gerey665174f2018-06-19 15:03:05 +020023 ComputeDigest(DIGEST_MD5, "abc"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024 EXPECT_EQ("c3fcd3d76192e4007dfb496cca67e13b",
Yves Gerey665174f2018-06-19 15:03:05 +020025 ComputeDigest(DIGEST_MD5, "abcdefghijklmnopqrstuvwxyz"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026
27 // Test the raw buffer versions of the APIs; also check output buffer size.
28 char output[16];
29 EXPECT_EQ(sizeof(output),
Yves Gerey665174f2018-06-19 15:03:05 +020030 ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000031 EXPECT_EQ("900150983cd24fb0d6963f7d28e17f72",
Yves Gerey665174f2018-06-19 15:03:05 +020032 hex_encode(output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033 EXPECT_EQ(0U,
Yves Gerey665174f2018-06-19 15:03:05 +020034 ComputeDigest(DIGEST_MD5, "abc", 3, output, sizeof(output) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035}
36
37// Test vectors from RFC 3174.
38TEST(MessageDigestTest, TestSha1Digest) {
39 // Test the string versions of the APIs.
40 EXPECT_EQ("da39a3ee5e6b4b0d3255bfef95601890afd80709",
Yves Gerey665174f2018-06-19 15:03:05 +020041 ComputeDigest(DIGEST_SHA_1, ""));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
Yves Gerey665174f2018-06-19 15:03:05 +020043 ComputeDigest(DIGEST_SHA_1, "abc"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000044 EXPECT_EQ("84983e441c3bd26ebaae4aa1f95129e5e54670f1",
Yves Gerey665174f2018-06-19 15:03:05 +020045 ComputeDigest(
46 DIGEST_SHA_1,
47 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048
49 // Test the raw buffer versions of the APIs; also check output buffer size.
50 char output[20];
51 EXPECT_EQ(sizeof(output),
Yves Gerey665174f2018-06-19 15:03:05 +020052 ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000053 EXPECT_EQ("a9993e364706816aba3e25717850c26c9cd0d89d",
Yves Gerey665174f2018-06-19 15:03:05 +020054 hex_encode(output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000055 EXPECT_EQ(0U,
Yves Gerey665174f2018-06-19 15:03:05 +020056 ComputeDigest(DIGEST_SHA_1, "abc", 3, output, sizeof(output) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000057}
58
59// Test that we fail properly if a bad digest algorithm is specified.
60TEST(MessageDigestTest, TestBadDigest) {
61 std::string output;
62 EXPECT_FALSE(ComputeDigest("sha-9000", "abc", &output));
63 EXPECT_EQ("", ComputeDigest("sha-9000", "abc"));
64}
65
66// Test vectors from RFC 2202.
67TEST(MessageDigestTest, TestMd5Hmac) {
68 // Test the string versions of the APIs.
69 EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d",
Yves Gerey665174f2018-06-19 15:03:05 +020070 ComputeHmac(DIGEST_MD5, std::string(16, '\x0b'), "Hi There"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000071 EXPECT_EQ("750c783e6ab0b503eaa86e310a5db738",
Yves Gerey665174f2018-06-19 15:03:05 +020072 ComputeHmac(DIGEST_MD5, "Jefe", "what do ya want for nothing?"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 EXPECT_EQ("56be34521d144c88dbb8c733f0e8b3f6",
Yves Gerey665174f2018-06-19 15:03:05 +020074 ComputeHmac(DIGEST_MD5, std::string(16, '\xaa'),
75 std::string(50, '\xdd')));
76 EXPECT_EQ(
77 "697eaf0aca3a3aea3a75164746ffaa79",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078 ComputeHmac(DIGEST_MD5,
Yves Gerey665174f2018-06-19 15:03:05 +020079 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
80 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
81 std::string(50, '\xcd')));
82 EXPECT_EQ(
83 "56461ef2342edc00f9bab995690efd4c",
84 ComputeHmac(DIGEST_MD5, std::string(16, '\x0c'), "Test With Truncation"));
85 EXPECT_EQ(
86 "6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000087 ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'),
Yves Gerey665174f2018-06-19 15:03:05 +020088 "Test Using Larger Than Block-Size Key - Hash Key First"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089 EXPECT_EQ("6f630fad67cda0ee1fb1f562db3aa53e",
Yves Gerey665174f2018-06-19 15:03:05 +020090 ComputeHmac(DIGEST_MD5, std::string(80, '\xaa'),
91 "Test Using Larger Than Block-Size Key and Larger "
92 "Than One Block-Size Data"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000093
94 // Test the raw buffer versions of the APIs; also check output buffer size.
95 std::string key(16, '\x0b');
96 std::string input("Hi There");
97 char output[16];
98 EXPECT_EQ(sizeof(output),
Yves Gerey665174f2018-06-19 15:03:05 +020099 ComputeHmac(DIGEST_MD5, key.c_str(), key.size(), input.c_str(),
100 input.size(), output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000101 EXPECT_EQ("9294727a3638bb1c13f48ef8158bfc9d",
Yves Gerey665174f2018-06-19 15:03:05 +0200102 hex_encode(output, sizeof(output)));
103 EXPECT_EQ(0U, ComputeHmac(DIGEST_MD5, key.c_str(), key.size(), input.c_str(),
104 input.size(), output, sizeof(output) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000105}
106
107// Test vectors from RFC 2202.
108TEST(MessageDigestTest, TestSha1Hmac) {
109 // Test the string versions of the APIs.
110 EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00",
Yves Gerey665174f2018-06-19 15:03:05 +0200111 ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0b'), "Hi There"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000112 EXPECT_EQ("effcdf6ae5eb2fa2d27416d5f184df9c259a7c79",
Yves Gerey665174f2018-06-19 15:03:05 +0200113 ComputeHmac(DIGEST_SHA_1, "Jefe", "what do ya want for nothing?"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000114 EXPECT_EQ("125d7342b9ac11cd91a39af48aa17b4f63f175d3",
Yves Gerey665174f2018-06-19 15:03:05 +0200115 ComputeHmac(DIGEST_SHA_1, std::string(20, '\xaa'),
116 std::string(50, '\xdd')));
117 EXPECT_EQ(
118 "4c9007f4026250c6bc8414f9bf50c86c2d7235da",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119 ComputeHmac(DIGEST_SHA_1,
Yves Gerey665174f2018-06-19 15:03:05 +0200120 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f"
121 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19",
122 std::string(50, '\xcd')));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123 EXPECT_EQ("4c1a03424b55e07fe7f27be1d58bb9324a9a5a04",
Yves Gerey665174f2018-06-19 15:03:05 +0200124 ComputeHmac(DIGEST_SHA_1, std::string(20, '\x0c'),
125 "Test With Truncation"));
126 EXPECT_EQ(
127 "aa4ae5e15272d00e95705637ce8a3b55ed402112",
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128 ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'),
Yves Gerey665174f2018-06-19 15:03:05 +0200129 "Test Using Larger Than Block-Size Key - Hash Key First"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130 EXPECT_EQ("e8e99d0f45237d786d6bbaa7965c7808bbff1a91",
Yves Gerey665174f2018-06-19 15:03:05 +0200131 ComputeHmac(DIGEST_SHA_1, std::string(80, '\xaa'),
132 "Test Using Larger Than Block-Size Key and Larger "
133 "Than One Block-Size Data"));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000134
135 // Test the raw buffer versions of the APIs; also check output buffer size.
136 std::string key(20, '\x0b');
137 std::string input("Hi There");
138 char output[20];
139 EXPECT_EQ(sizeof(output),
Yves Gerey665174f2018-06-19 15:03:05 +0200140 ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(), input.c_str(),
141 input.size(), output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000142 EXPECT_EQ("b617318655057264e28bc0b6fb378c8ef146be00",
Yves Gerey665174f2018-06-19 15:03:05 +0200143 hex_encode(output, sizeof(output)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000144 EXPECT_EQ(0U,
Yves Gerey665174f2018-06-19 15:03:05 +0200145 ComputeHmac(DIGEST_SHA_1, key.c_str(), key.size(), input.c_str(),
146 input.size(), output, sizeof(output) - 1));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000147}
148
149TEST(MessageDigestTest, TestBadHmac) {
150 std::string output;
151 EXPECT_FALSE(ComputeHmac("sha-9000", "key", "abc", &output));
152 EXPECT_EQ("", ComputeHmac("sha-9000", "key", "abc"));
153}
154
155} // namespace rtc