2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
59 #define OPENSSL_FIPSAPI
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>
72 int main(int argc, char *argv[])
74 printf("No FIPS SHAXXX support\n");
80 #include <openssl/fips.h>
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);
90 int main(int argc, char **argv)
92 FILE *in = NULL, *out = NULL;
95 fips_set_error_print();
102 in = fopen(argv[1], "r");
107 out = fopen(argv[2], "w");
111 fprintf(stderr, "FATAL input initialization error\n");
117 fprintf(stderr, "FATAL output initialization error\n");
121 if (!dgst_test(out, in))
123 fprintf(stderr, "FATAL digest file processing error\n");
131 if (in && (in != stdin))
133 if (out && (out != stdout))
140 #define SHA_TEST_MAX_BITS 102400
141 #define SHA_TEST_MAXLINELEN (((SHA_TEST_MAX_BITS >> 3) * 2) + 100)
143 int dgst_test(FILE *out, FILE *in)
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;
153 olinebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
154 linebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
156 if (!linebuf || !olinebuf)
160 while (fgets(olinebuf, SHA_TEST_MAXLINELEN, in))
163 strcpy(linebuf, olinebuf);
165 /* Skip leading space */
166 while (isspace((unsigned char)*keyword))
169 /* Look for = sign */
170 p = strchr(linebuf, '=');
172 /* If no = or starts with [ (for [L=20] line) just copy */
175 fputs(olinebuf, out);
181 /* Remove trailing space */
182 while (isspace((unsigned char)*q))
188 /* Remove leading space from value */
189 while (isspace((unsigned char)*value))
192 /* Remove trailing space from value */
193 p = value + strlen(value) - 1;
194 while (*p == '\n' || isspace((unsigned char)*p))
197 if (!strcmp(keyword,"[L") && *p==']')
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;
209 else if (!strcmp(keyword, "Len"))
216 /* Only handle multiples of 8 bits */
219 if (Len > SHA_TEST_MAX_BITS)
224 else if (!strcmp(keyword, "Msg"))
227 if (strlen(value) & 1)
231 Msg = hex2bin_m(value, &tmplen);
235 else if (!strcmp(keyword, "Seed"))
237 if (strlen(value) & 1)
241 Seed = hex2bin_m(value, &SeedLen);
245 else if (!strcmp(keyword, "MD"))
250 fputs(olinebuf, out);
252 if (md && Msg && (MsgLen >= 0))
254 if (!print_dgst(md, out, Msg, MsgLen))
261 else if (md && Seed && (SeedLen > 0))
263 if (!print_monte(md, out, Seed, SeedLen))
280 OPENSSL_free(olinebuf);
282 OPENSSL_free(linebuf);
292 fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
298 static int print_dgst(const EVP_MD *emd, FILE *out,
299 unsigned char *Msg, int Msglen)
302 unsigned char md[EVP_MAX_MD_SIZE];
303 if (!FIPS_digest(Msg, Msglen, md, (unsigned int *)&mdlen, emd))
305 fputs("Error calculating HASH\n", stderr);
309 for (i = 0; i < mdlen; i++)
310 fprintf(out, "%02x", md[i]);
315 static int print_monte(const EVP_MD *md, FILE *out,
316 unsigned char *Seed, int SeedLen)
318 unsigned int i, j, k;
321 unsigned char *m1, *m2, *m3, *p;
322 unsigned int mlen, m1len, m2len, m3len;
324 FIPS_md_ctx_init(&ctx);
326 if (SeedLen > EVP_MAX_MD_SIZE)
329 mlen = EVP_MAX_MD_SIZE;
331 m1 = OPENSSL_malloc(mlen);
332 m2 = OPENSSL_malloc(mlen);
333 m3 = OPENSSL_malloc(mlen);
335 if (!m1 || !m2 || !m3)
338 m1len = m2len = m3len = SeedLen;
339 memcpy(m1, Seed, SeedLen);
340 memcpy(m2, Seed, SeedLen);
341 memcpy(m3, Seed, SeedLen);
345 for (j = 0; j < 100; j++)
347 for (i = 0; i < 1000; i++)
349 FIPS_digestinit(&ctx, md);
350 FIPS_digestupdate(&ctx, m1, m1len);
351 FIPS_digestupdate(&ctx, m2, m2len);
352 FIPS_digestupdate(&ctx, m3, m3len);
359 FIPS_digestfinal(&ctx, m3, &m3len);
361 fprintf(out, "COUNT = %d\n", j);
363 for (k = 0; k < m3len; k++)
364 fprintf(out, "%02x", m3[k]);
366 memcpy(m1, m3, m3len);
367 memcpy(m2, m3, m3len);
368 m1len = m2len = m3len;
381 FIPS_md_ctx_cleanup(&ctx);