1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include <memory>
20 
21 #include <openssl/asn1.h>
22 #include <openssl/crypto.h>
23 #include <openssl/digest.h>
24 #include <openssl/err.h>
25 #include <openssl/md4.h>
26 #include <openssl/md5.h>
27 #include <openssl/nid.h>
28 #include <openssl/obj.h>
29 #include <openssl/sha.h>
30 
31 #include "../internal.h"
32 
33 
34 struct MD {
35   // name is the name of the digest.
36   const char* name;
37   // md_func is the digest to test.
38   const EVP_MD *(*func)(void);
39   // one_shot_func is the convenience one-shot version of the
40   // digest.
41   uint8_t *(*one_shot_func)(const uint8_t *, size_t, uint8_t *);
42 };
43 
44 static const MD md4 = { "MD4", &EVP_md4, nullptr };
45 static const MD md5 = { "MD5", &EVP_md5, &MD5 };
46 static const MD sha1 = { "SHA1", &EVP_sha1, &SHA1 };
47 static const MD sha224 = { "SHA224", &EVP_sha224, &SHA224 };
48 static const MD sha256 = { "SHA256", &EVP_sha256, &SHA256 };
49 static const MD sha384 = { "SHA384", &EVP_sha384, &SHA384 };
50 static const MD sha512 = { "SHA512", &EVP_sha512, &SHA512 };
51 static const MD md5_sha1 = { "MD5-SHA1", &EVP_md5_sha1, nullptr };
52 
53 struct TestVector {
54   // md is the digest to test.
55   const MD &md;
56   // input is a NUL-terminated string to hash.
57   const char *input;
58   // repeat is the number of times to repeat input.
59   size_t repeat;
60   // expected_hex is the expected digest in hexadecimal.
61   const char *expected_hex;
62 };
63 
64 static const TestVector kTestVectors[] = {
65     // MD4 tests, from RFC 1320. (crypto/md4 does not provide a
66     // one-shot MD4 function.)
67     { md4, "", 1, "31d6cfe0d16ae931b73c59d7e0c089c0" },
68     { md4, "a", 1, "bde52cb31de33e46245e05fbdbd6fb24" },
69     { md4, "abc", 1, "a448017aaf21d8525fc10ae87aa6729d" },
70     { md4, "message digest", 1, "d9130a8164549fe818874806e1c7014b" },
71     { md4, "abcdefghijklmnopqrstuvwxyz", 1,
72       "d79e1c308aa5bbcdeea8ed63df412da9" },
73     { md4,
74       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1,
75       "043f8582f241db351ce627e153e7f0e4" },
76     { md4, "1234567890", 8, "e33b4ddc9c38f2199c3e7b164fcc0536" },
77 
78     // MD5 tests, from RFC 1321.
79     { md5, "", 1, "d41d8cd98f00b204e9800998ecf8427e" },
80     { md5, "a", 1, "0cc175b9c0f1b6a831c399e269772661" },
81     { md5, "abc", 1, "900150983cd24fb0d6963f7d28e17f72" },
82     { md5, "message digest", 1, "f96b697d7cb7938d525a2f31aaf161d0" },
83     { md5, "abcdefghijklmnopqrstuvwxyz", 1,
84       "c3fcd3d76192e4007dfb496cca67e13b" },
85     { md5,
86       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 1,
87       "d174ab98d277d9f5a5611c2c9f419d9f" },
88     { md5, "1234567890", 8, "57edf4a22be3c955ac49da2e2107b67a" },
89 
90     // SHA-1 tests, from RFC 3174.
91     { sha1, "abc", 1, "a9993e364706816aba3e25717850c26c9cd0d89d" },
92     { sha1,
93       "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
94       "84983e441c3bd26ebaae4aa1f95129e5e54670f1" },
95     { sha1, "a", 1000000, "34aa973cd4c4daa4f61eeb2bdbad27316534016f" },
96     { sha1,
97       "0123456701234567012345670123456701234567012345670123456701234567", 10,
98       "dea356a2cddd90c7a7ecedc5ebb563934f460452" },
99 
100     // SHA-224 tests, from RFC 3874.
101     { sha224, "abc", 1,
102       "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" },
103     { sha224,
104       "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
105       "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" },
106     { sha224,
107       "a", 1000000,
108       "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" },
109 
110     // SHA-256 tests, from NIST.
111     { sha256, "abc", 1,
112       "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" },
113     { sha256,
114       "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 1,
115       "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" },
116 
117     // SHA-384 tests, from NIST.
118     { sha384, "abc", 1,
119       "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed"
120       "8086072ba1e7cc2358baeca134c825a7" },
121     { sha384,
122       "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
123       "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
124       "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712"
125       "fcc7c71a557e2db966c3e9fa91746039" },
126 
127     // SHA-512 tests, from NIST.
128     { sha512, "abc", 1,
129       "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
130       "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f" },
131     { sha512,
132       "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
133       "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 1,
134       "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
135       "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909" },
136 
137     // MD5-SHA1 tests.
138     { md5_sha1, "abc", 1,
139       "900150983cd24fb0d6963f7d28e17f72a9993e364706816aba3e25717850c26c9cd0d89d" },
140 };
141 
CompareDigest(const TestVector * test,const uint8_t * digest,size_t digest_len)142 static bool CompareDigest(const TestVector *test,
143                           const uint8_t *digest,
144                           size_t digest_len) {
145   static const char kHexTable[] = "0123456789abcdef";
146   char digest_hex[2*EVP_MAX_MD_SIZE + 1];
147 
148   for (size_t i = 0; i < digest_len; i++) {
149     digest_hex[2*i] = kHexTable[digest[i] >> 4];
150     digest_hex[2*i + 1] = kHexTable[digest[i] & 0xf];
151   }
152   digest_hex[2*digest_len] = '\0';
153 
154   if (strcmp(digest_hex, test->expected_hex) != 0) {
155     fprintf(stderr, "%s(\"%s\" * %d) = %s; want %s\n",
156             test->md.name, test->input, (int)test->repeat,
157             digest_hex, test->expected_hex);
158     return false;
159   }
160 
161   return true;
162 }
163 
TestDigest(const TestVector * test)164 static int TestDigest(const TestVector *test) {
165   bssl::ScopedEVP_MD_CTX ctx;
166 
167   // Test the input provided.
168   if (!EVP_DigestInit_ex(ctx.get(), test->md.func(), NULL)) {
169     fprintf(stderr, "EVP_DigestInit_ex failed\n");
170     return false;
171   }
172   for (size_t i = 0; i < test->repeat; i++) {
173     if (!EVP_DigestUpdate(ctx.get(), test->input, strlen(test->input))) {
174       fprintf(stderr, "EVP_DigestUpdate failed\n");
175       return false;
176     }
177   }
178   std::unique_ptr<uint8_t[]> digest(new uint8_t[EVP_MD_size(test->md.func())]);
179   unsigned digest_len;
180   if (!EVP_DigestFinal_ex(ctx.get(), digest.get(), &digest_len)) {
181     fprintf(stderr, "EVP_DigestFinal_ex failed\n");
182     return false;
183   }
184   if (!CompareDigest(test, digest.get(), digest_len)) {
185     return false;
186   }
187 
188   // Test the input one character at a time.
189   if (!EVP_DigestInit_ex(ctx.get(), test->md.func(), NULL)) {
190     fprintf(stderr, "EVP_DigestInit_ex failed\n");
191     return false;
192   }
193   if (!EVP_DigestUpdate(ctx.get(), NULL, 0)) {
194     fprintf(stderr, "EVP_DigestUpdate failed\n");
195     return false;
196   }
197   for (size_t i = 0; i < test->repeat; i++) {
198     for (const char *p = test->input; *p; p++) {
199       if (!EVP_DigestUpdate(ctx.get(), p, 1)) {
200         fprintf(stderr, "EVP_DigestUpdate failed\n");
201         return false;
202       }
203     }
204   }
205   if (!EVP_DigestFinal_ex(ctx.get(), digest.get(), &digest_len)) {
206     fprintf(stderr, "EVP_DigestFinal_ex failed\n");
207     return false;
208   }
209   if (digest_len != EVP_MD_size(test->md.func())) {
210     fprintf(stderr, "EVP_MD_size output incorrect\n");
211     return false;
212   }
213   if (!CompareDigest(test, digest.get(), digest_len)) {
214     return false;
215   }
216 
217   // Test the one-shot function.
218   if (test->md.one_shot_func && test->repeat == 1) {
219     uint8_t *out = test->md.one_shot_func((const uint8_t *)test->input,
220                                           strlen(test->input), digest.get());
221     if (out != digest.get()) {
222       fprintf(stderr, "one_shot_func gave incorrect return\n");
223       return false;
224     }
225     if (!CompareDigest(test, digest.get(), EVP_MD_size(test->md.func()))) {
226       return false;
227     }
228 
229     // Test the deprecated static buffer variant, until it's removed.
230     out = test->md.one_shot_func((const uint8_t *)test->input,
231                                  strlen(test->input), NULL);
232     if (!CompareDigest(test, out, EVP_MD_size(test->md.func()))) {
233       return false;
234     }
235   }
236 
237   return true;
238 }
239 
TestGetters()240 static int TestGetters() {
241   if (EVP_get_digestbyname("RSA-SHA512") != EVP_sha512() ||
242       EVP_get_digestbyname("sha512WithRSAEncryption") != EVP_sha512() ||
243       EVP_get_digestbyname("nonsense") != NULL ||
244       EVP_get_digestbyname("SHA512") != EVP_sha512() ||
245       EVP_get_digestbyname("sha512") != EVP_sha512()) {
246     return false;
247   }
248 
249   if (EVP_get_digestbynid(NID_sha512) != EVP_sha512() ||
250       EVP_get_digestbynid(NID_sha512WithRSAEncryption) != NULL ||
251       EVP_get_digestbynid(NID_undef) != NULL) {
252     return false;
253   }
254 
255   bssl::UniquePtr<ASN1_OBJECT> obj(OBJ_txt2obj("1.3.14.3.2.26", 0));
256   if (!obj ||
257       EVP_get_digestbyobj(obj.get()) != EVP_sha1() ||
258       EVP_get_digestbyobj(OBJ_nid2obj(NID_md5_sha1)) != EVP_md5_sha1() ||
259       EVP_get_digestbyobj(OBJ_nid2obj(NID_sha1)) != EVP_sha1()) {
260     return false;
261   }
262 
263   return true;
264 }
265 
main()266 int main() {
267   CRYPTO_library_init();
268 
269   for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTestVectors); i++) {
270     if (!TestDigest(&kTestVectors[i])) {
271       fprintf(stderr, "Test %d failed\n", (int)i);
272       return 1;
273     }
274   }
275 
276   if (!TestGetters()) {
277     return 1;
278   }
279 
280   printf("PASS\n");
281   return 0;
282 }
283