Have all algorithm test programs call fips_algtest_init() at startup:
[openssl.git] / fips / hmac / fips_hmactest.c
1 /* fips_hmactest.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/hmac.h>
67 #include <openssl/err.h>
68 #include <openssl/bn.h>
69
70 #ifndef OPENSSL_FIPS
71
72 int main(int argc, char *argv[])
73 {
74     printf("No FIPS HMAC support\n");
75     return(0);
76 }
77
78 #else
79
80 #include <openssl/fips.h>
81 #include "fips_utl.h"
82
83 static int hmac_test(const EVP_MD *md, FILE *out, FILE *in);
84 static int print_hmac(const EVP_MD *md, FILE *out,
85                 unsigned char *Key, int Klen,
86                 unsigned char *Msg, int Msglen, int Tlen);
87
88 int main(int argc, char **argv)
89         {
90         FILE *in = NULL, *out = NULL;
91
92         int ret = 1;
93         fips_algtest_init();
94
95         if (argc == 1)
96                 in = stdin;
97         else
98                 in = fopen(argv[1], "r");
99
100         if (argc < 2)
101                 out = stdout;
102         else
103                 out = fopen(argv[2], "w");
104
105         if (!in)
106                 {
107                 fprintf(stderr, "FATAL input initialization error\n");
108                 goto end;
109                 }
110
111         if (!out)
112                 {
113                 fprintf(stderr, "FATAL output initialization error\n");
114                 goto end;
115                 }
116
117         if (!hmac_test(EVP_sha1(), out, in))
118                 {
119                 fprintf(stderr, "FATAL hmac file processing error\n");
120                 goto end;
121                 }
122         else
123                 ret = 0;
124
125         end:
126
127         if (in && (in != stdin))
128                 fclose(in);
129         if (out && (out != stdout))
130                 fclose(out);
131
132         return ret;
133
134         }
135
136 #define HMAC_TEST_MAXLINELEN    1024
137
138 int hmac_test(const EVP_MD *md, FILE *out, FILE *in)
139         {
140         char *linebuf, *olinebuf, *p, *q;
141         char *keyword, *value;
142         unsigned char *Key = NULL, *Msg = NULL;
143         int Count, Klen, Tlen;
144         long Keylen, Msglen;
145         int ret = 0;
146         int lnum = 0;
147
148         olinebuf = OPENSSL_malloc(HMAC_TEST_MAXLINELEN);
149         linebuf = OPENSSL_malloc(HMAC_TEST_MAXLINELEN);
150
151         if (!linebuf || !olinebuf)
152                 goto error;
153
154         Count = -1;
155         Klen = -1;
156         Tlen = -1;
157
158         while (fgets(olinebuf, HMAC_TEST_MAXLINELEN, in))
159                 {
160                 lnum++;
161                 strcpy(linebuf, olinebuf);
162                 keyword = linebuf;
163                 /* Skip leading space */
164                 while (isspace((unsigned char)*keyword))
165                         keyword++;
166
167                 /* Look for = sign */
168                 p = strchr(linebuf, '=');
169
170                 /* If no = or starts with [ (for [L=20] line) just copy */
171                 if (!p)
172                         {
173                         if (fputs(olinebuf, out) < 0)
174                                 goto error;
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
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, "Count"))
210                         {
211                         if (Count != -1)
212                                 goto parse_error;
213                         Count = atoi(value);
214                         if (Count < 0)
215                                 goto parse_error;
216                         }
217                 else if (!strcmp(keyword, "Klen"))
218                         {
219                         if (Klen != -1)
220                                 goto parse_error;
221                         Klen = atoi(value);
222                         if (Klen < 0)
223                                 goto parse_error;
224                         }
225                 else if (!strcmp(keyword, "Tlen"))
226                         {
227                         if (Tlen != -1)
228                                 goto parse_error;
229                         Tlen = atoi(value);
230                         if (Tlen < 0)
231                                 goto parse_error;
232                         }
233                 else if (!strcmp(keyword, "Msg"))
234                         {
235                         if (Msg)
236                                 goto parse_error;
237                         Msg = hex2bin_m(value, &Msglen);
238                         if (!Msg)
239                                 goto parse_error;
240                         }
241                 else if (!strcmp(keyword, "Key"))
242                         {
243                         if (Key)
244                                 goto parse_error;
245                         Key = hex2bin_m(value, &Keylen);
246                         if (!Key)
247                                 goto parse_error;
248                         }
249                 else if (!strcmp(keyword, "Mac"))
250                         continue;
251                 else
252                         goto parse_error;
253
254                 fputs(olinebuf, out);
255
256                 if (Key && Msg && (Tlen > 0) && (Klen > 0))
257                         {
258                         if (!print_hmac(md, out, Key, Klen, Msg, Msglen, Tlen))
259                                 goto error;
260                         OPENSSL_free(Key);
261                         Key = NULL;
262                         OPENSSL_free(Msg);
263                         Msg = NULL;
264                         Klen = -1;
265                         Tlen = -1;
266                         Count = -1;
267                         }
268
269                 }
270
271
272         ret = 1;
273
274
275         error:
276
277         if (olinebuf)
278                 OPENSSL_free(olinebuf);
279         if (linebuf)
280                 OPENSSL_free(linebuf);
281         if (Key)
282                 OPENSSL_free(Key);
283         if (Msg)
284                 OPENSSL_free(Msg);
285
286         return ret;
287
288         parse_error:
289
290         fprintf(stderr, "FATAL parse error processing line %d\n", lnum);
291
292         goto error;
293
294         }
295
296 static int print_hmac(const EVP_MD *emd, FILE *out,
297                 unsigned char *Key, int Klen,
298                 unsigned char *Msg, int Msglen, int Tlen)
299         {
300         int i, mdlen;
301         unsigned char md[EVP_MAX_MD_SIZE];
302         if (!HMAC(emd, Key, Klen, Msg, Msglen, md,
303                                                 (unsigned int *)&mdlen))
304                 {
305                 fputs("Error calculating HMAC\n", stderr);
306                 return 0;
307                 }
308         if (Tlen > mdlen)
309                 {
310                 fputs("Parameter error, Tlen > HMAC length\n", stderr);
311                 return 0;
312                 }
313         fputs("Mac = ", out);
314         for (i = 0; i < Tlen; i++)
315                 fprintf(out, "%02x", md[i]);
316         fputs("\n", out);
317         return 1;
318         }
319
320 #endif