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