c6723f38cf5b1db6fb259098f27ad61b8ee78f6d
[openafs.git] / src / des / test / verify.c
1 /*
2  * Copyright 1988 by the Massachusetts Institute of Technology.
3  *
4  * For copying and distribution information, please see the file
5  * <mit-cpyright.h>.
6  *
7  * Program to test the correctness of the DES library
8  * implementation.
9  *
10  * exit returns  0 ==> success
11  *              -1 ==> error
12  */
13
14 #include <mit-cpyright.h>
15 #include <stdio.h>
16 #include <errno.h>
17 #include <des.h>
18 #include <afs/param.h>
19 #include <afsconfig.h>
20
21 RCSID("$Header$");
22
23
24 extern char *errmsg();
25 extern int des_string_to_key();
26 extern int des_key_sched();
27 extern int des_ecb_encrypt();
28 extern int des_cbc_encrypt();
29
30 char *progname;
31 int nflag = 2;
32 int vflag;
33 int mflag;
34 int zflag;
35 int pid;
36 int des_debug;
37 des_key_schedule KS;
38 unsigned char cipher_text[64];
39 unsigned char clear_text[64] = "Now is the time for all " ;
40 unsigned char clear_text2[64] = "7654321 Now is the time for ";
41 unsigned char clear_text3[64] = {2,0,0,0, 1,0,0,0};
42 unsigned char output[64];
43 unsigned char zero_text[8] = {0x0,0,0,0,0,0,0,0};
44 unsigned char msb_text[8] = {0x0,0,0,0, 0,0,0,0x40}; /* to ANSI MSB */
45 unsigned char *input;
46
47 /* 0x0123456789abcdef */
48 unsigned char default_key[8] = {
49     0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
50 };
51 unsigned char key2[8] = { 0x08,0x19,0x2a,0x3b,0x4c,0x5d,0x6e,0x7f };
52 unsigned char key3[8] = { 0x80,1,1,1,1,1,1,1 };
53 des_cblock s_key;
54 unsigned char default_ivec[8] = {
55     0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef
56 };
57 unsigned char *ivec;
58 unsigned char zero_key[8] = {1,1,1,1,1,1,1,1}; /* just parity bits */
59 int i,j;
60
61 /*
62  * Can also add :
63  * plaintext = 0, key = 0, cipher = 0x8ca64de9c1b123a7 (or is it a 1?)
64  */
65
66 main(argc,argv)
67     int argc;
68     char *argv[];
69 {
70     /* Local Declarations */
71     long in_length;
72
73     progname=argv[0];           /* salt away invoking program */
74
75     /* Assume a long is four bytes */
76     if (sizeof(long) != 4) {
77         printf("\nERROR,  size of long is %d",sizeof(long));
78         exit(-1);
79     }
80
81     while (--argc > 0 && (*++argv)[0] == '-')
82         for (i=1; argv[0][i] != '\0'; i++) {
83             switch (argv[0][i]) {
84
85                 /* debug flag */
86             case 'd':
87                 des_debug=3;
88                 continue;
89
90             case 'z':
91                 zflag = 1;
92                 continue;
93
94             case 'm':
95                 mflag = 1;
96                 continue;
97
98             default:
99                 printf("%s: illegal flag \"%c\" ",
100                        progname,argv[0][i]);
101                 exit(1);
102             }
103         };
104
105     if (argc) {
106         fprintf(stderr, "Usage: %s [-dmz]\n", progname);
107         exit(1);
108     }
109
110     /* use known input and key */
111
112     /* ECB zero text zero key */
113     if (zflag) {
114         input = zero_text;
115         des_key_sched(zero_key,KS);
116         printf("plaintext = key = 0, cipher = 0x8ca64de9c1b123a7\n");
117         do_encrypt(input,cipher_text);
118         printf("\tcipher  = (low to high bytes)\n\t\t");
119         for (j = 0; j<=7; j++)
120             printf("%02x ",cipher_text[j]);
121         printf("\n");
122         do_decrypt(output,cipher_text);
123         return;
124     }
125
126     if (mflag) {
127         input = msb_text;
128         des_key_sched(key3,KS);
129         printf("plaintext = 0x00 00 00 00 00 00 00 40, ");
130         printf("key = 0, cipher = 0x??\n");
131         do_encrypt(input,cipher_text);
132         printf("\tcipher  = (low to high bytes)\n\t\t");
133         for (j = 0; j<=7; j++) {
134             printf("%02x ",cipher_text[j]);
135         }
136         printf("\n");
137         do_decrypt(output,cipher_text);
138         return;
139     }
140
141     /* ECB mode Davies and Price */
142     {
143         input = zero_text;
144         des_key_sched(key2,KS);
145         printf("Examples per FIPS publication 81, keys ivs and cipher\n");
146         printf("in hex.  These are the correct answers, see below for\n");
147         printf("the actual answers.\n\n");
148         printf("Examples per Davies and Price.\n\n");
149         printf("EXAMPLE ECB\tkey = 08192a3b4c5d6e7f\n");
150         printf("\tclear = 0\n");
151         printf("\tcipher = 25 dd ac 3e 96 17 64 67\n");
152         printf("ACTUAL ECB\n");
153         printf("\tclear \"%s\"\n", input);
154         do_encrypt(input,cipher_text);
155         printf("\tcipher  = (low to high bytes)\n\t\t");
156         for (j = 0; j<=7; j++)
157             printf("%02x ",cipher_text[j]);
158         printf("\n\n");
159         do_decrypt(output,cipher_text);
160     }
161
162     /* ECB mode */
163     {
164         des_key_sched(default_key,KS);
165         input = clear_text;
166         ivec = default_ivec;
167         printf("EXAMPLE ECB\tkey = 0123456789abcdef\n");
168         printf("\tclear = \"Now is the time for all \"\n");
169         printf("\tcipher = 3f a4 0e 8a 98 4d 48 15 ...\n");
170         printf("ACTUAL ECB\n\tclear \"%s\"",input);
171         do_encrypt(input,cipher_text);
172         printf("\n\tcipher      = (low to high bytes)\n\t\t");
173         for (j = 0; j<=7; j++) {
174             printf("%02x ",cipher_text[j]);
175         }
176         printf("\n\n");
177         do_decrypt(output,cipher_text);
178     }
179
180     /* CBC mode */
181     printf("EXAMPLE CBC\tkey = 0123456789abcdef");
182     printf("\tiv = 1234567890abcdef\n");
183     printf("\tclear = \"Now is the time for all \"\n");
184     printf("\tcipher =\te5 c7 cd de 87 2b f2 7c\n");
185     printf("\t\t\t43 e9 34 00 8c 38 9c 0f\n");
186     printf("\t\t\t68 37 88 49 9a 7c 05 f6\n");
187
188     printf("ACTUAL CBC\n\tclear \"%s\"\n",input);
189     in_length = strlen(input);
190     des_cbc_encrypt(input,cipher_text,(long) in_length,KS,ivec,1);
191     printf("\tciphertext = (low to high bytes)\n");
192     for (i = 0; i <= 7; i++) {
193         printf("\t\t");
194         for (j = 0; j <= 7; j++) {
195             printf("%02x ",cipher_text[i*8+j]);
196         }
197         printf("\n");
198     }
199     des_cbc_encrypt(cipher_text,clear_text,(long) in_length,KS,ivec,0);
200     printf("\tdecrypted clear_text = \"%s\"\n",clear_text);
201
202     printf("EXAMPLE CBC checksum");
203     printf("\tkey =  0123456789abcdef\tiv =  1234567890abcdef\n");
204     printf("\tclear =\t\t\"7654321 Now is the time for \"\n");
205     printf("\tchecksum\t58 d2 e7 7e 86 06 27 33, ");
206     printf("or some part thereof\n");
207     input = clear_text2;
208     des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
209     printf("ACTUAL CBC checksum\n");
210     printf("\t\tencrypted cksum = (low to high bytes)\n\t\t");
211     for (j = 0; j<=7; j++)
212         printf("%02x ",cipher_text[j]);
213     printf("\n\n");
214 }
215
216 flip(array)
217     char *array;
218 {
219     register old,new,i,j;
220     /* flips the bit order within each byte from 0 lsb to 0 msb */
221     for (i = 0; i<=7; i++) {
222         old = *array;
223         new = 0;
224         for (j = 0; j<=7; j++) {
225             if (old & 01)
226                 new = new | 01;
227             if (j < 7) {
228                 old = old >> 1;
229                 new = new << 1;
230             }
231         }
232         *array = new;
233         array++;
234     }
235 }
236
237 do_encrypt(in,out)
238     char *in;
239     char *out;
240 {
241     for (i =1; i<=nflag; i++) {
242         des_ecb_encrypt(in,out,KS,1);
243         if (des_debug) {
244             printf("\nclear %s\n",in);
245             for (j = 0; j<=7; j++)
246                 printf("%02 X ",in[j] & 0xff);
247             printf("\tcipher ");
248             for (j = 0; j<=7; j++)
249                 printf("%02X ",out[j] & 0xff);
250         }
251     }
252 }
253
254 do_decrypt(in,out)
255     char *out;
256     char *in;
257     /* try to invert it */
258 {
259     for (i =1; i<=nflag; i++) {
260         des_ecb_encrypt(out,in,KS,0);
261         if (des_debug) {
262             printf("clear %s\n",in);
263             for (j = 0; j<=7; j++)
264                 printf("%02X ",in[j] & 0xff);
265             printf("\tcipher ");
266             for (j = 0; j<=7; j++)
267                 printf("%02X ",out[j] & 0xff);
268         }
269     }
270 }