2 * lib/crypto/des/verify.c
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
35 * Copyright (C) 1998 by the FundsXpress, INC.
37 * All rights reserved.
39 * Export of this software from the United States of America may require
40 * a specific license from the United States Government. It is the
41 * responsibility of any person or organization contemplating export to
42 * obtain such a license before exporting.
44 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
45 * distribute this software and its documentation for any purpose and
46 * without fee is hereby granted, provided that the above copyright
47 * notice appear in all copies and that both that copyright notice and
48 * this permission notice appear in supporting documentation, and that
49 * the name of FundsXpress. not be used in advertising or publicity pertaining
50 * to distribution of the software without specific, written prior
51 * permission. FundsXpress makes no representations about the suitability of
52 * this software for any purpose. It is provided "as is" without express
53 * or implied warranty.
55 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
56 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
57 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
65 static void do_encrypt(unsigned char *, unsigned char *);
66 static void do_decrypt(unsigned char *, unsigned char *);
76 unsigned char cipher_text[64];
77 unsigned char clear_text[64] = "Now is the time for all " ;
78 unsigned char clear_text2[64] = "7654321 Now is the time for ";
79 unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
80 unsigned char output[64];
81 unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
82 unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
85 /* 0x0123456789abcdef */
86 unsigned char default_key[8] = {
87 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
89 unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
90 unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
92 unsigned char default_ivec[8] = {
93 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
96 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
98 unsigned char cipher1[8] = {
99 0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
101 unsigned char cipher2[8] = {
102 0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
104 unsigned char cipher3[64] = {
105 0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
106 0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
107 0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
109 unsigned char checksum[8] = {
110 0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
113 unsigned char zresult[8] = {
114 0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
117 unsigned char mresult[8] = {
118 0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
124 * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
127 mit_des_key_schedule sched;
134 /* Local Declarations */
140 /* Set screen window buffer to infinite size -- MS default is tiny. */
141 _wsetscreenbuf (fileno (stdout), _WINBUFINF);
143 progname=argv[0]; /* salt away invoking program */
145 while (--argc > 0 && (*++argv)[0] == '-')
146 for (i=1; argv[0][i] != '\0'; i++) {
147 switch (argv[0][i]) {
163 printf("%s: illegal flag \"%c\" ",
164 progname,argv[0][i]);
170 fprintf(stderr, "Usage: %s [-dmz]\n", progname);
174 /* do some initialisation */
176 /* use known input and key */
178 /* ECB zero text zero key */
181 mit_des_key_sched(zero_key, sched);
182 printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
183 do_encrypt(input,cipher_text);
184 printf("\tcipher = (low to high bytes)\n\t\t");
185 for (j = 0; j<=7; j++)
186 printf("%02x ",cipher_text[j]);
188 do_decrypt(output,cipher_text);
189 if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
190 printf("verify: error in zero key test\n");
199 mit_des_key_sched(key3, sched);
200 printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
201 printf("key = 0x80 01 01 01 01 01 01 01\n");
202 printf(" cipher = 0xa380e02a6be54696\n");
203 do_encrypt(input,cipher_text);
204 printf("\tcipher = (low to high bytes)\n\t\t");
205 for (j = 0; j<=7; j++) {
206 printf("%02x ",cipher_text[j]);
209 do_decrypt(output,cipher_text);
210 if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
211 printf("verify: error in msb test\n");
217 /* ECB mode Davies and Price */
220 mit_des_key_sched(key2, sched);
221 printf("Examples per FIPS publication 81, keys ivs and cipher\n");
222 printf("in hex. These are the correct answers, see below for\n");
223 printf("the actual answers.\n\n");
224 printf("Examples per Davies and Price.\n\n");
225 printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
226 printf("\tclear = 0\n");
227 printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
228 printf("ACTUAL ECB\n");
229 printf("\tclear \"%s\"\n", input);
230 do_encrypt(input,cipher_text);
231 printf("\tcipher = (low to high bytes)\n\t\t");
232 for (j = 0; j<=7; j++)
233 printf("%02x ",cipher_text[j]);
235 do_decrypt(output,cipher_text);
236 if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
237 printf("verify: error in ECB encryption\n");
241 printf("verify: ECB encryption is correct\n\n");
246 mit_des_key_sched(default_key, sched);
249 printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
250 printf("\tclear = \"Now is the time for all \"\n");
251 printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
252 printf("ACTUAL ECB\n\tclear \"%s\"",input);
253 do_encrypt(input,cipher_text);
254 printf("\n\tcipher = (low to high bytes)\n\t\t");
255 for (j = 0; j<=7; j++) {
256 printf("%02x ",cipher_text[j]);
259 do_decrypt(output,cipher_text);
260 if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
261 printf("verify: error in ECB encryption\n");
265 printf("verify: ECB encryption is correct\n\n");
269 printf("EXAMPLE CBC\tkey = 0123456789abcdef");
270 printf("\tiv = 1234567890abcdef\n");
271 printf("\tclear = \"Now is the time for all \"\n");
272 printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
273 printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
274 printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
276 printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
277 in_length = strlen((char *)input);
278 if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) input,
279 (mit_des_cblock *) cipher_text,
284 com_err("des verify", retval, "can't encrypt");
287 printf("\tciphertext = (low to high bytes)\n");
288 for (i = 0; i <= 2; i++) {
290 for (j = 0; j <= 7; j++) {
291 printf("%02x ",cipher_text[i*8+j]);
295 if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) cipher_text,
296 (mit_des_cblock *) clear_text,
301 com_err("des verify", retval, "can't decrypt");
304 printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
306 if ( memcmp((char *)cipher_text, (char *)cipher3, in_length) ) {
307 printf("verify: error in CBC encryption\n");
311 printf("verify: CBC encryption is correct\n\n");
313 printf("EXAMPLE CBC checksum");
314 printf("\tkey = 0123456789abcdef\tiv = 1234567890abcdef\n");
315 printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
316 printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
317 printf("or some part thereof\n");
319 mit_des_cbc_cksum(input,cipher_text, strlen((char *)input),
321 printf("ACTUAL CBC checksum\n");
322 printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
323 for (j = 0; j<=7; j++)
324 printf("%02x ",cipher_text[j]);
326 if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
327 printf("verify: error in CBC cheksum\n");
331 printf("verify: CBC checksum is correct\n\n");
341 register int old,new,i,j;
342 /* flips the bit order within each byte from 0 lsb to 0 msb */
343 for (i = 0; i<=7; i++) {
346 for (j = 0; j<=7; j++) {
366 for (i =1; i<=nflag; i++) {
367 mit_des_cbc_encrypt((const mit_des_cblock *)in,
368 (mit_des_cblock *)out,
374 printf("\nclear %s\n",in);
375 for (j = 0; j<=7; j++)
376 printf("%02X ",in[j] & 0xff);
378 for (j = 0; j<=7; j++)
379 printf("%02X ",out[j] & 0xff);
388 /* try to invert it */
391 for (i =1; i<=nflag; i++) {
392 mit_des_cbc_encrypt((const mit_des_cblock *)out,
393 (mit_des_cblock *)in,
399 printf("clear %s\n",in);
400 for (j = 0; j<=7; j++)
401 printf("%02X ",in[j] & 0xff);
403 for (j = 0; j<=7; j++)
404 printf("%02X ",out[j] & 0xff);
410 * Fake out the DES library, for the purposes of testing.
414 mit_des_is_weak_key(key)
417 return 0; /* fake it out for testing */