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 * spm 8/85 MIT project athena
20 #include <afs/param.h>
21 #include <afsconfig.h>
25 #include <mit-cpyright.h>
27 #if defined(HAVE_STRINGS_H)
30 #if defined(HAVE_STRING_H)
35 #include "des_internal.h"
40 extern int des_debug_print();
41 extern int des_ecb_encrypt();
44 * This routine performs DES cipher-block-chaining checksum operation,
45 * a.k.a. Message Authentication Code. It ALWAYS encrypts from input
46 * to a single 64 bit output MAC checksum.
48 * The key schedule is passed as an arg, as well as the cleartext or
49 * ciphertext. The cleartext and ciphertext should be in host order.
51 * NOTE-- the output is ALWAYS 8 bytes long. If not enough space was
52 * provided, your program will get trashed.
54 * The input is null padded, at the end (highest addr), to an integral
55 * multiple of eight bytes.
59 des_cbc_cksum(in,out,length,key,iv)
60 des_cblock *in; /* >= length bytes of inputtext */
61 des_cblock *out; /* >= length bytes of outputtext */
62 register afs_int32 length; /* in bytes */
63 des_key_schedule key; /* precomputed key schedule */
64 des_cblock *iv; /* 8 bytes of ivec */
66 register afs_uint32 *input = (afs_uint32 *) in;
67 register afs_uint32 *output = (afs_uint32 *) out;
68 afs_uint32 *ivec = (afs_uint32 *) iv;
71 afs_uint32 t_input[2];
72 afs_uint32 t_output[8];
73 unsigned char *t_in_p = (unsigned char *) t_input;
76 if ((afs_int32) ivec & 3) {
77 bcopy((char *)ivec++,(char *)&t_output[0],sizeof(t_output[0]));
78 bcopy((char *)ivec,(char *)&t_output[1],sizeof(t_output[1]));
83 t_output[0] = *ivec++;
87 for (i = 0; length > 0; i++, length -= 8) {
90 if ((afs_int32) input & 3) {
91 bcopy((char *)input++,(char *)&t_input[0],sizeof(t_input[0]));
92 bcopy((char *)input++,(char *)&t_input[1],sizeof(t_input[1]));
97 t_input[0] = *input++;
98 t_input[1] = *input++;
103 for (j = length; j <= 7; j++)
108 des_debug_print("clear",length,t_input[0],t_input[1]);
110 /* do the xor for cbc into the temp */
111 t_input[0] ^= t_output[0] ;
112 t_input[1] ^= t_output[1] ;
114 (void) des_ecb_encrypt(t_input,t_output,key,1);
117 des_debug_print("xor'ed",i,t_input[0],t_input[1]);
118 des_debug_print("cipher",i,t_output[0],t_output[1]);
126 /* copy temp output and save it for checksum */
128 if ((afs_int32) output & 3) {
129 bcopy((char *)&t_output[0],(char *)output++,sizeof(t_output[0]));
130 bcopy((char *)&t_output[1],(char *)output,sizeof(t_output[1]));
135 *output++ = t_output[0];
136 *output = t_output[1];
139 return (afs_uint32) t_output[1];