c14df166016efd1882c5555d13489445fed97a35
[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_algtest_init();
95
96         if (argc == 1)
97                 in = stdin;
98         else
99                 in = fopen(argv[1], "r");
100
101         if (argc < 2)
102                 out = stdout;
103         else
104                 out = fopen(argv[2], "w");
105
106         if (!in)
107                 {
108                 fprintf(stderr, "FATAL input initialization error\n");
109                 goto end;
110                 }
111
112         if (!out)
113                 {
114                 fprintf(stderr, "FATAL output initialization error\n");
115                 goto end;
116                 }
117
118         if (!dgst_test(out, in))
119                 {
120                 fprintf(stderr, "FATAL digest file processing error\n");
121                 goto end;
122                 }
123         else
124                 ret = 0;
125
126         end:
127
128         if (in && (in != stdin))
129                 fclose(in);
130         if (out && (out != stdout))
131                 fclose(out);
132
133         return ret;
134
135         }
136
137 #define SHA_TEST_MAX_BITS       102400
138 #define SHA_TEST_MAXLINELEN     (((SHA_TEST_MAX_BITS >> 3) * 2) + 100)
139
140 int dgst_test(FILE *out, FILE *in)
141         {
142         const EVP_MD *md = NULL;
143         char *linebuf, *olinebuf, *p, *q;
144         char *keyword, *value;
145         unsigned char *Msg = NULL, *Seed = NULL;
146         long MsgLen = -1, Len = -1, SeedLen = -1;
147         int ret = 0;
148         int lnum = 0;
149
150         olinebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
151         linebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
152
153         if (!linebuf || !olinebuf)
154                 goto error;
155
156
157         while (fgets(olinebuf, SHA_TEST_MAXLINELEN, in))
158                 {
159                 lnum++;
160                 strcpy(linebuf, olinebuf);
161                 keyword = linebuf;
162                 /* Skip leading space */
163                 while (isspace((unsigned char)*keyword))
164                         keyword++;
165
166                 /* Look for = sign */
167                 p = strchr(linebuf, '=');
168
169                 /* If no = or starts with [ (for [L=20] line) just copy */
170                 if (!p)
171                         {
172                         fputs(olinebuf, out);
173                         continue;
174                         }
175
176                 q = p - 1;
177
178                 /* Remove trailing space */
179                 while (isspace((unsigned char)*q))
180                         *q-- = 0;
181
182                 *p = 0;
183                 value = p + 1;
184
185                 /* Remove leading space from value */
186                 while (isspace((unsigned char)*value))
187                         value++;
188
189                 /* Remove trailing space from value */
190                 p = value + strlen(value) - 1;
191                 while (*p == '\n' || isspace((unsigned char)*p))
192                         *p-- = 0;
193
194                 if (!strcmp(keyword,"[L") && *p==']')
195                         {
196                         switch (atoi(value))
197                                 {
198                                 case 20: md=EVP_sha1();   break;
199                                 case 28: md=EVP_sha224(); break;
200                                 case 32: md=EVP_sha256(); break;
201                                 case 48: md=EVP_sha384(); break;
202                                 case 64: md=EVP_sha512(); break;
203                                 default: goto parse_error;
204                                 }
205                         }
206                 else if (!strcmp(keyword, "Len"))
207                         {
208                         if (Len != -1)
209                                 goto parse_error;
210                         Len = atoi(value);
211                         if (Len < 0)
212                                 goto parse_error;
213                         /* Only handle multiples of 8 bits */
214                         if (Len & 0x7)
215                                 goto parse_error;
216                         if (Len > SHA_TEST_MAX_BITS)
217                                 goto parse_error;
218                         MsgLen = Len >> 3;
219                         }
220
221                 else if (!strcmp(keyword, "Msg"))
222                         {
223                         long tmplen;
224                         if (strlen(value) & 1)
225                                 *(--value) = '0';
226                         if (Msg)
227                                 goto parse_error;
228                         Msg = hex2bin_m(value, &tmplen);
229                         if (!Msg)
230                                 goto parse_error;
231                         }
232                 else if (!strcmp(keyword, "Seed"))
233                         {
234                         if (strlen(value) & 1)
235                                 *(--value) = '0';
236                         if (Seed)
237                                 goto parse_error;
238                         Seed = hex2bin_m(value, &SeedLen);
239                         if (!Seed)
240                                 goto parse_error;
241                         }
242                 else if (!strcmp(keyword, "MD"))
243                         continue;
244                 else
245                         goto parse_error;
246
247                 fputs(olinebuf, out);
248
249                 if (md && Msg && (MsgLen >= 0))
250                         {
251                         if (!print_dgst(md, out, Msg, MsgLen))
252                                 goto error;
253                         OPENSSL_free(Msg);
254                         Msg = NULL;
255                         MsgLen = -1;
256                         Len = -1;
257                         }
258                 else if (md && Seed && (SeedLen > 0))
259                         {
260                         if (!print_monte(md, out, Seed, SeedLen))
261                                 goto error;
262                         OPENSSL_free(Seed);
263                         Seed = NULL;
264                         SeedLen = -1;
265                         }
266         
267
268                 }
269
270
271         ret = 1;
272
273
274         error:
275
276         if (olinebuf)
277                 OPENSSL_free(olinebuf);
278         if (linebuf)
279                 OPENSSL_free(linebuf);
280         if (Msg)
281                 OPENSSL_free(Msg);
282         if (Seed)
283                 OPENSSL_free(Seed);
284
285         return ret;
286
287         parse_error:
288
289         fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
290
291         goto error;
292
293         }
294
295 static int print_dgst(const EVP_MD *emd, FILE *out,
296                 unsigned char *Msg, int Msglen)
297         {
298         int i, mdlen;
299         unsigned char md[EVP_MAX_MD_SIZE];
300         if (!FIPS_digest(Msg, Msglen, md, (unsigned int *)&mdlen, emd))
301                 {
302                 fputs("Error calculating HASH\n", stderr);
303                 return 0;
304                 }
305         fputs("MD = ", out);
306         for (i = 0; i < mdlen; i++)
307                 fprintf(out, "%02x", md[i]);
308         fputs(RESP_EOL, out);
309         return 1;
310         }
311
312 static int print_monte(const EVP_MD *md, FILE *out,
313                 unsigned char *Seed, int SeedLen)
314         {
315         unsigned int i, j, k;
316         int ret = 0;
317         EVP_MD_CTX ctx;
318         unsigned char *m1, *m2, *m3, *p;
319         unsigned int mlen, m1len, m2len, m3len;
320
321         FIPS_md_ctx_init(&ctx);
322
323         if (SeedLen > EVP_MAX_MD_SIZE)
324                 mlen = SeedLen;
325         else
326                 mlen = EVP_MAX_MD_SIZE;
327
328         m1 = OPENSSL_malloc(mlen);
329         m2 = OPENSSL_malloc(mlen);
330         m3 = OPENSSL_malloc(mlen);
331
332         if (!m1 || !m2 || !m3)
333                 goto mc_error;
334
335         m1len = m2len = m3len = SeedLen;
336         memcpy(m1, Seed, SeedLen);
337         memcpy(m2, Seed, SeedLen);
338         memcpy(m3, Seed, SeedLen);
339
340         fputs(RESP_EOL, out);
341
342         for (j = 0; j < 100; j++)
343                 {
344                 for (i = 0; i < 1000; i++)
345                         {
346                         FIPS_digestinit(&ctx, md);
347                         FIPS_digestupdate(&ctx, m1, m1len);
348                         FIPS_digestupdate(&ctx, m2, m2len);
349                         FIPS_digestupdate(&ctx, m3, m3len);
350                         p = m1;
351                         m1 = m2;
352                         m1len = m2len;
353                         m2 = m3;
354                         m2len = m3len;
355                         m3 = p;
356                         FIPS_digestfinal(&ctx, m3, &m3len);
357                         }
358                 fprintf(out, "COUNT = %d" RESP_EOL, j);
359                 fputs("MD = ", out);
360                 for (k = 0; k < m3len; k++)
361                         fprintf(out, "%02x", m3[k]);
362                 fputs(RESP_EOL RESP_EOL, out);
363                 memcpy(m1, m3, m3len);
364                 memcpy(m2, m3, m3len);
365                 m1len = m2len = m3len;
366                 }
367
368         ret = 1;
369
370         mc_error:
371         if (m1)
372                 OPENSSL_free(m1);
373         if (m2)
374                 OPENSSL_free(m2);
375         if (m3)
376                 OPENSSL_free(m3);
377
378         FIPS_md_ctx_cleanup(&ctx);
379
380         return ret;
381         }
382
383 #endif