1 /*	$OpenBSD: cryptotest.c,v 1.2 2016/09/02 16:54:28 mikeb Exp $	*/
2 /*	$EOM: cryptotest.c,v 1.5 1998/10/07 16:40:49 niklas Exp $	*/
3 
4 /*
5  * Copyright (c) 1998 Niels Provos.  All rights reserved.
6  * Copyright (c) 2001 Niklas Hallqvist.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * This code was written under funding by Ericsson Radio Systems.
31  */
32 
33 #include <sys/param.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 #include "crypto.h"
40 
41 void test_crypto (enum transform);
42 
43 #define SET_KEY(x,y) {size_t i; for (i=0; i < (y); i++) (x)[i] = i;}
44 
45 int
46 verify_buf (u_int8_t *buf, u_int16_t len)
47 {
48   int i;
49 
50   for (i = 0; i < len; i++)
51     if (buf[i] != i)
52       return 0;
53 
54   return 1;
55 }
56 
57 #define nibble2bin(y) (tolower((y)) < 'a' ? (y) - '0': tolower((y)) - 'a' + 10)
58 #define hexchar2bin(x) ((nibble2bin((x)[0]) << 4) + nibble2bin((x)[1]))
59 #define nibble2c(x) ((x) >= 10 ? ('a'-10+(x)) : ('0' + (x)))
60 
61 static void asc2bin (u_int8_t *bin, u_int8_t *asc, u_int16_t len)
62 {
63   int i;
64 
65   for (i = 0; i < len; i += 2, asc += 2)
66     {
67       *bin++ = hexchar2bin(asc);
68     }
69 }
70 
71 void
72 special_test_blf (void)
73 {
74   u_int8_t *akey = "0123456789ABCDEFF0E1D2C3B4A59687";
75   u_int8_t *aiv = "FEDCBA9876543210";
76   u_int8_t data[] = "7654321 Now is the time for \0\0\0"; /* len 29 */
77   u_int8_t *acipher
78     = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CCE7";
79   u_int8_t key[16], cipher[32], iv[8];
80   struct crypto_xf *xf;
81   struct keystate *ks;
82   enum cryptoerr err;
83   int i;
84 
85   asc2bin (key, akey, strlen (akey));
86   asc2bin (iv, aiv, strlen (aiv));
87   asc2bin (cipher, acipher, 64);
88 
89   xf = crypto_get (BLOWFISH_CBC);
90   printf ("Special Test-Case %s: ", xf->name);
91 
92   ks = crypto_init (xf, key, 16, &err);
93   if (!ks)
94     {
95       printf ("FAILED (init %d)", err);
96       goto fail;
97     }
98 
99   crypto_init_iv (ks, iv, xf->blocksize);
100   crypto_encrypt (ks, data, 32);
101 
102   for (i = 0; i < 32; i++)
103     if (data[i] != cipher[i])
104 	break;
105   if (i < 32)
106     printf ("FAILED ");
107   else
108     printf ("OKAY ");
109 
110   free (ks);
111 
112 fail:
113   printf ("\n");
114   return;
115 }
116 
117 int
118 main (void)
119 {
120   test_crypto (TRIPLEDES_CBC);
121 
122   test_crypto (BLOWFISH_CBC);
123 
124   test_crypto (CAST_CBC);
125 
126   test_crypto (AES_CBC);
127 
128   special_test_blf ();
129 
130   return 0;
131 }
132 
133 void
134 dump_buf (u_int8_t *buf, size_t len)
135 {
136   size_t i;
137 
138   for (i = 0; i < len; i++)
139     printf ("%02x ", buf[i]);
140   printf ("\n");
141 }
142 
143 void
144 test_crypto (enum transform which)
145 {
146   u_int8_t buf[256];
147   struct crypto_xf *xf;
148   struct keystate *ks;
149   enum cryptoerr err;
150 
151   xf = crypto_get (which);
152   printf ("Testing %s: ", xf->name);
153 
154   SET_KEY (buf, xf->keymax);
155   ks = crypto_init (xf, buf, xf->keymax, &err);
156   if (!ks)
157     {
158       printf ("FAILED (init %d)", err);
159       goto fail;
160     }
161   SET_KEY (buf, sizeof (buf));
162   crypto_init_iv (ks, buf, xf->blocksize);
163   crypto_encrypt (ks, buf, sizeof (buf));
164   dump_buf (buf, sizeof buf);
165   crypto_decrypt (ks, buf, sizeof (buf));
166   if (!verify_buf (buf, sizeof (buf)))
167     printf ("FAILED ");
168   else
169     printf ("OKAY ");
170 
171   free (ks);
172 
173  fail:
174   printf ("\n");
175   return;
176 }
177