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