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