summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/cast
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/cast')
-rw-r--r--src/lib/libcrypto/cast/Makefile99
-rw-r--r--src/lib/libcrypto/cast/asm/cast-586.pl2
-rw-r--r--src/lib/libcrypto/cast/asm/readme7
-rw-r--r--src/lib/libcrypto/cast/c_enc.c2
-rw-r--r--src/lib/libcrypto/cast/cast_spd.c278
-rw-r--r--src/lib/libcrypto/cast/castopts.c342
-rw-r--r--src/lib/libcrypto/cast/casts.cpp70
-rw-r--r--src/lib/libcrypto/cast/casttest.c233
8 files changed, 1032 insertions, 1 deletions
diff --git a/src/lib/libcrypto/cast/Makefile b/src/lib/libcrypto/cast/Makefile
new file mode 100644
index 0000000000..0acc38f28d
--- /dev/null
+++ b/src/lib/libcrypto/cast/Makefile
@@ -0,0 +1,99 @@
1#
2# OpenSSL/crypto/cast/Makefile
3#
4
5DIR= cast
6TOP= ../..
7CC= cc
8CPP= $(CC) -E
9INCLUDES=
10CFLAG=-g
11MAKEFILE= Makefile
12AR= ar r
13
14CAST_ENC=c_enc.o
15
16CFLAGS= $(INCLUDES) $(CFLAG)
17ASFLAGS= $(INCLUDES) $(ASFLAG)
18AFLAGS= $(ASFLAGS)
19
20GENERAL=Makefile
21TEST=casttest.c
22APPS=
23
24LIB=$(TOP)/libcrypto.a
25LIBSRC=c_skey.c c_ecb.c c_enc.c c_cfb64.c c_ofb64.c
26LIBOBJ=c_skey.o c_ecb.o $(CAST_ENC) c_cfb64.o c_ofb64.o
27
28SRC= $(LIBSRC)
29
30EXHEADER= cast.h
31HEADER= cast_s.h cast_lcl.h $(EXHEADER)
32
33ALL= $(GENERAL) $(SRC) $(HEADER)
34
35top:
36 (cd ../..; $(MAKE) DIRS=crypto SDIRS=$(DIR) sub_all)
37
38all: lib
39
40lib: $(LIBOBJ)
41 $(AR) $(LIB) $(LIBOBJ)
42 $(RANLIB) $(LIB) || echo Never mind.
43 @touch lib
44
45cast-586.s: asm/cast-586.pl ../perlasm/x86asm.pl ../perlasm/cbc.pl
46 $(PERL) asm/cast-586.pl $(PERLASM_SCHEME) $(CLAGS) $(PROCESSOR) > $@
47
48files:
49 $(PERL) $(TOP)/util/files.pl Makefile >> $(TOP)/MINFO
50
51links:
52 @$(PERL) $(TOP)/util/mklink.pl ../../include/openssl $(EXHEADER)
53 @$(PERL) $(TOP)/util/mklink.pl ../../test $(TEST)
54 @$(PERL) $(TOP)/util/mklink.pl ../../apps $(APPS)
55
56install:
57 @[ -n "$(INSTALLTOP)" ] # should be set by top Makefile...
58 @headerlist="$(EXHEADER)"; for i in $$headerlist ; \
59 do \
60 (cp $$i $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i; \
61 chmod 644 $(INSTALL_PREFIX)$(INSTALLTOP)/include/openssl/$$i ); \
62 done;
63
64tags:
65 ctags $(SRC)
66
67tests:
68
69lint:
70 lint -DLINT $(INCLUDES) $(SRC)>fluff
71
72depend:
73 @[ -n "$(MAKEDEPEND)" ] # should be set by upper Makefile...
74 $(MAKEDEPEND) -- $(CFLAG) $(INCLUDES) $(DEPFLAG) -- $(PROGS) $(LIBSRC)
75
76dclean:
77 $(PERL) -pe 'if (/^# DO NOT DELETE THIS LINE/) {print; exit(0);}' $(MAKEFILE) >Makefile.new
78 mv -f Makefile.new $(MAKEFILE)
79
80clean:
81 rm -f *.s *.o *.obj lib tags core .pure .nfs* *.old *.bak fluff
82
83# DO NOT DELETE THIS LINE -- make depend depends on it.
84
85c_cfb64.o: ../../e_os.h ../../include/openssl/cast.h
86c_cfb64.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
87c_cfb64.o: c_cfb64.c cast_lcl.h
88c_ecb.o: ../../e_os.h ../../include/openssl/cast.h
89c_ecb.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
90c_ecb.o: ../../include/openssl/opensslv.h c_ecb.c cast_lcl.h
91c_enc.o: ../../e_os.h ../../include/openssl/cast.h
92c_enc.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
93c_enc.o: c_enc.c cast_lcl.h
94c_ofb64.o: ../../e_os.h ../../include/openssl/cast.h
95c_ofb64.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
96c_ofb64.o: c_ofb64.c cast_lcl.h
97c_skey.o: ../../e_os.h ../../include/openssl/cast.h
98c_skey.o: ../../include/openssl/e_os2.h ../../include/openssl/opensslconf.h
99c_skey.o: c_skey.c cast_lcl.h cast_s.h
diff --git a/src/lib/libcrypto/cast/asm/cast-586.pl b/src/lib/libcrypto/cast/asm/cast-586.pl
index bf6810d335..7a0083ecb8 100644
--- a/src/lib/libcrypto/cast/asm/cast-586.pl
+++ b/src/lib/libcrypto/cast/asm/cast-586.pl
@@ -29,7 +29,7 @@ $S4="CAST_S_table3";
29 29
30&CAST_encrypt("CAST_encrypt",1); 30&CAST_encrypt("CAST_encrypt",1);
31&CAST_encrypt("CAST_decrypt",0); 31&CAST_encrypt("CAST_decrypt",0);
32&cbc("CAST_cbc_encrypt","CAST_encrypt","CAST_decrypt",1,4,5,3,-1,-1); 32&cbc("CAST_cbc_encrypt","CAST_encrypt","CAST_decrypt",1,4,5,3,-1,-1) unless $main'openbsd;
33 33
34&asm_finish(); 34&asm_finish();
35 35
diff --git a/src/lib/libcrypto/cast/asm/readme b/src/lib/libcrypto/cast/asm/readme
new file mode 100644
index 0000000000..fbcd76289e
--- /dev/null
+++ b/src/lib/libcrypto/cast/asm/readme
@@ -0,0 +1,7 @@
1There is a ppro flag in cast-586 which turns on/off
2generation of pentium pro/II friendly code
3
4This flag makes the inner loop one cycle longer, but generates
5code that runs %30 faster on the pentium pro/II, while only %7 slower
6on the pentium. By default, this flag is on.
7
diff --git a/src/lib/libcrypto/cast/c_enc.c b/src/lib/libcrypto/cast/c_enc.c
index 357c41ebf0..2ae54f5cfb 100644
--- a/src/lib/libcrypto/cast/c_enc.c
+++ b/src/lib/libcrypto/cast/c_enc.c
@@ -59,6 +59,7 @@
59#include <openssl/cast.h> 59#include <openssl/cast.h>
60#include "cast_lcl.h" 60#include "cast_lcl.h"
61 61
62#ifndef OPENBSD_CAST_ASM
62void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key) 63void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key)
63 { 64 {
64 register CAST_LONG l,r,t; 65 register CAST_LONG l,r,t;
@@ -124,6 +125,7 @@ void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key)
124 data[1]=l&0xffffffffL; 125 data[1]=l&0xffffffffL;
125 data[0]=r&0xffffffffL; 126 data[0]=r&0xffffffffL;
126 } 127 }
128#endif
127 129
128void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 130void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
129 const CAST_KEY *ks, unsigned char *iv, int enc) 131 const CAST_KEY *ks, unsigned char *iv, int enc)
diff --git a/src/lib/libcrypto/cast/cast_spd.c b/src/lib/libcrypto/cast/cast_spd.c
new file mode 100644
index 0000000000..d650af475c
--- /dev/null
+++ b/src/lib/libcrypto/cast/cast_spd.c
@@ -0,0 +1,278 @@
1/* crypto/cast/cast_spd.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#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC)) && !defined(OPENSSL_SYS_MACOSX)
63#define TIMES
64#endif
65
66#include <stdio.h>
67
68#include <openssl/e_os2.h>
69#include OPENSSL_UNISTD_IO
70OPENSSL_DECLARE_EXIT
71
72#ifndef OPENSSL_SYS_NETWARE
73#include <signal.h>
74#endif
75
76#ifndef _IRIX
77#include <time.h>
78#endif
79#ifdef TIMES
80#include <sys/types.h>
81#include <sys/times.h>
82#endif
83
84/* Depending on the VMS version, the tms structure is perhaps defined.
85 The __TMS macro will show if it was. If it wasn't defined, we should
86 undefine TIMES, since that tells the rest of the program how things
87 should be handled. -- Richard Levitte */
88#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
89#undef TIMES
90#endif
91
92#ifndef TIMES
93#include <sys/timeb.h>
94#endif
95
96#if defined(sun) || defined(__ultrix)
97#define _POSIX_SOURCE
98#include <limits.h>
99#include <sys/param.h>
100#endif
101
102#include <openssl/cast.h>
103
104/* The following if from times(3) man page. It may need to be changed */
105#ifndef HZ
106#ifndef CLK_TCK
107#define HZ 100.0
108#else /* CLK_TCK */
109#define HZ ((double)CLK_TCK)
110#endif
111#endif
112
113#define BUFSIZE ((long)1024)
114long run=0;
115
116double Time_F(int s);
117#ifdef SIGALRM
118#if defined(__STDC__) || defined(sgi) || defined(_AIX)
119#define SIGRETTYPE void
120#else
121#define SIGRETTYPE int
122#endif
123
124SIGRETTYPE sig_done(int sig);
125SIGRETTYPE sig_done(int sig)
126 {
127 signal(SIGALRM,sig_done);
128 run=0;
129#ifdef LINT
130 sig=sig;
131#endif
132 }
133#endif
134
135#define START 0
136#define STOP 1
137
138double Time_F(int s)
139 {
140 double ret;
141#ifdef TIMES
142 static struct tms tstart,tend;
143
144 if (s == START)
145 {
146 times(&tstart);
147 return(0);
148 }
149 else
150 {
151 times(&tend);
152 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
153 return((ret == 0.0)?1e-6:ret);
154 }
155#else /* !times() */
156 static struct timeb tstart,tend;
157 long i;
158
159 if (s == START)
160 {
161 ftime(&tstart);
162 return(0);
163 }
164 else
165 {
166 ftime(&tend);
167 i=(long)tend.millitm-(long)tstart.millitm;
168 ret=((double)(tend.time-tstart.time))+((double)i)/1e3;
169 return((ret == 0.0)?1e-6:ret);
170 }
171#endif
172 }
173
174int main(int argc, char **argv)
175 {
176 long count;
177 static unsigned char buf[BUFSIZE];
178 static unsigned char key[] ={
179 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
180 0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
181 };
182 CAST_KEY sch;
183 double a,b,c,d;
184#ifndef SIGALRM
185 long ca,cb,cc;
186#endif
187
188#ifndef TIMES
189 printf("To get the most accurate results, try to run this\n");
190 printf("program when this computer is idle.\n");
191#endif
192
193#ifndef SIGALRM
194 printf("First we calculate the approximate speed ...\n");
195 CAST_set_key(&sch,16,key);
196 count=10;
197 do {
198 long i;
199 CAST_LONG data[2];
200
201 count*=2;
202 Time_F(START);
203 for (i=count; i; i--)
204 CAST_encrypt(data,&sch);
205 d=Time_F(STOP);
206 } while (d < 3.0);
207 ca=count/512;
208 cb=count;
209 cc=count*8/BUFSIZE+1;
210 printf("Doing CAST_set_key %ld times\n",ca);
211#define COND(d) (count != (d))
212#define COUNT(d) (d)
213#else
214#define COND(c) (run)
215#define COUNT(d) (count)
216 signal(SIGALRM,sig_done);
217 printf("Doing CAST_set_key for 10 seconds\n");
218 alarm(10);
219#endif
220
221 Time_F(START);
222 for (count=0,run=1; COND(ca); count+=4)
223 {
224 CAST_set_key(&sch,16,key);
225 CAST_set_key(&sch,16,key);
226 CAST_set_key(&sch,16,key);
227 CAST_set_key(&sch,16,key);
228 }
229 d=Time_F(STOP);
230 printf("%ld cast set_key's in %.2f seconds\n",count,d);
231 a=((double)COUNT(ca))/d;
232
233#ifdef SIGALRM
234 printf("Doing CAST_encrypt's for 10 seconds\n");
235 alarm(10);
236#else
237 printf("Doing CAST_encrypt %ld times\n",cb);
238#endif
239 Time_F(START);
240 for (count=0,run=1; COND(cb); count+=4)
241 {
242 CAST_LONG data[2];
243
244 CAST_encrypt(data,&sch);
245 CAST_encrypt(data,&sch);
246 CAST_encrypt(data,&sch);
247 CAST_encrypt(data,&sch);
248 }
249 d=Time_F(STOP);
250 printf("%ld CAST_encrypt's in %.2f second\n",count,d);
251 b=((double)COUNT(cb)*8)/d;
252
253#ifdef SIGALRM
254 printf("Doing CAST_cbc_encrypt on %ld byte blocks for 10 seconds\n",
255 BUFSIZE);
256 alarm(10);
257#else
258 printf("Doing CAST_cbc_encrypt %ld times on %ld byte blocks\n",cc,
259 BUFSIZE);
260#endif
261 Time_F(START);
262 for (count=0,run=1; COND(cc); count++)
263 CAST_cbc_encrypt(buf,buf,BUFSIZE,&sch,
264 &(key[0]),CAST_ENCRYPT);
265 d=Time_F(STOP);
266 printf("%ld CAST_cbc_encrypt's of %ld byte blocks in %.2f second\n",
267 count,BUFSIZE,d);
268 c=((double)COUNT(cc)*BUFSIZE)/d;
269
270 printf("CAST set_key per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
271 printf("CAST raw ecb bytes per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
272 printf("CAST cbc bytes per sec = %12.2f (%9.3fuS)\n",c,8.0e6/c);
273 exit(0);
274#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
275 return(0);
276#endif
277 }
278
diff --git a/src/lib/libcrypto/cast/castopts.c b/src/lib/libcrypto/cast/castopts.c
new file mode 100644
index 0000000000..33b2c7b06f
--- /dev/null
+++ b/src/lib/libcrypto/cast/castopts.c
@@ -0,0 +1,342 @@
1/* crypto/cast/castopts.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#if !defined(OPENSSL_SYS_MSDOS) && (!defined(OPENSSL_SYS_VMS) || defined(__DECC))
63#define TIMES
64#endif
65
66#include <stdio.h>
67
68#include <openssl/e_os2.h>
69#include OPENSSL_UNISTD_IO
70OPENSSL_DECLARE_EXIT
71
72#ifndef OPENSSL_SYS_NETWARE
73#include <signal.h>
74#endif
75
76#ifndef _IRIX
77#include <time.h>
78#endif
79#ifdef TIMES
80#include <sys/types.h>
81#include <sys/times.h>
82#endif
83
84/* Depending on the VMS version, the tms structure is perhaps defined.
85 The __TMS macro will show if it was. If it wasn't defined, we should
86 undefine TIMES, since that tells the rest of the program how things
87 should be handled. -- Richard Levitte */
88#if defined(OPENSSL_SYS_VMS_DECC) && !defined(__TMS)
89#undef TIMES
90#endif
91
92#ifndef TIMES
93#include <sys/timeb.h>
94#endif
95
96#if defined(sun) || defined(__ultrix)
97#define _POSIX_SOURCE
98#include <limits.h>
99#include <sys/param.h>
100#endif
101
102#include <openssl/cast.h>
103
104#define CAST_DEFAULT_OPTIONS
105
106#undef E_CAST
107#define CAST_encrypt CAST_encrypt_normal
108#define CAST_decrypt CAST_decrypt_normal
109#define CAST_cbc_encrypt CAST_cbc_encrypt_normal
110#undef HEADER_CAST_LOCL_H
111#include "c_enc.c"
112
113#define CAST_PTR
114#undef CAST_PTR2
115#undef E_CAST
116#undef CAST_encrypt
117#undef CAST_decrypt
118#undef CAST_cbc_encrypt
119#define CAST_encrypt CAST_encrypt_ptr
120#define CAST_decrypt CAST_decrypt_ptr
121#define CAST_cbc_encrypt CAST_cbc_encrypt_ptr
122#undef HEADER_CAST_LOCL_H
123#include "c_enc.c"
124
125#undef CAST_PTR
126#define CAST_PTR2
127#undef E_CAST
128#undef CAST_encrypt
129#undef CAST_decrypt
130#undef CAST_cbc_encrypt
131#define CAST_encrypt CAST_encrypt_ptr2
132#define CAST_decrypt CAST_decrypt_ptr2
133#define CAST_cbc_encrypt CAST_cbc_encrypt_ptr2
134#undef HEADER_CAST_LOCL_H
135#include "c_enc.c"
136
137/* The following if from times(3) man page. It may need to be changed */
138#ifndef HZ
139# ifndef CLK_TCK
140# ifndef _BSD_CLK_TCK_ /* FreeBSD fix */
141# define HZ 100.0
142# else /* _BSD_CLK_TCK_ */
143# define HZ ((double)_BSD_CLK_TCK_)
144# endif
145# else /* CLK_TCK */
146# define HZ ((double)CLK_TCK)
147# endif
148#endif
149
150#define BUFSIZE ((long)1024)
151long run=0;
152
153double Time_F(int s);
154#ifdef SIGALRM
155#if defined(__STDC__) || defined(sgi)
156#define SIGRETTYPE void
157#else
158#define SIGRETTYPE int
159#endif
160
161SIGRETTYPE sig_done(int sig);
162SIGRETTYPE sig_done(int sig)
163 {
164 signal(SIGALRM,sig_done);
165 run=0;
166#ifdef LINT
167 sig=sig;
168#endif
169 }
170#endif
171
172#define START 0
173#define STOP 1
174
175double Time_F(int s)
176 {
177 double ret;
178#ifdef TIMES
179 static struct tms tstart,tend;
180
181 if (s == START)
182 {
183 times(&tstart);
184 return(0);
185 }
186 else
187 {
188 times(&tend);
189 ret=((double)(tend.tms_utime-tstart.tms_utime))/HZ;
190 return((ret == 0.0)?1e-6:ret);
191 }
192#else /* !times() */
193 static struct timeb tstart,tend;
194 long i;
195
196 if (s == START)
197 {
198 ftime(&tstart);
199 return(0);
200 }
201 else
202 {
203 ftime(&tend);
204 i=(long)tend.millitm-(long)tstart.millitm;
205 ret=((double)(tend.time-tstart.time))+((double)i)/1000.0;
206 return((ret == 0.0)?1e-6:ret);
207 }
208#endif
209 }
210
211#ifdef SIGALRM
212#define print_name(name) fprintf(stderr,"Doing %s's for 10 seconds\n",name); alarm(10);
213#else
214#define print_name(name) fprintf(stderr,"Doing %s %ld times\n",name,cb);
215#endif
216
217#define time_it(func,name,index) \
218 print_name(name); \
219 Time_F(START); \
220 for (count=0,run=1; COND(cb); count+=4) \
221 { \
222 unsigned long d[2]; \
223 func(d,&sch); \
224 func(d,&sch); \
225 func(d,&sch); \
226 func(d,&sch); \
227 } \
228 tm[index]=Time_F(STOP); \
229 fprintf(stderr,"%ld %s's in %.2f second\n",count,name,tm[index]); \
230 tm[index]=((double)COUNT(cb))/tm[index];
231
232#define print_it(name,index) \
233 fprintf(stderr,"%s bytes per sec = %12.2f (%5.1fuS)\n",name, \
234 tm[index]*8,1.0e6/tm[index]);
235
236int main(int argc, char **argv)
237 {
238 long count;
239 static unsigned char buf[BUFSIZE];
240 static char key[16]={ 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0,
241 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0};
242 CAST_KEY sch;
243 double d,tm[16],max=0;
244 int rank[16];
245 char *str[16];
246 int max_idx=0,i,num=0,j;
247#ifndef SIGALARM
248 long ca,cb,cc,cd,ce;
249#endif
250
251 for (i=0; i<12; i++)
252 {
253 tm[i]=0.0;
254 rank[i]=0;
255 }
256
257#ifndef TIMES
258 fprintf(stderr,"To get the most accurate results, try to run this\n");
259 fprintf(stderr,"program when this computer is idle.\n");
260#endif
261
262 CAST_set_key(&sch,16,key);
263
264#ifndef SIGALRM
265 fprintf(stderr,"First we calculate the approximate speed ...\n");
266 count=10;
267 do {
268 long i;
269 unsigned long data[2];
270
271 count*=2;
272 Time_F(START);
273 for (i=count; i; i--)
274 CAST_encrypt(data,&sch);
275 d=Time_F(STOP);
276 } while (d < 3.0);
277 ca=count;
278 cb=count*3;
279 cc=count*3*8/BUFSIZE+1;
280 cd=count*8/BUFSIZE+1;
281
282 ce=count/20+1;
283#define COND(d) (count != (d))
284#define COUNT(d) (d)
285#else
286#define COND(c) (run)
287#define COUNT(d) (count)
288 signal(SIGALRM,sig_done);
289 alarm(10);
290#endif
291
292 time_it(CAST_encrypt_normal, "CAST_encrypt_normal ", 0);
293 time_it(CAST_encrypt_ptr, "CAST_encrypt_ptr ", 1);
294 time_it(CAST_encrypt_ptr2, "CAST_encrypt_ptr2 ", 2);
295 num+=3;
296
297 str[0]="<nothing>";
298 print_it("CAST_encrypt_normal ",0);
299 max=tm[0];
300 max_idx=0;
301 str[1]="ptr ";
302 print_it("CAST_encrypt_ptr ",1);
303 if (max < tm[1]) { max=tm[1]; max_idx=1; }
304 str[2]="ptr2 ";
305 print_it("CAST_encrypt_ptr2 ",2);
306 if (max < tm[2]) { max=tm[2]; max_idx=2; }
307
308 printf("options CAST ecb/s\n");
309 printf("%s %12.2f 100.0%%\n",str[max_idx],tm[max_idx]);
310 d=tm[max_idx];
311 tm[max_idx]= -2.0;
312 max= -1.0;
313 for (;;)
314 {
315 for (i=0; i<3; i++)
316 {
317 if (max < tm[i]) { max=tm[i]; j=i; }
318 }
319 if (max < 0.0) break;
320 printf("%s %12.2f %4.1f%%\n",str[j],tm[j],tm[j]/d*100.0);
321 tm[j]= -2.0;
322 max= -1.0;
323 }
324
325 switch (max_idx)
326 {
327 case 0:
328 printf("-DCAST_DEFAULT_OPTIONS\n");
329 break;
330 case 1:
331 printf("-DCAST_PTR\n");
332 break;
333 case 2:
334 printf("-DCAST_PTR2\n");
335 break;
336 }
337 exit(0);
338#if defined(LINT) || defined(OPENSSL_SYS_MSDOS)
339 return(0);
340#endif
341 }
342
diff --git a/src/lib/libcrypto/cast/casts.cpp b/src/lib/libcrypto/cast/casts.cpp
new file mode 100644
index 0000000000..8d7bd468d2
--- /dev/null
+++ b/src/lib/libcrypto/cast/casts.cpp
@@ -0,0 +1,70 @@
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 <openssl/cast.h>
36
37void main(int argc,char *argv[])
38 {
39 CAST_KEY key;
40 unsigned long s1,s2,e1,e2;
41 unsigned long data[2];
42 int i,j;
43 static unsigned char d[16]={0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
44
45 CAST_set_key(&key, 16,d);
46
47 for (j=0; j<6; j++)
48 {
49 for (i=0; i<1000; i++) /**/
50 {
51 CAST_encrypt(&data[0],&key);
52 GetTSC(s1);
53 CAST_encrypt(&data[0],&key);
54 CAST_encrypt(&data[0],&key);
55 CAST_encrypt(&data[0],&key);
56 GetTSC(e1);
57 GetTSC(s2);
58 CAST_encrypt(&data[0],&key);
59 CAST_encrypt(&data[0],&key);
60 CAST_encrypt(&data[0],&key);
61 CAST_encrypt(&data[0],&key);
62 GetTSC(e2);
63 CAST_encrypt(&data[0],&key);
64 }
65
66 printf("cast %d %d (%d)\n",
67 e1-s1,e2-s2,((e2-s2)-(e1-s1)));
68 }
69 }
70
diff --git a/src/lib/libcrypto/cast/casttest.c b/src/lib/libcrypto/cast/casttest.c
new file mode 100644
index 0000000000..0d020d6975
--- /dev/null
+++ b/src/lib/libcrypto/cast/casttest.c
@@ -0,0 +1,233 @@
1/* crypto/cast/casttest.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#include <stdio.h>
60#include <string.h>
61#include <stdlib.h>
62#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_CAST is defined */
63
64#include "../e_os.h"
65
66#ifdef OPENSSL_NO_CAST
67int main(int argc, char *argv[])
68{
69 printf("No CAST support\n");
70 return(0);
71}
72#else
73#include <openssl/cast.h>
74
75#define FULL_TEST
76
77static unsigned char k[16]={
78 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
79 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A
80 };
81
82static unsigned char in[8]={ 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
83
84static int k_len[3]={16,10,5};
85static unsigned char c[3][8]={
86 {0x23,0x8B,0x4F,0xE5,0x84,0x7E,0x44,0xB2},
87 {0xEB,0x6A,0x71,0x1A,0x2C,0x02,0x27,0x1B},
88 {0x7A,0xC8,0x16,0xD1,0x6E,0x9B,0x30,0x2E},
89 };
90static unsigned char out[80];
91
92static unsigned char in_a[16]={
93 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
94 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A};
95static unsigned char in_b[16]={
96 0x01,0x23,0x45,0x67,0x12,0x34,0x56,0x78,
97 0x23,0x45,0x67,0x89,0x34,0x56,0x78,0x9A};
98
99static unsigned char c_a[16]={
100 0xEE,0xA9,0xD0,0xA2,0x49,0xFD,0x3B,0xA6,
101 0xB3,0x43,0x6F,0xB8,0x9D,0x6D,0xCA,0x92};
102static unsigned char c_b[16]={
103 0xB2,0xC9,0x5E,0xB0,0x0C,0x31,0xAD,0x71,
104 0x80,0xAC,0x05,0xB8,0xE8,0x3D,0x69,0x6E};
105
106#if 0
107char *text="Hello to all people out there";
108
109static unsigned char cfb_key[16]={
110 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
111 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
112 };
113static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
114static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
115#define CFB_TEST_SIZE 24
116static unsigned char plain[CFB_TEST_SIZE]=
117 {
118 0x4e,0x6f,0x77,0x20,0x69,0x73,
119 0x20,0x74,0x68,0x65,0x20,0x74,
120 0x69,0x6d,0x65,0x20,0x66,0x6f,
121 0x72,0x20,0x61,0x6c,0x6c,0x20
122 };
123static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
124 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
125 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
126 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
127
128/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
129 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
130 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
131 };
132#endif
133
134int main(int argc, char *argv[])
135 {
136#ifdef FULL_TEST
137 long l;
138 CAST_KEY key_b;
139#endif
140 int i,z,err=0;
141 CAST_KEY key;
142
143 for (z=0; z<3; z++)
144 {
145 CAST_set_key(&key,k_len[z],k);
146
147 CAST_ecb_encrypt(in,out,&key,CAST_ENCRYPT);
148 if (memcmp(out,&(c[z][0]),8) != 0)
149 {
150 printf("ecb cast error encrypting for keysize %d\n",k_len[z]*8);
151 printf("got :");
152 for (i=0; i<8; i++)
153 printf("%02X ",out[i]);
154 printf("\n");
155 printf("expected:");
156 for (i=0; i<8; i++)
157 printf("%02X ",c[z][i]);
158 err=20;
159 printf("\n");
160 }
161
162 CAST_ecb_encrypt(out,out,&key,CAST_DECRYPT);
163 if (memcmp(out,in,8) != 0)
164 {
165 printf("ecb cast error decrypting for keysize %d\n",k_len[z]*8);
166 printf("got :");
167 for (i=0; i<8; i++)
168 printf("%02X ",out[i]);
169 printf("\n");
170 printf("expected:");
171 for (i=0; i<8; i++)
172 printf("%02X ",in[i]);
173 printf("\n");
174 err=3;
175 }
176 }
177 if (err == 0)
178 printf("ecb cast5 ok\n");
179
180#ifdef FULL_TEST
181 {
182 unsigned char out_a[16],out_b[16];
183 static char *hex="0123456789ABCDEF";
184
185 printf("This test will take some time....");
186 fflush(stdout);
187 memcpy(out_a,in_a,sizeof(in_a));
188 memcpy(out_b,in_b,sizeof(in_b));
189 i=1;
190
191 for (l=0; l<1000000L; l++)
192 {
193 CAST_set_key(&key_b,16,out_b);
194 CAST_ecb_encrypt(&(out_a[0]),&(out_a[0]),&key_b,CAST_ENCRYPT);
195 CAST_ecb_encrypt(&(out_a[8]),&(out_a[8]),&key_b,CAST_ENCRYPT);
196 CAST_set_key(&key,16,out_a);
197 CAST_ecb_encrypt(&(out_b[0]),&(out_b[0]),&key,CAST_ENCRYPT);
198 CAST_ecb_encrypt(&(out_b[8]),&(out_b[8]),&key,CAST_ENCRYPT);
199 if ((l & 0xffff) == 0xffff)
200 {
201 printf("%c",hex[i&0x0f]);
202 fflush(stdout);
203 i++;
204 }
205 }
206
207 if ( (memcmp(out_a,c_a,sizeof(c_a)) != 0) ||
208 (memcmp(out_b,c_b,sizeof(c_b)) != 0))
209 {
210 printf("\n");
211 printf("Error\n");
212
213 printf("A out =");
214 for (i=0; i<16; i++) printf("%02X ",out_a[i]);
215 printf("\nactual=");
216 for (i=0; i<16; i++) printf("%02X ",c_a[i]);
217 printf("\n");
218
219 printf("B out =");
220 for (i=0; i<16; i++) printf("%02X ",out_b[i]);
221 printf("\nactual=");
222 for (i=0; i<16; i++) printf("%02X ",c_b[i]);
223 printf("\n");
224 }
225 else
226 printf(" ok\n");
227 }
228#endif
229
230 EXIT(err);
231 return(err);
232 }
233#endif