ticket-2618-patches-20031207
[openafs.git] / src / des / test / testit.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  * exit returns  0 ==> success
8  *              -1 ==> error
9  */
10
11 #include <mit-cpyright.h>
12 #include <stdio.h>
13 #include <errno.h>
14 #include <des.h>
15 #include <afsconfig.h>
16 #include <afs/param.h>
17
18 RCSID
19     ("$Header$");
20
21
22 #define MIN_ARGC        0       /* min # args, not incl flags */
23 #define MAX_ARGC        2       /* max # args, not incl flags */
24
25 /* MIN_ARGC == MAX_ARGC ==> required # args */
26
27 extern char *errmsg();
28 extern void des_string_to_key();
29 extern int des_key_sched();
30 extern int des_ecb_encrypt();
31 extern int des_cbc_encrypt();
32 extern int des_pcbc_encrypt();
33
34 char *progname;
35 int sflag;
36 int vflag;
37 int tflag;
38 int nflag = 1000;
39 int cflag;
40 int des_debug;
41 des_key_schedule KS;
42 unsigned char cipher_text[64];
43 unsigned char clear_text[64] = "Now is the time for all ";
44 unsigned char clear_text2[64] = "7654321 Now is the time for ";
45 unsigned char clear_text3[64] = { 2, 0, 0, 0, 1, 0, 0, 0 };
46 unsigned char *input;
47
48 /* 0x0123456789abcdef */
49 des_cblock default_key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
50 des_cblock s_key;
51 des_cblock default_ivec = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
52 unsigned char *ivec;
53 des_cblock zero_key = { 1 };
54 int i, j;
55
56 main(argc, argv)
57      int argc;
58      char *argv[];
59 {
60     /*  Local Declarations */
61
62     long in_length;
63
64     progname = argv[0];         /* salt away invoking program */
65
66     while (--argc > 0 && (*++argv)[0] == '-')
67         for (i = 1; argv[0][i] != '\0'; i++) {
68             switch (argv[0][i]) {
69
70                 /* debug flag */
71             case 'd':
72                 des_debug = 1;
73                 continue;
74
75                 /* verbose flag */
76             case 'v':
77                 vflag = 1;
78                 continue;
79
80                 /* cbc flag */
81             case 'c':
82                 cflag = 1;
83                 continue;
84
85                 /* string to key only flag */
86             case 's':
87                 sflag = 1;
88                 continue;
89
90                 /* test flag - use known key and cleartext */
91             case 't':
92                 tflag = 1;
93                 continue;
94
95                 /* iteration count */
96             case 'n':
97                 sscanf(&argv[0][i + 1], "%d", &nflag);
98                 argv[0][i + 1] = '\0';  /* force it to stop */
99                 break;
100
101             default:
102                 printf("%s: illegal flag \"%c\" ", progname, argv[0][i]);
103                 exit(1);
104             }
105         };
106
107     if (argc < MIN_ARGC || argc > MAX_ARGC) {
108         printf("Usage: xxx [-xxx]  xxx xxx\n");
109         exit(1);
110     }
111
112     /* argv[0] now points to first non-option arg, if any */
113
114     if (tflag) {
115         /* use known input and key */
116         des_key_sched(default_key, KS);
117         input = clear_text;
118         ivec = (unsigned char *)default_ivec;
119     } else {
120         /*des_string_to_key(argv[0],s_key); */
121         des_string_to_key("test", s_key);
122         if (vflag) {
123             input = (unsigned char *)s_key;
124             fprintf(stdout, "\nstring = %s, key = ", argv[0]);
125             for (i = 0; i <= 7; i++)
126                 fprintf(stdout, "%02x ", *input++);
127         }
128         des_string_to_key("test", s_key);
129         if (vflag) {
130             input = (unsigned char *)s_key;
131             fprintf(stdout, "\nstring = %s, key = ", argv[0]);
132             for (i = 0; i <= 7; i++)
133                 fprintf(stdout, "%02x ", *input++);
134         }
135         des_key_sched(s_key, KS);
136         input = (unsigned char *)argv[1];
137         ivec = (unsigned char *)argv[2];
138     }
139
140
141     if (cflag) {
142         fprintf(stdout, "\nclear %s\n", input);
143         in_length = strlen(input);
144         des_cbc_encrypt(input, cipher_text, (long)in_length, KS, ivec, 1);
145         fprintf(stdout, "\n\nencrypted ciphertext = (low to high bytes)");
146         for (i = 0; i <= 7; i++) {
147             fprintf(stdout, "\n");
148             for (j = 0; j <= 7; j++)
149                 fprintf(stdout, "%02x ", cipher_text[i * 8 + j]);
150         }
151         des_cbc_encrypt(cipher_text, clear_text, (long)in_length, KS, ivec,
152                         0);
153         fprintf(stdout, "\n\ndecrypted clear_text = %s", clear_text);
154
155         fprintf(stdout, "\nclear %s\n", input);
156         input = clear_text2;
157         des_cbc_cksum(input, cipher_text, (long)strlen(input), KS, ivec, 1);
158         fprintf(stdout, "\n\nencrypted cksum = (low to high bytes)\n");
159         for (j = 0; j <= 7; j++)
160             fprintf(stdout, "%02x ", cipher_text[j]);
161
162         /* test out random number generator */
163         for (i = 0; i <= 7; i++) {
164             des_random_key(cipher_text);
165             des_key_sched(cipher_text, KS);
166             fprintf(stdout, "\n\nrandom key = (low to high bytes)\n");
167             for (j = 0; j <= 7; j++)
168                 fprintf(stdout, "%02x ", cipher_text[j]);
169         }
170     } else {
171         if (vflag)
172             fprintf(stdout, "\nclear %s\n", input);
173         do_encrypt(input, cipher_text);
174         do_decrypt(clear_text, cipher_text);
175     }
176 }
177
178 flip(array)
179      char *array;
180 {
181     register old, new, i, j;
182     /* flips the bit order within each byte from 0 lsb to 0 msb */
183     for (i = 0; i <= 7; i++) {
184         old = *array;
185         new = 0;
186         for (j = 0; j <= 7; j++) {
187             if (old & 01)
188                 new = new | 01;
189             if (j < 7) {
190                 old = old >> 1;
191                 new = new << 1;
192             }
193         }
194         *array = new;
195         array++;
196     }
197 }
198
199 do_encrypt(in, out)
200      char *in;
201      char *out;
202 {
203     for (i = 1; i <= nflag; i++) {
204         des_ecb_encrypt(in, out, KS, 1);
205         if (vflag) {
206             fprintf(stdout, "\nclear %s\n", in);
207             for (j = 0; j <= 7; j++)
208                 fprintf(stdout, "%02 X ", in[j] & 0xff);
209             fprintf(stdout, "\tcipher ");
210             for (j = 0; j <= 7; j++)
211                 fprintf(stdout, "%02X ", out[j] & 0xff);
212         }
213     }
214 }
215
216 do_decrypt(in, out)
217      char *out;
218      char *in;
219     /* try to invert it */
220 {
221     for (i = 1; i <= nflag; i++) {
222         des_ecb_encrypt(out, in, KS, 0);
223         if (vflag) {
224             fprintf(stdout, "\nclear %s\n", in);
225             for (j = 0; j <= 7; j++)
226                 fprintf(stdout, "%02X ", in[j] & 0xff);
227             fprintf(stdout, "\tcipher ");
228             for (j = 0; j <= 7; j++)
229                 fprintf(stdout, "%02X ", out[j] & 0xff);
230         }
231     }
232 }