4
* Copyright 1988,1990 by the Massachusetts Institute of Technology.
7
* Export of this software from the United States of America may
8
* require a specific license from the United States Government.
9
* It is the responsibility of any person or organization contemplating
10
* export to obtain such a license before exporting.
12
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13
* distribute this software and its documentation for any purpose and
14
* without fee is hereby granted, provided that the above copyright
15
* notice appear in all copies and that both that copyright notice and
16
* this permission notice appear in supporting documentation, and that
17
* the name of M.I.T. not be used in advertising or publicity pertaining
18
* to distribution of the software without specific, written prior
19
* permission. Furthermore if you modify this software you must label
20
* your software as modified software and not distribute it in such a
21
* fashion that it might be confused with the original M.I.T. software.
22
* M.I.T. makes no representations about the suitability of
23
* this software for any purpose. It is provided "as is" without express
24
* or implied warranty.
27
* Program to test the correctness of the DES library
30
* exit returns 0 ==> success
48
unsigned char cipher_text[64];
49
unsigned char clear_text[64] = "Now is the time for all " ;
50
unsigned char clear_text2[64] = "7654321 Now is the time for ";
51
unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
52
unsigned char output[64];
53
unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
54
unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
57
/* 0x0123456789abcdef */
58
unsigned char default_key[8] = {
59
0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
61
unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
62
unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
64
unsigned char default_ivec[8] = {
65
0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
68
unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
71
unsigned char cipher1[8] = {
72
0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
74
unsigned char cipher2[8] = {
75
0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
77
unsigned char cipher3[64] = {
78
0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
79
0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
80
0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
82
unsigned char checksum[8] = {
83
0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
86
unsigned char zresult[8] = {
87
0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
90
unsigned char mresult[8] = {
91
0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
97
* plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
100
void do_encrypt (unsigned char *, unsigned char *);
101
void do_decrypt (unsigned char *, unsigned char *);
108
/* Local Declarations */
109
unsigned long in_length;
111
progname=argv[0]; /* salt away invoking program */
113
while (--argc > 0 && (*++argv)[0] == '-')
114
for (i=1; argv[0][i] != '\0'; i++) {
115
switch (argv[0][i]) {
131
printf("%s: illegal flag \"%c\" ",
132
progname,argv[0][i]);
138
fprintf(stderr, "Usage: %s [-dmz]\n", progname);
142
/* use known input and key */
144
/* ECB zero text zero key */
147
des_key_sched(zero_key,KS);
148
printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
149
do_encrypt(input,cipher_text);
150
printf("\tcipher = (low to high bytes)\n\t\t");
151
for (j = 0; j<=7; j++)
152
printf("%02x ",cipher_text[j]);
154
do_decrypt(output,cipher_text);
155
if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
156
printf("verify: error in zero key test\n");
164
des_key_sched(key3,KS);
165
printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
166
printf("key = 0, cipher = 0x??\n");
167
do_encrypt(input,cipher_text);
168
printf("\tcipher = (low to high bytes)\n\t\t");
169
for (j = 0; j<=7; j++) {
170
printf("%02x ",cipher_text[j]);
173
do_decrypt(output,cipher_text);
174
if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
175
printf("verify: error in msb test\n");
181
/* ECB mode Davies and Price */
184
des_key_sched(key2,KS);
185
printf("Examples per FIPS publication 81, keys ivs and cipher\n");
186
printf("in hex. These are the correct answers, see below for\n");
187
printf("the actual answers.\n\n");
188
printf("Examples per Davies and Price.\n\n");
189
printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
190
printf("\tclear = 0\n");
191
printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
192
printf("ACTUAL ECB\n");
193
printf("\tclear \"%s\"\n", input);
194
do_encrypt(input,cipher_text);
195
printf("\tcipher = (low to high bytes)\n\t\t");
196
for (j = 0; j<=7; j++)
197
printf("%02x ",cipher_text[j]);
199
do_decrypt(output,cipher_text);
200
if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
201
printf("verify: error in ECB encryption\n");
205
printf("verify: ECB encription is correct\n\n");
210
des_key_sched(default_key,KS);
213
printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
214
printf("\tclear = \"Now is the time for all \"\n");
215
printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
216
printf("ACTUAL ECB\n\tclear \"%s\"",input);
217
do_encrypt(input,cipher_text);
218
printf("\n\tcipher = (low to high bytes)\n\t\t");
219
for (j = 0; j<=7; j++) {
220
printf("%02x ",cipher_text[j]);
223
do_decrypt(output,cipher_text);
224
if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
225
printf("verify: error in ECB encryption\n");
229
printf("verify: ECB encription is correct\n\n");
233
printf("EXAMPLE CBC\tkey = 0123456789abcdef");
234
printf("\tiv = 1234567890abcdef\n");
235
printf("\tclear = \"Now is the time for all \"\n");
236
printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
237
printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
238
printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
240
printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
241
in_length = strlen((char *) input);
242
des_cbc_encrypt(input,cipher_text, in_length,KS,ivec,1);
243
printf("\tciphertext = (low to high bytes)\n");
244
for (i = 0; i <= 7; i++) {
246
for (j = 0; j <= 7; j++) {
247
printf("%02x ",cipher_text[i*8+j]);
251
des_cbc_encrypt(cipher_text,clear_text,in_length,KS,ivec,0);
252
printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
254
if ( memcmp(cipher_text, cipher3, (size_t) in_length) ) {
255
printf("verify: error in CBC encryption\n");
259
printf("verify: CBC encription is correct\n\n");
261
printf("EXAMPLE CBC checksum");
262
printf("\tkey = 0123456789abcdef\tiv = 1234567890abcdef\n");
263
printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
264
printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
265
printf("or some part thereof\n");
267
des_cbc_cksum(input,cipher_text,(long) strlen((char *) input),KS,ivec);
268
printf("ACTUAL CBC checksum\n");
269
printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
270
for (j = 0; j<=7; j++)
271
printf("%02x ",cipher_text[j]);
273
if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
274
printf("verify: error in CBC cheksum\n");
278
printf("verify: CBC checksum is correct\n\n");
287
for (i =1; i<=nflag; i++) {
288
des_ecb_encrypt((unsigned long *) in, (unsigned long *)out, KS, 1);
290
printf("\nclear %s\n",in);
291
for (j = 0; j<=7; j++)
292
printf("%02X ",in[j] & 0xff);
294
for (j = 0; j<=7; j++)
295
printf("%02X ",out[j] & 0xff);
304
/* try to invert it */
306
for (i =1; i<=nflag; i++) {
307
des_ecb_encrypt((unsigned long *) out, (unsigned long *)in,KS,0);
309
printf("clear %s\n",in);
310
for (j = 0; j<=7; j++)
311
printf("%02X ",in[j] & 0xff);
313
for (j = 0; j<=7; j++)
314
printf("%02X ",out[j] & 0xff);