Use EVP_MD_CTX_create()/EVP_MD_CTX_destroy().
[heimdal.git] / lib / hcrypto / mdtest.c
blobe8da78439c91138e626b64806d438c564fc943c9
1 /*
2 * Copyright (c) 1995 - 2002 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.
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.
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.
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.
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 RCSID("$Id$");
37 #endif
39 #include <stdio.h>
40 #ifdef HAVE_STDLIB_H
41 #include <stdlib.h>
42 #endif
43 #include <string.h>
44 #ifdef KRB5
45 #include <krb5-types.h>
46 #endif
47 #include <md2.h>
48 #include <md4.h>
49 #include <md5.h>
50 #include <sha.h>
51 #include <evp.h>
53 #define ONE_MILLION_A "one million a's"
55 struct hash_foo {
56 const char *name;
57 size_t psize;
58 size_t hsize;
59 void (*init)(void*);
60 void (*update)(void*, const void*, size_t);
61 void (*final)(void*, void*);
62 const EVP_MD * (*evp)(void);
63 } md2 = {
64 "MD2",
65 sizeof(MD2_CTX),
66 16,
67 (void (*)(void*))MD2_Init,
68 (void (*)(void*,const void*, size_t))MD2_Update,
69 (void (*)(void*, void*))MD2_Final,
70 EVP_md2
71 }, md4 = {
72 "MD4",
73 sizeof(MD4_CTX),
74 16,
75 (void (*)(void*))MD4_Init,
76 (void (*)(void*,const void*, size_t))MD4_Update,
77 (void (*)(void*, void*))MD4_Final,
78 EVP_md4
79 }, md5 = {
80 "MD5",
81 sizeof(MD5_CTX),
82 16,
83 (void (*)(void*))MD5_Init,
84 (void (*)(void*,const void*, size_t))MD5_Update,
85 (void (*)(void*, void*))MD5_Final,
86 EVP_md5
87 }, sha1 = {
88 "SHA-1",
89 sizeof(struct sha),
90 20,
91 (void (*)(void*))SHA1_Init,
92 (void (*)(void*,const void*, size_t))SHA1_Update,
93 (void (*)(void*, void*))SHA1_Final,
94 EVP_sha1
96 struct hash_foo sha256 = {
97 "SHA-256",
98 sizeof(SHA256_CTX),
99 32,
100 (void (*)(void*))SHA256_Init,
101 (void (*)(void*,const void*, size_t))SHA256_Update,
102 (void (*)(void*, void*))SHA256_Final,
103 EVP_sha256
105 #ifdef HAVE_SHA384
106 struct hash_foo sha384 = {
107 "SHA-384",
108 sizeof(struct sha512),
110 (void (*)(void*))SHA384_Init,
111 (void (*)(void*,const void*, size_t))SHA384_Update,
112 (void (*)(void*, void*))SHA384_Final
114 #endif
115 #ifdef HAVE_SHA512
116 struct hash_foo sha512 = {
117 "SHA-512",
118 sizeof(struct sha512),
120 (void (*)(void*))SHA512_Init,
121 (void (*)(void*,const void*, size_t))SHA512_Update,
122 (void (*)(void*, void*))SHA512_Final
124 #endif
126 struct test {
127 char *str;
128 unsigned char hash[64];
131 struct test md2_tests[] = {
132 {"",
133 "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69\x27\x73" },
134 {"a",
135 "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0\xb5\xd1" },
136 {"abc",
137 "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde\xd6\xbb" },
138 {"message digest",
139 "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe\x06\xb0" },
140 {"abcdefghijklmnopqrstuvwxyz",
141 "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47\x94\x0b" },
142 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
143 "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03\x38\xcd" },
144 {"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
145 "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3\xef\xd8" },
146 {NULL }
149 struct test md4_tests[] = {
150 {"",
151 {0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31, 0xb7, 0x3c, 0x59,
152 0xd7, 0xe0, 0xc0, 0x89, 0xc0}},
153 {"a",
154 {0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46, 0x24, 0x5e, 0x05,
155 0xfb, 0xdb, 0xd6, 0xfb, 0x24}},
156 {"abc",
157 {0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52, 0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d}},
158 {"message digest",
159 {0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8, 0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b}},
160 {"abcdefghijklmnopqrstuvwxyz", {0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd, 0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9, }},
161 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
162 {0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35, 0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4}},
163 {"12345678901234567890123456789012345678901234567890123456789012345678901234567890",
164 {0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19, 0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36, }},
165 {NULL, { 0x0 }}};
167 struct test md5_tests[] = {
168 {"", {0xd4, 0x1d, 0x8c, 0xd9, 0x8f, 0x00, 0xb2, 0x04, 0xe9, 0x80, 0x09, 0x98, 0xec, 0xf8, 0x42, 0x7e}},
169 {"a", {0x0c, 0xc1, 0x75, 0xb9, 0xc0, 0xf1, 0xb6, 0xa8, 0x31, 0xc3, 0x99, 0xe2, 0x69, 0x77, 0x26, 0x61}},
170 {"abc", {0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72}},
171 {"message digest", {0xf9, 0x6b, 0x69, 0x7d, 0x7c, 0xb7, 0x93, 0x8d, 0x52, 0x5a, 0x2f, 0x31, 0xaa, 0xf1, 0x61, 0xd0}},
172 {"abcdefghijklmnopqrstuvwxyz", {0xc3, 0xfc, 0xd3, 0xd7, 0x61, 0x92, 0xe4, 0x00, 0x7d, 0xfb, 0x49, 0x6c, 0xca, 0x67, 0xe1, 0x3b}},
173 {"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", {0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f}},
174 {"12345678901234567890123456789012345678901234567890123456789012345678901234567890", {0x57, 0xed, 0xf4, 0xa2, 0x2b, 0xe3, 0xc9, 0x55, 0xac, 0x49, 0xda, 0x2e, 0x21, 0x07, 0xb6, 0x7a}},
175 {NULL, { 0x0 }}};
177 struct test sha1_tests[] = {
178 { "abc",
179 {0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
180 0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C,
181 0x9C, 0xD0, 0xD8, 0x9D}},
182 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
183 {0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
184 0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
185 0xE5, 0x46, 0x70, 0xF1}},
186 { ONE_MILLION_A,
187 {0x34, 0xaa, 0x97, 0x3c, 0xd4, 0xc4, 0xda, 0xa4,
188 0xf6, 0x1e, 0xeb, 0x2b, 0xdb, 0xad, 0x27, 0x31,
189 0x65, 0x34, 0x01, 0x6f}},
190 { NULL }
193 struct test sha256_tests[] = {
194 { "abc",
195 { 0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea,
196 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23,
197 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c,
198 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad }},
199 { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
200 { 0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8,
201 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39,
202 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67,
203 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1 }},
204 { ONE_MILLION_A,
205 {0xcd,0xc7,0x6e,0x5c, 0x99,0x14,0xfb,0x92,
206 0x81,0xa1,0xc7,0xe2, 0x84,0xd7,0x3e,0x67,
207 0xf1,0x80,0x9a,0x48, 0xa4,0x97,0x20,0x0e,
208 0x04,0x6d,0x39,0xcc, 0xc7,0x11,0x2c,0xd0 }},
209 { NULL }
212 #ifdef HAVE_SHA384
213 struct test sha384_tests[] = {
214 { "abc",
215 { 0xcb,0x00,0x75,0x3f,0x45,0xa3,0x5e,0x8b,
216 0xb5,0xa0,0x3d,0x69,0x9a,0xc6,0x50,0x07,
217 0x27,0x2c,0x32,0xab,0x0e,0xde,0xd1,0x63,
218 0x1a,0x8b,0x60,0x5a,0x43,0xff,0x5b,0xed,
219 0x80,0x86,0x07,0x2b,0xa1,0xe7,0xcc,0x23,
220 0x58,0xba,0xec,0xa1,0x34,0xc8,0x25,0xa7}},
221 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
222 "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
223 { 0x09,0x33,0x0c,0x33,0xf7,0x11,0x47,0xe8,
224 0x3d,0x19,0x2f,0xc7,0x82,0xcd,0x1b,0x47,
225 0x53,0x11,0x1b,0x17,0x3b,0x3b,0x05,0xd2,
226 0x2f,0xa0,0x80,0x86,0xe3,0xb0,0xf7,0x12,
227 0xfc,0xc7,0xc7,0x1a,0x55,0x7e,0x2d,0xb9,
228 0x66,0xc3,0xe9,0xfa,0x91,0x74,0x60,0x39}},
229 { ONE_MILLION_A,
230 { 0x9d,0x0e,0x18,0x09,0x71,0x64,0x74,0xcb,
231 0x08,0x6e,0x83,0x4e,0x31,0x0a,0x4a,0x1c,
232 0xed,0x14,0x9e,0x9c,0x00,0xf2,0x48,0x52,
233 0x79,0x72,0xce,0xc5,0x70,0x4c,0x2a,0x5b,
234 0x07,0xb8,0xb3,0xdc,0x38,0xec,0xc4,0xeb,
235 0xae,0x97,0xdd,0xd8,0x7f,0x3d,0x89,0x85}},
236 {NULL}
238 #endif
239 #ifdef HAVE_SHA512
240 struct test sha512_tests[] = {
241 { "abc",
242 { 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba,
243 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31,
244 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2,
245 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a,
246 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8,
247 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd,
248 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e,
249 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f }},
250 { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"
251 "ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
252 { 0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda,
253 0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f,
254 0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1,
255 0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18,
256 0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4,
257 0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a,
258 0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54,
259 0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09 }},
260 { ONE_MILLION_A,
261 { 0xe7,0x18,0x48,0x3d,0x0c,0xe7,0x69,0x64,
262 0x4e,0x2e,0x42,0xc7,0xbc,0x15,0xb4,0x63,
263 0x8e,0x1f,0x98,0xb1,0x3b,0x20,0x44,0x28,
264 0x56,0x32,0xa8,0x03,0xaf,0xa9,0x73,0xeb,
265 0xde,0x0f,0xf2,0x44,0x87,0x7e,0xa6,0x0a,
266 0x4c,0xb0,0x43,0x2c,0xe5,0x77,0xc3,0x1b,
267 0xeb,0x00,0x9c,0x5c,0x2c,0x49,0xaa,0x2e,
268 0x4e,0xad,0xb2,0x17,0xad,0x8c,0xc0,0x9b }},
269 { NULL }
271 #endif
273 static int
274 hash_test (struct hash_foo *hash, struct test *tests)
276 struct test *t;
277 EVP_MD_CTX *ectx;
278 unsigned int esize;
279 void *ctx = malloc(hash->psize);
280 unsigned char *res = malloc(hash->hsize);
282 printf ("%s... ", hash->name);
283 for (t = tests; t->str; ++t) {
284 char buf[1000];
286 ectx = EVP_MD_CTX_create();
287 EVP_DigestInit_ex(ectx, hash->evp(), NULL);
289 (*hash->init)(ctx);
290 if(strcmp(t->str, ONE_MILLION_A) == 0) {
291 int i;
292 memset(buf, 'a', sizeof(buf));
293 for(i = 0; i < 1000; i++) {
294 (*hash->update)(ctx, buf, sizeof(buf));
295 EVP_DigestUpdate(ectx, buf, sizeof(buf));
297 } else {
298 (*hash->update)(ctx, (unsigned char *)t->str, strlen(t->str));
299 EVP_DigestUpdate(ectx, t->str, strlen(t->str));
302 (*hash->final) (res, ctx);
303 if (memcmp (res, t->hash, hash->hsize) != 0) {
304 int i;
306 printf ("%s(\"%s\") failed\n", hash->name, t->str);
307 printf("should be: ");
308 for(i = 0; i < hash->hsize; ++i) {
309 if(i > 0 && (i % 16) == 0)
310 printf("\n ");
311 printf("%02x ", t->hash[i]);
313 printf("\nresult was: ");
314 for(i = 0; i < hash->hsize; ++i) {
315 if(i > 0 && (i % 16) == 0)
316 printf("\n ");
317 printf("%02x ", res[i]);
319 printf("\n");
320 return 1;
323 EVP_DigestFinal_ex(ectx, res, &esize);
324 EVP_MD_CTX_destroy(ectx);
326 if (hash->hsize != esize) {
327 printf("EVP %s returned wrong hash size\n", hash->name);
328 return 1;
331 if (memcmp (res, t->hash, hash->hsize) != 0) {
332 printf("EVP %s failed here old function where successful!\n",
333 hash->name);
334 return 1;
337 free(ctx);
338 free(res);
339 printf ("success\n");
340 return 0;
344 main (void)
346 return
347 hash_test(&md2, md2_tests) +
348 hash_test(&md4, md4_tests) +
349 hash_test(&md5, md5_tests) +
350 hash_test(&sha1, sha1_tests)
351 + hash_test(&sha256, sha256_tests)
352 #ifdef HAVE_SHA384
353 + hash_test(&sha384, sha384_tests)
354 #endif
355 #ifdef HAVE_SHA512
356 + hash_test(&sha512, sha512_tests)
357 #endif