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