a6ad07cb8962d56ce5a104b8430cc983db2d55b0
[krb5.git] / src / lib / crypto / crypto_tests / t_verify.c
1 /*
2  * lib/crypto/des/verify.c
3  *
4  * Copyright 1988, 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
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.
11  * 
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.
25  * 
26  *
27  * Program to test the correctness of the DES library
28  * implementation.
29  *
30  * exit returns  0 ==> success
31  *              -1 ==> error
32  */
33
34 /*
35  * Copyright (C) 1998 by the FundsXpress, INC.
36  * 
37  * All rights reserved.
38  * 
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.
43  * 
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.
54  * 
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.
58  */
59
60 #include "k5-int.h"
61 #include "des_int.h"
62 #include <stdio.h>
63 #include "com_err.h"
64
65 static void do_encrypt(unsigned char *, unsigned char *);
66 static void do_decrypt(unsigned char *, unsigned char *);
67
68 char *progname;
69 int nflag = 2;
70 int vflag;
71 int mflag;
72 int zflag;
73 int pid;
74 int mit_des_debug;
75
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 */
83 unsigned char *input;
84
85 /* 0x0123456789abcdef */
86 unsigned char default_key[8] = {
87     0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
88 };
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 };
91 mit_des_cblock s_key;
92 unsigned char default_ivec[8] = {
93     0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
94 };
95 unsigned char *ivec;
96 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
97
98 unsigned char cipher1[8] = {
99     0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
100 };
101 unsigned char cipher2[8] = {
102     0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
103 };
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
108 };
109 unsigned char checksum[8] = {
110     0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
111 };
112
113 unsigned char zresult[8] = {
114     0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
115 };
116
117 unsigned char mresult[8] = {
118     0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
119 };
120
121     
122 /*
123  * Can also add :
124  * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
125  */
126
127 mit_des_key_schedule sched;
128
129 int
130 main(argc,argv)
131     int argc;
132     char *argv[];
133 {
134     /* Local Declarations */
135     size_t  in_length;
136     int  retval;
137     int i, j;
138
139 #ifdef WINDOWS
140     /* Set screen window buffer to infinite size -- MS default is tiny.  */
141     _wsetscreenbuf (fileno (stdout), _WINBUFINF);
142 #endif
143     progname=argv[0];           /* salt away invoking program */
144
145     while (--argc > 0 && (*++argv)[0] == '-')
146         for (i=1; argv[0][i] != '\0'; i++) {
147             switch (argv[0][i]) {
148
149                 /* debug flag */
150             case 'd':
151                 mit_des_debug=3;
152                 continue;
153
154             case 'z':
155                 zflag = 1;
156                 continue;
157
158             case 'm':
159                 mflag = 1;
160                 continue;
161
162             default:
163                 printf("%s: illegal flag \"%c\" ",
164                        progname,argv[0][i]);
165                 exit(1);
166             }
167         };
168
169     if (argc) {
170         fprintf(stderr, "Usage: %s [-dmz]\n", progname);
171         exit(1);
172     }
173
174     /* do some initialisation */
175
176     /* use known input and key */
177
178     /* ECB zero text zero key */
179     if (zflag) {
180         input = zero_text;
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]);
187         printf("\n");
188         do_decrypt(output,cipher_text);
189         if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
190             printf("verify: error in zero key test\n");
191             exit(-1);
192         }
193
194         exit(0);
195     }
196
197     if (mflag) {
198         input = msb_text;
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]);
207         }
208         printf("\n");
209         do_decrypt(output,cipher_text);
210         if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
211             printf("verify: error in msb test\n");
212             exit(-1);
213         }
214         exit(0);
215     }
216
217     /* ECB mode Davies and Price */
218     {
219         input = zero_text;
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]);
234         printf("\n\n");
235         do_decrypt(output,cipher_text);
236         if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
237             printf("verify: error in ECB encryption\n");
238             exit(-1);
239         }
240         else 
241             printf("verify: ECB encryption is correct\n\n");
242     }
243
244     /* ECB mode */
245     {
246         mit_des_key_sched(default_key, sched);
247         input = clear_text;
248         ivec = default_ivec;
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]);
257         }
258         printf("\n\n");
259         do_decrypt(output,cipher_text);
260         if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
261             printf("verify: error in ECB encryption\n");
262             exit(-1);
263         }
264         else 
265             printf("verify: ECB encryption is correct\n\n");
266     }
267
268     /* CBC mode */
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");
275
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,
280                                       (size_t) in_length, 
281                                       sched,
282                                       ivec,
283                                       MIT_DES_ENCRYPT))) {
284         com_err("des verify", retval, "can't encrypt");
285         exit(-1);
286     }
287     printf("\tciphertext = (low to high bytes)\n");
288     for (i = 0; i <= 2; i++) {
289         printf("\t\t");
290         for (j = 0; j <= 7; j++) {
291             printf("%02x ",cipher_text[i*8+j]);
292         }
293         printf("\n");
294     }
295     if ((retval = mit_des_cbc_encrypt((const mit_des_cblock *) cipher_text,
296                                       (mit_des_cblock *) clear_text,
297                                       (size_t) in_length, 
298                                       sched,
299                                       ivec,
300                                       MIT_DES_DECRYPT))) {
301         com_err("des verify", retval, "can't decrypt");
302         exit(-1);
303     }
304     printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
305
306     if ( memcmp((char *)cipher_text, (char *)cipher3, in_length) ) {
307         printf("verify: error in CBC encryption\n");
308         exit(-1);
309     }
310     else 
311         printf("verify: CBC encryption is correct\n\n");
312
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");
318     input = clear_text2;
319     mit_des_cbc_cksum(input,cipher_text, strlen((char *)input),
320                       sched,ivec);
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]);
325     printf("\n\n");
326     if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
327         printf("verify: error in CBC cheksum\n");
328         exit(-1);
329     }
330     else 
331         printf("verify: CBC checksum is correct\n\n");
332
333     exit(0);
334 }
335
336 #if 0
337 void
338 flip(array)
339     char *array;
340 {
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++) {
344         old = *array;
345         new = 0;
346         for (j = 0; j<=7; j++) {
347             if (old & 01)
348                 new = new | 01;
349             if (j < 7) {
350                 old = old >> 1;
351                 new = new << 1;
352             }
353         }
354         *array = new;
355         array++;
356     }
357 }
358 #endif
359
360 static void
361 do_encrypt(in,out)
362     unsigned char *in;
363     unsigned char *out;
364 {
365     int i, j;
366     for (i =1; i<=nflag; i++) {
367         mit_des_cbc_encrypt((const mit_des_cblock *)in,
368                             (mit_des_cblock *)out,
369                             8,
370                             sched,
371                             zero_text,
372                             MIT_DES_ENCRYPT);
373         if (mit_des_debug) {
374             printf("\nclear %s\n",in);
375             for (j = 0; j<=7; j++)
376                 printf("%02X ",in[j] & 0xff);
377             printf("\tcipher ");
378             for (j = 0; j<=7; j++)
379                 printf("%02X ",out[j] & 0xff);
380         }
381     }
382 }
383
384 static void
385 do_decrypt(in,out)
386     unsigned char *out;
387     unsigned char *in;
388     /* try to invert it */
389 {
390     int i, j;
391     for (i =1; i<=nflag; i++) {
392         mit_des_cbc_encrypt((const mit_des_cblock *)out,
393                             (mit_des_cblock *)in,
394                             8,
395                             sched,
396                             zero_text,
397                             MIT_DES_DECRYPT);
398         if (mit_des_debug) {
399             printf("clear %s\n",in);
400             for (j = 0; j<=7; j++)
401                 printf("%02X ",in[j] & 0xff);
402             printf("\tcipher ");
403             for (j = 0; j<=7; j++)
404                 printf("%02X ",out[j] & 0xff);
405         }
406     }
407 }
408
409 /*
410  * Fake out the DES library, for the purposes of testing.
411  */
412
413 int
414 mit_des_is_weak_key(key)
415     mit_des_cblock key;
416 {
417     return 0;                           /* fake it out for testing */
418 }