1 /* ====================================================================
2 * Copyright (c) 2007 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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.
50 #define OPENSSL_FIPSAPI
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);
69 static void put_err_cb(int lib, int func,int reason,const char *file,int line)
73 fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
75 ERR_PACK(lib, func, reason),
76 lib, func, reason, file, line);
79 static void add_err_cb(int num, va_list args)
86 for (i = 0; i < num; i++)
88 str = va_arg(args, char *);
95 static void fips_set_error_print(void)
97 FIPS_set_error_callbacks(put_err_cb, add_err_cb);
100 int hex2bin(const char *in, unsigned char *out)
102 int n1, n2, isodd = 0;
106 if (in[n1 - 1] == '\n')
112 for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
114 if ((in[n1] >= '0') && (in[n1] <= '9'))
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;
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
131 if (n1 == 1 && isodd)
138 if ((in[n1] >= '0') && (in[n1] <= '9'))
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;
151 unsigned char *hex2bin_m(const char *in, long *plen)
157 return OPENSSL_malloc(1);
159 p = OPENSSL_malloc((strlen(in) + 1)/2);
160 *plen = hex2bin(in, p);
164 int do_hex2bn(BIGNUM **pr, const char *in)
169 p = hex2bin_m(in, &plen);
176 if (BN_bin2bn(p, plen, *pr))
182 int do_bn_print(FILE *out, const BIGNUM *bn)
186 len = BN_num_bytes(bn);
193 tmp = OPENSSL_malloc(len);
196 fprintf(stderr, "Memory allocation error\n");
200 for (i = 0; i < len; i++)
201 fprintf(out, "%02x", tmp[i]);
206 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
209 fprintf(out, "%s = ", name);
210 r = do_bn_print(out, bn);
217 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
219 char *keyword, *value, *p, *q;
220 strcpy(linebuf, olinebuf);
222 /* Skip leading space */
223 while (isspace((unsigned char)*keyword))
226 /* Look for = sign */
227 p = strchr(linebuf, '=');
235 /* Remove trailing space */
236 while (isspace((unsigned char)*q))
242 /* Remove leading space from value */
243 while (isspace((unsigned char)*value))
246 /* Remove trailing space from value */
247 p = value + strlen(value) - 1;
249 while (*p == '\n' || isspace((unsigned char)*p))
257 BIGNUM *hex2bn(const char *in)
261 if (!do_hex2bn(&p, in))
267 int bin2hex(const unsigned char *in,int len,char *out)
272 for (n1=0,n2=0 ; n1 < len ; ++n1)
289 void pv(const char *tag,const unsigned char *val,int len)
293 bin2hex(val,len,obuf);
294 printf("%s = %s\n",tag,obuf);
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.
303 int tidy_line(char *linebuf, char *olinebuf)
305 char *keyword, *value, *p, *q;
306 strcpy(linebuf, olinebuf);
308 /* Skip leading space */
309 while (isspace((unsigned char)*keyword))
311 /* Look for = sign */
312 p = strchr(linebuf, '=');
314 /* If no '=' just chop leading, trailing ws */
317 p = keyword + strlen(keyword) - 1;
318 while (*p == '\n' || isspace((unsigned char)*p))
320 strcpy(olinebuf, keyword);
321 strcat(olinebuf, "\n");
327 /* Remove trailing space */
328 while (isspace((unsigned char)*q))
334 /* Remove leading space from value */
335 while (isspace((unsigned char)*value))
338 /* Remove trailing space from value */
339 p = value + strlen(value) - 1;
341 while (*p == '\n' || isspace((unsigned char)*p))
344 strcpy(olinebuf, keyword);
345 strcat(olinebuf, " = ");
346 strcat(olinebuf, value);
347 strcat(olinebuf, "\n");
352 /* NB: this return the number of _bits_ read */
353 int bint2bin(const char *in, int len, unsigned char *out)
358 for(n=0 ; n < len ; ++n)
360 out[n/8]|=(0x80 >> (n%8));
364 int bin2bint(const unsigned char *in,int len,char *out)
368 for(n=0 ; n < len ; ++n)
369 out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
373 /*-----------------------------------------------*/
375 void PrintValue(char *tag, unsigned char *val, int len)
380 olen = bin2hex(val, len, obuf);
381 printf("%s = %.*s\n", tag, olen, obuf);
385 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
391 olen=bin2bint(val,len,obuf);
393 olen=bin2hex(val,len,obuf);
395 fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
397 printf("%s = %.*s\n", tag, olen, obuf);