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