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