3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2005 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #include <openssl/bio.h>
64 #include <openssl/evp.h>
65 #include <openssl/err.h>
66 #include <openssl/bn.h>
67 #include <openssl/x509v3.h>
71 int main(int argc, char *argv[])
73 printf("No FIPS SHAXXX support\n");
79 # include "fips_utl.h"
81 static int dgst_test(FILE *out, FILE *in);
82 static int print_dgst(const EVP_MD *md, FILE *out,
83 unsigned char *Msg, int Msglen);
84 static int print_monte(const EVP_MD *md, FILE *out,
85 unsigned char *Seed, int SeedLen);
87 int main(int argc, char **argv)
89 FILE *in = NULL, *out = NULL;
93 if (!FIPS_mode_set(1)) {
101 in = fopen(argv[1], "r");
106 out = fopen(argv[2], "w");
109 fprintf(stderr, "FATAL input initialization error\n");
114 fprintf(stderr, "FATAL output initialization error\n");
118 if (!dgst_test(out, in)) {
119 fprintf(stderr, "FATAL digest file processing error\n");
129 if (in && (in != stdin))
131 if (out && (out != stdout))
138 # define SHA_TEST_MAX_BITS 102400
139 # define SHA_TEST_MAXLINELEN (((SHA_TEST_MAX_BITS >> 3) * 2) + 100)
141 int dgst_test(FILE *out, FILE *in)
143 const EVP_MD *md = NULL;
144 char *linebuf, *olinebuf, *p, *q;
145 char *keyword, *value;
146 unsigned char *Msg = NULL, *Seed = NULL;
147 long MsgLen = -1, Len = -1, SeedLen = -1;
151 olinebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
152 linebuf = OPENSSL_malloc(SHA_TEST_MAXLINELEN);
154 if (!linebuf || !olinebuf)
157 while (fgets(olinebuf, SHA_TEST_MAXLINELEN, in)) {
159 strcpy(linebuf, olinebuf);
161 /* Skip leading space */
162 while (isspace((unsigned char)*keyword))
165 /* Look for = sign */
166 p = strchr(linebuf, '=');
168 /* If no = or starts with [ (for [L=20] line) just copy */
170 fputs(olinebuf, out);
176 /* Remove trailing space */
177 while (isspace((unsigned char)*q))
183 /* Remove leading space from value */
184 while (isspace((unsigned char)*value))
187 /* Remove trailing space from value */
188 p = value + strlen(value) - 1;
189 while (*p == '\n' || isspace((unsigned char)*p))
192 if (!strcmp(keyword, "[L") && *p == ']') {
193 switch (atoi(value)) {
212 } else if (!strcmp(keyword, "Len")) {
218 /* Only handle multiples of 8 bits */
221 if (Len > SHA_TEST_MAX_BITS)
226 else if (!strcmp(keyword, "Msg")) {
228 if (strlen(value) & 1)
232 Msg = hex2bin_m(value, &tmplen);
235 } else if (!strcmp(keyword, "Seed")) {
236 if (strlen(value) & 1)
240 Seed = hex2bin_m(value, &SeedLen);
243 } else if (!strcmp(keyword, "MD"))
248 fputs(olinebuf, out);
250 if (md && Msg && (MsgLen >= 0)) {
251 if (!print_dgst(md, out, Msg, MsgLen))
257 } else if (md && Seed && (SeedLen > 0)) {
258 if (!print_monte(md, out, Seed, SeedLen))
272 OPENSSL_free(olinebuf);
274 OPENSSL_free(linebuf);
284 fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
290 static int print_dgst(const EVP_MD *emd, FILE *out,
291 unsigned char *Msg, int Msglen)
294 unsigned char md[EVP_MAX_MD_SIZE];
295 if (!EVP_Digest(Msg, Msglen, md, (unsigned int *)&mdlen, emd, NULL)) {
296 fputs("Error calculating HASH\n", stderr);
300 for (i = 0; i < mdlen; i++)
301 fprintf(out, "%02x", md[i]);
306 static int print_monte(const EVP_MD *md, FILE *out,
307 unsigned char *Seed, int SeedLen)
309 unsigned int i, j, k;
312 unsigned char *m1, *m2, *m3, *p;
313 unsigned int mlen, m1len, m2len, m3len;
315 EVP_MD_CTX_init(&ctx);
317 if (SeedLen > EVP_MAX_MD_SIZE)
320 mlen = EVP_MAX_MD_SIZE;
322 m1 = OPENSSL_malloc(mlen);
323 m2 = OPENSSL_malloc(mlen);
324 m3 = OPENSSL_malloc(mlen);
326 if (!m1 || !m2 || !m3)
329 m1len = m2len = m3len = SeedLen;
330 memcpy(m1, Seed, SeedLen);
331 memcpy(m2, Seed, SeedLen);
332 memcpy(m3, Seed, SeedLen);
336 for (j = 0; j < 100; j++) {
337 for (i = 0; i < 1000; i++) {
338 EVP_DigestInit_ex(&ctx, md, NULL);
339 EVP_DigestUpdate(&ctx, m1, m1len);
340 EVP_DigestUpdate(&ctx, m2, m2len);
341 EVP_DigestUpdate(&ctx, m3, m3len);
348 EVP_DigestFinal_ex(&ctx, m3, &m3len);
350 fprintf(out, "COUNT = %d\n", j);
352 for (k = 0; k < m3len; k++)
353 fprintf(out, "%02x", m3[k]);
355 memcpy(m1, m3, m3len);
356 memcpy(m2, m3, m3len);
357 m1len = m2len = m3len;
370 EVP_MD_CTX_cleanup(&ctx);