Zero prime doits.
[openssl.git] / test / fips_algvs.c
1 /* test/fips_algvs.c */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project 2011
4  */
5 /* ====================================================================
6  * Copyright (c) 2011 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 #include <stdio.h>
60 #include <openssl/crypto.h>
61 #include <openssl/opensslconf.h>
62
63 #ifndef OPENSSL_FIPS
64 #include <stdio.h>
65
66 int main(int argc, char **argv)
67 {
68     printf("No FIPS ALGVS support\n");
69     return 0;
70 }
71 #else
72
73 #define FIPS_ALGVS
74
75 extern int fips_aesavs_main(int argc, char **argv);
76 extern int fips_cmactest_main(int argc, char **argv);
77 extern int fips_desmovs_main(int argc, char **argv);
78 extern int fips_dhvs_main(int argc, char **argv);
79 extern int fips_drbgvs_main(int argc,char **argv);
80 extern int fips_dssvs_main(int argc, char **argv);
81 extern int fips_ecdhvs_main(int argc, char **argv);
82 extern int fips_ecdsavs_main(int argc, char **argv);
83 extern int fips_gcmtest_main(int argc, char **argv);
84 extern int fips_hmactest_main(int argc, char **argv);
85 extern int fips_rngvs_main(int argc, char **argv);
86 extern int fips_rsagtest_main(int argc, char **argv);
87 extern int fips_rsastest_main(int argc, char **argv);
88 extern int fips_rsavtest_main(int argc, char **argv);
89 extern int fips_shatest_main(int argc, char **argv);
90 extern int fips_test_suite_main(int argc, char **argv);
91
92 #include "fips_aesavs.c"
93 #include "fips_cmactest.c"
94 #include "fips_desmovs.c"
95 #include "fips_dhvs.c"
96 #include "fips_drbgvs.c"
97 #include "fips_dssvs.c"
98 #include "fips_ecdhvs.c"
99 #include "fips_ecdsavs.c"
100 #include "fips_gcmtest.c"
101 #include "fips_hmactest.c"
102 #include "fips_rngvs.c"
103 #include "fips_rsagtest.c"
104 #include "fips_rsastest.c"
105 #include "fips_rsavtest.c"
106 #include "fips_shatest.c"
107 #include "fips_test_suite.c"
108
109 typedef struct
110         {
111         const char *name;
112         int (*func)(int argc, char **argv);
113         } ALGVS_FUNCTION;
114
115 static ALGVS_FUNCTION algvs[] = {
116         {"fips_aesavs", fips_aesavs_main}, 
117         {"fips_cmactest", fips_cmactest_main}, 
118         {"fips_desmovs", fips_desmovs_main}, 
119         {"fips_dhvs", fips_dhvs_main}, 
120         {"fips_drbgvs", fips_drbgvs_main}, 
121         {"fips_dssvs", fips_dssvs_main}, 
122         {"fips_ecdhvs", fips_ecdhvs_main}, 
123         {"fips_ecdsavs", fips_ecdsavs_main}, 
124         {"fips_gcmtest", fips_gcmtest_main}, 
125         {"fips_hmactest", fips_hmactest_main}, 
126         {"fips_rngvs", fips_rngvs_main}, 
127         {"fips_rsagtest", fips_rsagtest_main}, 
128         {"fips_rsastest", fips_rsastest_main}, 
129         {"fips_rsavtest", fips_rsavtest_main}, 
130         {"fips_shatest", fips_shatest_main}, 
131         {"fips_test_suite", fips_test_suite_main}, 
132         {NULL, 0}
133         };
134
135 /* Argument parsing taken from apps/apps.c */
136
137 typedef struct args_st
138         {
139         char **data;
140         int count;
141         } ARGS;
142
143 static int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
144         {
145         int num,i;
146         char *p;
147
148         *argc=0;
149         *argv=NULL;
150
151         i=0;
152         if (arg->count == 0)
153                 {
154                 arg->count=20;
155                 arg->data=(char **)OPENSSL_malloc(sizeof(char *)*arg->count);
156                 }
157         for (i=0; i<arg->count; i++)
158                 arg->data[i]=NULL;
159
160         num=0;
161         p=buf;
162         for (;;)
163                 {
164                 /* first scan over white space */
165                 if (!*p) break;
166                 while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
167                         p++;
168                 if (!*p) break;
169
170                 /* The start of something good :-) */
171                 if (num >= arg->count)
172                         {
173                         fprintf(stderr, "Too many arguments!!\n");
174                         return 0;
175                         }
176                 arg->data[num++]=p;
177
178                 /* now look for the end of this */
179                 if ((*p == '\'') || (*p == '\"')) /* scan for closing quote */
180                         {
181                         i= *(p++);
182                         arg->data[num-1]++; /* jump over quote */
183                         while (*p && (*p != i))
184                                 p++;
185                         *p='\0';
186                         }
187                 else
188                         {
189                         while (*p && ((*p != ' ') &&
190                                 (*p != '\t') && (*p != '\n')))
191                                 p++;
192
193                         if (*p == '\0')
194                                 p--;
195                         else
196                                 *p='\0';
197                         }
198                 p++;
199                 }
200         *argc=num;
201         *argv=arg->data;
202         return(1);
203         }
204
205 static int run_prg(int argc, char **argv)
206         {
207         ALGVS_FUNCTION *t;
208         const char *prg_name;
209         prg_name = strrchr(argv[0], '/');
210         if (prg_name)
211                 prg_name++;
212         else
213                 prg_name = argv[0];
214         for (t = algvs; t->name; t++)
215                 {
216                 if (!strcmp(prg_name, t->name))
217                         return t->func(argc, argv);
218                 }
219         return -100;
220         }
221
222 int main(int argc, char **argv)
223         {
224         char buf[1024];
225         char **args = argv + 1;
226         const char *sname = "fipstests.sh";
227         ARGS arg;
228         int xargc;
229         char **xargv;
230         int lineno = 0, badarg = 0;
231         int nerr = 0, quiet = 0, verbose = 0;
232         int rv;
233         FILE *in = NULL;
234 #ifdef FIPS_ALGVS_MEMCHECK
235         CRYPTO_malloc_debug_init();
236         OPENSSL_init();
237         CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
238         CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
239 #endif
240
241         if (*args && *args[0] != '-')
242                 {
243                 rv = run_prg(argc - 1, args);
244 #ifdef FIPS_ALGVS_MEMCHECK
245                 CRYPTO_mem_leaks_fp(stderr);
246 #endif
247                 return rv;
248                 }
249         while (!badarg && *args && *args[0] == '-')
250                 {
251                 if (!strcmp(*args, "-script"))
252                         {
253                         if (args[1])
254                                 {
255                                 args++;
256                                 sname = *args;
257                                 }
258                         else
259                                 badarg = 1;
260                         }
261                 else if (!strcmp(*args, "-quiet"))
262                         quiet = 1;
263                 else if (!strcmp(*args, "-verbose"))
264                         verbose = 1;
265                 else
266                         badarg = 1;
267                 args++;
268                 }
269
270         if (badarg)
271                 {
272                 fprintf(stderr, "Error processing arguments\n");
273                 return 1;
274                 }
275
276         in = fopen(sname, "r");
277         if (!in)
278                 {
279                 fprintf(stderr, "Error opening script file \"%s\"\n", sname);
280                 return 1;
281                 }
282
283         arg.data = NULL;
284         arg.count = 0;
285
286         while (fgets(buf, sizeof(buf), in))
287                 {
288                 lineno++;
289                 if (!chopup_args(&arg, buf, &xargc, &xargv))
290                         fprintf(stderr, "Error processing line %d\n", lineno);
291                 else
292                         {
293                         if (!quiet)
294                                 {
295                                 int i;
296                                 int narg = verbose ? xargc : xargc - 2;
297                                 printf("Running command line:");
298                                 for (i = 0; i < narg; i++)
299                                         printf(" %s", xargv[i]);
300                                 printf("\n");
301                                 }
302                         rv = run_prg(xargc, xargv);
303                         if (FIPS_module_mode())
304                                 FIPS_module_mode_set(0, NULL);
305                         if (rv != 0)
306                                 nerr++;
307                         if (rv == -100)
308                                 fprintf(stderr, "ERROR: Command not found\n");
309                         else if (rv != 0)
310                                 fprintf(stderr, "ERROR: returned %d\n", rv);
311                         else if (verbose)
312                                 printf("\tCommand run successfully\n");
313                         }
314                 }
315
316         if (!quiet)
317                 printf("Completed with %d errors\n", nerr);
318
319         if (arg.data)
320                 OPENSSL_free(arg.data);
321
322         fclose(in);
323 #ifdef FIPS_ALGVS_MEMCHECK
324         CRYPTO_mem_leaks_fp(stderr);
325 #endif
326         if (nerr == 0)
327                 return 0;
328         return 1;
329         }
330 #endif