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