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