blob: 4e3da6e1863877fece7a4cc29040c365d5ade344 [file] [log] [blame]
Philip P. Moltmann4d3acf42017-03-20 11:05:52 -07001// Copyright (c) 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Originally from chromium's /src/base/md5_unittest.cc.
6
7#include "core/fdrm/crypto/fx_crypt.h"
8
9#include <memory>
10#include <string>
11
12#include "core/fxcrt/fx_basic.h"
13#include "testing/gtest/include/gtest/gtest.h"
14
15namespace {
16
17std::string CRYPT_ToBase16(const uint8_t* digest) {
18 static char const zEncode[] = "0123456789abcdef";
19 std::string ret;
20 ret.resize(32);
21 for (int i = 0, j = 0; i < 16; i++, j += 2) {
22 uint8_t a = digest[i];
23 ret[j] = zEncode[(a >> 4) & 0xf];
24 ret[j + 1] = zEncode[a & 0xf];
25 }
26 return ret;
27}
28
29std::string CRYPT_MD5String(const char* str) {
30 uint8_t digest[16];
31 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(str), strlen(str), digest);
32 return CRYPT_ToBase16(digest);
33}
34
35} // namespace
36
37TEST(FXCRYPT, CRYPT_ToBase16) {
38 uint8_t data[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
39 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
40
41 std::string actual = CRYPT_ToBase16(data);
42 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
43
44 EXPECT_EQ(expected, actual);
45}
46
47TEST(FXCRYPT, MD5GenerateEmtpyData) {
48 uint8_t digest[16];
49 const char data[] = "";
50 uint32_t length = static_cast<uint32_t>(strlen(data));
51
52 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(data), length, digest);
53
54 uint8_t expected[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
55 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
56
57 for (int i = 0; i < 16; ++i)
58 EXPECT_EQ(expected[i], digest[i]);
59}
60
61TEST(FXCRYPT, MD5GenerateOneByteData) {
62 uint8_t digest[16];
63 const char data[] = "a";
64 uint32_t length = static_cast<uint32_t>(strlen(data));
65
66 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(data), length, digest);
67
68 uint8_t expected[] = {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8,
69 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61};
70
71 for (int i = 0; i < 16; ++i)
72 EXPECT_EQ(expected[i], digest[i]);
73}
74
75TEST(FXCRYPT, MD5GenerateLongData) {
76 const uint32_t length = 10 * 1024 * 1024 + 1;
77 std::unique_ptr<char[]> data(new char[length]);
78
79 for (uint32_t i = 0; i < length; ++i)
80 data[i] = i & 0xFF;
81
82 uint8_t digest[16];
83 CRYPT_MD5Generate(reinterpret_cast<const uint8_t*>(data.get()), length,
84 digest);
85
86 uint8_t expected[] = {0x90, 0xbd, 0x6a, 0xd9, 0x0a, 0xce, 0xf5, 0xad,
87 0xaa, 0x92, 0x20, 0x3e, 0x21, 0xc7, 0xa1, 0x3e};
88
89 for (int i = 0; i < 16; ++i)
90 EXPECT_EQ(expected[i], digest[i]);
91}
92
93TEST(FXCRYPT, ContextWithEmptyData) {
94 CRYPT_md5_context ctx;
95 CRYPT_MD5Start(&ctx);
96
97 uint8_t digest[16];
98 CRYPT_MD5Finish(&ctx, digest);
99
100 uint8_t expected[] = {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04,
101 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e};
102
103 for (int i = 0; i < 16; ++i)
104 EXPECT_EQ(expected[i], digest[i]);
105}
106
107TEST(FXCRYPT, ContextWithLongData) {
108 CRYPT_md5_context ctx;
109 CRYPT_MD5Start(&ctx);
110
111 const uint32_t length = 10 * 1024 * 1024 + 1;
112 std::unique_ptr<uint8_t[]> data(new uint8_t[length]);
113
114 for (uint32_t i = 0; i < length; ++i)
115 data[i] = i & 0xFF;
116
117 uint32_t total = 0;
118 while (total < length) {
119 uint32_t len = 4097; // intentionally not 2^k.
120 if (len > length - total)
121 len = length - total;
122
123 CRYPT_MD5Update(&ctx, data.get() + total, len);
124 total += len;
125 }
126
127 EXPECT_EQ(length, total);
128
129 uint8_t digest[16];
130 CRYPT_MD5Finish(&ctx, digest);
131
132 uint8_t expected[] = {0x90, 0xbd, 0x6a, 0xd9, 0x0a, 0xce, 0xf5, 0xad,
133 0xaa, 0x92, 0x20, 0x3e, 0x21, 0xc7, 0xa1, 0x3e};
134
135 for (int i = 0; i < 16; ++i)
136 EXPECT_EQ(expected[i], digest[i]);
137}
138
139// Example data from http://www.ietf.org/rfc/rfc1321.txt A.5 Test Suite
140TEST(FXCRYPT, MD5StringTestSuite1) {
141 std::string actual = CRYPT_MD5String("");
142 std::string expected = "d41d8cd98f00b204e9800998ecf8427e";
143 EXPECT_EQ(expected, actual);
144}
145
146TEST(FXCRYPT, MD5StringTestSuite2) {
147 std::string actual = CRYPT_MD5String("a");
148 std::string expected = "0cc175b9c0f1b6a831c399e269772661";
149 EXPECT_EQ(expected, actual);
150}
151
152TEST(FXCRYPT, MD5StringTestSuite3) {
153 std::string actual = CRYPT_MD5String("abc");
154 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
155 EXPECT_EQ(expected, actual);
156}
157
158TEST(FXCRYPT, MD5StringTestSuite4) {
159 std::string actual = CRYPT_MD5String("message digest");
160 std::string expected = "f96b697d7cb7938d525a2f31aaf161d0";
161 EXPECT_EQ(expected, actual);
162}
163
164TEST(FXCRYPT, MD5StringTestSuite5) {
165 std::string actual = CRYPT_MD5String("abcdefghijklmnopqrstuvwxyz");
166 std::string expected = "c3fcd3d76192e4007dfb496cca67e13b";
167 EXPECT_EQ(expected, actual);
168}
169
170TEST(FXCRYPT, MD5StringTestSuite6) {
171 std::string actual = CRYPT_MD5String(
172 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
173 "abcdefghijklmnopqrstuvwxyz"
174 "0123456789");
175 std::string expected = "d174ab98d277d9f5a5611c2c9f419d9f";
176 EXPECT_EQ(expected, actual);
177}
178
179TEST(FXCRYPT, MD5StringTestSuite7) {
180 std::string actual = CRYPT_MD5String(
181 "12345678901234567890"
182 "12345678901234567890"
183 "12345678901234567890"
184 "12345678901234567890");
185 std::string expected = "57edf4a22be3c955ac49da2e2107b67a";
186 EXPECT_EQ(expected, actual);
187}
188
189TEST(FXCRYPT, ContextWithStringData) {
190 CRYPT_md5_context ctx;
191 CRYPT_MD5Start(&ctx);
192 CRYPT_MD5Update(&ctx, reinterpret_cast<const uint8_t*>("abc"), 3);
193
194 uint8_t digest[16];
195 CRYPT_MD5Finish(&ctx, digest);
196
197 std::string actual = CRYPT_ToBase16(digest);
198 std::string expected = "900150983cd24fb0d6963f7d28e17f72";
199 EXPECT_EQ(expected, actual);
200}
201
202TEST(FXCRYPT, Sha256TestB1) {
203 // Example B.1 from FIPS 180-2: one-block message.
204 const char* input = "abc";
205 const uint8_t expected[32] = {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
206 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
207 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
208 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad};
209 uint8_t actual[32];
210 CRYPT_SHA256Generate(reinterpret_cast<const uint8_t*>(input), strlen(input),
211 actual);
212 for (size_t i = 0; i < 32; ++i)
213 EXPECT_EQ(expected[i], actual[i]) << " at byte " << i;
214}
215
216TEST(FXCRYPT, Sha256TestB2) {
217 // Example B.2 from FIPS 180-2: multi-block message.
218 const char* input =
219 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
220 const uint8_t expected[32] = {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
221 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
222 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
223 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1};
224 uint8_t actual[32];
225 CRYPT_SHA256Generate(reinterpret_cast<const uint8_t*>(input), strlen(input),
226 actual);
227 for (size_t i = 0; i < 32; ++i)
228 EXPECT_EQ(expected[i], actual[i]) << " at byte " << i;
229}