2 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
5 * For copying and distribution information, please see the file
8 * These routines perform encryption and decryption using the DES
9 * private key algorithm, or else a subset of it -- fewer inner loops.
10 * (AUTH_DES_ITER defaults to 16, may be less.)
12 * Under U.S. law, this software may not be exported outside the US
13 * without license from the U.S. Commerce department.
15 * These routines form the library interface to the DES facilities.
17 * Originally written 8/85 by Steve Miller, MIT Project Athena.
20 #include <mit-cpyright.h>
23 #include <afsconfig.h>
24 #include <afs/param.h>
29 #define XPRT_CBC_ENCRYPT
32 extern int des_debug_print();
33 extern int des_ecb_encrypt();
36 * This routine performs DES cipher-block-chaining operation, either
37 * encrypting from cleartext to ciphertext, if encrypt != 0 or
38 * decrypting from ciphertext to cleartext, if encrypt == 0.
40 * The key schedule is passed as an arg, as well as the cleartext or
41 * ciphertext. The cleartext and ciphertext should be in host order.
43 * NOTE-- the output is ALWAYS an multiple of 8 bytes long. If not
44 * enough space was provided, your program will get trashed.
46 * For encryption, the cleartext string is null padded, at the end, to
47 * an integral multiple of eight bytes.
49 * For decryption, the ciphertext will be used in integral multiples
50 * of 8 bytes, but only the first "length" bytes returned into the
55 des_cbc_encrypt(in,out,length,key,iv,encrypt)
56 des_cblock *in; /* >= length bytes of input text */
57 des_cblock *out; /* >= length bytes of output text */
58 register afs_int32 length; /* in bytes */
59 int encrypt; /* 0 ==> decrypt, else encrypt */
60 des_key_schedule key; /* precomputed key schedule */
61 des_cblock *iv; /* 8 bytes of ivec */
63 register afs_uint32 *input = (afs_uint32 *) in;
64 register afs_uint32 *output = (afs_uint32 *) out;
65 register afs_uint32 *ivec = (afs_uint32 *) iv;
68 afs_uint32 t_input[2];
69 afs_uint32 t_output[2];
70 unsigned char *t_in_p = (unsigned char *) t_input;
71 afs_uint32 xor_0, xor_1;
75 if ((afs_int32) ivec & 3) {
76 memcpy((char *)&t_output[0], (char *)ivec++, sizeof(t_output[0]));
77 memcpy((char *)&t_output[1], (char *)ivec, sizeof(t_output[1]));
82 t_output[0] = *ivec++;
86 for (i = 0; length > 0; i++, length -= 8) {
89 if ((afs_int32) input & 3) {
90 memcpy((char *)&t_input[0], (char *)input++, sizeof(t_input[0]));
91 memcpy((char *)&t_input[1], (char *)input++, sizeof(t_input[1]));
96 t_input[0] = *input++;
97 t_input[1] = *input++;
102 for (j = length; j <= 7; j++)
107 des_debug_print("clear",length,t_input[0],t_input[1]);
109 /* do the xor for cbc into the temp */
110 t_input[0] ^= t_output[0];
111 t_input[1] ^= t_output[1];
113 (void) des_ecb_encrypt(t_input,t_output,key,encrypt);
114 /* copy temp output and save it for cbc */
116 if ((afs_int32) output & 3) {
117 memcpy((char *)output++, (char *)&t_output[0], sizeof(t_output[0]));
118 memcpy((char *)output++, (char *)&t_output[1], sizeof(t_output[1]));
123 *output++ = t_output[0];
124 *output++ = t_output[1];
129 des_debug_print("xor'ed",i,t_input[0],t_input[1]);
130 des_debug_print("cipher",i,t_output[0],t_output[1]);
140 if ((afs_int32) ivec & 3) {
141 memcpy((char *)&xor_0, (char *)ivec++, sizeof(xor_0));
142 memcpy((char *)&xor_1, (char *)ivec, sizeof(xor_1));
151 for (i = 0; length > 0; i++, length -= 8) {
154 if ((afs_int32) input & 3) {
155 memcpy((char *)&t_input[0], (char *)input++, sizeof(t_input[0]));
156 memcpy((char *)&t_input[1], (char *)input++, sizeof(t_input[0]));
161 t_input[0] = *input++;
162 t_input[1] = *input++;
165 /* no padding for decrypt */
168 des_debug_print("cipher",i,t_input[0],t_input[1]);
175 (void) des_ecb_encrypt(t_input,t_output,key,encrypt);
178 des_debug_print("out pre xor",i,t_output[0],t_output[1]);
180 /* do the xor for cbc into the output */
181 t_output[0] ^= xor_0;
182 t_output[1] ^= xor_1;
183 /* copy temp output */
185 if ((afs_int32) output & 3) {
186 memcpy((char *)output++, (char *)&t_output[0], sizeof(t_output[0]));
187 memcpy((char *)output++, (char *)&t_output[1], sizeof(t_output[1]));
192 *output++ = t_output[0];
193 *output++ = t_output[1];
196 /* save xor value for next round */
201 des_debug_print("clear",i,t_output[0],t_output[1]);