summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/rc2
diff options
context:
space:
mode:
authorryker <>1998-10-05 20:13:15 +0000
committerryker <>1998-10-05 20:13:15 +0000
commit9e77c62555877f9a64805c49d0dcd7dbfbb40f4e (patch)
tree2a6396b738ecede1e1dd3ad84c90e47e21d0bcbd /src/lib/libcrypto/rc2
parentfe5d0717e2760d02faf23bf5a714f17b33ae4abb (diff)
parent536c76cbb863bab152f19842ab88772c01e922c7 (diff)
downloadopenbsd-9e77c62555877f9a64805c49d0dcd7dbfbb40f4e.tar.gz
openbsd-9e77c62555877f9a64805c49d0dcd7dbfbb40f4e.tar.bz2
openbsd-9e77c62555877f9a64805c49d0dcd7dbfbb40f4e.zip
This commit was generated by cvs2git to track changes on a CVS vendor
branch.
Diffstat (limited to 'src/lib/libcrypto/rc2')
-rw-r--r--src/lib/libcrypto/rc2/rc2speed.c293
-rw-r--r--src/lib/libcrypto/rc2/rc2test.c270
2 files changed, 563 insertions, 0 deletions
diff --git a/src/lib/libcrypto/rc2/rc2speed.c b/src/lib/libcrypto/rc2/rc2speed.c
new file mode 100644
index 0000000000..6cd8ea8f27
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2speed.c
@@ -0,0 +1,293 @@
1/* crypto/rc2/rc2speed.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 "rc2.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 RC2_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 RC2_set_key(&sch,16,key,128);
212 count=10;
213 do {
214 long i;
215 unsigned long data[2];
216
217 count*=2;
218 Time_F(START);
219 for (i=count; i; i--)
220 RC2_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 RC2_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 RC2_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 RC2_set_key(&sch,16,key,128);
241 RC2_set_key(&sch,16,key,128);
242 RC2_set_key(&sch,16,key,128);
243 RC2_set_key(&sch,16,key,128);
244 }
245 d=Time_F(STOP);
246 printf("%ld RC2_set_key's in %.2f seconds\n",count,d);
247 a=((double)COUNT(ca))/d;
248
249#ifdef SIGALRM
250 printf("Doing RC2_encrypt's for 10 seconds\n");
251 alarm(10);
252#else
253 printf("Doing RC2_encrypt %ld times\n",cb);
254#endif
255 Time_F(START);
256 for (count=0,run=1; COND(cb); count+=4)
257 {
258 unsigned long data[2];
259
260 RC2_encrypt(data,&sch);
261 RC2_encrypt(data,&sch);
262 RC2_encrypt(data,&sch);
263 RC2_encrypt(data,&sch);
264 }
265 d=Time_F(STOP);
266 printf("%ld RC2_encrypt's in %.2f second\n",count,d);
267 b=((double)COUNT(cb)*8)/d;
268
269#ifdef SIGALRM
270 printf("Doing RC2_cbc_encrypt on %ld byte blocks for 10 seconds\n",
271 BUFSIZE);
272 alarm(10);
273#else
274 printf("Doing RC2_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 RC2_cbc_encrypt(buf,buf,BUFSIZE,&sch,
280 &(key[0]),RC2_ENCRYPT);
281 d=Time_F(STOP);
282 printf("%ld RC2_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("RC2 set_key per sec = %12.2f (%9.3fuS)\n",a,1.0e6/a);
287 printf("RC2 raw ecb bytes per sec = %12.2f (%9.3fuS)\n",b,8.0e6/b);
288 printf("RC2 cbc bytes per sec = %12.2f (%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/rc2/rc2test.c b/src/lib/libcrypto/rc2/rc2test.c
new file mode 100644
index 0000000000..9d0f8016ec
--- /dev/null
+++ b/src/lib/libcrypto/rc2/rc2test.c
@@ -0,0 +1,270 @@
1/* crypto/rc2/rc2test.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 "rc2.h"
66
67unsigned char RC2key[4][16]={
68 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
69 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
70 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
71 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01},
72 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
73 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
74 {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
75 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F},
76 };
77
78unsigned char RC2plain[4][8]={
79 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
80 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
81 {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
82 {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
83 };
84
85unsigned char RC2cipher[4][8]={
86 {0x1C,0x19,0x8A,0x83,0x8D,0xF0,0x28,0xB7},
87 {0x21,0x82,0x9C,0x78,0xA9,0xF9,0xC0,0x74},
88 {0x13,0xDB,0x35,0x17,0xD3,0x21,0x86,0x9E},
89 {0x50,0xDC,0x01,0x62,0xBD,0x75,0x7F,0x31},
90 };
91/************/
92#ifdef undef
93unsigned char k[16]={
94 0x00,0x01,0x00,0x02,0x00,0x03,0x00,0x04,
95 0x00,0x05,0x00,0x06,0x00,0x07,0x00,0x08};
96
97unsigned char in[8]={0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x03};
98unsigned char c[8]={0x11,0xFB,0xED,0x2B,0x01,0x98,0x6D,0xE5};
99unsigned char out[80];
100
101char *text="Hello to all people out there";
102
103static unsigned char cfb_key[16]={
104 0xe1,0xf0,0xc3,0xd2,0xa5,0xb4,0x87,0x96,
105 0x69,0x78,0x4b,0x5a,0x2d,0x3c,0x0f,0x1e,
106 };
107static unsigned char cfb_iv[80]={0x34,0x12,0x78,0x56,0xab,0x90,0xef,0xcd};
108static unsigned char cfb_buf1[40],cfb_buf2[40],cfb_tmp[8];
109#define CFB_TEST_SIZE 24
110static unsigned char plain[CFB_TEST_SIZE]=
111 {
112 0x4e,0x6f,0x77,0x20,0x69,0x73,
113 0x20,0x74,0x68,0x65,0x20,0x74,
114 0x69,0x6d,0x65,0x20,0x66,0x6f,
115 0x72,0x20,0x61,0x6c,0x6c,0x20
116 };
117static unsigned char cfb_cipher64[CFB_TEST_SIZE]={
118 0x59,0xD8,0xE2,0x65,0x00,0x58,0x6C,0x3F,
119 0x2C,0x17,0x25,0xD0,0x1A,0x38,0xB7,0x2A,
120 0x39,0x61,0x37,0xDC,0x79,0xFB,0x9F,0x45
121
122/* 0xF9,0x78,0x32,0xB5,0x42,0x1A,0x6B,0x38,
123 0x9A,0x44,0xD6,0x04,0x19,0x43,0xC4,0xD9,
124 0x3D,0x1E,0xAE,0x47,0xFC,0xCF,0x29,0x0B,*/
125 };
126
127
128#ifndef NOPROTO
129/*static int cfb64_test(unsigned char *cfb_cipher);*/
130static char *pt(unsigned char *p);
131#else
132/*static int cfb64_test(); */
133static char *pt();
134#endif
135
136#endif
137
138int main(argc,argv)
139int argc;
140char *argv[];
141 {
142 int i,n,err=0;
143 RC2_KEY key;
144 unsigned char buf[8],buf2[8];
145
146 for (n=0; n<4; n++)
147 {
148 RC2_set_key(&key,16,&(RC2key[n][0]),0 /* or 1024 */);
149
150 RC2_ecb_encrypt(&(RC2plain[n][0]),buf,&key,RC2_ENCRYPT);
151 if (memcmp(&(RC2cipher[n][0]),buf,8) != 0)
152 {
153 printf("ecb rc2 error encrypting\n");
154 printf("got :");
155 for (i=0; i<8; i++)
156 printf("%02X ",buf[i]);
157 printf("\n");
158 printf("expected:");
159 for (i=0; i<8; i++)
160 printf("%02X ",RC2cipher[n][i]);
161 err=20;
162 printf("\n");
163 }
164
165 RC2_ecb_encrypt(buf,buf2,&key,RC2_DECRYPT);
166 if (memcmp(&(RC2plain[n][0]),buf2,8) != 0)
167 {
168 printf("ecb RC2 error decrypting\n");
169 printf("got :");
170 for (i=0; i<8; i++)
171 printf("%02X ",buf[i]);
172 printf("\n");
173 printf("expected:");
174 for (i=0; i<8; i++)
175 printf("%02X ",RC2plain[n][i]);
176 printf("\n");
177 err=3;
178 }
179 }
180
181 if (err == 0) printf("ecb RC2 ok\n");
182#ifdef undef
183 memcpy(iv,k,8);
184 idea_cbc_encrypt((unsigned char *)text,out,strlen(text)+1,&key,iv,1);
185 memcpy(iv,k,8);
186 idea_cbc_encrypt(out,out,8,&dkey,iv,0);
187 idea_cbc_encrypt(&(out[8]),&(out[8]),strlen(text)+1-8,&dkey,iv,0);
188 if (memcmp(text,out,strlen(text)+1) != 0)
189 {
190 printf("cbc idea bad\n");
191 err=4;
192 }
193 else
194 printf("cbc idea ok\n");
195
196 printf("cfb64 idea ");
197 if (cfb64_test(cfb_cipher64))
198 {
199 printf("bad\n");
200 err=5;
201 }
202 else
203 printf("ok\n");
204#endif
205
206 exit(err);
207 return(err);
208 }
209
210#ifdef undef
211static int cfb64_test(cfb_cipher)
212unsigned char *cfb_cipher;
213 {
214 IDEA_KEY_SCHEDULE eks,dks;
215 int err=0,i,n;
216
217 idea_set_encrypt_key(cfb_key,&eks);
218 idea_set_decrypt_key(&eks,&dks);
219 memcpy(cfb_tmp,cfb_iv,8);
220 n=0;
221 idea_cfb64_encrypt(plain,cfb_buf1,(long)12,&eks,
222 cfb_tmp,&n,IDEA_ENCRYPT);
223 idea_cfb64_encrypt(&(plain[12]),&(cfb_buf1[12]),
224 (long)CFB_TEST_SIZE-12,&eks,
225 cfb_tmp,&n,IDEA_ENCRYPT);
226 if (memcmp(cfb_cipher,cfb_buf1,CFB_TEST_SIZE) != 0)
227 {
228 err=1;
229 printf("idea_cfb64_encrypt encrypt error\n");
230 for (i=0; i<CFB_TEST_SIZE; i+=8)
231 printf("%s\n",pt(&(cfb_buf1[i])));
232 }
233 memcpy(cfb_tmp,cfb_iv,8);
234 n=0;
235 idea_cfb64_encrypt(cfb_buf1,cfb_buf2,(long)17,&eks,
236 cfb_tmp,&n,IDEA_DECRYPT);
237 idea_cfb64_encrypt(&(cfb_buf1[17]),&(cfb_buf2[17]),
238 (long)CFB_TEST_SIZE-17,&dks,
239 cfb_tmp,&n,IDEA_DECRYPT);
240 if (memcmp(plain,cfb_buf2,CFB_TEST_SIZE) != 0)
241 {
242 err=1;
243 printf("idea_cfb_encrypt decrypt error\n");
244 for (i=0; i<24; i+=8)
245 printf("%s\n",pt(&(cfb_buf2[i])));
246 }
247 return(err);
248 }
249
250static char *pt(p)
251unsigned char *p;
252 {
253 static char bufs[10][20];
254 static int bnum=0;
255 char *ret;
256 int i;
257 static char *f="0123456789ABCDEF";
258
259 ret= &(bufs[bnum++][0]);
260 bnum%=10;
261 for (i=0; i<8; i++)
262 {
263 ret[i*2]=f[(p[i]>>4)&0xf];
264 ret[i*2+1]=f[p[i]&0xf];
265 }
266 ret[16]='\0';
267 return(ret);
268 }
269
270#endif