1 /* ====================================================================
2 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * openssl-core@openssl.org.
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 #define OPENSSL_FIPSAPI
52 #include <openssl/fips_rand.h>
53 #include <openssl/objects.h>
55 int hex2bin(const char *in, unsigned char *out);
56 unsigned char *hex2bin_m(const char *in, long *plen);
57 int do_hex2bn(BIGNUM **pr, const char *in);
58 int do_bn_print(FILE *out, const BIGNUM *bn);
59 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
60 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
61 BIGNUM *hex2bn(const char *in);
62 int tidy_line(char *linebuf, char *olinebuf);
63 int bint2bin(const char *in, int len, unsigned char *out);
64 int bin2bint(const unsigned char *in,int len,char *out);
65 void PrintValue(char *tag, unsigned char *val, int len);
66 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode);
67 void fips_algtest_init(void);
68 void do_entropy_stick(void);
69 int fips_strncasecmp(const char *str1, const char *str2, size_t n);
70 int fips_strcasecmp(const char *str1, const char *str2);
74 static void put_err_cb(int lib, int func,int reason,const char *file,int line)
78 fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
80 ERR_PACK(lib, func, reason),
81 lib, func, reason, file, line);
84 static void add_err_cb(int num, va_list args)
91 for (i = 0; i < num; i++)
93 str = va_arg(args, char *);
100 /* Dummy Entropy to keep DRBG happy. WARNING: THIS IS TOTALLY BOGUS
101 * HAS ZERO SECURITY AND MUST NOT BE USED IN REAL APPLICATIONS.
104 static unsigned char dummy_entropy[1024];
106 static size_t dummy_cb(DRBG_CTX *ctx, unsigned char **pout,
107 int entropy, size_t min_len, size_t max_len)
109 *pout = dummy_entropy;
113 static int entropy_stick = 0;
115 static void fips_algtest_init_nofips(void)
119 FIPS_set_error_callbacks(put_err_cb, add_err_cb);
120 for (i = 0; i < sizeof(dummy_entropy); i++)
121 dummy_entropy[i] = i & 0xff;
123 memcpy(dummy_entropy + 32, dummy_entropy + 16, 16);
124 ctx = FIPS_get_default_drbg();
125 FIPS_drbg_init(ctx, NID_aes_256_ctr, DRBG_FLAG_CTR_USE_DF);
126 FIPS_drbg_set_callbacks(ctx, dummy_cb, 0, 16, dummy_cb, 0);
127 FIPS_drbg_instantiate(ctx, dummy_entropy, 10);
128 FIPS_rand_set_method(FIPS_drbg_method());
131 void do_entropy_stick(void)
136 void fips_algtest_init(void)
138 fips_algtest_init_nofips();
139 if (!FIPS_module_mode_set(1))
141 fprintf(stderr, "Error entering FIPS mode\n");
146 int hex2bin(const char *in, unsigned char *out)
148 int n1, n2, isodd = 0;
152 if (in[n1 - 1] == '\n')
158 for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
160 if ((in[n1] >= '0') && (in[n1] <= '9'))
162 else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
163 ch = in[n1++] - 'A' + 10;
164 else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
165 ch = in[n1++] - 'a' + 10;
173 /* If input is odd length first digit is least significant: assumes
174 * all digits valid hex and null terminated which is true for the
177 if (n1 == 1 && isodd)
184 if ((in[n1] >= '0') && (in[n1] <= '9'))
186 else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
187 ch = in[n1++] - 'A' + 10;
188 else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
189 ch = in[n1++] - 'a' + 10;
197 unsigned char *hex2bin_m(const char *in, long *plen)
203 return OPENSSL_malloc(1);
205 p = OPENSSL_malloc((strlen(in) + 1)/2);
206 *plen = hex2bin(in, p);
210 int do_hex2bn(BIGNUM **pr, const char *in)
215 p = hex2bin_m(in, &plen);
222 if (BN_bin2bn(p, plen, *pr))
228 int do_bn_print(FILE *out, const BIGNUM *bn)
232 len = BN_num_bytes(bn);
239 tmp = OPENSSL_malloc(len);
242 fprintf(stderr, "Memory allocation error\n");
246 for (i = 0; i < len; i++)
247 fprintf(out, "%02x", tmp[i]);
252 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
255 fprintf(out, "%s = ", name);
256 r = do_bn_print(out, bn);
263 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
265 char *keyword, *value, *p, *q;
266 strcpy(linebuf, olinebuf);
268 /* Skip leading space */
269 while (isspace((unsigned char)*keyword))
272 /* Look for = sign */
273 p = strchr(linebuf, '=');
281 /* Remove trailing space */
282 while (isspace((unsigned char)*q))
288 /* Remove leading space from value */
289 while (isspace((unsigned char)*value))
292 /* Remove trailing space from value */
293 p = value + strlen(value) - 1;
296 fprintf(stderr, "Warning: missing EOL\n");
298 while (*p == '\n' || isspace((unsigned char)*p))
306 BIGNUM *hex2bn(const char *in)
310 if (!do_hex2bn(&p, in))
316 /* To avoid extensive changes to test program at this stage just convert
317 * the input line into an acceptable form. Keyword lines converted to form
318 * "keyword = value\n" no matter what white space present, all other lines
319 * just have leading and trailing space removed.
322 int tidy_line(char *linebuf, char *olinebuf)
324 char *keyword, *value, *p, *q;
325 strcpy(linebuf, olinebuf);
327 /* Skip leading space */
328 while (isspace((unsigned char)*keyword))
330 /* Look for = sign */
331 p = strchr(linebuf, '=');
333 /* If no '=' just chop leading, trailing ws */
336 p = keyword + strlen(keyword) - 1;
337 while (*p == '\n' || isspace((unsigned char)*p))
339 strcpy(olinebuf, keyword);
340 strcat(olinebuf, "\n");
346 /* Remove trailing space */
347 while (isspace((unsigned char)*q))
353 /* Remove leading space from value */
354 while (isspace((unsigned char)*value))
357 /* Remove trailing space from value */
358 p = value + strlen(value) - 1;
360 while (*p == '\n' || isspace((unsigned char)*p))
363 strcpy(olinebuf, keyword);
364 strcat(olinebuf, " = ");
365 strcat(olinebuf, value);
366 strcat(olinebuf, "\n");
371 /* NB: this return the number of _bits_ read */
372 int bint2bin(const char *in, int len, unsigned char *out)
377 for(n=0 ; n < len ; ++n)
379 out[n/8]|=(0x80 >> (n%8));
383 int bin2bint(const unsigned char *in,int len,char *out)
387 for(n=0 ; n < len ; ++n)
388 out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
392 /*-----------------------------------------------*/
394 void PrintValue(char *tag, unsigned char *val, int len)
397 OutputValue(tag, val, len, stdout, 0);
401 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
408 olen=bin2bint(val,len,obuf);
409 fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
414 fprintf(rfp, "%s = ", tag);
415 for (i = 0; i < len; i++)
416 fprintf(rfp, "%02x", val[i]);
421 printf("%s = %.*s\n", tag, olen, obuf);
425 /* Not all platforms support strcasecmp and strncasecmp: implement versions
426 * in here to avoid need to include them in the validated module. Taken
427 * from crypto/o_str.c written by Richard Levitte (richard@levitte.org)
430 int fips_strncasecmp(const char *str1, const char *str2, size_t n)
432 while (*str1 && *str2 && n)
434 int res = toupper(*str1) - toupper(*str2);
435 if (res) return res < 0 ? -1 : 1;
449 int fips_strcasecmp(const char *str1, const char *str2)
451 return fips_strncasecmp(str1, str2, (size_t)-1);