2 * Copyright 1988 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
7 * exit returns 0 ==> success
11 #include <mit-cpyright.h>
15 #include <afs/param.h>
17 #define MIN_ARGC 0 /* min # args, not incl flags */
18 #define MAX_ARGC 2 /* max # args, not incl flags */
20 /* MIN_ARGC == MAX_ARGC ==> required # args */
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();
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};
43 /* 0x0123456789abcdef */
44 des_cblock default_key = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
46 des_cblock default_ivec = { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
48 des_cblock zero_key = {1};
55 /* Local Declarations */
59 progname=argv[0]; /* salt away invoking program */
61 /* Assume a long is four bytes */
62 if (sizeof(long) != 4) {
63 fprintf(stdout,"\nERROR, size of long is %d",sizeof(long));
67 while (--argc > 0 && (*++argv)[0] == '-')
68 for (i=1; argv[0][i] != '\0'; i++) {
86 /* string to key only flag */
91 /* test flag - use known key and cleartext */
98 sscanf(&argv[0][i+1],"%d",&nflag);
99 argv[0][i+1] = '\0'; /* force it to stop */
103 printf("%s: illegal flag \"%c\" ",
104 progname,argv[0][i]);
109 if (argc < MIN_ARGC || argc >MAX_ARGC) {
110 printf("Usage: xxx [-xxx] xxx xxx\n");
114 /* argv[0] now points to first non-option arg, if any */
117 /* use known input and key */
118 des_key_sched(default_key,KS);
120 ivec = (unsigned char *) default_ivec;
123 /*des_string_to_key(argv[0],s_key); */
124 des_string_to_key("test",s_key);
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++);
130 des_string_to_key("test",s_key);
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++);
136 des_key_sched(s_key,KS);
137 input = (unsigned char *) argv[1];
138 ivec = (unsigned char *) argv[2];
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);
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]);
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);
157 fprintf(stdout,"\nclear %s\n",input);
159 des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
161 "\n\nencrypted cksum = (low to high bytes)\n");
162 for (j = 0; j <= 7; j++)
163 fprintf(stdout,"%02x ",cipher_text[j]);
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);
170 "\n\nrandom key = (low to high bytes)\n");
171 for (j = 0; j<=7; j++)
172 fprintf(stdout,"%02x ",cipher_text[j]);
177 fprintf(stdout,"\nclear %s\n",input);
178 do_encrypt(input,cipher_text);
179 do_decrypt(clear_text,cipher_text);
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++) {
191 for (j = 0; j <= 7; j++) {
192 if (old & 01) new = new | 01;
207 for (i = 1; i <= nflag; i++) {
208 des_ecb_encrypt(in,out,KS,1);
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);
223 /* try to invert it */
225 for (i = 1; i <= nflag; i++) {
226 des_ecb_encrypt(out,in,KS,0);
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);