1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/crypto/builtin/des/t_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 * Copyright (C) 1998 by the FundsXpress, INC.
29 * All rights reserved.
31 * Export of this software from the United States of America may require
32 * a specific license from the United States Government. It is the
33 * responsibility of any person or organization contemplating export to
34 * obtain such a license before exporting.
36 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
37 * distribute this software and its documentation for any purpose and
38 * without fee is hereby granted, provided that the above copyright
39 * notice appear in all copies and that both that copyright notice and
40 * this permission notice appear in supporting documentation, and that
41 * the name of FundsXpress. not be used in advertising or publicity pertaining
42 * to distribution of the software without specific, written prior
43 * permission. FundsXpress makes no representations about the suitability of
44 * this software for any purpose. It is provided "as is" without express
45 * or implied warranty.
47 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
48 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
49 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
54 * Program to test the correctness of the DES library
57 * exit returns 0 ==> success
66 static void do_encrypt(unsigned char *, unsigned char *);
67 static void do_decrypt(unsigned char *, unsigned char *);
77 unsigned char cipher_text[64];
78 unsigned char clear_text[64] = "Now is the time for all " ;
79 unsigned char clear_text2[64] = "7654321 Now is the time for ";
80 unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
81 unsigned char output[64];
82 unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
83 unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
86 /* 0x0123456789abcdef */
87 unsigned char default_key[8] = {
88 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
90 unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
91 unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
93 unsigned char default_ivec[8] = {
94 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
97 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
99 unsigned char cipher1[8] = {
100 0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
102 unsigned char cipher2[8] = {
103 0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
105 unsigned char cipher3[64] = {
106 0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
107 0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
108 0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
110 unsigned char checksum[8] = {
111 0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
114 unsigned char zresult[8] = {
115 0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
118 unsigned char mresult[8] = {
119 0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
125 * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
128 mit_des_key_schedule sched;
135 /* Local Declarations */
141 /* Set screen window buffer to infinite size -- MS default is tiny. */
142 _wsetscreenbuf (fileno (stdout), _WINBUFINF);
144 progname=argv[0]; /* salt away invoking program */
146 while (--argc > 0 && (*++argv)[0] == '-')
147 for (i=1; argv[0][i] != '\0'; i++) {
148 switch (argv[0][i]) {
164 printf("%s: illegal flag \"%c\" ",
165 progname,argv[0][i]);
171 fprintf(stderr, "Usage: %s [-dmz]\n", progname);
175 /* do some initialisation */
177 /* use known input and key */
179 /* ECB zero text zero key */
182 mit_des_key_sched(zero_key, sched);
183 printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
184 do_encrypt(input,cipher_text);
185 printf("\tcipher = (low to high bytes)\n\t\t");
186 for (j = 0; j<=7; j++)
187 printf("%02x ",cipher_text[j]);
189 do_decrypt(output,cipher_text);
190 if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
191 printf("verify: error in zero key test\n");
200 mit_des_key_sched(key3, sched);
201 printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
202 printf("key = 0x80 01 01 01 01 01 01 01\n");
203 printf(" cipher = 0xa380e02a6be54696\n");
204 do_encrypt(input,cipher_text);
205 printf("\tcipher = (low to high bytes)\n\t\t");
206 for (j = 0; j<=7; j++) {
207 printf("%02x ",cipher_text[j]);
210 do_decrypt(output,cipher_text);
211 if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
212 printf("verify: error in msb test\n");
218 /* ECB mode Davies and Price */
221 mit_des_key_sched(key2, sched);
222 printf("Examples per FIPS publication 81, keys ivs and cipher\n");
223 printf("in hex. These are the correct answers, see below for\n");
224 printf("the actual answers.\n\n");
225 printf("Examples per Davies and Price.\n\n");
226 printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
227 printf("\tclear = 0\n");
228 printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
229 printf("ACTUAL ECB\n");
230 printf("\tclear \"%s\"\n", input);
231 do_encrypt(input,cipher_text);
232 printf("\tcipher = (low to high bytes)\n\t\t");
233 for (j = 0; j<=7; j++)
234 printf("%02x ",cipher_text[j]);
236 do_decrypt(output,cipher_text);
237 if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
238 printf("verify: error in ECB encryption\n");
242 printf("verify: ECB encryption is correct\n\n");
247 mit_des_key_sched(default_key, sched);
250 printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
251 printf("\tclear = \"Now is the time for all \"\n");
252 printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
253 printf("ACTUAL ECB\n\tclear \"%s\"",input);
254 do_encrypt(input,cipher_text);
255 printf("\n\tcipher = (low to high bytes)\n\t\t");
256 for (j = 0; j<=7; j++) {
257 printf("%02x ",cipher_text[j]);
260 do_decrypt(output,cipher_text);
261 if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
262 printf("verify: error in ECB encryption\n");
266 printf("verify: ECB encryption is correct\n\n");
270 printf("EXAMPLE CBC\tkey = 0123456789abcdef");
271 printf("\tiv = 1234567890abcdef\n");
272 printf("\tclear = \"Now is the time for all \"\n");
273 printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
274 printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
275 printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
277 printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
278 in_length = strlen((char *)input);
279 if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) input,
280 (mit_des_cblock *) cipher_text,
285 com_err("des verify", retval, "can't encrypt");
288 printf("\tciphertext = (low to high bytes)\n");
289 for (i = 0; i <= 2; i++) {
291 for (j = 0; j <= 7; j++) {
292 printf("%02x ",cipher_text[i*8+j]);
296 if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) cipher_text,
297 (mit_des_cblock *) clear_text,
302 com_err("des verify", retval, "can't decrypt");
305 printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
307 if ( memcmp((char *)cipher_text, (char *)cipher3, in_length) ) {
308 printf("verify: error in CBC encryption\n");
312 printf("verify: CBC encryption is correct\n\n");
314 printf("EXAMPLE CBC checksum");
315 printf("\tkey = 0123456789abcdef\tiv = 1234567890abcdef\n");
316 printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
317 printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
318 printf("or some part thereof\n");
320 mit_des_cbc_cksum(input,cipher_text, strlen((char *)input),
322 printf("ACTUAL CBC checksum\n");
323 printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
324 for (j = 0; j<=7; j++)
325 printf("%02x ",cipher_text[j]);
327 if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
328 printf("verify: error in CBC cheksum\n");
332 printf("verify: CBC checksum is correct\n\n");
342 register int old,new,i,j;
343 /* flips the bit order within each byte from 0 lsb to 0 msb */
344 for (i = 0; i<=7; i++) {
347 for (j = 0; j<=7; j++) {
367 for (i =1; i<=nflag; i++) {
368 mit_des_cbc_encrypt((const mit_des_cblock *)in,
369 (mit_des_cblock *)out,
375 printf("\nclear %s\n",in);
376 for (j = 0; j<=7; j++)
377 printf("%02X ",in[j] & 0xff);
379 for (j = 0; j<=7; j++)
380 printf("%02X ",out[j] & 0xff);
389 /* try to invert it */
392 for (i =1; i<=nflag; i++) {
393 mit_des_cbc_encrypt((const mit_des_cblock *)out,
394 (mit_des_cblock *)in,
400 printf("clear %s\n",in);
401 for (j = 0; j<=7; j++)
402 printf("%02X ",in[j] & 0xff);
404 for (j = 0; j<=7; j++)
405 printf("%02X ",out[j] & 0xff);
411 * Fake out the DES library, for the purposes of testing.
415 mit_des_is_weak_key(key)
418 return 0; /* fake it out for testing */