Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / regress / sys / crypto / blowfish / bftest.c
blob9d27f7ac28b192696f97e35e91418f37a274bc87
1 /* $NetBSD: bftest.c,v 1.4 2002/02/27 01:41:54 itojun Exp $ */
2 /* $KAME: bftest.c,v 1.3 2000/11/08 05:58:24 itojun Exp $ */
4 /*
5 * Copyright (C) 2000 WIDE Project.
6 * All rights reserved.
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 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
33 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
34 * All rights reserved.
36 * This package is an SSL implementation written
37 * by Eric Young (eay@cryptsoft.com).
38 * The implementation was written so as to conform with Netscapes SSL.
40 * This library is free for commercial and non-commercial use as long as
41 * the following conditions are aheared to. The following conditions
42 * apply to all code found in this distribution, be it the RC4, RSA,
43 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
44 * included with this distribution is covered by the same copyright terms
45 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
47 * Copyright remains Eric Young's, and as such any Copyright notices in
48 * the code are not to be removed.
49 * If this package is used in a product, Eric Young should be given attribution
50 * as the author of the parts of the library used.
51 * This can be in the form of a textual message at program startup or
52 * in documentation (online or textual) provided with the package.
54 * Redistribution and use in source and binary forms, with or without
55 * modification, are permitted provided that the following conditions
56 * are met:
57 * 1. Redistributions of source code must retain the copyright
58 * notice, this list of conditions and the following disclaimer.
59 * 2. Redistributions in binary form must reproduce the above copyright
60 * notice, this list of conditions and the following disclaimer in the
61 * documentation and/or other materials provided with the distribution.
62 * 3. All advertising materials mentioning features or use of this software
63 * must display the following acknowledgement:
64 * "This product includes cryptographic software written by
65 * Eric Young (eay@cryptsoft.com)"
66 * The word 'cryptographic' can be left out if the rouines from the library
67 * being used are not cryptographic related :-).
68 * 4. If you include any Windows specific code (or a derivative thereof) from
69 * the apps directory (application code) you must include an acknowledgement:
70 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
72 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
73 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
74 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
76 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
77 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
78 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
79 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
80 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
81 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
82 * SUCH DAMAGE.
84 * The licence and distribution terms for any publically available version or
85 * derivative of this code cannot be changed. i.e. this code cannot simply be
86 * copied and put under another distribution licence
87 * [including the GNU Public Licence.]
90 #include <sys/cdefs.h>
91 #include <sys/types.h>
93 #include <stdio.h>
94 #include <string.h>
95 #include <stdlib.h>
96 #include <unistd.h>
98 #include <crypto/blowfish/blowfish.h>
100 static char *bf_key[2]={
101 "abcdefghijklmnopqrstuvwxyz",
102 "Who is John Galt?"
105 /* big endian */
106 static const char *bf_plain[2] = {
107 "424c4f5746495348", "fedcba9876543210"
110 static const char *bf_cipher[2] = {
111 "324ed0fef413a203", "cc91732b8022f684"
114 static void hex2bin(u_int8_t *, const char *);
115 static const char *pt(u_int8_t *);
116 int main(int, char **);
118 static void
119 hex2bin(p, s)
120 u_int8_t *p;
121 const char *s;
123 int i;
124 u_int v;
126 for (i = 0; i < 8; i++) {
127 sscanf(s, "%02x", &v);
128 *p++ = v & 0xff;
129 s += 2;
133 static const char *
134 pt(p)
135 u_int8_t *p;
137 static char bufs[10][20];
138 static int bnum = 0;
139 char *ret;
140 int i;
142 ret = bufs[bnum++];
143 bnum %= 10;
144 for (i = 0; i < 8; i++)
145 snprintf(&ret[i * 2], 3, "%02x", p[i]);
146 ret[8 * 2] = '\0';
147 return(ret);
151 main(argc, argv)
152 int argc;
153 char **argv;
155 int n, error = 0;
156 BF_KEY key;
157 BF_LONG data[2], plain[2], cipher[2];
158 int rounds;
160 if (argc > 1)
161 rounds = atoi(argv[1]);
162 else
163 rounds = 1;
165 printf("testing blowfish in raw ecb mode\n");
166 again:
167 for (n = 0; n < 2; n++) {
168 BF_set_key(&key, strlen(bf_key[n]), (unsigned char *)bf_key[n]);
170 hex2bin((u_int8_t *)plain, bf_plain[n]);
171 hex2bin((u_int8_t *)cipher, bf_cipher[n]);
173 memcpy(data, plain, 8);
175 data[0] = (BF_LONG)ntohl(data[0]);
176 data[1] = (BF_LONG)ntohl(data[1]);
177 BF_encrypt(data, &key);
178 data[0] = (BF_LONG)htonl(data[0]);
179 data[1] = (BF_LONG)htonl(data[1]);
180 if (memcmp(data, cipher, 8) != 0) {
181 printf("BF_encrypt error encrypting\n");
182 printf("got : %s", pt((u_int8_t *)data));
183 printf("\n");
184 printf("expected: %s", pt((u_int8_t *)cipher));
185 error = 1;
186 printf("\n");
189 data[0] = (BF_LONG)ntohl(data[0]);
190 data[1] = (BF_LONG)ntohl(data[1]);
191 BF_decrypt(data, &key);
192 data[0] = (BF_LONG)htonl(data[0]);
193 data[1] = (BF_LONG)htonl(data[1]);
194 if (memcmp(data, plain, 8) != 0) {
195 printf("BF_encrypt error decrypting\n");
196 printf("got : %s", pt((u_int8_t *)data));
197 printf("\n");
198 printf("expected: %s", pt((u_int8_t *)plain));
199 printf("\n");
200 error = 1;
204 if (--rounds > 0)
205 goto again;
207 exit(error);