]> git.ipfire.org Git - thirdparty/openssl.git/blob - fips/fips_utl.h
Add single call public key sign and verify functions.
[thirdparty/openssl.git] / fips / fips_utl.h
1 /* ====================================================================
2 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
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
14 * distribution.
15 *
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/)"
20 *
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.
25 *
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.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
34 *
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.
47 *
48 */
49
50 #ifndef FIPS_UTL_H
51 #define FIPS_UTL_H
52
53 #define OPENSSL_FIPSAPI
54
55 #include <openssl/fips_rand.h>
56 #include <openssl/objects.h>
57
58 #ifdef OPENSSL_SYS_WIN32
59 #define RESP_EOL "\n"
60 #else
61 #define RESP_EOL "\r\n"
62 #endif
63
64 #ifndef FIPS_AUTH_OFFICER_PASS
65 #define FIPS_AUTH_OFFICER_PASS "Default FIPS Crypto Officer Password"
66 #endif
67
68 #ifndef FIPS_AUTH_USER_PASS
69 #define FIPS_AUTH_USER_PASS "Default FIPS Crypto User Password"
70 #endif
71
72
73 int hex2bin(const char *in, unsigned char *out);
74 unsigned char *hex2bin_m(const char *in, long *plen);
75 int do_hex2bn(BIGNUM **pr, const char *in);
76 int do_bn_print(FILE *out, const BIGNUM *bn);
77 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
78 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
79 int parse_line2(char **pkw, char **pval, char *linebuf, char *olinebuf, int eol);
80 BIGNUM *hex2bn(const char *in);
81 int tidy_line(char *linebuf, char *olinebuf);
82 int copy_line(const char *in, FILE *ofp);
83 int bint2bin(const char *in, int len, unsigned char *out);
84 int bin2bint(const unsigned char *in,int len,char *out);
85 void PrintValue(char *tag, unsigned char *val, int len);
86 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode);
87 void fips_algtest_init(void);
88 void do_entropy_stick(void);
89 int fips_strncasecmp(const char *str1, const char *str2, size_t n);
90 int fips_strcasecmp(const char *str1, const char *str2);
91
92 static int no_err;
93
94 static void put_err_cb(int lib, int func,int reason,const char *file,int line)
95 {
96 if (no_err)
97 return;
98 fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
99 ":file=%s:line=%d\n",
100 ERR_PACK(lib, func, reason),
101 lib, func, reason, file, line);
102 }
103
104 static void add_err_cb(int num, va_list args)
105 {
106 int i;
107 char *str;
108 if (no_err)
109 return;
110 fputs("\t", stderr);
111 for (i = 0; i < num; i++)
112 {
113 str = va_arg(args, char *);
114 if (str)
115 fputs(str, stderr);
116 }
117 fputs("\n", stderr);
118 }
119
120 /* Dummy Entropy to keep DRBG happy. WARNING: THIS IS TOTALLY BOGUS
121 * HAS ZERO SECURITY AND MUST NOT BE USED IN REAL APPLICATIONS.
122 */
123
124 static unsigned char dummy_entropy[1024];
125
126 static size_t dummy_cb(DRBG_CTX *ctx, unsigned char **pout,
127 int entropy, size_t min_len, size_t max_len)
128 {
129 *pout = dummy_entropy;
130 return min_len;
131 }
132
133 static int entropy_stick = 0;
134
135 static void fips_algtest_init_nofips(void)
136 {
137 DRBG_CTX *ctx;
138 size_t i;
139 FIPS_set_error_callbacks(put_err_cb, add_err_cb);
140 for (i = 0; i < sizeof(dummy_entropy); i++)
141 dummy_entropy[i] = i & 0xff;
142 if (entropy_stick)
143 memcpy(dummy_entropy + 32, dummy_entropy + 16, 16);
144 ctx = FIPS_get_default_drbg();
145 FIPS_drbg_init(ctx, NID_aes_256_ctr, DRBG_FLAG_CTR_USE_DF);
146 FIPS_drbg_set_callbacks(ctx, dummy_cb, 0, 16, dummy_cb, 0);
147 FIPS_drbg_instantiate(ctx, dummy_entropy, 10);
148 FIPS_rand_set_method(FIPS_drbg_method());
149 }
150
151 void do_entropy_stick(void)
152 {
153 entropy_stick = 1;
154 }
155
156 void fips_algtest_init(void)
157 {
158 fips_algtest_init_nofips();
159 if (!FIPS_module_mode_set(1, FIPS_AUTH_USER_PASS))
160 {
161 fprintf(stderr, "Error entering FIPS mode\n");
162 exit(1);
163 }
164 }
165
166 int hex2bin(const char *in, unsigned char *out)
167 {
168 int n1, n2, isodd = 0;
169 unsigned char ch;
170
171 n1 = strlen(in);
172 if (in[n1 - 1] == '\n')
173 n1--;
174
175 if (n1 & 1)
176 isodd = 1;
177
178 for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
179 { /* first byte */
180 if ((in[n1] >= '0') && (in[n1] <= '9'))
181 ch = in[n1++] - '0';
182 else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
183 ch = in[n1++] - 'A' + 10;
184 else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
185 ch = in[n1++] - 'a' + 10;
186 else
187 return -1;
188 if(!in[n1])
189 {
190 out[n2++]=ch;
191 break;
192 }
193 /* If input is odd length first digit is least significant: assumes
194 * all digits valid hex and null terminated which is true for the
195 * strings we pass.
196 */
197 if (n1 == 1 && isodd)
198 {
199 out[n2++] = ch;
200 continue;
201 }
202 out[n2] = ch << 4;
203 /* second byte */
204 if ((in[n1] >= '0') && (in[n1] <= '9'))
205 ch = in[n1++] - '0';
206 else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
207 ch = in[n1++] - 'A' + 10;
208 else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
209 ch = in[n1++] - 'a' + 10;
210 else
211 return -1;
212 out[n2++] |= ch;
213 }
214 return n2;
215 }
216
217 unsigned char *hex2bin_m(const char *in, long *plen)
218 {
219 unsigned char *p;
220 if (strlen(in) == 0)
221 {
222 *plen = 0;
223 return OPENSSL_malloc(1);
224 }
225 p = OPENSSL_malloc((strlen(in) + 1)/2);
226 *plen = hex2bin(in, p);
227 return p;
228 }
229
230 int do_hex2bn(BIGNUM **pr, const char *in)
231 {
232 unsigned char *p;
233 long plen;
234 int r = 0;
235 p = hex2bin_m(in, &plen);
236 if (!p)
237 return 0;
238 if (!*pr)
239 *pr = BN_new();
240 if (!*pr)
241 return 0;
242 if (BN_bin2bn(p, plen, *pr))
243 r = 1;
244 OPENSSL_free(p);
245 return r;
246 }
247
248 int do_bn_print(FILE *out, const BIGNUM *bn)
249 {
250 int len, i;
251 unsigned char *tmp;
252 len = BN_num_bytes(bn);
253 if (len == 0)
254 {
255 fputs("00", out);
256 return 1;
257 }
258
259 tmp = OPENSSL_malloc(len);
260 if (!tmp)
261 {
262 fprintf(stderr, "Memory allocation error\n");
263 return 0;
264 }
265 BN_bn2bin(bn, tmp);
266 for (i = 0; i < len; i++)
267 fprintf(out, "%02x", tmp[i]);
268 OPENSSL_free(tmp);
269 return 1;
270 }
271
272 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
273 {
274 int r;
275 fprintf(out, "%s = ", name);
276 r = do_bn_print(out, bn);
277 if (!r)
278 return 0;
279 fputs(RESP_EOL, out);
280 return 1;
281 }
282
283 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
284 {
285 return parse_line2(pkw, pval, linebuf, olinebuf, 1);
286 }
287
288 int parse_line2(char **pkw, char **pval, char *linebuf, char *olinebuf, int eol)
289 {
290 char *keyword, *value, *p, *q;
291 strcpy(linebuf, olinebuf);
292 keyword = linebuf;
293 /* Skip leading space */
294 while (isspace((unsigned char)*keyword))
295 keyword++;
296
297 /* Look for = sign */
298 p = strchr(linebuf, '=');
299
300 /* If no '=' exit */
301 if (!p)
302 return 0;
303
304 q = p - 1;
305
306 /* Remove trailing space */
307 while (isspace((unsigned char)*q))
308 *q-- = 0;
309
310 *p = 0;
311 value = p + 1;
312
313 /* Remove leading space from value */
314 while (isspace((unsigned char)*value))
315 value++;
316
317 /* Remove trailing space from value */
318 p = value + strlen(value) - 1;
319
320 if (eol && *p != '\n')
321 fprintf(stderr, "Warning: missing EOL\n");
322
323 while (*p == '\n' || isspace((unsigned char)*p))
324 *p-- = 0;
325
326 *pkw = keyword;
327 *pval = value;
328 return 1;
329 }
330
331 BIGNUM *hex2bn(const char *in)
332 {
333 BIGNUM *p=NULL;
334
335 if (!do_hex2bn(&p, in))
336 return NULL;
337
338 return p;
339 }
340
341 /* To avoid extensive changes to test program at this stage just convert
342 * the input line into an acceptable form. Keyword lines converted to form
343 * "keyword = value\n" no matter what white space present, all other lines
344 * just have leading and trailing space removed.
345 */
346
347 int tidy_line(char *linebuf, char *olinebuf)
348 {
349 char *keyword, *value, *p, *q;
350 strcpy(linebuf, olinebuf);
351 keyword = linebuf;
352 /* Skip leading space */
353 while (isspace((unsigned char)*keyword))
354 keyword++;
355 /* Look for = sign */
356 p = strchr(linebuf, '=');
357
358 /* If no '=' just chop leading, trailing ws */
359 if (!p)
360 {
361 p = keyword + strlen(keyword) - 1;
362 while (*p == '\n' || isspace((unsigned char)*p))
363 *p-- = 0;
364 strcpy(olinebuf, keyword);
365 strcat(olinebuf, "\n");
366 return 1;
367 }
368
369 q = p - 1;
370
371 /* Remove trailing space */
372 while (isspace((unsigned char)*q))
373 *q-- = 0;
374
375 *p = 0;
376 value = p + 1;
377
378 /* Remove leading space from value */
379 while (isspace((unsigned char)*value))
380 value++;
381
382 /* Remove trailing space from value */
383 p = value + strlen(value) - 1;
384
385 while (*p == '\n' || isspace((unsigned char)*p))
386 *p-- = 0;
387
388 strcpy(olinebuf, keyword);
389 strcat(olinebuf, " = ");
390 strcat(olinebuf, value);
391 strcat(olinebuf, "\n");
392
393 return 1;
394 }
395 /* Copy supplied line to ofp replacing \n with \r\n */
396 int copy_line(const char *in, FILE *ofp)
397 {
398 const char *p;
399 p = strchr(in, '\n');
400 if (p)
401 {
402 fwrite(in, 1, (size_t)(p - in), ofp);
403 fputs(RESP_EOL, ofp);
404 }
405 else
406 fputs(in, ofp);
407 return 1;
408 }
409
410 /* NB: this return the number of _bits_ read */
411 int bint2bin(const char *in, int len, unsigned char *out)
412 {
413 int n;
414
415 memset(out,0,len);
416 for(n=0 ; n < len ; ++n)
417 if(in[n] == '1')
418 out[n/8]|=(0x80 >> (n%8));
419 return len;
420 }
421
422 int bin2bint(const unsigned char *in,int len,char *out)
423 {
424 int n;
425
426 for(n=0 ; n < len ; ++n)
427 out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
428 return n;
429 }
430
431 /*-----------------------------------------------*/
432
433 void PrintValue(char *tag, unsigned char *val, int len)
434 {
435 #ifdef VERBOSE
436 OutputValue(tag, val, len, stdout, 0);
437 #endif
438 }
439
440 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
441 {
442 char obuf[2048];
443 int olen;
444
445 if(bitmode)
446 {
447 olen=bin2bint(val,len,obuf);
448 fprintf(rfp, "%s = %.*s" RESP_EOL, tag, olen, obuf);
449 }
450 else
451 {
452 int i;
453 fprintf(rfp, "%s = ", tag);
454 for (i = 0; i < len; i++)
455 fprintf(rfp, "%02x", val[i]);
456 fputs(RESP_EOL, rfp);
457 }
458
459 #if VERBOSE
460 printf("%s = %.*s\n", tag, olen, obuf);
461 #endif
462 }
463
464 /* Not all platforms support strcasecmp and strncasecmp: implement versions
465 * in here to avoid need to include them in the validated module. Taken
466 * from crypto/o_str.c written by Richard Levitte (richard@levitte.org)
467 */
468
469 int fips_strncasecmp(const char *str1, const char *str2, size_t n)
470 {
471 while (*str1 && *str2 && n)
472 {
473 int res = toupper(*str1) - toupper(*str2);
474 if (res) return res < 0 ? -1 : 1;
475 str1++;
476 str2++;
477 n--;
478 }
479 if (n == 0)
480 return 0;
481 if (*str1)
482 return 1;
483 if (*str2)
484 return -1;
485 return 0;
486 }
487
488 int fips_strcasecmp(const char *str1, const char *str2)
489 {
490 return fips_strncasecmp(str1, str2, (size_t)-1);
491 }
492
493
494 #endif