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