]>
git.ipfire.org Git - thirdparty/openssl.git/blob - fips/fips_utl.h
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
51 #include <openssl/fips.h>
53 int hex2bin(const char *in
, unsigned char *out
);
54 unsigned char *hex2bin_m(const char *in
, long *plen
);
55 int do_hex2bn(BIGNUM
**pr
, const char *in
);
56 int do_bn_print(FILE *out
, BIGNUM
*bn
);
57 int do_bn_print_name(FILE *out
, const char *name
, BIGNUM
*bn
);
58 int parse_line(char **pkw
, char **pval
, char *linebuf
, char *olinebuf
);
59 BIGNUM
*hex2bn(const char *in
);
60 int bin2hex(const unsigned char *in
,int len
,char *out
);
61 void pv(const char *tag
,const unsigned char *val
,int len
);
62 int tidy_line(char *linebuf
, char *olinebuf
);
63 int bint2bin(const char *in
, int len
, unsigned char *out
);
64 int bin2bint(const unsigned char *in
,int len
,char *out
);
65 void PrintValue(char *tag
, unsigned char *val
, int len
);
66 void OutputValue(char *tag
, unsigned char *val
, int len
, FILE *rfp
,int bitmode
);
70 static void put_err_cb(int lib
, int func
,int reason
,const char *file
,int line
)
74 fprintf(stderr
, "ERROR:lib=%d,func=%d,reason=%d"
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
)
105 for (n1
=0,n2
=0 ; in
[n1
] && in
[n1
] != '\n' ; )
107 if ((in
[n1
] >= '0') && (in
[n1
] <= '9'))
109 else if ((in
[n1
] >= 'A') && (in
[n1
] <= 'F'))
110 ch
= in
[n1
++] - 'A' + 10;
111 else if ((in
[n1
] >= 'a') && (in
[n1
] <= 'f'))
112 ch
= in
[n1
++] - 'a' + 10;
122 if ((in
[n1
] >= '0') && (in
[n1
] <= '9'))
124 else if ((in
[n1
] >= 'A') && (in
[n1
] <= 'F'))
125 ch
= in
[n1
++] - 'A' + 10;
126 else if ((in
[n1
] >= 'a') && (in
[n1
] <= 'f'))
127 ch
= in
[n1
++] - 'a' + 10;
135 unsigned char *hex2bin_m(const char *in
, long *plen
)
138 p
= OPENSSL_malloc((strlen(in
) + 1)/2);
139 *plen
= hex2bin(in
, p
);
143 int do_hex2bn(BIGNUM
**pr
, const char *in
)
148 p
= hex2bin_m(in
, &plen
);
155 if (BN_bin2bn(p
, plen
, *pr
))
161 int do_bn_print(FILE *out
, BIGNUM
*bn
)
165 len
= BN_num_bytes(bn
);
172 tmp
= OPENSSL_malloc(len
);
175 fprintf(stderr
, "Memory allocation error\n");
179 for (i
= 0; i
< len
; i
++)
180 fprintf(out
, "%02x", tmp
[i
]);
185 int do_bn_print_name(FILE *out
, const char *name
, BIGNUM
*bn
)
188 fprintf(out
, "%s = ", name
);
189 r
= do_bn_print(out
, bn
);
196 int parse_line(char **pkw
, char **pval
, char *linebuf
, char *olinebuf
)
198 char *keyword
, *value
, *p
, *q
;
199 strcpy(linebuf
, olinebuf
);
201 /* Skip leading space */
202 while (isspace((unsigned char)*keyword
))
205 /* Look for = sign */
206 p
= strchr(linebuf
, '=');
214 /* Remove trailing space */
215 while (isspace((unsigned char)*q
))
221 /* Remove leading space from value */
222 while (isspace((unsigned char)*value
))
225 /* Remove trailing space from value */
226 p
= value
+ strlen(value
) - 1;
228 while (*p
== '\n' || isspace((unsigned char)*p
))
236 BIGNUM
*hex2bn(const char *in
)
240 if (!do_hex2bn(&p
, in
))
246 int bin2hex(const unsigned char *in
,int len
,char *out
)
251 for (n1
=0,n2
=0 ; n1
< len
; ++n1
)
268 void pv(const char *tag
,const unsigned char *val
,int len
)
272 bin2hex(val
,len
,obuf
);
273 printf("%s = %s\n",tag
,obuf
);
276 /* To avoid extensive changes to test program at this stage just convert
277 * the input line into an acceptable form. Keyword lines converted to form
278 * "keyword = value\n" no matter what white space present, all other lines
279 * just have leading and trailing space removed.
282 int tidy_line(char *linebuf
, char *olinebuf
)
284 char *keyword
, *value
, *p
, *q
;
285 strcpy(linebuf
, olinebuf
);
287 /* Skip leading space */
288 while (isspace((unsigned char)*keyword
))
290 /* Look for = sign */
291 p
= strchr(linebuf
, '=');
293 /* If no '=' just chop leading, trailing ws */
296 p
= keyword
+ strlen(keyword
) - 1;
297 while (*p
== '\n' || isspace((unsigned char)*p
))
299 strcpy(olinebuf
, keyword
);
300 strcat(olinebuf
, "\n");
306 /* Remove trailing space */
307 while (isspace((unsigned char)*q
))
313 /* Remove leading space from value */
314 while (isspace((unsigned char)*value
))
317 /* Remove trailing space from value */
318 p
= value
+ strlen(value
) - 1;
320 while (*p
== '\n' || isspace((unsigned char)*p
))
323 strcpy(olinebuf
, keyword
);
324 strcat(olinebuf
, " = ");
325 strcat(olinebuf
, value
);
326 strcat(olinebuf
, "\n");
331 /* NB: this return the number of _bits_ read */
332 int bint2bin(const char *in
, int len
, unsigned char *out
)
337 for(n
=0 ; n
< len
; ++n
)
339 out
[n
/8]|=(0x80 >> (n
%8));
343 int bin2bint(const unsigned char *in
,int len
,char *out
)
347 for(n
=0 ; n
< len
; ++n
)
348 out
[n
]=(in
[n
/8]&(0x80 >> (n
%8))) ? '1' : '0';
352 /*-----------------------------------------------*/
354 void PrintValue(char *tag
, unsigned char *val
, int len
)
359 olen
= bin2hex(val
, len
, obuf
);
360 printf("%s = %.*s\n", tag
, olen
, obuf
);
364 void OutputValue(char *tag
, unsigned char *val
, int len
, FILE *rfp
,int bitmode
)
370 olen
=bin2bint(val
,len
,obuf
);
372 olen
=bin2hex(val
,len
,obuf
);
374 fprintf(rfp
, "%s = %.*s\n", tag
, olen
, obuf
);
376 printf("%s = %.*s\n", tag
, olen
, obuf
);