Remove old DES API
[openssl.git] / test / fips_algvs.c
1 /* test/fips_algvs.c */
2 /*
3  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4  * 2011
5  */
6 /* ====================================================================
7  * Copyright (c) 2011 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
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
19  *    distribution.
20  *
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/)"
25  *
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.
30  *
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.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
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  * ====================================================================
53  *
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).
57  *
58  */
59
60 #include <stdio.h>
61 #include <openssl/crypto.h>
62 #include <openssl/opensslconf.h>
63
64 #ifndef OPENSSL_FIPS
65 # include <stdio.h>
66
67 int main(int argc, char **argv)
68 {
69     printf("No FIPS ALGVS support\n");
70     return 0;
71 }
72 #else
73
74 # define FIPS_ALGVS
75
76 extern int fips_aesavs_main(int argc, char **argv);
77 extern int fips_cmactest_main(int argc, char **argv);
78 extern int fips_desmovs_main(int argc, char **argv);
79 extern int fips_dhvs_main(int argc, char **argv);
80 extern int fips_drbgvs_main(int argc, char **argv);
81 extern int fips_dssvs_main(int argc, char **argv);
82 extern int fips_ecdhvs_main(int argc, char **argv);
83 extern int fips_ecdsavs_main(int argc, char **argv);
84 extern int fips_gcmtest_main(int argc, char **argv);
85 extern int fips_hmactest_main(int argc, char **argv);
86 extern int fips_rngvs_main(int argc, char **argv);
87 extern int fips_rsagtest_main(int argc, char **argv);
88 extern int fips_rsastest_main(int argc, char **argv);
89 extern int fips_rsavtest_main(int argc, char **argv);
90 extern int fips_shatest_main(int argc, char **argv);
91 extern int fips_test_suite_main(int argc, char **argv);
92
93 # include "fips_aesavs.c"
94 # include "fips_cmactest.c"
95 # include "fips_desmovs.c"
96 # include "fips_dhvs.c"
97 # include "fips_drbgvs.c"
98 # include "fips_dssvs.c"
99 # include "fips_ecdhvs.c"
100 # include "fips_ecdsavs.c"
101 # include "fips_gcmtest.c"
102 # include "fips_hmactest.c"
103 # include "fips_rngvs.c"
104 # include "fips_rsagtest.c"
105 # include "fips_rsastest.c"
106 # include "fips_rsavtest.c"
107 # include "fips_shatest.c"
108 # include "fips_test_suite.c"
109
110 typedef struct {
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     char **data;
139     int count;
140 } ARGS;
141
142 static int chopup_args(ARGS *arg, char *buf, int *argc, char **argv[])
143 {
144     int num, i;
145     char *p;
146
147     *argc = 0;
148     *argv = NULL;
149
150     i = 0;
151     if (arg->count == 0) {
152         arg->count = 20;
153         arg->data = (char **)OPENSSL_malloc(sizeof(char *) * arg->count);
154     }
155     for (i = 0; i < arg->count; i++)
156         arg->data[i] = NULL;
157
158     num = 0;
159     p = buf;
160     for (;;) {
161         /* first scan over white space */
162         if (!*p)
163             break;
164         while (*p && ((*p == ' ') || (*p == '\t') || (*p == '\n')))
165             p++;
166         if (!*p)
167             break;
168
169         /* The start of something good :-) */
170         if (num >= arg->count) {
171             fprintf(stderr, "Too many arguments!!\n");
172             return 0;
173         }
174         arg->data[num++] = p;
175
176         /* now look for the end of this */
177         if ((*p == '\'') || (*p == '\"')) { /* scan for closing quote */
178             i = *(p++);
179             arg->data[num - 1]++; /* jump over quote */
180             while (*p && (*p != i))
181                 p++;
182             *p = '\0';
183         } else {
184             while (*p && ((*p != ' ') && (*p != '\t') && (*p != '\n')))
185                 p++;
186
187             if (*p == '\0')
188                 p--;
189             else
190                 *p = '\0';
191         }
192         p++;
193     }
194     *argc = num;
195     *argv = arg->data;
196     return (1);
197 }
198
199 static int run_prg(int argc, char **argv)
200 {
201     ALGVS_FUNCTION *t;
202     const char *prg_name;
203     prg_name = strrchr(argv[0], '/');
204     if (prg_name)
205         prg_name++;
206     else
207         prg_name = argv[0];
208     for (t = algvs; t->name; t++) {
209         if (!strcmp(prg_name, t->name))
210             return t->func(argc, argv);
211     }
212     return -100;
213 }
214
215 int main(int argc, char **argv)
216 {
217     char buf[1024];
218     char **args = argv + 1;
219     const char *sname = "fipstests.sh";
220     ARGS arg;
221     int xargc;
222     char **xargv;
223     int lineno = 0, badarg = 0;
224     int nerr = 0, quiet = 0, verbose = 0;
225     int rv;
226     FILE *in = NULL;
227 # ifdef FIPS_ALGVS_MEMCHECK
228     CRYPTO_malloc_debug_init();
229     OPENSSL_init();
230     CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
231     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
232 # endif
233
234     if (*args && *args[0] != '-') {
235         rv = run_prg(argc - 1, args);
236 # ifdef FIPS_ALGVS_MEMCHECK
237         CRYPTO_mem_leaks_fp(stderr);
238 # endif
239         return rv;
240     }
241     while (!badarg && *args && *args[0] == '-') {
242         if (!strcmp(*args, "-script")) {
243             if (args[1]) {
244                 args++;
245                 sname = *args;
246             } else
247                 badarg = 1;
248         } else if (!strcmp(*args, "-quiet"))
249             quiet = 1;
250         else if (!strcmp(*args, "-verbose"))
251             verbose = 1;
252         else
253             badarg = 1;
254         args++;
255     }
256
257     if (badarg) {
258         fprintf(stderr, "Error processing arguments\n");
259         return 1;
260     }
261
262     in = fopen(sname, "r");
263     if (!in) {
264         fprintf(stderr, "Error opening script file \"%s\"\n", sname);
265         return 1;
266     }
267
268     arg.data = NULL;
269     arg.count = 0;
270
271     while (fgets(buf, sizeof(buf), in)) {
272         lineno++;
273         if (!chopup_args(&arg, buf, &xargc, &xargv))
274             fprintf(stderr, "Error processing line %d\n", lineno);
275         else {
276             if (!quiet) {
277                 int i;
278                 int narg = verbose ? xargc : xargc - 2;
279                 printf("Running command line:");
280                 for (i = 0; i < narg; i++)
281                     printf(" %s", xargv[i]);
282                 printf("\n");
283             }
284             rv = run_prg(xargc, xargv);
285             if (FIPS_module_mode())
286                 FIPS_module_mode_set(0, NULL);
287             if (rv != 0)
288                 nerr++;
289             if (rv == -100)
290                 fprintf(stderr, "ERROR: Command not found\n");
291             else if (rv != 0)
292                 fprintf(stderr, "ERROR: returned %d\n", rv);
293             else if (verbose)
294                 printf("\tCommand run successfully\n");
295         }
296     }
297
298     if (!quiet)
299         printf("Completed with %d errors\n", nerr);
300
301     if (arg.data)
302         OPENSSL_free(arg.data);
303
304     fclose(in);
305 # ifdef FIPS_ALGVS_MEMCHECK
306     CRYPTO_mem_leaks_fp(stderr);
307 # endif
308     if (nerr == 0)
309         return 0;
310     return 1;
311 }
312 #endif