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>
20 #define MIN_ARGC 0 /* min # args, not incl flags */
21 #define MAX_ARGC 2 /* max # args, not incl flags */
23 /* MIN_ARGC == MAX_ARGC ==> required # args */
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();
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 };
46 /* 0x0123456789abcdef */
47 des_cblock default_key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
49 des_cblock default_ivec = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
51 des_cblock zero_key = { 1 };
58 /* Local Declarations */
62 progname = argv[0]; /* salt away invoking program */
64 while (--argc > 0 && (*++argv)[0] == '-')
65 for (i = 1; argv[0][i] != '\0'; i++) {
83 /* string to key only flag */
88 /* test flag - use known key and cleartext */
95 sscanf(&argv[0][i + 1], "%d", &nflag);
96 argv[0][i + 1] = '\0'; /* force it to stop */
100 printf("%s: illegal flag \"%c\" ", progname, argv[0][i]);
105 if (argc < MIN_ARGC || argc > MAX_ARGC) {
106 printf("Usage: xxx [-xxx] xxx xxx\n");
110 /* argv[0] now points to first non-option arg, if any */
113 /* use known input and key */
114 des_key_sched(default_key, KS);
116 ivec = (unsigned char *)default_ivec;
118 /*des_string_to_key(argv[0],s_key); */
119 des_string_to_key("test", s_key);
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++);
126 des_string_to_key("test", s_key);
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++);
133 des_key_sched(s_key, KS);
134 input = (unsigned char *)argv[1];
135 ivec = (unsigned char *)argv[2];
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]);
149 des_cbc_encrypt(cipher_text, clear_text, (long)in_length, KS, ivec,
151 fprintf(stdout, "\n\ndecrypted clear_text = %s", clear_text);
153 fprintf(stdout, "\nclear %s\n", input);
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]);
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]);
170 fprintf(stdout, "\nclear %s\n", input);
171 do_encrypt(input, cipher_text);
172 do_decrypt(clear_text, cipher_text);
180 /* flips the bit order within each byte from 0 lsb to 0 msb */
181 for (i = 0; i <= 7; i++) {
184 for (j = 0; j <= 7; j++) {
201 for (i = 1; i <= nflag; i++) {
202 des_ecb_encrypt(in, out, KS, 1);
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);
217 /* try to invert it */
219 for (i = 1; i <= nflag; i++) {
220 des_ecb_encrypt(out, in, KS, 0);
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);