CCM encrypt algorithm test support.
[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 #include <openssl/fips_rand.h>
53 #include <openssl/objects.h>
54
55 int hex2bin(const char *in, unsigned char *out);
56 unsigned char *hex2bin_m(const char *in, long *plen);
57 int do_hex2bn(BIGNUM **pr, const char *in);
58 int do_bn_print(FILE *out, const BIGNUM *bn);
59 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn);
60 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf);
61 BIGNUM *hex2bn(const char *in);
62 int bin2hex(const unsigned char *in,int len,char *out);
63 void pv(const char *tag,const unsigned char *val,int len);
64 int tidy_line(char *linebuf, char *olinebuf);
65 int bint2bin(const char *in, int len, unsigned char *out);
66 int bin2bint(const unsigned char *in,int len,char *out);
67 void PrintValue(char *tag, unsigned char *val, int len);
68 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode);
69 void fips_algtest_init(void);
70
71 static int no_err;
72
73 static void put_err_cb(int lib, int func,int reason,const char *file,int line)
74         {
75                 if (no_err)
76                         return;
77                 fprintf(stderr, "ERROR:%08lX:lib=%d,func=%d,reason=%d"
78                                 ":file=%s:line=%d\n",
79                         ERR_PACK(lib, func, reason),
80                         lib, func, reason, file, line);
81         }
82
83 static void add_err_cb(int num, va_list args)
84         {
85         int i;
86         char *str;
87         if (no_err)
88                 return;
89         fputs("\t", stderr);
90         for (i = 0; i < num; i++)
91                 {
92                 str = va_arg(args, char *);
93                 if (str)
94                         fputs(str, stderr);
95                 }
96         fputs("\n", stderr);
97         }
98
99 /* Dummy Entropy to keep DRBG happy. WARNING: THIS IS TOTALLY BOGUS
100  * HAS ZERO SECURITY AND MUST NOT BE USED IN REAL APPLICATIONS.
101  */
102
103 static unsigned char dummy_entropy[1024];
104
105 static size_t dummy_cb(DRBG_CTX *ctx, unsigned char **pout,
106                                 int entropy, size_t min_len, size_t max_len)
107         {
108         *pout = dummy_entropy;
109         return min_len;
110         }
111
112 static void fips_algtest_init_nofips(void)
113         {
114         DRBG_CTX *ctx;
115         FIPS_set_error_callbacks(put_err_cb, add_err_cb);
116         OPENSSL_cleanse(dummy_entropy, 1024);
117         ctx = FIPS_get_default_drbg();
118         FIPS_drbg_init(ctx, NID_aes_256_ctr, DRBG_FLAG_CTR_USE_DF);
119         FIPS_drbg_set_callbacks(ctx, dummy_cb, 0, dummy_cb, 0);
120         FIPS_drbg_instantiate(ctx, dummy_entropy, 10);
121         FIPS_rand_set_method(FIPS_drbg_method());
122         }
123
124 void fips_algtest_init(void)
125         {
126         fips_algtest_init_nofips();
127         if (!FIPS_mode_set(1))
128                 {
129                 fprintf(stderr, "Error entering FIPS mode\n");
130                 exit(1);
131                 }
132         }
133
134 int hex2bin(const char *in, unsigned char *out)
135     {
136     int n1, n2, isodd = 0;
137     unsigned char ch;
138
139     n1 = strlen(in);
140     if (in[n1 - 1] == '\n')
141         n1--;
142
143     if (n1 & 1)
144         isodd = 1;
145
146     for (n1=0,n2=0 ; in[n1] && in[n1] != '\n' ; )
147         { /* first byte */
148         if ((in[n1] >= '0') && (in[n1] <= '9'))
149             ch = in[n1++] - '0';
150         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
151             ch = in[n1++] - 'A' + 10;
152         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
153             ch = in[n1++] - 'a' + 10;
154         else
155             return -1;
156         if(!in[n1])
157             {
158             out[n2++]=ch;
159             break;
160             }
161         /* If input is odd length first digit is least significant: assumes
162          * all digits valid hex and null terminated which is true for the
163          * strings we pass.
164          */
165         if (n1 == 1 && isodd)
166                 {
167                 out[n2++] = ch;
168                 continue;
169                 }
170         out[n2] = ch << 4;
171         /* second byte */
172         if ((in[n1] >= '0') && (in[n1] <= '9'))
173             ch = in[n1++] - '0';
174         else if ((in[n1] >= 'A') && (in[n1] <= 'F'))
175             ch = in[n1++] - 'A' + 10;
176         else if ((in[n1] >= 'a') && (in[n1] <= 'f'))
177             ch = in[n1++] - 'a' + 10;
178         else
179             return -1;
180         out[n2++] |= ch;
181         }
182     return n2;
183     }
184
185 unsigned char *hex2bin_m(const char *in, long *plen)
186         {
187         unsigned char *p;
188         if (strlen(in) == 0)
189                 {
190                 *plen = 0;
191                 return OPENSSL_malloc(1);
192                 }
193         p = OPENSSL_malloc((strlen(in) + 1)/2);
194         *plen = hex2bin(in, p);
195         return p;
196         }
197
198 int do_hex2bn(BIGNUM **pr, const char *in)
199         {
200         unsigned char *p;
201         long plen;
202         int r = 0;
203         p = hex2bin_m(in, &plen);
204         if (!p)
205                 return 0;
206         if (!*pr)
207                 *pr = BN_new();
208         if (!*pr)
209                 return 0;
210         if (BN_bin2bn(p, plen, *pr))
211                 r = 1;
212         OPENSSL_free(p);
213         return r;
214         }
215
216 int do_bn_print(FILE *out, const BIGNUM *bn)
217         {
218         int len, i;
219         unsigned char *tmp;
220         len = BN_num_bytes(bn);
221         if (len == 0)
222                 {
223                 fputs("00", out);
224                 return 1;
225                 }
226
227         tmp = OPENSSL_malloc(len);
228         if (!tmp)
229                 {
230                 fprintf(stderr, "Memory allocation error\n");
231                 return 0;
232                 }
233         BN_bn2bin(bn, tmp);
234         for (i = 0; i < len; i++)
235                 fprintf(out, "%02x", tmp[i]);
236         OPENSSL_free(tmp);
237         return 1;
238         }
239
240 int do_bn_print_name(FILE *out, const char *name, const BIGNUM *bn)
241         {
242         int r;
243         fprintf(out, "%s = ", name);
244         r = do_bn_print(out, bn);
245         if (!r)
246                 return 0;
247         fputs("\n", out);
248         return 1;
249         }
250
251 int parse_line(char **pkw, char **pval, char *linebuf, char *olinebuf)
252         {
253         char *keyword, *value, *p, *q;
254         strcpy(linebuf, olinebuf);
255         keyword = linebuf;
256         /* Skip leading space */
257         while (isspace((unsigned char)*keyword))
258                 keyword++;
259
260         /* Look for = sign */
261         p = strchr(linebuf, '=');
262
263         /* If no '=' exit */
264         if (!p)
265                 return 0;
266
267         q = p - 1;
268
269         /* Remove trailing space */
270         while (isspace((unsigned char)*q))
271                 *q-- = 0;
272
273         *p = 0;
274         value = p + 1;
275
276         /* Remove leading space from value */
277         while (isspace((unsigned char)*value))
278                 value++;
279
280         /* Remove trailing space from value */
281         p = value + strlen(value) - 1;
282
283         while (*p == '\n' || isspace((unsigned char)*p))
284                 *p-- = 0;
285
286         *pkw = keyword;
287         *pval = value;
288         return 1;
289         }
290
291 BIGNUM *hex2bn(const char *in)
292     {
293     BIGNUM *p=NULL;
294
295     if (!do_hex2bn(&p, in))
296         return NULL;
297
298     return p;
299     }
300
301 int bin2hex(const unsigned char *in,int len,char *out)
302     {
303     int n1, n2;
304     unsigned char ch;
305
306     for (n1=0,n2=0 ; n1 < len ; ++n1)
307         {
308         ch=in[n1] >> 4;
309         if (ch <= 0x09)
310             out[n2++]=ch+'0';
311         else
312             out[n2++]=ch-10+'a';
313         ch=in[n1] & 0x0f;
314         if(ch <= 0x09)
315             out[n2++]=ch+'0';
316         else
317             out[n2++]=ch-10+'a';
318         }
319     out[n2]='\0';
320     return n2;
321     }
322
323 void pv(const char *tag,const unsigned char *val,int len)
324     {
325     char obuf[2048];
326
327     bin2hex(val,len,obuf);
328     printf("%s = %s\n",tag,obuf);
329     }
330
331 /* To avoid extensive changes to test program at this stage just convert
332  * the input line into an acceptable form. Keyword lines converted to form
333  * "keyword = value\n" no matter what white space present, all other lines
334  * just have leading and trailing space removed.
335  */
336
337 int tidy_line(char *linebuf, char *olinebuf)
338         {
339         char *keyword, *value, *p, *q;
340         strcpy(linebuf, olinebuf);
341         keyword = linebuf;
342         /* Skip leading space */
343         while (isspace((unsigned char)*keyword))
344                 keyword++;
345         /* Look for = sign */
346         p = strchr(linebuf, '=');
347
348         /* If no '=' just chop leading, trailing ws */
349         if (!p)
350                 {
351                 p = keyword + strlen(keyword) - 1;
352                 while (*p == '\n' || isspace((unsigned char)*p))
353                         *p-- = 0;
354                 strcpy(olinebuf, keyword);
355                 strcat(olinebuf, "\n");
356                 return 1;
357                 }
358
359         q = p - 1;
360
361         /* Remove trailing space */
362         while (isspace((unsigned char)*q))
363                 *q-- = 0;
364
365         *p = 0;
366         value = p + 1;
367
368         /* Remove leading space from value */
369         while (isspace((unsigned char)*value))
370                 value++;
371
372         /* Remove trailing space from value */
373         p = value + strlen(value) - 1;
374
375         while (*p == '\n' || isspace((unsigned char)*p))
376                 *p-- = 0;
377
378         strcpy(olinebuf, keyword);
379         strcat(olinebuf, " = ");
380         strcat(olinebuf, value);
381         strcat(olinebuf, "\n");
382
383         return 1;
384         }
385
386 /* NB: this return the number of _bits_ read */
387 int bint2bin(const char *in, int len, unsigned char *out)
388     {
389     int n;
390
391     memset(out,0,len);
392     for(n=0 ; n < len ; ++n)
393         if(in[n] == '1')
394             out[n/8]|=(0x80 >> (n%8));
395     return len;
396     }
397
398 int bin2bint(const unsigned char *in,int len,char *out)
399     {
400     int n;
401
402     for(n=0 ; n < len ; ++n)
403         out[n]=(in[n/8]&(0x80 >> (n%8))) ? '1' : '0';
404     return n;
405     }
406
407 /*-----------------------------------------------*/
408
409 void PrintValue(char *tag, unsigned char *val, int len)
410 {
411 #if VERBOSE
412   char obuf[2048];
413   int olen;
414   olen = bin2hex(val, len, obuf);
415   printf("%s = %.*s\n", tag, olen, obuf);
416 #endif
417 }
418
419 void OutputValue(char *tag, unsigned char *val, int len, FILE *rfp,int bitmode)
420     {
421     char obuf[2048];
422     int olen;
423
424     if(bitmode)
425         olen=bin2bint(val,len,obuf);
426     else
427         olen=bin2hex(val,len,obuf);
428
429     fprintf(rfp, "%s = %.*s\n", tag, olen, obuf);
430 #if VERBOSE
431     printf("%s = %.*s\n", tag, olen, obuf);
432 #endif
433     }
434