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