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