Have all algorithm test programs call fips_algtest_init() at startup:
[openssl.git] / fips / fips_utl.h
1 /* ====================================================================
2  * Copyright (c) 2007 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 #define OPENSSL_FIPSAPI
51
52 int hex2bin(const char *in, unsigned char *out);
53 unsigned char *hex2bin_m(const char *in, long *plen);
54 int do_hex2bn(BIGNUM **pr, const char *in);
55 int do_bn_print(FILE *out, const BIGNUM *bn);
56 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
57 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
58 BIGNUM *hex2bn(const char *in);
59 int bin2hex(const unsigned char *in,int len,char *out);
60 void pv(const char *tag,const unsigned char *val,int len);
61 int tidy_line(char *linebuf, char *olinebuf);
62 int bint2bin(const char *in, int len, unsigned char *out);
63 int bin2bint(const unsigned char *in,int len,char *out);
64 void PrintValue(char *tag, unsigned char *val, int len);
65 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode);
66 void fips_algtest_init(void);
67
68 static int no_err;
69
70 static void put_err_cb(int lib, int func,int reason,const char *file,int line)
71         {
72                 if (no_err)
73                         return;
74                 fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
75                                 ":file=%s:line=%d\n",
76                         ERR_PACK(lib, func, reason),
77                         lib, func, reason, file, line);
78         }
79
80 static void add_err_cb(int num, va_list args)
81         {
82         int i;
83         char *str;
84         if (no_err)
85                 return;
86         fputs("\t", stderr);
87         for (i = 0; i < num; i++)
88                 {
89                 str = va_arg(args, char *);
90                 if (str)
91                         fputs(str, stderr);
92                 }
93         fputs("\n", stderr);
94         }
95
96 static void fips_set_error_print(void)
97         {
98         FIPS_set_error_callbacks(put_err_cb, add_err_cb);
99         }
100
101 void fips_algtest_init(void)
102         {
103         fips_set_error_print();
104         if (!FIPS_mode_set(1))
105                 {
106                 fprintf(stderr, "Error entering FIPS mode\n");
107                 exit(1);
108                 }
109         }
110
111 int hex2bin(const char *in, unsigned char *out)
112     {
113     int n1, n2, isodd = 0;
114     unsigned char ch;
115
116     n1 = strlen(in);
117     if (in[n1 - 1] == '\n')
118         n1--;
119
120     if (n1 & 1)
121         isodd = 1;
122
123     for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
124         { /* first byte */
125         if ((in[n1] >= '0') && (in[n1] <= '9'))
126             ch = in[n1++] - '0';
127         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
128             ch = in[n1++] - 'A' + 10;
129         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
130             ch = in[n1++] - 'a' + 10;
131         else
132             return -1;
133         if(!in[n1])
134             {
135             out[n2++]=ch;
136             break;
137             }
138         /* If input is odd length first digit is least significant: assumes
139          * all digits valid hex and null terminated which is true for the
140          * strings we pass.
141          */
142         if (n1 == 1 && isodd)
143                 {
144                 out[n2++] = ch;
145                 continue;
146                 }
147         out[n2] = ch << 4;
148         /* second byte */
149         if ((in[n1] >= '0') && (in[n1] <= '9'))
150             ch = in[n1++] - '0';
151         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
152             ch = in[n1++] - 'A' + 10;
153         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
154             ch = in[n1++] - 'a' + 10;
155         else
156             return -1;
157         out[n2++] |= ch;
158         }
159     return n2;
160     }
161
162 unsigned char *hex2bin_m(const char *in, long *plen)
163         {
164         unsigned char *p;
165         if (strlen(in) == 0)
166                 {
167                 *plen = 0;
168                 return OPENSSL_malloc(1);
169                 }
170         p = OPENSSL_malloc((strlen(in) + 1)/2);
171         *plen = hex2bin(in, p);
172         return p;
173         }
174
175 int do_hex2bn(BIGNUM **pr, const char *in)
176         {
177         unsigned char *p;
178         long plen;
179         int r = 0;
180         p = hex2bin_m(in, &plen);
181         if (!p)
182                 return 0;
183         if (!*pr)
184                 *pr = BN_new();
185         if (!*pr)
186                 return 0;
187         if (BN_bin2bn(p, plen, *pr))
188                 r = 1;
189         OPENSSL_free(p);
190         return r;
191         }
192
193 int do_bn_print(FILE *out, const BIGNUM *bn)
194         {
195         int len, i;
196         unsigned char *tmp;
197         len = BN_num_bytes(bn);
198         if (len == 0)
199                 {
200                 fputs("00", out);
201                 return 1;
202                 }
203
204         tmp = OPENSSL_malloc(len);
205         if (!tmp)
206                 {
207                 fprintf(stderr, "Memory allocation error\n");
208                 return 0;
209                 }
210         BN_bn2bin(bn, tmp);
211         for (i = 0; i < len; i++)
212                 fprintf(out, "%02x", tmp[i]);
213         OPENSSL_free(tmp);
214         return 1;
215         }
216
217 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
218         {
219         int r;
220         fprintf(out, "%s = ", name);
221         r = do_bn_print(out, bn);
222         if (!r)
223                 return 0;
224         fputs("\n", out);
225         return 1;
226         }
227
228 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
229         {
230         char *keyword, *value, *p, *q;
231         strcpy(linebuf, olinebuf);
232         keyword = linebuf;
233         /* Skip leading space */
234         while (isspace((unsigned char)*keyword))
235                 keyword++;
236
237         /* Look for = sign */
238         p = strchr(linebuf, '=');
239
240         /* If no '=' exit */
241         if (!p)
242                 return 0;
243
244         q = p - 1;
245
246         /* Remove trailing space */
247         while (isspace((unsigned char)*q))
248                 *q-- = 0;
249
250         *p = 0;
251         value = p + 1;
252
253         /* Remove leading space from value */
254         while (isspace((unsigned char)*value))
255                 value++;
256
257         /* Remove trailing space from value */
258         p = value + strlen(value) - 1;
259
260         while (*p == '\n' || isspace((unsigned char)*p))
261                 *p-- = 0;
262
263         *pkw = keyword;
264         *pval = value;
265         return 1;
266         }
267
268 BIGNUM *hex2bn(const char *in)
269     {
270     BIGNUM *p=NULL;
271
272     if (!do_hex2bn(&p, in))
273         return NULL;
274
275     return p;
276     }
277
278 int bin2hex(const unsigned char *in,int len,char *out)
279     {
280     int n1, n2;
281     unsigned char ch;
282
283     for (n1=0,n2=0 ; n1 < len ; ++n1)
284         {
285         ch=in[n1] >> 4;
286         if (ch <= 0x09)
287             out[n2++]=ch+'0';
288         else
289             out[n2++]=ch-10+'a';
290         ch=in[n1] & 0x0f;
291         if(ch <= 0x09)
292             out[n2++]=ch+'0';
293         else
294             out[n2++]=ch-10+'a';
295         }
296     out[n2]='\0';
297     return n2;
298     }
299
300 void pv(const char *tag,const unsigned char *val,int len)
301     {
302     char obuf[2048];
303
304     bin2hex(val,len,obuf);
305     printf("%s = %s\n",tag,obuf);
306     }
307
308 /* To avoid extensive changes to test program at this stage just convert
309  * the input line into an acceptable form. Keyword lines converted to form
310  * "keyword = value\n" no matter what white space present, all other lines
311  * just have leading and trailing space removed.
312  */
313
314 int tidy_line(char *linebuf, char *olinebuf)
315         {
316         char *keyword, *value, *p, *q;
317         strcpy(linebuf, olinebuf);
318         keyword = linebuf;
319         /* Skip leading space */
320         while (isspace((unsigned char)*keyword))
321                 keyword++;
322         /* Look for = sign */
323         p = strchr(linebuf, '=');
324
325         /* If no '=' just chop leading, trailing ws */
326         if (!p)
327                 {
328                 p = keyword + strlen(keyword) - 1;
329                 while (*p == '\n' || isspace((unsigned char)*p))
330                         *p-- = 0;
331                 strcpy(olinebuf, keyword);
332                 strcat(olinebuf, "\n");
333                 return 1;
334                 }
335
336         q = p - 1;
337
338         /* Remove trailing space */
339         while (isspace((unsigned char)*q))
340                 *q-- = 0;
341
342         *p = 0;
343         value = p + 1;
344
345         /* Remove leading space from value */
346         while (isspace((unsigned char)*value))
347                 value++;
348
349         /* Remove trailing space from value */
350         p = value + strlen(value) - 1;
351
352         while (*p == '\n' || isspace((unsigned char)*p))
353                 *p-- = 0;
354
355         strcpy(olinebuf, keyword);
356         strcat(olinebuf, " = ");
357         strcat(olinebuf, value);
358         strcat(olinebuf, "\n");
359
360         return 1;
361         }
362
363 /* NB: this return the number of _bits_ read */
364 int bint2bin(const char *in, int len, unsigned char *out)
365     {
366     int n;
367
368     memset(out,0,len);
369     for(n=0 ; n < len ; ++n)
370         if(in[n] == '1')
371             out[n/8]|=(0x80 >> (n%8));
372     return len;
373     }
374
375 int bin2bint(const unsigned char *in,int len,char *out)
376     {
377     int n;
378
379     for(n=0 ; n < len ; ++n)
380         out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
381     return n;
382     }
383
384 /*-----------------------------------------------*/
385
386 void PrintValue(char *tag, unsigned char *val, int len)
387 {
388 #if VERBOSE
389   char obuf[2048];
390   int olen;
391   olen = bin2hex(val, len, obuf);
392   printf("%s = %.*s\n", tag, olen, obuf);
393 #endif
394 }
395
396 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
397     {
398     char obuf[2048];
399     int olen;
400
401     if(bitmode)
402         olen=bin2bint(val,len,obuf);
403     else
404         olen=bin2hex(val,len,obuf);
405
406     fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
407 #if VERBOSE
408     printf("%s = %.*s\n", tag, olen, obuf);
409 #endif
410     }
411