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