Make no-ec2m work again.
[oweals/openssl.git] / fips / sha / fips_shatest.c
1 /* fips_shatest.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2005.
4  */
5 /* ====================================================================
6  * Copyright (c) 2005 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #define OPENSSL_FIPSAPI
60
61 #include <stdio.h>
62 #include <ctype.h>
63 #include <string.h>
64 #include <openssl/bio.h>
65 #include <openssl/evp.h>
66 #include <openssl/err.h>
67 #include <openssl/bn.h>
68
69 #ifndef OPENSSL_FIPS
70
71 int main(int argc, char *argv[])
72 {
73     printf("No FIPS SHAXXX support\n");
74     return(0);
75 }
76
77 #else
78
79 #include <openssl/fips.h>
80
81 #include "fips_utl.h"
82
83 static int dgst_test(FILE *out, FILE *in);
84 static int print_dgst(const EVP_MD *md, FILE *out,
85                 unsigned char *Msg, int Msglen);
86 static int print_monte(const EVP_MD *md, FILE *out,
87                 unsigned char *Seed, int SeedLen);
88
89 int main(int argc, char **argv)
90         {
91         FILE *in = NULL, *out = NULL;
92
93         int ret = 1;
94         fips_set_error_print();
95         if(!FIPS_mode_set(1))
96                 goto end;
97
98         if (argc == 1)
99                 in = stdin;
100         else
101                 in = fopen(argv[1], "r");
102
103         if (argc < 2)
104                 out = stdout;
105         else
106                 out = fopen(argv[2], "w");
107
108         if (!in)
109                 {
110                 fprintf(stderr, "FATAL input initialization error\n");
111                 goto end;
112                 }
113
114         if (!out)
115                 {
116                 fprintf(stderr, "FATAL output initialization error\n");
117                 goto end;
118                 }
119
120         if (!dgst_test(out, in))
121                 {
122                 fprintf(stderr, "FATAL digest file processing error\n");
123                 goto end;
124                 }
125         else
126                 ret = 0;
127
128         end:
129
130         if (in && (in != stdin))
131                 fclose(in);
132         if (out && (out != stdout))
133                 fclose(out);
134
135         return ret;
136
137         }
138
139 #define SHA_TEST_MAX_BITS       102400
140 #define SHA_TEST_MAXLINELEN     (((SHA_TEST_MAX_BITS >> 3) * 2) + 100)
141
142 int dgst_test(FILE *out, FILE *in)
143         {
144         const EVP_MD *md = NULL;
145         char *linebuf, *olinebuf, *p, *q;
146         char *keyword, *value;
147         unsigned char *Msg = NULL, *Seed = NULL;
148         long MsgLen = -1, Len = -1, SeedLen = -1;
149         int ret = 0;
150         int lnum = 0;
151
152         olinebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
153         linebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
154
155         if (!linebuf || !olinebuf)
156                 goto error;
157
158
159         while (fgets(olinebuf, SHA_TEST_MAXLINELEN, in))
160                 {
161                 lnum++;
162                 strcpy(linebuf, olinebuf);
163                 keyword = linebuf;
164                 /* Skip leading space */
165                 while (isspace((unsigned char)*keyword))
166                         keyword++;
167
168                 /* Look for = sign */
169                 p = strchr(linebuf, '=');
170
171                 /* If no = or starts with [ (for [L=20] line) just copy */
172                 if (!p)
173                         {
174                         fputs(olinebuf, out);
175                         continue;
176                         }
177
178                 q = p - 1;
179
180                 /* Remove trailing space */
181                 while (isspace((unsigned char)*q))
182                         *q-- = 0;
183
184                 *p = 0;
185                 value = p + 1;
186
187                 /* Remove leading space from value */
188                 while (isspace((unsigned char)*value))
189                         value++;
190
191                 /* Remove trailing space from value */
192                 p = value + strlen(value) - 1;
193                 while (*p == '\n' || isspace((unsigned char)*p))
194                         *p-- = 0;
195
196                 if (!strcmp(keyword,"[L") && *p==']')
197                         {
198                         switch (atoi(value))
199                                 {
200                                 case 20: md=EVP_sha1();   break;
201                                 case 28: md=EVP_sha224(); break;
202                                 case 32: md=EVP_sha256(); break;
203                                 case 48: md=EVP_sha384(); break;
204                                 case 64: md=EVP_sha512(); break;
205                                 default: goto parse_error;
206                                 }
207                         }
208                 else if (!strcmp(keyword, "Len"))
209                         {
210                         if (Len != -1)
211                                 goto parse_error;
212                         Len = atoi(value);
213                         if (Len < 0)
214                                 goto parse_error;
215                         /* Only handle multiples of 8 bits */
216                         if (Len & 0x7)
217                                 goto parse_error;
218                         if (Len > SHA_TEST_MAX_BITS)
219                                 goto parse_error;
220                         MsgLen = Len >> 3;
221                         }
222
223                 else if (!strcmp(keyword, "Msg"))
224                         {
225                         long tmplen;
226                         if (strlen(value) & 1)
227                                 *(--value) = '0';
228                         if (Msg)
229                                 goto parse_error;
230                         Msg = hex2bin_m(value, &tmplen);
231                         if (!Msg)
232                                 goto parse_error;
233                         }
234                 else if (!strcmp(keyword, "Seed"))
235                         {
236                         if (strlen(value) & 1)
237                                 *(--value) = '0';
238                         if (Seed)
239                                 goto parse_error;
240                         Seed = hex2bin_m(value, &SeedLen);
241                         if (!Seed)
242                                 goto parse_error;
243                         }
244                 else if (!strcmp(keyword, "MD"))
245                         continue;
246                 else
247                         goto parse_error;
248
249                 fputs(olinebuf, out);
250
251                 if (md && Msg && (MsgLen >= 0))
252                         {
253                         if (!print_dgst(md, out, Msg, MsgLen))
254                                 goto error;
255                         OPENSSL_free(Msg);
256                         Msg = NULL;
257                         MsgLen = -1;
258                         Len = -1;
259                         }
260                 else if (md && Seed && (SeedLen > 0))
261                         {
262                         if (!print_monte(md, out, Seed, SeedLen))
263                                 goto error;
264                         OPENSSL_free(Seed);
265                         Seed = NULL;
266                         SeedLen = -1;
267                         }
268         
269
270                 }
271
272
273         ret = 1;
274
275
276         error:
277
278         if (olinebuf)
279                 OPENSSL_free(olinebuf);
280         if (linebuf)
281                 OPENSSL_free(linebuf);
282         if (Msg)
283                 OPENSSL_free(Msg);
284         if (Seed)
285                 OPENSSL_free(Seed);
286
287         return ret;
288
289         parse_error:
290
291         fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
292
293         goto error;
294
295         }
296
297 static int print_dgst(const EVP_MD *emd, FILE *out,
298                 unsigned char *Msg, int Msglen)
299         {
300         int i, mdlen;
301         unsigned char md[EVP_MAX_MD_SIZE];
302         if (!FIPS_digest(Msg, Msglen, md, (unsigned int *)&mdlen, emd))
303                 {
304                 fputs("Error calculating HASH\n", stderr);
305                 return 0;
306                 }
307         fputs("MD = ", out);
308         for (i = 0; i < mdlen; i++)
309                 fprintf(out, "%02x", md[i]);
310         fputs("\n", out);
311         return 1;
312         }
313
314 static int print_monte(const EVP_MD *md, FILE *out,
315                 unsigned char *Seed, int SeedLen)
316         {
317         unsigned int i, j, k;
318         int ret = 0;
319         EVP_MD_CTX ctx;
320         unsigned char *m1, *m2, *m3, *p;
321         unsigned int mlen, m1len, m2len, m3len;
322
323         FIPS_md_ctx_init(&ctx);
324
325         if (SeedLen > EVP_MAX_MD_SIZE)
326                 mlen = SeedLen;
327         else
328                 mlen = EVP_MAX_MD_SIZE;
329
330         m1 = OPENSSL_malloc(mlen);
331         m2 = OPENSSL_malloc(mlen);
332         m3 = OPENSSL_malloc(mlen);
333
334         if (!m1 || !m2 || !m3)
335                 goto mc_error;
336
337         m1len = m2len = m3len = SeedLen;
338         memcpy(m1, Seed, SeedLen);
339         memcpy(m2, Seed, SeedLen);
340         memcpy(m3, Seed, SeedLen);
341
342         fputs("\n", out);
343
344         for (j = 0; j < 100; j++)
345                 {
346                 for (i = 0; i < 1000; i++)
347                         {
348                         FIPS_digestinit(&ctx, md);
349                         FIPS_digestupdate(&ctx, m1, m1len);
350                         FIPS_digestupdate(&ctx, m2, m2len);
351                         FIPS_digestupdate(&ctx, m3, m3len);
352                         p = m1;
353                         m1 = m2;
354                         m1len = m2len;
355                         m2 = m3;
356                         m2len = m3len;
357                         m3 = p;
358                         FIPS_digestfinal(&ctx, m3, &m3len);
359                         }
360                 fprintf(out, "COUNT = %d\n", j);
361                 fputs("MD = ", out);
362                 for (k = 0; k < m3len; k++)
363                         fprintf(out, "%02x", m3[k]);
364                 fputs("\n\n", out);
365                 memcpy(m1, m3, m3len);
366                 memcpy(m2, m3, m3len);
367                 m1len = m2len = m3len;
368                 }
369
370         ret = 1;
371
372         mc_error:
373         if (m1)
374                 OPENSSL_free(m1);
375         if (m2)
376                 OPENSSL_free(m2);
377         if (m3)
378                 OPENSSL_free(m3);
379
380         FIPS_md_ctx_cleanup(&ctx);
381
382         return ret;
383         }
384
385 #endif