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