2 * Copyright 1988 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
7 * Program to test the correctness of the DES library
10 * exit returns 0 ==> success
14 #include <mit-cpyright.h>
18 #include <afsconfig.h>
19 #include <afs/param.h>
23 extern char *errmsg();
24 extern void des_string_to_key();
25 extern int des_key_sched();
26 extern int des_ecb_encrypt();
27 extern int des_cbc_encrypt();
37 unsigned char cipher_text[64];
38 unsigned char clear_text[64] = "Now is the time for all ";
39 unsigned char clear_text2[64] = "7654321 Now is the time for ";
40 unsigned char clear_text3[64] = { 2, 0, 0, 0, 1, 0, 0, 0 };
41 unsigned char output[64];
42 unsigned char zero_text[8] = { 0x0, 0, 0, 0, 0, 0, 0, 0 };
43 unsigned char msb_text[8] = { 0x0, 0, 0, 0, 0, 0, 0, 0x40 }; /* to ANSI MSB */
46 /* 0x0123456789abcdef */
47 unsigned char default_key[8] = {
48 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef
50 unsigned char key2[8] = { 0x08, 0x19, 0x2a, 0x3b, 0x4c, 0x5d, 0x6e, 0x7f };
51 unsigned char key3[8] = { 0x80, 1, 1, 1, 1, 1, 1, 1 };
53 unsigned char default_ivec[8] = {
54 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef
57 unsigned char zero_key[8] = { 1, 1, 1, 1, 1, 1, 1, 1 }; /* just parity bits */
62 * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
69 /* Local Declarations */
72 progname = argv[0]; /* salt away invoking program */
74 while (--argc > 0 && (*++argv)[0] == '-')
75 for (i = 1; argv[0][i] != '\0'; i++) {
92 printf("%s: illegal flag \"%c\" ", progname, argv[0][i]);
98 fprintf(stderr, "Usage: %s [-dmz]\n", progname);
102 /* use known input and key */
104 /* ECB zero text zero key */
107 des_key_sched(zero_key, KS);
108 printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
109 do_encrypt(input, cipher_text);
110 printf("\tcipher = (low to high bytes)\n\t\t");
111 for (j = 0; j <= 7; j++)
112 printf("%02x ", cipher_text[j]);
114 do_decrypt(output, cipher_text);
120 des_key_sched(key3, KS);
121 printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
122 printf("key = 0, cipher = 0x??\n");
123 do_encrypt(input, cipher_text);
124 printf("\tcipher = (low to high bytes)\n\t\t");
125 for (j = 0; j <= 7; j++) {
126 printf("%02x ", cipher_text[j]);
129 do_decrypt(output, cipher_text);
133 /* ECB mode Davies and Price */
136 des_key_sched(key2, KS);
137 printf("Examples per FIPS publication 81, keys ivs and cipher\n");
138 printf("in hex. These are the correct answers, see below for\n");
139 printf("the actual answers.\n\n");
140 printf("Examples per Davies and Price.\n\n");
141 printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
142 printf("\tclear = 0\n");
143 printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
144 printf("ACTUAL ECB\n");
145 printf("\tclear \"%s\"\n", input);
146 do_encrypt(input, cipher_text);
147 printf("\tcipher = (low to high bytes)\n\t\t");
148 for (j = 0; j <= 7; j++)
149 printf("%02x ", cipher_text[j]);
151 do_decrypt(output, cipher_text);
156 des_key_sched(default_key, KS);
159 printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
160 printf("\tclear = \"Now is the time for all \"\n");
161 printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
162 printf("ACTUAL ECB\n\tclear \"%s\"", input);
163 do_encrypt(input, cipher_text);
164 printf("\n\tcipher = (low to high bytes)\n\t\t");
165 for (j = 0; j <= 7; j++) {
166 printf("%02x ", cipher_text[j]);
169 do_decrypt(output, cipher_text);
173 printf("EXAMPLE CBC\tkey = 0123456789abcdef");
174 printf("\tiv = 1234567890abcdef\n");
175 printf("\tclear = \"Now is the time for all \"\n");
176 printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
177 printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
178 printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
180 printf("ACTUAL CBC\n\tclear \"%s\"\n", input);
181 in_length = strlen(input);
182 des_cbc_encrypt(input, cipher_text, (afs_int32) in_length, KS, ivec, 1);
183 printf("\tciphertext = (low to high bytes)\n");
184 for (i = 0; i <= 7; i++) {
186 for (j = 0; j <= 7; j++) {
187 printf("%02x ", cipher_text[i * 8 + j]);
191 des_cbc_encrypt(cipher_text, clear_text, (afs_int32) in_length, KS, ivec,
193 printf("\tdecrypted clear_text = \"%s\"\n", clear_text);
195 printf("EXAMPLE CBC checksum");
196 printf("\tkey = 0123456789abcdef\tiv = 1234567890abcdef\n");
197 printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
198 printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
199 printf("or some part thereof\n");
201 des_cbc_cksum(input, cipher_text, (afs_int32) strlen(input), KS, ivec, 1);
202 printf("ACTUAL CBC checksum\n");
203 printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
204 for (j = 0; j <= 7; j++)
205 printf("%02x ", cipher_text[j]);
213 /* flips the bit order within each byte from 0 lsb to 0 msb */
214 for (i = 0; i <= 7; i++) {
217 for (j = 0; j <= 7; j++) {
234 for (i = 1; i <= nflag; i++) {
235 des_ecb_encrypt(in, out, KS, 1);
237 printf("\nclear %s\n", in);
238 for (j = 0; j <= 7; j++)
239 printf("%02 X ", in[j] & 0xff);
241 for (j = 0; j <= 7; j++)
242 printf("%02X ", out[j] & 0xff);
250 /* try to invert it */
252 for (i = 1; i <= nflag; i++) {
253 des_ecb_encrypt(out, in, KS, 0);
255 printf("clear %s\n", in);
256 for (j = 0; j <= 7; j++)
257 printf("%02X ", in[j] & 0xff);
259 for (j = 0; j <= 7; j++)
260 printf("%02X ", out[j] & 0xff);