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