summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/bf
diff options
context:
space:
mode:
authorryker <>1998-10-05 20:13:15 +0000
committerryker <>1998-10-05 20:13:15 +0000
commit536c76cbb863bab152f19842ab88772c01e922c7 (patch)
treedfecec371a097b73d605aae665887946d9982219 /src/lib/libcrypto/bf
downloadopenbsd-536c76cbb863bab152f19842ab88772c01e922c7.tar.gz
openbsd-536c76cbb863bab152f19842ab88772c01e922c7.tar.bz2
openbsd-536c76cbb863bab152f19842ab88772c01e922c7.zip
Import of SSLeay-0.9.0b with RSA and IDEA stubbed + OpenBSD build
functionality for shared libs. Note that routines such as sslv2_init and friends that use RSA will not work due to lack of RSA in this library. Needs documentation and help from ports for easy upgrade to full functionality where legally possible.
Diffstat (limited to 'src/lib/libcrypto/bf')
-rw-r--r--src/lib/libcrypto/bf/asm/bf-686.pl128
-rw-r--r--src/lib/libcrypto/bf/asm/readme10
-rw-r--r--src/lib/libcrypto/bf/bf_opts.c347
-rw-r--r--src/lib/libcrypto/bf/bfs.cpp67
-rw-r--r--src/lib/libcrypto/bf/bfspeed.c293
-rw-r--r--src/lib/libcrypto/bf/bftest.c521
6 files changed, 1366 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bf/asm/bf-686.pl b/src/lib/libcrypto/bf/asm/bf-686.pl
new file mode 100644
index 0000000000..bed303d786
--- /dev/null
+++ b/src/lib/libcrypto/bf/asm/bf-686.pl
@@ -0,0 +1,128 @@
1#!/usr/bin/perl
2#!/usr/local/bin/perl
3
4push(@INC,"perlasm","../../perlasm");
5require "x86asm.pl";
6require "cbc.pl";
7
8&asm_init($ARGV[0],"bf-686.pl");
9
10$BF_ROUNDS=16;
11$BF_OFF=($BF_ROUNDS+2)*4;
12$L="ecx";
13$R="edx";
14$P="edi";
15$tot="esi";
16$tmp1="eax";
17$tmp2="ebx";
18$tmp3="ebp";
19
20&des_encrypt("BF_encrypt",1);
21&des_encrypt("BF_decrypt",0);
22&cbc("BF_cbc_encrypt","BF_encrypt","BF_decrypt",1,4,5,3,-1,-1);
23
24&asm_finish();
25
26&file_end();
27
28sub des_encrypt
29 {
30 local($name,$enc)=@_;
31
32 &function_begin($name,"");
33
34 &comment("");
35 &comment("Load the 2 words");
36 &mov("eax",&wparam(0));
37 &mov($L,&DWP(0,"eax","",0));
38 &mov($R,&DWP(4,"eax","",0));
39
40 &comment("");
41 &comment("P pointer, s and enc flag");
42 &mov($P,&wparam(1));
43
44 &xor( $tmp1, $tmp1);
45 &xor( $tmp2, $tmp2);
46
47 # encrypting part
48
49 if ($enc)
50 {
51 &xor($L,&DWP(0,$P,"",0));
52 for ($i=0; $i<$BF_ROUNDS; $i+=2)
53 {
54 &comment("");
55 &comment("Round $i");
56 &BF_ENCRYPT($i+1,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
57
58 &comment("");
59 &comment("Round ".sprintf("%d",$i+1));
60 &BF_ENCRYPT($i+2,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
61 }
62 &xor($R,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
63
64 &mov("eax",&wparam(0));
65 &mov(&DWP(0,"eax","",0),$R);
66 &mov(&DWP(4,"eax","",0),$L);
67 &function_end_A($name);
68 }
69 else
70 {
71 &xor($L,&DWP(($BF_ROUNDS+1)*4,$P,"",0));
72 for ($i=$BF_ROUNDS; $i>0; $i-=2)
73 {
74 &comment("");
75 &comment("Round $i");
76 &BF_ENCRYPT($i,$R,$L,$P,$tot,$tmp1,$tmp2,$tmp3);
77 &comment("");
78 &comment("Round ".sprintf("%d",$i-1));
79 &BF_ENCRYPT($i-1,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3);
80 }
81 &xor($R,&DWP(0,$P,"",0));
82
83 &mov("eax",&wparam(0));
84 &mov(&DWP(0,"eax","",0),$R);
85 &mov(&DWP(4,"eax","",0),$L);
86 &function_end_A($name);
87 }
88
89 &function_end_B($name);
90 }
91
92sub BF_ENCRYPT
93 {
94 local($i,$L,$R,$P,$tot,$tmp1,$tmp2,$tmp3)=@_;
95
96 &rotr( $R, 16);
97 &mov( $tot, &DWP(&n2a($i*4),$P,"",0));
98
99 &movb( &LB($tmp1), &HB($R));
100 &movb( &LB($tmp2), &LB($R));
101
102 &rotr( $R, 16);
103 &xor( $L, $tot);
104
105 &mov( $tot, &DWP(&n2a($BF_OFF+0x0000),$P,$tmp1,4));
106 &mov( $tmp3, &DWP(&n2a($BF_OFF+0x0400),$P,$tmp2,4));
107
108 &movb( &LB($tmp1), &HB($R));
109 &movb( &LB($tmp2), &LB($R));
110
111 &add( $tot, $tmp3);
112 &mov( $tmp1, &DWP(&n2a($BF_OFF+0x0800),$P,$tmp1,4)); # delay
113
114 &xor( $tot, $tmp1);
115 &mov( $tmp3, &DWP(&n2a($BF_OFF+0x0C00),$P,$tmp2,4));
116
117 &add( $tot, $tmp3);
118 &xor( $tmp1, $tmp1);
119
120 &xor( $L, $tot);
121 # delay
122 }
123
124sub n2a
125 {
126 sprintf("%d",$_[0]);
127 }
128
diff --git a/src/lib/libcrypto/bf/asm/readme b/src/lib/libcrypto/bf/asm/readme
new file mode 100644
index 0000000000..2385fa3812
--- /dev/null
+++ b/src/lib/libcrypto/bf/asm/readme
@@ -0,0 +1,10 @@
1There are blowfish assembler generation scripts.
2bf-586.pl version is for the pentium and
3bf-686.pl is my original version, which is faster on the pentium pro.
4
5When using a bf-586.pl, the pentium pro/II is %8 slower than using
6bf-686.pl. When using a bf-686.pl, the pentium is %16 slower
7than bf-586.pl
8
9So the default is bf-586.pl
10
diff --git a/src/lib/libcrypto/bf/bf_opts.c b/src/lib/libcrypto/bf/bf_opts.c
new file mode 100644
index 0000000000..5cfa60c537
--- /dev/null
+++ b/src/lib/libcrypto/bf/bf_opts.c
@@ -0,0 +1,347 @@
1/* crypto/bf/bf_opts.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* define PART1, PART2, PART3 or PART4 to build only with a few of the options.
60 * This is for machines with 64k code segment size restrictions. */
61
62#ifndef MSDOS
63#define TIMES
64#endif
65
66#include <stdio.h>
67#ifndef MSDOS
68#include <unistd.h>
69#else
70#include <io.h>
71extern void exit();
72#endif
73#include <signal.h>
74#ifndef VMS
75#ifndef _IRIX
76#include <time.h>
77#endif
78#ifdef TIMES
79#include <sys/types.h>
80#include <sys/times.h>
81#endif
82#else /* VMS */
83#include <types.h>
84struct tms {
85 time_t tms_utime;
86 time_t tms_stime;
87 time_t tms_uchild; /* I dunno... */
88 time_t tms_uchildsys; /* so these names are a guess :-) */
89 }
90#endif
91#ifndef TIMES
92#include <sys/timeb.h>
93#endif
94
95#ifdef sun
96#include <limits.h>
97#include <sys/param.h>
98#endif
99
100#include "blowfish.h"
101
102#define BF_DEFAULT_OPTIONS
103
104#undef BF_ENC
105#define BF_encrypt BF_encrypt_normal
106#undef HEADER_BF_LOCL_H
107#include "bf_enc.c"
108
109#define BF_PTR
110#undef BF_PTR2
111#undef BF_ENC
112#undef BF_encrypt
113#define BF_encrypt BF_encrypt_ptr
114#undef HEADER_BF_LOCL_H
115#include "bf_enc.c"
116
117#undef BF_PTR
118#define BF_PTR2
119#undef BF_ENC
120#undef BF_encrypt
121#define BF_encrypt BF_encrypt_ptr2
122#undef HEADER_BF_LOCL_H
123#include "bf_enc.c"
124
125/* The following if from times(3) man page. It may need to be changed */
126#ifndef HZ
127# ifndef CLK_TCK
128# ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
129# ifndef VMS
130# define HZ 100.0
131# else /* VMS */
132# define HZ 100.0
133# endif
134# else /* _BSD_CLK_TCK_ */
135# define HZ ((double)_BSD_CLK_TCK_)
136# endif
137# else /* CLK_TCK */
138# define HZ ((double)CLK_TCK)
139# endif
140#endif
141
142#define BUFSIZE ((long)1024)
143long run=0;
144
145#ifndef NOPROTO
146double Time_F(int s);
147#else
148double Time_F();
149#endif
150
151#ifdef SIGALRM
152#if defined(__STDC__) || defined(sgi)
153#define SIGRETTYPE void
154#else
155#define SIGRETTYPE int
156#endif
157
158#ifndef NOPROTO
159SIGRETTYPE sig_done(int sig);
160#else
161SIGRETTYPE sig_done();
162#endif
163
164SIGRETTYPE sig_done(sig)
165int sig;
166 {
167 signal(SIGALRM,sig_done);
168 run=0;
169#ifdef LINT
170 sig=sig;
171#endif
172 }
173#endif
174
175#define START 0
176#define STOP 1
177
178double Time_F(s)
179int s;
180 {
181 double ret;
182#ifdef TIMES
183 static struct tms tstart,tend;
184
185 if (s == START)
186 {
187 times(&tstart);
188 return(0);
189 }
190 else
191 {
192 times(&tend);
193 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
194 return((ret == 0.0)?1e-6:ret);
195 }
196#else /* !times() */
197 static struct timeb tstart,tend;
198 long i;
199
200 if (s == START)
201 {
202 ftime(&tstart);
203 return(0);
204 }
205 else
206 {
207 ftime(&tend);
208 i=(long)tend.millitm-(long)tstart.millitm;
209 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
210 return((ret == 0.0)?1e-6:ret);
211 }
212#endif
213 }
214
215#ifdef SIGALRM
216#define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
217#else
218#define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
219#endif
220
221#define time_it(func,name,index) \
222 print_name(name); \
223 Time_F(START); \
224 for (count=0,run=1; COND(cb); count+=4) \
225 { \
226 unsigned long d[2]; \
227 func(d,&sch); \
228 func(d,&sch); \
229 func(d,&sch); \
230 func(d,&sch); \
231 } \
232 tm[index]=Time_F(STOP); \
233 fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
234 tm[index]=((double)COUNT(cb))/tm[index];
235
236#define print_it(name,index) \
237 fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
238 tm[index]*8,1.0e6/tm[index]);
239
240int main(argc,argv)
241int argc;
242char **argv;
243 {
244 long count;
245 static unsigned char buf[BUFSIZE];
246 static char key[16]={ 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
247 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
248 BF_KEY sch;
249 double d,tm[16],max=0;
250 int rank[16];
251 char *str[16];
252 int max_idx=0,i,num=0,j;
253#ifndef SIGALARM
254 long ca,cb,cc,cd,ce;
255#endif
256
257 for (i=0; i<12; i++)
258 {
259 tm[i]=0.0;
260 rank[i]=0;
261 }
262
263#ifndef TIMES
264 fprintf(stderr,"To get the most acurate results, try to run this\n");
265 fprintf(stderr,"program when this computer is idle.\n");
266#endif
267
268 BF_set_key(&sch,16,key);
269
270#ifndef SIGALRM
271 fprintf(stderr,"First we calculate the approximate speed ...\n");
272 count=10;
273 do {
274 long i;
275 unsigned long data[2];
276
277 count*=2;
278 Time_F(START);
279 for (i=count; i; i--)
280 BF_encrypt(data,&sch);
281 d=Time_F(STOP);
282 } while (d < 3.0);
283 ca=count;
284 cb=count*3;
285 cc=count*3*8/BUFSIZE+1;
286 cd=count*8/BUFSIZE+1;
287
288 ce=count/20+1;
289#define COND(d) (count != (d))
290#define COUNT(d) (d)
291#else
292#define COND(c) (run)
293#define COUNT(d) (count)
294 signal(SIGALRM,sig_done);
295 alarm(10);
296#endif
297
298 time_it(BF_encrypt_normal, "BF_encrypt_normal ", 0);
299 time_it(BF_encrypt_ptr, "BF_encrypt_ptr ", 1);
300 time_it(BF_encrypt_ptr2, "BF_encrypt_ptr2 ", 2);
301 num+=3;
302
303 str[0]="<nothing>";
304 print_it("BF_encrypt_normal ",0);
305 max=tm[0];
306 max_idx=0;
307 str[1]="ptr ";
308 print_it("BF_encrypt_ptr ",1);
309 if (max < tm[1]) { max=tm[1]; max_idx=1; }
310 str[2]="ptr2 ";
311 print_it("BF_encrypt_ptr2 ",2);
312 if (max < tm[2]) { max=tm[2]; max_idx=2; }
313
314 printf("options BF ecb/s\n");
315 printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
316 d=tm[max_idx];
317 tm[max_idx]= -2.0;
318 max= -1.0;
319 for (;;)
320 {
321 for (i=0; i<3; i++)
322 {
323 if (max < tm[i]) { max=tm[i]; j=i; }
324 }
325 if (max < 0.0) break;
326 printf("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
327 tm[j]= -2.0;
328 max= -1.0;
329 }
330
331 switch (max_idx)
332 {
333 case 0:
334 printf("-DBF_DEFAULT_OPTIONS\n");
335 break;
336 case 1:
337 printf("-DBF_PTR\n");
338 break;
339 case 2:
340 printf("-DBF_PTR2\n");
341 break;
342 }
343 exit(0);
344#if defined(LINT) || defined(MSDOS)
345 return(0);
346#endif
347 }
diff --git a/src/lib/libcrypto/bf/bfs.cpp b/src/lib/libcrypto/bf/bfs.cpp
new file mode 100644
index 0000000000..272ed2f978
--- /dev/null
+++ b/src/lib/libcrypto/bf/bfs.cpp
@@ -0,0 +1,67 @@
1//
2// gettsc.inl
3//
4// gives access to the Pentium's (secret) cycle counter
5//
6// This software was written by Leonard Janke (janke@unixg.ubc.ca)
7// in 1996-7 and is entered, by him, into the public domain.
8
9#if defined(__WATCOMC__)
10void GetTSC(unsigned long&);
11#pragma aux GetTSC = 0x0f 0x31 "mov [edi], eax" parm [edi] modify [edx eax];
12#elif defined(__GNUC__)
13inline
14void GetTSC(unsigned long& tsc)
15{
16 asm volatile(".byte 15, 49\n\t"
17 : "=eax" (tsc)
18 :
19 : "%edx", "%eax");
20}
21#elif defined(_MSC_VER)
22inline
23void GetTSC(unsigned long& tsc)
24{
25 unsigned long a;
26 __asm _emit 0fh
27 __asm _emit 31h
28 __asm mov a, eax;
29 tsc=a;
30}
31#endif
32
33#include <stdio.h>
34#include <stdlib.h>
35#include "blowfish.h"
36
37void main(int argc,char *argv[])
38 {
39 BF_KEY key;
40 unsigned long s1,s2,e1,e2;
41 unsigned long data[2];
42 int i,j;
43
44 for (j=0; j<6; j++)
45 {
46 for (i=0; i<1000; i++) /**/
47 {
48 BF_encrypt(&data[0],&key);
49 GetTSC(s1);
50 BF_encrypt(&data[0],&key);
51 BF_encrypt(&data[0],&key);
52 BF_encrypt(&data[0],&key);
53 GetTSC(e1);
54 GetTSC(s2);
55 BF_encrypt(&data[0],&key);
56 BF_encrypt(&data[0],&key);
57 BF_encrypt(&data[0],&key);
58 BF_encrypt(&data[0],&key);
59 GetTSC(e2);
60 BF_encrypt(&data[0],&key);
61 }
62
63 printf("blowfish %d %d (%d)\n",
64 e1-s1,e2-s2,((e2-s2)-(e1-s1)));
65 }
66 }
67
diff --git a/src/lib/libcrypto/bf/bfspeed.c b/src/lib/libcrypto/bf/bfspeed.c
new file mode 100644
index 0000000000..640d820dd3
--- /dev/null
+++ b/src/lib/libcrypto/bf/bfspeed.c
@@ -0,0 +1,293 @@
1/* crypto/bf/bfspeed.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* 11-Sep-92 Andrew Daviel Support for Silicon Graphics IRIX added */
60/* 06-Apr-92 Luke Brennan Support for VMS and add extra signal calls */
61
62#ifndef MSDOS
63#define TIMES
64#endif
65
66#include <stdio.h>
67#ifndef MSDOS
68#include <unistd.h>
69#else
70#include <io.h>
71extern int exit();
72#endif
73#include <signal.h>
74#ifndef VMS
75#ifndef _IRIX
76#include <time.h>
77#endif
78#ifdef TIMES
79#include <sys/types.h>
80#include <sys/times.h>
81#endif
82#else /* VMS */
83#include <types.h>
84struct tms {
85 time_t tms_utime;
86 time_t tms_stime;
87 time_t tms_uchild; /* I dunno... */
88 time_t tms_uchildsys; /* so these names are a guess :-) */
89 }
90#endif
91#ifndef TIMES
92#include <sys/timeb.h>
93#endif
94
95#ifdef sun
96#include <limits.h>
97#include <sys/param.h>
98#endif
99
100#include "blowfish.h"
101
102/* The following if from times(3) man page. It may need to be changed */
103#ifndef HZ
104#ifndef CLK_TCK
105#ifndef VMS
106#define HZ 100.0
107#else /* VMS */
108#define HZ 100.0
109#endif
110#else /* CLK_TCK */
111#define HZ ((double)CLK_TCK)
112#endif
113#endif
114
115#define BUFSIZE ((long)1024)
116long run=0;
117
118#ifndef NOPROTO
119double Time_F(int s);
120#else
121double Time_F();
122#endif
123
124#ifdef SIGALRM
125#if defined(__STDC__) || defined(sgi) || defined(_AIX)
126#define SIGRETTYPE void
127#else
128#define SIGRETTYPE int
129#endif
130
131#ifndef NOPROTO
132SIGRETTYPE sig_done(int sig);
133#else
134SIGRETTYPE sig_done();
135#endif
136
137SIGRETTYPE sig_done(sig)
138int sig;
139 {
140 signal(SIGALRM,sig_done);
141 run=0;
142#ifdef LINT
143 sig=sig;
144#endif
145 }
146#endif
147
148#define START 0
149#define STOP 1
150
151double Time_F(s)
152int s;
153 {
154 double ret;
155#ifdef TIMES
156 static struct tms tstart,tend;
157
158 if (s == START)
159 {
160 times(&tstart);
161 return(0);
162 }
163 else
164 {
165 times(&tend);
166 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
167 return((ret == 0.0)?1e-6:ret);
168 }
169#else /* !times() */
170 static struct timeb tstart,tend;
171 long i;
172
173 if (s == START)
174 {
175 ftime(&tstart);
176 return(0);
177 }
178 else
179 {
180 ftime(&tend);
181 i=(long)tend.millitm-(long)tstart.millitm;
182 ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
183 return((ret == 0.0)?1e-6:ret);
184 }
185#endif
186 }
187
188int main(argc,argv)
189int argc;
190char **argv;
191 {
192 long count;
193 static unsigned char buf[BUFSIZE];
194 static unsigned char key[] ={
195 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
196 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
197 };
198 BF_KEY sch;
199 double a,b,c,d;
200#ifndef SIGALRM
201 long ca,cb,cc;
202#endif
203
204#ifndef TIMES
205 printf("To get the most acurate results, try to run this\n");
206 printf("program when this computer is idle.\n");
207#endif
208
209#ifndef SIGALRM
210 printf("First we calculate the approximate speed ...\n");
211 BF_set_key(&sch,16,key);
212 count=10;
213 do {
214 long i;
215 BF_LONG data[2];
216
217 count*=2;
218 Time_F(START);
219 for (i=count; i; i--)
220 BF_encrypt(data,&sch);
221 d=Time_F(STOP);
222 } while (d < 3.0);
223 ca=count/512;
224 cb=count;
225 cc=count*8/BUFSIZE+1;
226 printf("Doing BF_set_key %ld times\n",ca);
227#define COND(d) (count != (d))
228#define COUNT(d) (d)
229#else
230#define COND(c) (run)
231#define COUNT(d) (count)
232 signal(SIGALRM,sig_done);
233 printf("Doing BF_set_key for 10 seconds\n");
234 alarm(10);
235#endif
236
237 Time_F(START);
238 for (count=0,run=1; COND(ca); count+=4)
239 {
240 BF_set_key(&sch,16,key);
241 BF_set_key(&sch,16,key);
242 BF_set_key(&sch,16,key);
243 BF_set_key(&sch,16,key);
244 }
245 d=Time_F(STOP);
246 printf("%ld BF_set_key's in %.2f seconds\n",count,d);
247 a=((double)COUNT(ca))/d;
248
249#ifdef SIGALRM
250 printf("Doing BF_encrypt's for 10 seconds\n");
251 alarm(10);
252#else
253 printf("Doing BF_encrypt %ld times\n",cb);
254#endif
255 Time_F(START);
256 for (count=0,run=1; COND(cb); count+=4)
257 {
258 BF_LONG data[2];
259
260 BF_encrypt(data,&sch);
261 BF_encrypt(data,&sch);
262 BF_encrypt(data,&sch);
263 BF_encrypt(data,&sch);
264 }
265 d=Time_F(STOP);
266 printf("%ld BF_encrypt's in %.2f second\n",count,d);
267 b=((double)COUNT(cb)*8)/d;
268
269#ifdef SIGALRM
270 printf("Doing BF_cbc_encrypt on %ld byte blocks for 10 seconds\n",
271 BUFSIZE);
272 alarm(10);
273#else
274 printf("Doing BF_cbc_encrypt %ld times on %ld byte blocks\n",cc,
275 BUFSIZE);
276#endif
277 Time_F(START);
278 for (count=0,run=1; COND(cc); count++)
279 BF_cbc_encrypt(buf,buf,BUFSIZE,&sch,
280 &(key[0]),BF_ENCRYPT);
281 d=Time_F(STOP);
282 printf("%ld BF_cbc_encrypt's of %ld byte blocks in %.2f second\n",
283 count,BUFSIZE,d);
284 c=((double)COUNT(cc)*BUFSIZE)/d;
285
286 printf("Blowfish set_key per sec = %12.3f (%9.3fuS)\n",a,1.0e6/a);
287 printf("Blowfish raw ecb bytes per sec = %12.3f (%9.3fuS)\n",b,8.0e6/b);
288 printf("Blowfish cbc bytes per sec = %12.3f (%9.3fuS)\n",c,8.0e6/c);
289 exit(0);
290#if defined(LINT) || defined(MSDOS)
291 return(0);
292#endif
293 }
diff --git a/src/lib/libcrypto/bf/bftest.c b/src/lib/libcrypto/bf/bftest.c
new file mode 100644
index 0000000000..9266cf813a
--- /dev/null
+++ b/src/lib/libcrypto/bf/bftest.c
@@ -0,0 +1,521 @@
1/* crypto/bf/bftest.c */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59/* This has been a quickly hacked 'ideatest.c'. When I add tests for other
60 * RC2 modes, more of the code will be uncommented. */
61
62#include <stdio.h>
63#include <string.h>
64#include <stdlib.h>
65#include "blowfish.h"
66
67char *bf_key[2]={
68 "abcdefghijklmnopqrstuvwxyz",
69 "Who is John Galt?"
70 };
71
72/* big endian */
73BF_LONG bf_plain[2][2]={
74 {0x424c4f57L,0x46495348L},
75 {0xfedcba98L,0x76543210L}
76 };
77
78BF_LONG bf_cipher[2][2]={
79 {0x324ed0feL,0xf413a203L},
80 {0xcc91732bL,0x8022f684L}
81 };
82/************/
83
84/* Lets use the DES test vectors :-) */
85#define NUM_TESTS 34
86static unsigned char ecb_data[NUM_TESTS][8]={
87 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
88 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
89 {0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
90 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
91 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
92 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
93 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
94 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10},
95 {0x7C,0xA1,0x10,0x45,0x4A,0x1A,0x6E,0x57},
96 {0x01,0x31,0xD9,0x61,0x9D,0xC1,0x37,0x6E},
97 {0x07,0xA1,0x13,0x3E,0x4A,0x0B,0x26,0x86},
98 {0x38,0x49,0x67,0x4C,0x26,0x02,0x31,0x9E},
99 {0x04,0xB9,0x15,0xBA,0x43,0xFE,0xB5,0xB6},
100 {0x01,0x13,0xB9,0x70,0xFD,0x34,0xF2,0xCE},
101 {0x01,0x70,0xF1,0x75,0x46,0x8F,0xB5,0xE6},
102 {0x43,0x29,0x7F,0xAD,0x38,0xE3,0x73,0xFE},
103 {0x07,0xA7,0x13,0x70,0x45,0xDA,0x2A,0x16},
104 {0x04,0x68,0x91,0x04,0xC2,0xFD,0x3B,0x2F},
105 {0x37,0xD0,0x6B,0xB5,0x16,0xCB,0x75,0x46},
106 {0x1F,0x08,0x26,0x0D,0x1A,0xC2,0x46,0x5E},
107 {0x58,0x40,0x23,0x64,0x1A,0xBA,0x61,0x76},
108 {0x02,0x58,0x16,0x16,0x46,0x29,0xB0,0x07},
109 {0x49,0x79,0x3E,0xBC,0x79,0xB3,0x25,0x8F},
110 {0x4F,0xB0,0x5E,0x15,0x15,0xAB,0x73,0xA7},
111 {0x49,0xE9,0x5D,0x6D,0x4C,0xA2,0x29,0xBF},
112 {0x01,0x83,0x10,0xDC,0x40,0x9B,0x26,0xD6},
113 {0x1C,0x58,0x7F,0x1C,0x13,0x92,0x4F,0xEF},
114 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01},
115 {0x1F,0x1F,0x1F,0x1F,0x0E,0x0E,0x0E,0x0E},
116 {0xE0,0xFE,0xE0,0xFE,0xF1,0xFE,0xF1,0xFE},
117 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
118 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
119 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
120 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10}};
121
122static unsigned char plain_data[NUM_TESTS][8]={
123 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
124 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
125 {0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
126 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
127 {0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11},
128 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
129 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
130 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
131 {0x01,0xA1,0xD6,0xD0,0x39,0x77,0x67,0x42},
132 {0x5C,0xD5,0x4C,0xA8,0x3D,0xEF,0x57,0xDA},
133 {0x02,0x48,0xD4,0x38,0x06,0xF6,0x71,0x72},
134 {0x51,0x45,0x4B,0x58,0x2D,0xDF,0x44,0x0A},
135 {0x42,0xFD,0x44,0x30,0x59,0x57,0x7F,0xA2},
136 {0x05,0x9B,0x5E,0x08,0x51,0xCF,0x14,0x3A},
137 {0x07,0x56,0xD8,0xE0,0x77,0x47,0x61,0xD2},
138 {0x76,0x25,0x14,0xB8,0x29,0xBF,0x48,0x6A},
139 {0x3B,0xDD,0x11,0x90,0x49,0x37,0x28,0x02},
140 {0x26,0x95,0x5F,0x68,0x35,0xAF,0x60,0x9A},
141 {0x16,0x4D,0x5E,0x40,0x4F,0x27,0x52,0x32},
142 {0x6B,0x05,0x6E,0x18,0x75,0x9F,0x5C,0xCA},
143 {0x00,0x4B,0xD6,0xEF,0x09,0x17,0x60,0x62},
144 {0x48,0x0D,0x39,0x00,0x6E,0xE7,0x62,0xF2},
145 {0x43,0x75,0x40,0xC8,0x69,0x8F,0x3C,0xFA},
146 {0x07,0x2D,0x43,0xA0,0x77,0x07,0x52,0x92},
147 {0x02,0xFE,0x55,0x77,0x81,0x17,0xF1,0x2A},
148 {0x1D,0x9D,0x5C,0x50,0x18,0xF7,0x28,0xC2},
149 {0x30,0x55,0x32,0x28,0x6D,0x6F,0x29,0x5A},
150 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
151 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
152 {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF},
153 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
154 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
155 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
156 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
157
158static unsigned char cipher_data[NUM_TESTS][8]={
159 {0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78},
160 {0x51,0x86,0x6F,0xD5,0xB8,0x5E,0xCB,0x8A},
161 {0x7D,0x85,0x6F,0x9A,0x61,0x30,0x63,0xF2},
162 {0x24,0x66,0xDD,0x87,0x8B,0x96,0x3C,0x9D},
163 {0x61,0xF9,0xC3,0x80,0x22,0x81,0xB0,0x96},
164 {0x7D,0x0C,0xC6,0x30,0xAF,0xDA,0x1E,0xC7},
165 {0x4E,0xF9,0x97,0x45,0x61,0x98,0xDD,0x78},
166 {0x0A,0xCE,0xAB,0x0F,0xC6,0xA0,0xA2,0x8D},
167 {0x59,0xC6,0x82,0x45,0xEB,0x05,0x28,0x2B},
168 {0xB1,0xB8,0xCC,0x0B,0x25,0x0F,0x09,0xA0},
169 {0x17,0x30,0xE5,0x77,0x8B,0xEA,0x1D,0xA4},
170 {0xA2,0x5E,0x78,0x56,0xCF,0x26,0x51,0xEB},
171 {0x35,0x38,0x82,0xB1,0x09,0xCE,0x8F,0x1A},
172 {0x48,0xF4,0xD0,0x88,0x4C,0x37,0x99,0x18},
173 {0x43,0x21,0x93,0xB7,0x89,0x51,0xFC,0x98},
174 {0x13,0xF0,0x41,0x54,0xD6,0x9D,0x1A,0xE5},
175 {0x2E,0xED,0xDA,0x93,0xFF,0xD3,0x9C,0x79},
176 {0xD8,0x87,0xE0,0x39,0x3C,0x2D,0xA6,0xE3},
177 {0x5F,0x99,0xD0,0x4F,0x5B,0x16,0x39,0x69},
178 {0x4A,0x05,0x7A,0x3B,0x24,0xD3,0x97,0x7B},
179 {0x45,0x20,0x31,0xC1,0xE4,0xFA,0xDA,0x8E},
180 {0x75,0x55,0xAE,0x39,0xF5,0x9B,0x87,0xBD},
181 {0x53,0xC5,0x5F,0x9C,0xB4,0x9F,0xC0,0x19},
182 {0x7A,0x8E,0x7B,0xFA,0x93,0x7E,0x89,0xA3},
183 {0xCF,0x9C,0x5D,0x7A,0x49,0x86,0xAD,0xB5},
184 {0xD1,0xAB,0xB2,0x90,0x65,0x8B,0xC7,0x78},
185 {0x55,0xCB,0x37,0x74,0xD1,0x3E,0xF2,0x01},
186 {0xFA,0x34,0xEC,0x48,0x47,0xB2,0x68,0xB2},
187 {0xA7,0x90,0x79,0x51,0x08,0xEA,0x3C,0xAE},
188 {0xC3,0x9E,0x07,0x2D,0x9F,0xAC,0x63,0x1D},
189 {0x01,0x49,0x33,0xE0,0xCD,0xAF,0xF6,0xE4},
190 {0xF2,0x1E,0x9A,0x77,0xB7,0x1C,0x49,0xBC},
191 {0x24,0x59,0x46,0x88,0x57,0x54,0x36,0x9A},
192 {0x6B,0x5C,0x5A,0x9C,0x5D,0x9E,0x0A,0x5A},
193 };
194
195static unsigned char cbc_key [16]={
196 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
197 0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87};
198static unsigned char cbc_iv [8]={0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10};
199static char cbc_data[40]="7654321 Now is the time for ";
200static unsigned char cbc_ok[32]={
201 0x6B,0x77,0xB4,0xD6,0x30,0x06,0xDE,0xE6,
202 0x05,0xB1,0x56,0xE2,0x74,0x03,0x97,0x93,
203 0x58,0xDE,0xB9,0xE7,0x15,0x46,0x16,0xD9,
204 0x59,0xF1,0x65,0x2B,0xD5,0xFF,0x92,0xCC};
205
206static unsigned char cfb64_ok[]={
207 0xE7,0x32,0x14,0xA2,0x82,0x21,0x39,0xCA,
208 0xF2,0x6E,0xCF,0x6D,0x2E,0xB9,0xE7,0x6E,
209 0x3D,0xA3,0xDE,0x04,0xD1,0x51,0x72,0x00,
210 0x51,0x9D,0x57,0xA6,0xC3};
211
212static unsigned char ofb64_ok[]={
213 0xE7,0x32,0x14,0xA2,0x82,0x21,0x39,0xCA,
214 0x62,0xB3,0x43,0xCC,0x5B,0x65,0x58,0x73,
215 0x10,0xDD,0x90,0x8D,0x0C,0x24,0x1B,0x22,
216 0x63,0xC2,0xCF,0x80,0xDA};
217
218#define KEY_TEST_NUM 25
219unsigned char key_test[KEY_TEST_NUM]={
220 0xf0,0xe1,0xd2,0xc3,0xb4,0xa5,0x96,0x87,
221 0x78,0x69,0x5a,0x4b,0x3c,0x2d,0x1e,0x0f,
222 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,
223 0x88};
224
225unsigned char key_data[8]=
226 {0xFE,0xDC,0xBA,0x98,0x76,0x54,0x32,0x10};
227
228unsigned char key_out[KEY_TEST_NUM][8]={
229 {0xF9,0xAD,0x59,0x7C,0x49,0xDB,0x00,0x5E},
230 {0xE9,0x1D,0x21,0xC1,0xD9,0x61,0xA6,0xD6},
231 {0xE9,0xC2,0xB7,0x0A,0x1B,0xC6,0x5C,0xF3},
232 {0xBE,0x1E,0x63,0x94,0x08,0x64,0x0F,0x05},
233 {0xB3,0x9E,0x44,0x48,0x1B,0xDB,0x1E,0x6E},
234 {0x94,0x57,0xAA,0x83,0xB1,0x92,0x8C,0x0D},
235 {0x8B,0xB7,0x70,0x32,0xF9,0x60,0x62,0x9D},
236 {0xE8,0x7A,0x24,0x4E,0x2C,0xC8,0x5E,0x82},
237 {0x15,0x75,0x0E,0x7A,0x4F,0x4E,0xC5,0x77},
238 {0x12,0x2B,0xA7,0x0B,0x3A,0xB6,0x4A,0xE0},
239 {0x3A,0x83,0x3C,0x9A,0xFF,0xC5,0x37,0xF6},
240 {0x94,0x09,0xDA,0x87,0xA9,0x0F,0x6B,0xF2},
241 {0x88,0x4F,0x80,0x62,0x50,0x60,0xB8,0xB4},
242 {0x1F,0x85,0x03,0x1C,0x19,0xE1,0x19,0x68},
243 {0x79,0xD9,0x37,0x3A,0x71,0x4C,0xA3,0x4F},
244 {0x93,0x14,0x28,0x87,0xEE,0x3B,0xE1,0x5C},
245 {0x03,0x42,0x9E,0x83,0x8C,0xE2,0xD1,0x4B},
246 {0xA4,0x29,0x9E,0x27,0x46,0x9F,0xF6,0x7B},
247 {0xAF,0xD5,0xAE,0xD1,0xC1,0xBC,0x96,0xA8},
248 {0x10,0x85,0x1C,0x0E,0x38,0x58,0xDA,0x9F},
249 {0xE6,0xF5,0x1E,0xD7,0x9B,0x9D,0xB2,0x1F},
250 {0x64,0xA6,0xE1,0x4A,0xFD,0x36,0xB4,0x6F},
251 {0x80,0xC7,0xD7,0xD4,0x5A,0x54,0x79,0xAD},
252 {0x05,0x04,0x4B,0x62,0xFA,0x52,0xD0,0x80},
253 };
254
255#ifndef NOPROTO
256static int test(void );
257static int print_test_data(void );
258#else
259static int test();
260static int print_test_data();
261#endif
262
263int main(argc,argv)
264int argc;
265char *argv[];
266 {
267 int ret;
268
269 if (argc > 1)
270 ret=print_test_data();
271 else
272 ret=test();
273
274 exit(ret);
275 return(0);
276 }
277
278static int print_test_data()
279 {
280 unsigned int i,j;
281
282 printf("ecb test data\n");
283 printf("key bytes\t\tclear bytes\t\tcipher bytes\n");
284 for (i=0; i<NUM_TESTS; i++)
285 {
286 for (j=0; j<8; j++)
287 printf("%02X",ecb_data[i][j]);
288 printf("\t");
289 for (j=0; j<8; j++)
290 printf("%02X",plain_data[i][j]);
291 printf("\t");
292 for (j=0; j<8; j++)
293 printf("%02X",cipher_data[i][j]);
294 printf("\n");
295 }
296
297 printf("set_key test data\n");
298 printf("data[8]= ");
299 for (j=0; j<8; j++)
300 printf("%02X",key_data[j]);
301 printf("\n");
302 for (i=0; i<KEY_TEST_NUM-1; i++)
303 {
304 printf("c=");
305 for (j=0; j<8; j++)
306 printf("%02X",key_out[i][j]);
307 printf(" k[%2d]=",i+1);
308 for (j=0; j<i+1; j++)
309 printf("%02X",key_test[j]);
310 printf("\n");
311 }
312
313 printf("\nchaining mode test data\n");
314 printf("key[16] = ");
315 for (j=0; j<16; j++)
316 printf("%02X",cbc_key[j]);
317 printf("\niv[8] = ");
318 for (j=0; j<8; j++)
319 printf("%02X",cbc_iv[j]);
320 printf("\ndata[%d] = '%s'",(int)strlen(cbc_data)+1,cbc_data);
321 printf("\ndata[%d] = ",(int)strlen(cbc_data)+1);
322 for (j=0; j<strlen(cbc_data)+1; j++)
323 printf("%02X",cbc_data[j]);
324 printf("\n");
325 printf("cbc cipher text\n");
326 printf("cipher[%d]= ",32);
327 for (j=0; j<32; j++)
328 printf("%02X",cbc_ok[j]);
329 printf("\n");
330
331 printf("cfb64 cipher text\n");
332 printf("cipher[%d]= ",(int)strlen(cbc_data)+1);
333 for (j=0; j<strlen(cbc_data)+1; j++)
334 printf("%02X",cfb64_ok[j]);
335 printf("\n");
336
337 printf("ofb64 cipher text\n");
338 printf("cipher[%d]= ",(int)strlen(cbc_data)+1);
339 for (j=0; j<strlen(cbc_data)+1; j++)
340 printf("%02X",ofb64_ok[j]);
341 printf("\n");
342 return(0);
343 }
344
345static int test()
346 {
347 unsigned char cbc_in[40],cbc_out[40],iv[8];
348 int i,n,err=0;
349 BF_KEY key;
350 BF_LONG data[2];
351 unsigned char out[8];
352 BF_LONG len;
353
354 printf("testing blowfish in raw ecb mode\n");
355 for (n=0; n<2; n++)
356 {
357 BF_set_key(&key,strlen(bf_key[n]),(unsigned char *)bf_key[n]);
358
359 data[0]=bf_plain[n][0];
360 data[1]=bf_plain[n][1];
361 BF_encrypt(data,&key);
362 if (memcmp(&(bf_cipher[n][0]),&(data[0]),8) != 0)
363 {
364 printf("BF_encrypt error encrypting\n");
365 printf("got :");
366 for (i=0; i<2; i++)
367 printf("%08lX ",data[i]);
368 printf("\n");
369 printf("expected:");
370 for (i=0; i<2; i++)
371 printf("%08lX ",bf_cipher[n][i]);
372 err=1;
373 printf("\n");
374 }
375
376 BF_decrypt(&(data[0]),&key);
377 if (memcmp(&(bf_plain[n][0]),&(data[0]),8) != 0)
378 {
379 printf("BF_encrypt error decrypting\n");
380 printf("got :");
381 for (i=0; i<2; i++)
382 printf("%08lX ",data[i]);
383 printf("\n");
384 printf("expected:");
385 for (i=0; i<2; i++)
386 printf("%08lX ",bf_plain[n][i]);
387 printf("\n");
388 err=1;
389 }
390 }
391
392 printf("testing blowfish in ecb mode\n");
393
394 for (n=0; n<NUM_TESTS; n++)
395 {
396 BF_set_key(&key,8,ecb_data[n]);
397
398 BF_ecb_encrypt(&(plain_data[n][0]),out,&key,BF_ENCRYPT);
399 if (memcmp(&(cipher_data[n][0]),out,8) != 0)
400 {
401 printf("BF_ecb_encrypt blowfish error encrypting\n");
402 printf("got :");
403 for (i=0; i<8; i++)
404 printf("%02X ",out[i]);
405 printf("\n");
406 printf("expected:");
407 for (i=0; i<8; i++)
408 printf("%02X ",cipher_data[n][i]);
409 err=1;
410 printf("\n");
411 }
412
413 BF_ecb_encrypt(out,out,&key,BF_DECRYPT);
414 if (memcmp(&(plain_data[n][0]),out,8) != 0)
415 {
416 printf("BF_ecb_encrypt error decrypting\n");
417 printf("got :");
418 for (i=0; i<8; i++)
419 printf("%02X ",out[i]);
420 printf("\n");
421 printf("expected:");
422 for (i=0; i<8; i++)
423 printf("%02X ",plain_data[n][i]);
424 printf("\n");
425 err=1;
426 }
427 }
428
429 printf("testing blowfish set_key\n");
430 for (n=1; n<KEY_TEST_NUM; n++)
431 {
432 BF_set_key(&key,n,key_test);
433 BF_ecb_encrypt(key_data,out,&key,BF_ENCRYPT);
434 if (memcmp(out,&(key_out[n-1][0]),8) != 0)
435 {
436 printf("blowfish setkey error\n");
437 err=1;
438 }
439 }
440
441 printf("testing blowfish in cbc mode\n");
442 len=strlen(cbc_data)+1;
443
444 BF_set_key(&key,16,cbc_key);
445 memset(cbc_in,0,40);
446 memset(cbc_out,0,40);
447 memcpy(iv,cbc_iv,8);
448 BF_cbc_encrypt((unsigned char *)cbc_data,cbc_out,len,
449 &key,iv,BF_ENCRYPT);
450 if (memcmp(cbc_out,cbc_ok,32) != 0)
451 {
452 err=1;
453 printf("BF_cbc_encrypt encrypt error\n");
454 for (i=0; i<32; i++) printf("0x%02X,",cbc_out[i]);
455 }
456 memcpy(iv,cbc_iv,8);
457 BF_cbc_encrypt(cbc_out,cbc_in,len,
458 &key,iv,BF_DECRYPT);
459 if (memcmp(cbc_in,cbc_data,strlen(cbc_data)+1) != 0)
460 {
461 printf("BF_cbc_encrypt decrypt error\n");
462 err=1;
463 }
464
465 printf("testing blowfish in cfb64 mode\n");
466
467 BF_set_key(&key,16,cbc_key);
468 memset(cbc_in,0,40);
469 memset(cbc_out,0,40);
470 memcpy(iv,cbc_iv,8);
471 n=0;
472 BF_cfb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,
473 &key,iv,&n,BF_ENCRYPT);
474 BF_cfb64_encrypt((unsigned char *)&(cbc_data[13]),&(cbc_out[13]),len-13,
475 &key,iv,&n,BF_ENCRYPT);
476 if (memcmp(cbc_out,cfb64_ok,(int)len) != 0)
477 {
478 err=1;
479 printf("BF_cfb64_encrypt encrypt error\n");
480 for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);
481 }
482 n=0;
483 memcpy(iv,cbc_iv,8);
484 BF_cfb64_encrypt(cbc_out,cbc_in,17,
485 &key,iv,&n,BF_DECRYPT);
486 BF_cfb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,
487 &key,iv,&n,BF_DECRYPT);
488 if (memcmp(cbc_in,cbc_data,(int)len) != 0)
489 {
490 printf("BF_cfb64_encrypt decrypt error\n");
491 err=1;
492 }
493
494 printf("testing blowfish in ofb64\n");
495
496 BF_set_key(&key,16,cbc_key);
497 memset(cbc_in,0,40);
498 memset(cbc_out,0,40);
499 memcpy(iv,cbc_iv,8);
500 n=0;
501 BF_ofb64_encrypt((unsigned char *)cbc_data,cbc_out,(long)13,&key,iv,&n);
502 BF_ofb64_encrypt((unsigned char *)&(cbc_data[13]),
503 &(cbc_out[13]),len-13,&key,iv,&n);
504 if (memcmp(cbc_out,ofb64_ok,(int)len) != 0)
505 {
506 err=1;
507 printf("BF_ofb64_encrypt encrypt error\n");
508 for (i=0; i<(int)len; i++) printf("0x%02X,",cbc_out[i]);
509 }
510 n=0;
511 memcpy(iv,cbc_iv,8);
512 BF_ofb64_encrypt(cbc_out,cbc_in,17,&key,iv,&n);
513 BF_ofb64_encrypt(&(cbc_out[17]),&(cbc_in[17]),len-17,&key,iv,&n);
514 if (memcmp(cbc_in,cbc_data,(int)len) != 0)
515 {
516 printf("BF_ofb64_encrypt decrypt error\n");
517 err=1;
518 }
519
520 return(err);
521 }