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