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. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
24 * Program to test the correctness of the DES library
27 * exit returns 0 ==> success
36 extern char *errmsg();
38 extern int des_string_to_key();
39 extern int des_key_sched();
40 extern int des_ecb_encrypt();
41 extern int des_cbc_encrypt();
50 unsigned char cipher_text[64];
51 unsigned char clear_text[64] = "Now is the time for all " ;
52 unsigned char clear_text2[64] = "7654321 Now is the time for ";
53 unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
54 unsigned char output[64];
55 unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
56 unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
59 /* 0x0123456789abcdef */
60 unsigned char default_key[8] = {
61 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
63 unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
64 unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
66 unsigned char default_ivec[8] = {
67 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
70 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
73 unsigned char cipher1[8] = {
74 0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
76 unsigned char cipher2[8] = {
77 0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
79 unsigned char cipher3[64] = {
80 0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
81 0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
82 0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
84 unsigned char checksum[8] = {
85 0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
88 unsigned char zresult[8] = {
89 0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
92 unsigned char mresult[8] = {
93 0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
99 * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
106 /* Local Declarations */
109 progname=argv[0]; /* salt away invoking program */
111 while (--argc > 0 && (*++argv)[0] == '-')
112 for (i=1; argv[0][i] != '\0'; i++) {
113 switch (argv[0][i]) {
129 printf("%s: illegal flag \"%c\" ",
130 progname,argv[0][i]);
136 fprintf(stderr, "Usage: %s [-dmz]\n", progname);
140 /* use known input and key */
142 /* ECB zero text zero key */
145 des_key_sched(zero_key,KS);
146 printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
147 do_encrypt(input,cipher_text);
148 printf("\tcipher = (low to high bytes)\n\t\t");
149 for (j = 0; j<=7; j++)
150 printf("%02x ",cipher_text[j]);
152 do_decrypt(output,cipher_text);
153 if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
154 printf("verify: error in zero key test\n");
162 des_key_sched(key3,KS);
163 printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
164 printf("key = 0, cipher = 0x??\n");
165 do_encrypt(input,cipher_text);
166 printf("\tcipher = (low to high bytes)\n\t\t");
167 for (j = 0; j<=7; j++) {
168 printf("%02x ",cipher_text[j]);
171 do_decrypt(output,cipher_text);
172 if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
173 printf("verify: error in msb test\n");
179 /* ECB mode Davies and Price */
182 des_key_sched(key2,KS);
183 printf("Examples per FIPS publication 81, keys ivs and cipher\n");
184 printf("in hex. These are the correct answers, see below for\n");
185 printf("the actual answers.\n\n");
186 printf("Examples per Davies and Price.\n\n");
187 printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
188 printf("\tclear = 0\n");
189 printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
190 printf("ACTUAL ECB\n");
191 printf("\tclear \"%s\"\n", input);
192 do_encrypt(input,cipher_text);
193 printf("\tcipher = (low to high bytes)\n\t\t");
194 for (j = 0; j<=7; j++)
195 printf("%02x ",cipher_text[j]);
197 do_decrypt(output,cipher_text);
198 if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
199 printf("verify: error in ECB encryption\n");
203 printf("verify: ECB encription is correct\n\n");
208 des_key_sched(default_key,KS);
211 printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
212 printf("\tclear = \"Now is the time for all \"\n");
213 printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
214 printf("ACTUAL ECB\n\tclear \"%s\"",input);
215 do_encrypt(input,cipher_text);
216 printf("\n\tcipher = (low to high bytes)\n\t\t");
217 for (j = 0; j<=7; j++) {
218 printf("%02x ",cipher_text[j]);
221 do_decrypt(output,cipher_text);
222 if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
223 printf("verify: error in ECB encryption\n");
227 printf("verify: ECB encription is correct\n\n");
231 printf("EXAMPLE CBC\tkey = 0123456789abcdef");
232 printf("\tiv = 1234567890abcdef\n");
233 printf("\tclear = \"Now is the time for all \"\n");
234 printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
235 printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
236 printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
238 printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
239 in_length = strlen(input);
240 des_cbc_encrypt(input,cipher_text,(long) in_length,KS,ivec,1);
241 printf("\tciphertext = (low to high bytes)\n");
242 for (i = 0; i <= 7; i++) {
244 for (j = 0; j <= 7; j++) {
245 printf("%02x ",cipher_text[i*8+j]);
249 des_cbc_encrypt(cipher_text,clear_text,(long) in_length,KS,ivec,0);
250 printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
252 if ( memcmp((char *)cipher_text, (char *)cipher3, in_length) ) {
253 printf("verify: error in CBC encryption\n");
257 printf("verify: CBC encription is correct\n\n");
259 printf("EXAMPLE CBC checksum");
260 printf("\tkey = 0123456789abcdef\tiv = 1234567890abcdef\n");
261 printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
262 printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
263 printf("or some part thereof\n");
265 des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
266 printf("ACTUAL CBC checksum\n");
267 printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
268 for (j = 0; j<=7; j++)
269 printf("%02x ",cipher_text[j]);
271 if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
272 printf("verify: error in CBC cheksum\n");
276 printf("verify: CBC checksum is correct\n\n");
283 register old,new,i,j;
284 /* flips the bit order within each byte from 0 lsb to 0 msb */
285 for (i = 0; i<=7; i++) {
288 for (j = 0; j<=7; j++) {
305 for (i =1; i<=nflag; i++) {
306 des_ecb_encrypt(in,out,KS,1);
308 printf("\nclear %s\n",in);
309 for (j = 0; j<=7; j++)
310 printf("%02 X ",in[j] & 0xff);
312 for (j = 0; j<=7; j++)
313 printf("%02X ",out[j] & 0xff);
321 /* try to invert it */
323 for (i =1; i<=nflag; i++) {
324 des_ecb_encrypt(out,in,KS,0);
326 printf("clear %s\n",in);
327 for (j = 0; j<=7; j++)
328 printf("%02X ",in[j] & 0xff);
330 for (j = 0; j<=7; j++)
331 printf("%02X ",out[j] & 0xff);