2 * Copyright 1988 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
7 * exit returns 0 ==> success
12 static char rcsid_testit_c[] =
15 #include <mit-cpyright.h>
19 #include <afs/param.h>
21 #define MIN_ARGC 0 /* min # args, not incl flags */
22 #define MAX_ARGC 2 /* max # args, not incl flags */
24 /* MIN_ARGC == MAX_ARGC ==> required # args */
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();
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};
47 /* 0x0123456789abcdef */
48 des_cblock default_key = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
50 des_cblock default_ivec = { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
52 des_cblock zero_key = {1};
59 /* Local Declarations */
63 progname=argv[0]; /* salt away invoking program */
65 /* Assume a long is four bytes */
66 if (sizeof(long) != 4) {
67 fprintf(stdout,"\nERROR, size of long is %d",sizeof(long));
71 while (--argc > 0 && (*++argv)[0] == '-')
72 for (i=1; argv[0][i] != '\0'; i++) {
90 /* string to key only flag */
95 /* test flag - use known key and cleartext */
100 /* iteration count */
102 sscanf(&argv[0][i+1],"%d",&nflag);
103 argv[0][i+1] = '\0'; /* force it to stop */
107 printf("%s: illegal flag \"%c\" ",
108 progname,argv[0][i]);
113 if (argc < MIN_ARGC || argc >MAX_ARGC) {
114 printf("Usage: xxx [-xxx] xxx xxx\n");
118 /* argv[0] now points to first non-option arg, if any */
121 /* use known input and key */
122 des_key_sched(default_key,KS);
124 ivec = (unsigned char *) default_ivec;
127 /*des_string_to_key(argv[0],s_key); */
128 des_string_to_key("test",s_key);
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++);
134 des_string_to_key("test",s_key);
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++);
140 des_key_sched(s_key,KS);
141 input = (unsigned char *) argv[1];
142 ivec = (unsigned char *) argv[2];
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);
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]);
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);
161 fprintf(stdout,"\nclear %s\n",input);
163 des_cbc_cksum(input,cipher_text,(long) strlen(input),KS,ivec,1);
165 "\n\nencrypted cksum = (low to high bytes)\n");
166 for (j = 0; j <= 7; j++)
167 fprintf(stdout,"%02x ",cipher_text[j]);
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);
174 "\n\nrandom key = (low to high bytes)\n");
175 for (j = 0; j<=7; j++)
176 fprintf(stdout,"%02x ",cipher_text[j]);
181 fprintf(stdout,"\nclear %s\n",input);
182 do_encrypt(input,cipher_text);
183 do_decrypt(clear_text,cipher_text);
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++) {
195 for (j = 0; j <= 7; j++) {
196 if (old & 01) new = new | 01;
211 for (i = 1; i <= nflag; i++) {
212 des_ecb_encrypt(in,out,KS,1);
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);
227 /* try to invert it */
229 for (i = 1; i <= nflag; i++) {
230 des_ecb_encrypt(out,in,KS,0);
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);