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 <afsconfig.h>
16 #include <afs/param.h>
22 #define MIN_ARGC 0 /* min # args, not incl flags */
23 #define MAX_ARGC 2 /* max # args, not incl flags */
25 /* MIN_ARGC == MAX_ARGC ==> required # args */
27 extern char *errmsg();
28 extern int des_string_to_key();
29 extern int des_key_sched();
30 extern int des_ecb_encrypt();
31 extern int des_cbc_encrypt();
32 extern int des_pcbc_encrypt();
42 unsigned char cipher_text[64];
43 unsigned char clear_text[64] = "Now is the time for all ";
44 unsigned char clear_text2[64] = "7654321 Now is the time for ";
45 unsigned char clear_text3[64] = { 2, 0, 0, 0, 1, 0, 0, 0 };
48 /* 0x0123456789abcdef */
49 des_cblock default_key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
51 des_cblock default_ivec = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
53 des_cblock zero_key = { 1 };
60 /* Local Declarations */
64 progname = argv[0]; /* salt away invoking program */
66 while (--argc > 0 && (*++argv)[0] == '-')
67 for (i = 1; argv[0][i] != '\0'; i++) {
85 /* string to key only flag */
90 /* test flag - use known key and cleartext */
97 sscanf(&argv[0][i + 1], "%d", &nflag);
98 argv[0][i + 1] = '\0'; /* force it to stop */
102 printf("%s: illegal flag \"%c\" ", progname, argv[0][i]);
107 if (argc < MIN_ARGC || argc > MAX_ARGC) {
108 printf("Usage: xxx [-xxx] xxx xxx\n");
112 /* argv[0] now points to first non-option arg, if any */
115 /* use known input and key */
116 des_key_sched(default_key, KS);
118 ivec = (unsigned char *)default_ivec;
120 /*des_string_to_key(argv[0],s_key); */
121 des_string_to_key("test", s_key);
123 input = (unsigned char *)s_key;
124 fprintf(stdout, "\nstring = %s, key = ", argv[0]);
125 for (i = 0; i <= 7; i++)
126 fprintf(stdout, "%02x ", *input++);
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++)
133 fprintf(stdout, "%02x ", *input++);
135 des_key_sched(s_key, KS);
136 input = (unsigned char *)argv[1];
137 ivec = (unsigned char *)argv[2];
142 fprintf(stdout, "\nclear %s\n", input);
143 in_length = strlen(input);
144 des_cbc_encrypt(input, cipher_text, (long)in_length, KS, ivec, 1);
145 fprintf(stdout, "\n\nencrypted ciphertext = (low to high bytes)");
146 for (i = 0; i <= 7; i++) {
147 fprintf(stdout, "\n");
148 for (j = 0; j <= 7; j++)
149 fprintf(stdout, "%02x ", cipher_text[i * 8 + j]);
151 des_cbc_encrypt(cipher_text, clear_text, (long)in_length, KS, ivec,
153 fprintf(stdout, "\n\ndecrypted clear_text = %s", clear_text);
155 fprintf(stdout, "\nclear %s\n", input);
157 des_cbc_cksum(input, cipher_text, (long)strlen(input), KS, ivec, 1);
158 fprintf(stdout, "\n\nencrypted cksum = (low to high bytes)\n");
159 for (j = 0; j <= 7; j++)
160 fprintf(stdout, "%02x ", cipher_text[j]);
162 /* test out random number generator */
163 for (i = 0; i <= 7; i++) {
164 des_random_key(cipher_text);
165 des_key_sched(cipher_text, KS);
166 fprintf(stdout, "\n\nrandom key = (low to high bytes)\n");
167 for (j = 0; j <= 7; j++)
168 fprintf(stdout, "%02x ", cipher_text[j]);
172 fprintf(stdout, "\nclear %s\n", input);
173 do_encrypt(input, cipher_text);
174 do_decrypt(clear_text, cipher_text);
181 register old, new, i, j;
182 /* flips the bit order within each byte from 0 lsb to 0 msb */
183 for (i = 0; i <= 7; i++) {
186 for (j = 0; j <= 7; j++) {
203 for (i = 1; i <= nflag; i++) {
204 des_ecb_encrypt(in, out, KS, 1);
206 fprintf(stdout, "\nclear %s\n", in);
207 for (j = 0; j <= 7; j++)
208 fprintf(stdout, "%02 X ", in[j] & 0xff);
209 fprintf(stdout, "\tcipher ");
210 for (j = 0; j <= 7; j++)
211 fprintf(stdout, "%02X ", out[j] & 0xff);
219 /* try to invert it */
221 for (i = 1; i <= nflag; i++) {
222 des_ecb_encrypt(out, in, KS, 0);
224 fprintf(stdout, "\nclear %s\n", in);
225 for (j = 0; j <= 7; j++)
226 fprintf(stdout, "%02X ", in[j] & 0xff);
227 fprintf(stdout, "\tcipher ");
228 for (j = 0; j <= 7; j++)
229 fprintf(stdout, "%02X ", out[j] & 0xff);