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