Remove declaration of exit()
[krb5.git] / src / lib / des425 / verify.c
1 /*
2  * lib/des425/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.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * Program to test the correctness of the DES library
25  * implementation.
26  *
27  * exit returns  0 ==> success
28  *              -1 ==> error
29  */
30
31
32 #include <stdio.h>
33 #include <errno.h>
34 #include "./des.h"
35
36 extern char *errmsg();
37 extern int errno;
38 extern int des_string_to_key();
39 extern int des_key_sched();
40 extern int des_ecb_encrypt();
41 extern int des_cbc_encrypt();
42 char *progname;
43 int nflag = 2;
44 int vflag;
45 int mflag;
46 int zflag;
47 int pid;
48 int des_debug;
49 des_key_schedule KS;
50 unsigned char cipher_text[64];
51 unsigned char clear_text[64] = "Now is the time for all " ;
52 unsigned char clear_text2[64] = "7654321 Now is the time for ";
53 unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
54 unsigned char output[64];
55 unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
56 unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
57 unsigned char *input;
58
59 /* 0x0123456789abcdef */
60 unsigned char default_key[8] = {
61     0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
62 };
63 unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
64 unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
65 des_cblock s_key;
66 unsigned char default_ivec[8] = {
67     0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
68 };
69 unsigned char *ivec;
70 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
71 int i,j;
72
73 unsigned char cipher1[8] = {
74     0x25,0xdd,0xac,0x3e,0x96,0x17,0x64,0x67
75 };
76 unsigned char cipher2[8] = {
77     0x3f,0xa4,0x0e,0x8a,0x98,0x4d,0x48,0x15
78 };
79 unsigned char cipher3[64] = {
80     0xe5,0xc7,0xcd,0xde,0x87,0x2b,0xf2,0x7c,
81     0x43,0xe9,0x34,0x00,0x8c,0x38,0x9c,0x0f,
82     0x68,0x37,0x88,0x49,0x9a,0x7c,0x05,0xf6
83 };
84 unsigned char checksum[8] = {
85     0x58,0xd2,0xe7,0x7e,0x86,0x06,0x27,0x33
86 };
87
88 unsigned char zresult[8] = {
89     0x8c, 0xa6, 0x4d, 0xe9, 0xc1, 0xb1, 0x23, 0xa7
90 };
91
92 unsigned char mresult[8] = {
93     0xa3, 0x80, 0xe0, 0x2a, 0x6b, 0xe5, 0x46, 0x96
94 };
95
96     
97 /*
98  * Can also add :
99  * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
100  */
101
102 main(argc,argv)
103     int argc;
104     char *argv[];
105 {
106     /* Local Declarations */
107     long in_length;
108
109     progname=argv[0];           /* salt away invoking program */
110
111     while (--argc > 0 && (*++argv)[0] == '-')
112         for (i=1; argv[0][i] != '\0'; i++) {
113             switch (argv[0][i]) {
114
115                 /* debug flag */
116             case 'd':
117                 des_debug=3;
118                 continue;
119
120             case 'z':
121                 zflag = 1;
122                 continue;
123
124             case 'm':
125                 mflag = 1;
126                 continue;
127
128             default:
129                 printf("%s: illegal flag \"%c\" ",
130                        progname,argv[0][i]);
131                 exit(1);
132             }
133         };
134
135     if (argc) {
136         fprintf(stderr, "Usage: %s [-dmz]\n", progname);
137         exit(1);
138     }
139
140     /* use known input and key */
141
142     /* ECB zero text zero key */
143     if (zflag) {
144         input = zero_text;
145         des_key_sched(zero_key,KS);
146         printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
147         do_encrypt(input,cipher_text);
148         printf("\tcipher  = (low to high bytes)\n\t\t");
149         for (j = 0; j<=7; j++)
150             printf("%02x ",cipher_text[j]);
151         printf("\n");
152         do_decrypt(output,cipher_text);
153         if ( memcmp((char *)cipher_text, (char *)zresult, 8) ) {
154             printf("verify: error in zero key test\n");
155             exit(-1);
156         }
157         exit(0);
158     }
159
160     if (mflag) {
161         input = msb_text;
162         des_key_sched(key3,KS);
163         printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
164         printf("key = 0, cipher = 0x??\n");
165         do_encrypt(input,cipher_text);
166         printf("\tcipher  = (low to high bytes)\n\t\t");
167         for (j = 0; j<=7; j++) {
168             printf("%02x ",cipher_text[j]);
169         }
170         printf("\n");
171         do_decrypt(output,cipher_text);
172         if ( memcmp((char *)cipher_text, (char *)mresult, 8) ) {
173             printf("verify: error in msb test\n");
174             exit(-1);
175         }
176         exit(0);
177     }
178
179     /* ECB mode Davies and Price */
180     {
181         input = zero_text;
182         des_key_sched(key2,KS);
183         printf("Examples per FIPS publication 81, keys ivs and cipher\n");
184         printf("in hex.  These are the correct answers, see below for\n");
185         printf("the actual answers.\n\n");
186         printf("Examples per Davies and Price.\n\n");
187         printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
188         printf("\tclear = 0\n");
189         printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
190         printf("ACTUAL ECB\n");
191         printf("\tclear \"%s\"\n", input);
192         do_encrypt(input,cipher_text);
193         printf("\tcipher  = (low to high bytes)\n\t\t");
194         for (j = 0; j<=7; j++)
195             printf("%02x ",cipher_text[j]);
196         printf("\n\n");
197         do_decrypt(output,cipher_text);
198         if ( memcmp((char *)cipher_text, (char *)cipher1, 8) ) {
199             printf("verify: error in ECB encryption\n");
200             exit(-1);
201         }
202         else 
203             printf("verify: ECB encription is correct\n\n");
204     }
205
206     /* ECB mode */
207     {
208         des_key_sched(default_key,KS);
209         input = clear_text;
210         ivec = default_ivec;
211         printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
212         printf("\tclear = \"Now is the time for all \"\n");
213         printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
214         printf("ACTUAL ECB\n\tclear \"%s\"",input);
215         do_encrypt(input,cipher_text);
216         printf("\n\tcipher      = (low to high bytes)\n\t\t");
217         for (j = 0; j<=7; j++) {
218             printf("%02x ",cipher_text[j]);
219         }
220         printf("\n\n");
221         do_decrypt(output,cipher_text);
222         if ( memcmp((char *)cipher_text, (char *)cipher2, 8) ) {
223             printf("verify: error in ECB encryption\n");
224             exit(-1);
225         }
226         else 
227             printf("verify: ECB encription is correct\n\n");
228     }
229
230     /* CBC mode */
231     printf("EXAMPLE CBC\tkey = 0123456789abcdef");
232     printf("\tiv = 1234567890abcdef\n");
233     printf("\tclear = \"Now is the time for all \"\n");
234     printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
235     printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
236     printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
237
238     printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
239     in_length = strlen(input);
240     des_cbc_encrypt(input,cipher_text,(long) in_length,KS,ivec,1);
241     printf("\tciphertext = (low to high bytes)\n");
242     for (i = 0; i <= 7; i++) {
243         printf("\t\t");
244         for (j = 0; j <= 7; j++) {
245             printf("%02x ",cipher_text[i*8+j]);
246         }
247         printf("\n");
248     }
249     des_cbc_encrypt(cipher_text,clear_text,(long) in_length,KS,ivec,0);
250     printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
251
252     if ( memcmp((char *)cipher_text, (char *)cipher3, in_length) ) {
253         printf("verify: error in CBC encryption\n");
254         exit(-1);
255     }
256     else 
257         printf("verify: CBC encription is correct\n\n");
258
259     printf("EXAMPLE CBC checksum");
260     printf("\tkey =  0123456789abcdef\tiv =  1234567890abcdef\n");
261     printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
262     printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
263     printf("or some part thereof\n");
264     input = clear_text2;
265     des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
266     printf("ACTUAL CBC checksum\n");
267     printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
268     for (j = 0; j<=7; j++)
269         printf("%02x ",cipher_text[j]);
270     printf("\n\n");
271     if ( memcmp((char *)cipher_text, (char *)checksum, 8) ) {
272         printf("verify: error in CBC cheksum\n");
273         exit(-1);
274     }
275     else 
276         printf("verify: CBC checksum is correct\n\n");
277     exit(0);
278 }
279
280 flip(array)
281     char *array;
282 {
283     register old,new,i,j;
284     /* flips the bit order within each byte from 0 lsb to 0 msb */
285     for (i = 0; i<=7; i++) {
286         old = *array;
287         new = 0;
288         for (j = 0; j<=7; j++) {
289             if (old & 01)
290                 new = new | 01;
291             if (j < 7) {
292                 old = old >> 1;
293                 new = new << 1;
294             }
295         }
296         *array = new;
297         array++;
298     }
299 }
300
301 do_encrypt(in,out)
302     char *in;
303     char *out;
304 {
305     for (i =1; i<=nflag; i++) {
306         des_ecb_encrypt(in,out,KS,1);
307         if (des_debug) {
308             printf("\nclear %s\n",in);
309             for (j = 0; j<=7; j++)
310                 printf("%02 X ",in[j] & 0xff);
311             printf("\tcipher ");
312             for (j = 0; j<=7; j++)
313                 printf("%02X ",out[j] & 0xff);
314         }
315     }
316 }
317
318 do_decrypt(in,out)
319     char *out;
320     char *in;
321     /* try to invert it */
322 {
323     for (i =1; i<=nflag; i++) {
324         des_ecb_encrypt(out,in,KS,0);
325         if (des_debug) {
326             printf("clear %s\n",in);
327             for (j = 0; j<=7; j++)
328                 printf("%02X ",in[j] & 0xff);
329             printf("\tcipher ");
330             for (j = 0; j<=7; j++)
331                 printf("%02X ",out[j] & 0xff);
332         }
333     }
334 }