3
* Fast DES evaluation & benchmarking for UNIX
4
* Compares output of fencrypt()/fsetkey() with UNIX crypt()/encrypt()/setkey()
5
* and measures speed using times().
11
#include <sys/types.h>
13
#include <sys/param.h> /* for HZ */
14
#include <sys/times.h>
32
#if BSD >= 43 && defined(vax) && !defined(HZ)
43
for(i = 0; i < 64; i += 4)
44
sprintf(&s[i/4], "%1x", b[i]<<3 | b[i+1]<<2 | b[i+2]<<1 | b[i+3]);
54
for(i = 0; i < 8; i++)
55
sprintf(&s[2*i], "%02x", v->b[i] & 0xff);
59
void getv(char *s, chunk *v)
63
if(s[0] == '0' && s[1] == 'x')
65
for(i = 0; i < 8; i++) {
67
if(*s >= '0' && *s <= '9') t = *s++ - '0';
68
else if(*s >= 'a' && *s <= 'f') t = *s++ - 'a' + 10;
69
else if(*s >= 'A' && *s <= 'F') t = *s++ - 'A' + 10;
71
if(*s >= '0' && *s <= '9') t |= *s++ - '0';
72
else if(*s >= 'a' && *s <= 'f') t |= *s++ - 'a' + 10;
73
else if(*s >= 'A' && *s <= 'F') t |= *s++ - 'A' + 10;
82
void expand(chunk *v, char bits[64])
84
register unsigned int i;
86
for(i = 0; i < 64; i++)
87
bits[i] = (v->b[i/8] >> (7 - i%8)) & 1;
92
main(int argc, char *argv[])
95
chunk key, olddata, newdata;
96
char bkey[64], bdata[64];
100
if(argc < 2 || argv[1][0] == '-') {
104
Usage: %s key [ -{ckCK} count ] [ data ... ]\n\
105
Demonstrate and/or time fast DES routines.\n\
106
``key'' and ``data'' are left-justified, 0-padded hex values <= 16 digits\n\
107
By default, encrypts & decrypts each 'data' block with both fastdes and\n\
108
crypt() library DES routines to show equality.\n\
109
-c N encrypt N times using fast DES\n\
110
-k N set-key N times using fast DES\n\
111
-C N encrypt N times using library DES\n\
115
-K N set-key N times using library DES\n");
123
/* System V systems don't seem to have setkey, just crypt
124
* so we use that to set the key.
126
for(i = 0; i < 8; i++)
127
bdata[i] = (key.b[i] >> 1) | 0x80;
129
crypt((char *)bdata, ".."); /* Key, no salt */
133
printf("key\t%s\n", bprint(bkey));
135
for(i = 2; i < argc; i++) {
136
if(argv[i][0] == '-') {
139
struct tms now, then;
144
if((op = argv[++i]) == NULL)
150
expand(&olddata, bdata);
155
do fencrypt(newdata.b, 0, &KS); while(--n > 0); break;
158
do fsetkey(key.b, &KS); while(--n > 0); break;
161
op = "library encrypt";
162
do encrypt(bdata, decrypt); while(--n > 0); break;
166
printf("UNIX library has no setkey() function on this system\n");
169
op = "library setkey";
170
do setkey(bkey); while(--n > 0); break;
174
printf("Unknown option -%c\n", c);
178
n = then.tms_utime - now.tms_utime;
179
printf("%d %s's in %0.2f seconds (%.2f us apiece)\n",
180
count, op, (float) n / HZ,
181
(1.0e6 * n / (HZ * count)));
183
/* Demonstrate that it works for a particular data block.
184
* To compare with UNIX encrypt(), we must play its game.
185
* On BSD systems, encrypt(block, 1) is the inverse of (..., 0)
186
* but on USG systems the second parameter is ignored and
187
* encrypt(block, x) always encrypts.
189
getv(argv[i], &olddata);
192
printf("\tOriginal data\t\tEncrypted\t\t%s\n",
193
USG ? "Encrypted again" : "Decrypted");
195
printf("fastdes\t%s", wprint(&olddata));
196
fencrypt(newdata.b, 0, &KS);
197
printf("\t%s", wprint(&newdata));
198
fencrypt(newdata.b, USG ? 0 : 1, &KS);
199
printf("\t%s\n", wprint(&newdata));
201
expand(&olddata, bdata);
202
printf("UNIXdes\t%s", bprint(bdata));
204
printf("\t%s", bprint(bdata));
205
encrypt(bdata, USG ? 0 : 1);
206
printf("\t%s\n", bprint(bdata));