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