9e570edd82f009bc6efb1d649e937089487388c7
[openafs.git] / src / des / cksum.c
1 /*
2  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
3  * of Technology.
4  *
5  * For copying and distribution information, please see the file
6  * <mit-cpyright.h>.
7  *
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.)
11  *
12  * Under U.S. law, this software may not be exported outside the US
13  * without license from the U.S. Commerce department.
14  * 
15  * These routines form the library interface to the DES facilities.
16  *
17  *      spm     8/85    MIT project athena
18  */
19
20 #include <afsconfig.h>
21 #include <afs/param.h>
22
23
24 #include <mit-cpyright.h>
25 #ifndef KERNEL
26 #include <stdio.h>
27 #endif
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #else
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34 #endif
35
36 #include <des.h>
37 #include "des_internal.h"
38 #include "des_prototypes.h"
39
40 #define XPRT_CKSUM
41
42 /*
43  * This routine performs DES cipher-block-chaining checksum operation,
44  * a.k.a.  Message Authentication Code.  It ALWAYS encrypts from input
45  * to a single 64 bit output MAC checksum.
46  *
47  * The key schedule is passed as an arg, as well as the cleartext or
48  * ciphertext. The cleartext and ciphertext should be in host order.
49  *
50  * NOTE-- the output is ALWAYS 8 bytes long.  If not enough space was
51  * provided, your program will get trashed.
52  *
53  * The input is null padded, at the end (highest addr), to an integral
54  * multiple of eight bytes.
55  */
56 /*
57     des_cblock *in;             * >= length bytes of inputtext *
58     des_cblock *out;            * >= length bytes of outputtext *
59     register afs_int32 length;  * in bytes *
60     des_key_schedule key;       * precomputed key schedule *
61     des_cblock *iv;             * 8 bytes of ivec *
62 */
63
64 afs_uint32
65 des_cbc_cksum(des_cblock * in, des_cblock * out, register afs_int32 length,
66               des_key_schedule key, des_cblock * iv)
67 {
68     register afs_uint32 *input = (afs_uint32 *) in;
69     register afs_uint32 *output = (afs_uint32 *) out;
70     afs_uint32 *ivec = (afs_uint32 *) iv;
71
72     afs_uint32 i, j;
73     afs_uint32 t_input[2];
74     afs_uint32 t_output[8];
75     unsigned char *t_in_p = (unsigned char *)t_input;
76
77 #ifdef MUSTALIGN
78     if ((afs_int32) ivec & 3) {
79         memcpy((char *)&t_output[0], (char *)ivec++, sizeof(t_output[0]));
80         memcpy((char *)&t_output[1], (char *)ivec, sizeof(t_output[1]));
81     } else
82 #endif
83     {
84         t_output[0] = *ivec++;
85         t_output[1] = *ivec;
86     }
87
88     for (i = 0; length > 0; i++, length -= 8) {
89         /* get input */
90 #ifdef MUSTALIGN
91         if ((afs_int32) input & 3) {
92             memcpy((char *)&t_input[0], (char *)input++, sizeof(t_input[0]));
93             memcpy((char *)&t_input[1], (char *)input++, sizeof(t_input[1]));
94         } else
95 #endif
96         {
97             t_input[0] = *input++;
98             t_input[1] = *input++;
99         }
100
101         /* zero pad */
102         if (length < 8)
103             for (j = length; j <= 7; j++)
104                 *(t_in_p + j) = 0;
105
106 #ifdef DEBUG
107         if (des_debug)
108             des_debug_print("clear", length, t_input[0], t_input[1]);
109 #endif
110         /* do the xor for cbc into the temp */
111         t_input[0] ^= t_output[0];
112         t_input[1] ^= t_output[1];
113         /* encrypt */
114         (void)des_ecb_encrypt(t_input, t_output, key, 1);
115 #ifdef DEBUG
116         if (des_debug) {
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]);
119         }
120 #else
121 #ifdef lint
122         i = i;
123 #endif
124 #endif
125     }
126     /* copy temp output and save it for checksum */
127 #ifdef MUSTALIGN
128     if ((afs_int32) output & 3) {
129         memcpy((char *)output++, (char *)&t_output[0], sizeof(t_output[0]));
130         memcpy((char *)output, (char *)&t_output[1], sizeof(t_output[1]));
131     } else
132 #endif
133     {
134         *output++ = t_output[0];
135         *output = t_output[1];
136     }
137
138     return (afs_uint32) t_output[1];
139 }