1 /*	$OpenBSD: cryptotest.c,v 1.3 2021/12/13 16:56:49 deraadt 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 <ctype.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #include "crypto.h"
39 
40 void test_crypto (enum transform);
41 
42 #define SET_KEY(x,y) {size_t i; for (i=0; i < (y); i++) (x)[i] = i;}
43 
44 int
45 verify_buf (u_int8_t *buf, u_int16_t len)
46 {
47   int i;
48 
49   for (i = 0; i < len; i++)
50     if (buf[i] != i)
51       return 0;
52 
53   return 1;
54 }
55 
56 #define nibble2bin(y) (tolower((y)) < 'a' ? (y) - '0': tolower((y)) - 'a' + 10)
57 #define hexchar2bin(x) ((nibble2bin((x)[0]) << 4) + nibble2bin((x)[1]))
58 #define nibble2c(x) ((x) >= 10 ? ('a'-10+(x)) : ('0' + (x)))
59 
60 static void asc2bin (u_int8_t *bin, u_int8_t *asc, u_int16_t len)
61 {
62   int i;
63 
64   for (i = 0; i < len; i += 2, asc += 2)
65     {
66       *bin++ = hexchar2bin(asc);
67     }
68 }
69 
70 void
71 special_test_blf (void)
72 {
73   u_int8_t *akey = "0123456789ABCDEFF0E1D2C3B4A59687";
74   u_int8_t *aiv = "FEDCBA9876543210";
75   u_int8_t data[] = "7654321 Now is the time for \0\0\0"; /* len 29 */
76   u_int8_t *acipher
77     = "6B77B4D63006DEE605B156E27403979358DEB9E7154616D959F1652BD5FF92CCE7";
78   u_int8_t key[16], cipher[32], iv[8];
79   struct crypto_xf *xf;
80   struct keystate *ks;
81   enum cryptoerr err;
82   int i;
83 
84   asc2bin (key, akey, strlen (akey));
85   asc2bin (iv, aiv, strlen (aiv));
86   asc2bin (cipher, acipher, 64);
87 
88   xf = crypto_get (BLOWFISH_CBC);
89   printf ("Special Test-Case %s: ", xf->name);
90 
91   ks = crypto_init (xf, key, 16, &err);
92   if (!ks)
93     {
94       printf ("FAILED (init %d)", err);
95       goto fail;
96     }
97 
98   crypto_init_iv (ks, iv, xf->blocksize);
99   crypto_encrypt (ks, data, 32);
100 
101   for (i = 0; i < 32; i++)
102     if (data[i] != cipher[i])
103 	break;
104   if (i < 32)
105     printf ("FAILED ");
106   else
107     printf ("OKAY ");
108 
109   free (ks);
110 
111 fail:
112   printf ("\n");
113   return;
114 }
115 
116 int
117 main (void)
118 {
119   test_crypto (TRIPLEDES_CBC);
120 
121   test_crypto (BLOWFISH_CBC);
122 
123   test_crypto (CAST_CBC);
124 
125   test_crypto (AES_CBC);
126 
127   special_test_blf ();
128 
129   return 0;
130 }
131 
132 void
133 dump_buf (u_int8_t *buf, size_t len)
134 {
135   size_t i;
136 
137   for (i = 0; i < len; i++)
138     printf ("%02x ", buf[i]);
139   printf ("\n");
140 }
141 
142 void
143 test_crypto (enum transform which)
144 {
145   u_int8_t buf[256];
146   struct crypto_xf *xf;
147   struct keystate *ks;
148   enum cryptoerr err;
149 
150   xf = crypto_get (which);
151   printf ("Testing %s: ", xf->name);
152 
153   SET_KEY (buf, xf->keymax);
154   ks = crypto_init (xf, buf, xf->keymax, &err);
155   if (!ks)
156     {
157       printf ("FAILED (init %d)", err);
158       goto fail;
159     }
160   SET_KEY (buf, sizeof (buf));
161   crypto_init_iv (ks, buf, xf->blocksize);
162   crypto_encrypt (ks, buf, sizeof (buf));
163   dump_buf (buf, sizeof buf);
164   crypto_decrypt (ks, buf, sizeof (buf));
165   if (!verify_buf (buf, sizeof (buf)))
166     printf ("FAILED ");
167   else
168     printf ("OKAY ");
169 
170   free (ks);
171 
172  fail:
173   printf ("\n");
174   return;
175 }
176