Debian Codegraph @

lib/hcrypto/mdtest.c

raw
1 /*
2 * Copyright (c) 1995 - 2016 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <config.h>
35 #include <roken.h>
36
37 #define HC_DEPRECATED_CRYPTO
38
39 #ifdef KRB5
40 #include <krb5-types.h>
41 #endif
42 #include <md4.h>
43 #include <md5.h>
44 #include <sha.h>
45 #include <evp.h>
46
47 #define ONE_MILLION_A "one million a's"
48
49 struct hash_foo {
50 const char *name;
51 size_t psize;
52 size_t hsize;
53 int (*init)(void*);
54 int (*update)(void*, const void*, size_t);
55 int (*final)(void*, void*);
56 const EVP_MD * (*evp)(void);
57 } md4 = {
58 "MD4",
59 sizeof(MD4_CTX),
60 16,
61 (int (*)(void*))MD4_Init,
62 (int (*)(void*,const void*, size_t))MD4_Update,
63 (int (*)(void*, void*))MD4_Final,
64 EVP_md4
65 }, md5 = {
66 "MD5",
67 sizeof(MD5_CTX),
68 16,
69 (int (*)(void*))MD5_Init,
70 (int (*)(void*,const void*, size_t))MD5_Update,
71 (int (*)(void*, void*))MD5_Final,
72 EVP_md5
73 }, sha1 = {
74 "SHA-1",
75 sizeof(struct sha),
76 20,
77 (int (*)(void*))SHA1_Init,
78 (int (*)(void*,const void*, size_t))SHA1_Update,
79 (int (*)(void*, void*))SHA1_Final,
80 EVP_sha1
81 };
82 struct hash_foo sha256 = {
83 "SHA-256",
84 sizeof(SHA256_CTX),
85 32,
86 (int (*)(void*))SHA256_Init,
87 (int (*)(void*,const void*, size_t))SHA256_Update,
88 (int (*)(void*, void*))SHA256_Final,
89 EVP_sha256
90 };
91 struct hash_foo sha384 = {
92 "SHA-384",
93 sizeof(SHA384_CTX),
94 48,
95 (int (*)(void*))SHA384_Init,
96 (int (*)(void*,const void*, size_t))SHA384_Update,
97 (int (*)(void*, void*))SHA384_Final,
98 EVP_sha384
99 };
100 struct hash_foo sha512 = {
101 "SHA-512",
102 sizeof(SHA512_CTX),
103 64,
104 (int (*)(void*))SHA512_Init,
105 (int (*)(void*,const void*, size_t))SHA512_Update,
106 (int (*)(void*, void*))SHA512_Final,
107 EVP_sha512
108 };
109
110 struct test {
111 char *str;
112 unsigned char hash[64];
113 };
114
115 struct test md4_tests[] = {
116 {"",
117 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59,
118 0xd7, 0xe0, 0xc0, 0x89, 0xc0}},
119 {"a",
120 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05,
121 0xfb, 0xdb, 0xd6, 0xfb, 0x24}},
122 {"abc",
123 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d}},
124 {"message digest",
125 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b}},
126 {"abcdefghijklmnopqrstuvwxyz", {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9, }},
127 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
128 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4}},
129 {"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
130 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36, }},
131 {NULL, { 0x0 }}};
132
133 struct test md5_tests[] = {
134 {"", {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}},
135 {"a", {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61}},
136 {"abc", {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72}},
137 {"message digest", {0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0}},
138 {"abcdefghijklmnopqrstuvwxyz", {0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b}},
139 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f}},
140 {"12345678901234567890123456789012345678901234567890123456789012345678901234567890", {0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a}},
141 {NULL, { 0x0 }}};
142
143 struct test sha1_tests[] = {
144 { "abc",
145 {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
146 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C,
147 0x9C, 0xD0, 0xD8, 0x9D}},
148 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
149 {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
150 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
151 0xE5, 0x46, 0x70, 0xF1}},
152 { ONE_MILLION_A,
153 {0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4,
154 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31,
155 0x65, 0x34, 0x01, 0x6f}},
156 { NULL, { 0 } }
157 };
158
159 struct test sha256_tests[] = {
160 { "abc",
161 { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
162 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
163 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
164 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }},
165 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
166 { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
167 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
168 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
169 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }},
170 { ONE_MILLION_A,
171 {0xcd,0xc7,0x6e,0x5c, 0x99,0x14,0xfb,0x92,
172 0x81,0xa1,0xc7,0xe2, 0x84,0xd7,0x3e,0x67,
173 0xf1,0x80,0x9a,0x48, 0xa4,0x97,0x20,0x0e,
174 0x04,0x6d,0x39,0xcc, 0xc7,0x11,0x2c,0xd0 }},
175 { NULL, { 0 } }
176 };
177
178 struct test sha384_tests[] = {
179 { "abc",
180 { 0xcb,0x00,0x75,0x3f,0x45,0xa3,0x5e,0x8b,
181 0xb5,0xa0,0x3d,0x69,0x9a,0xc6,0x50,0x07,
182 0x27,0x2c,0x32,0xab,0x0e,0xde,0xd1,0x63,
183 0x1a,0x8b,0x60,0x5a,0x43,0xff,0x5b,0xed,
184 0x80,0x86,0x07,0x2b,0xa1,0xe7,0xcc,0x23,
185 0x58,0xba,0xec,0xa1,0x34,0xc8,0x25,0xa7}},
186 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
187 "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
188 { 0x09,0x33,0x0c,0x33,0xf7,0x11,0x47,0xe8,
189 0x3d,0x19,0x2f,0xc7,0x82,0xcd,0x1b,0x47,
190 0x53,0x11,0x1b,0x17,0x3b,0x3b,0x05,0xd2,
191 0x2f,0xa0,0x80,0x86,0xe3,0xb0,0xf7,0x12,
192 0xfc,0xc7,0xc7,0x1a,0x55,0x7e,0x2d,0xb9,
193 0x66,0xc3,0xe9,0xfa,0x91,0x74,0x60,0x39}},
194 { ONE_MILLION_A,
195 { 0x9d,0x0e,0x18,0x09,0x71,0x64,0x74,0xcb,
196 0x08,0x6e,0x83,0x4e,0x31,0x0a,0x4a,0x1c,
197 0xed,0x14,0x9e,0x9c,0x00,0xf2,0x48,0x52,
198 0x79,0x72,0xce,0xc5,0x70,0x4c,0x2a,0x5b,
199 0x07,0xb8,0xb3,0xdc,0x38,0xec,0xc4,0xeb,
200 0xae,0x97,0xdd,0xd8,0x7f,0x3d,0x89,0x85}},
201 {NULL, { 0 }}
202 };
203
204 struct test sha512_tests[] = {
205 { "abc",
206 { 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
207 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
208 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
209 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
210 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
211 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
212 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
213 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f }},
214 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
215 "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
216 { 0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda,
217 0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f,
218 0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1,
219 0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18,
220 0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4,
221 0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a,
222 0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54,
223 0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09 }},
224 { ONE_MILLION_A,
225 { 0xe7,0x18,0x48,0x3d,0x0c,0xe7,0x69,0x64,
226 0x4e,0x2e,0x42,0xc7,0xbc,0x15,0xb4,0x63,
227 0x8e,0x1f,0x98,0xb1,0x3b,0x20,0x44,0x28,
228 0x56,0x32,0xa8,0x03,0xaf,0xa9,0x73,0xeb,
229 0xde,0x0f,0xf2,0x44,0x87,0x7e,0xa6,0x0a,
230 0x4c,0xb0,0x43,0x2c,0xe5,0x77,0xc3,0x1b,
231 0xeb,0x00,0x9c,0x5c,0x2c,0x49,0xaa,0x2e,
232 0x4e,0xad,0xb2,0x17,0xad,0x8c,0xc0,0x9b }},
233 { NULL, { 0 } }
234 };
235
236 static int
237 hash_test (struct hash_foo *hash, struct test *tests)
238 {
239 struct test *t;
240 EVP_MD_CTX *ectx;
241 unsigned int esize;
242 void *ctx = malloc(hash->psize);
243 unsigned char *res = malloc(hash->hsize);
244
245 printf ("%s... ", hash->name);
246 for (t = tests; t->str; ++t) {
247 char buf[1000];
248
249 ectx = EVP_MD_CTX_create();
250 if (hash->evp() == NULL) {
251 printf("unavailable\n");
252 continue;
253 }
254 EVP_DigestInit_ex(ectx, hash->evp(), NULL);
255
256 (*hash->init)(ctx);
257 if(strcmp(t->str, ONE_MILLION_A) == 0) {
258 int i;
259 memset(buf, 'a', sizeof(buf));
260 for(i = 0; i < 1000; i++) {
261 (*hash->update)(ctx, buf, sizeof(buf));
262 EVP_DigestUpdate(ectx, buf, sizeof(buf));
263 }
264 } else {
265 (*hash->update)(ctx, (unsigned char *)t->str, strlen(t->str));
266 EVP_DigestUpdate(ectx, t->str, strlen(t->str));
267 }
268
269 (*hash->final) (res, ctx);
270 if (memcmp (res, t->hash, hash->hsize) != 0) {
271 int i;
272
273 printf ("%s(\"%s\") failed\n", hash->name, t->str);
274 printf("should be: ");
275 for(i = 0; i < hash->hsize; ++i) {
276 if(i > 0 && (i % 16) == 0)
277 printf("\n ");
278 printf("%02x ", t->hash[i]);
279 }
280 printf("\nresult was: ");
281 for(i = 0; i < hash->hsize; ++i) {
282 if(i > 0 && (i % 16) == 0)
283 printf("\n ");
284 printf("%02x ", res[i]);
285 }
286 printf("\n");
287 return 1;
288 }
289
290 EVP_DigestFinal_ex(ectx, res, &esize);
291 EVP_MD_CTX_destroy(ectx);
292
293 if (hash->hsize != esize) {
294 printf("EVP %s returned wrong hash size\n", hash->name);
295 return 1;
296 }
297
298 if (memcmp (res, t->hash, hash->hsize) != 0) {
299 printf("EVP %s failed here old function where successful!\n",
300 hash->name);
301 return 1;
302 }
303 }
304 free(ctx);
305 free(res);
306 printf ("success\n");
307 return 0;
308 }
309
310 int
311 main (void)
312 {
313 return
314 hash_test(&md4, md4_tests) +
315 hash_test(&md5, md5_tests) +
316 hash_test(&sha1, sha1_tests) +
317 hash_test(&sha256, sha256_tests) +
318 hash_test(&sha384, sha384_tests) +
319 hash_test(&sha512, sha512_tests);
320 }