amd64-linux-port-20030428
[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 RCSID("$Header$");
24
25 #include <mit-cpyright.h>
26 #ifndef KERNEL
27 #include <stdio.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #else
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #endif
36
37 #include <des.h>
38 #include "des_internal.h"
39 #include "des_prototypes.h"
40
41 #define XPRT_CKSUM
42
43 /*
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.
47  *
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.
50  *
51  * NOTE-- the output is ALWAYS 8 bytes long.  If not enough space was
52  * provided, your program will get trashed.
53  *
54  * The input is null padded, at the end (highest addr), to an integral
55  * multiple of eight bytes.
56  */
57 /*
58     des_cblock *in;             * >= length bytes of inputtext *
59     des_cblock *out;            * >= length bytes of outputtext *
60     register afs_int32 length;  * in bytes *
61     des_key_schedule key;       * precomputed key schedule *
62     des_cblock *iv;             * 8 bytes of ivec *
63 */
64
65 afs_uint32 des_cbc_cksum(des_cblock *in, des_cblock *out,
66         register afs_int32 length, 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     }
82     else
83 #endif
84     {
85         t_output[0] = *ivec++;
86         t_output[1] = *ivec;
87     }
88
89     for (i = 0; length > 0; i++, length -= 8) {
90         /* get input */
91 #ifdef MUSTALIGN
92         if ((afs_int32) input & 3) {
93             memcpy((char *)&t_input[0], (char *)input++, sizeof(t_input[0]));
94             memcpy((char *)&t_input[1], (char *)input++, sizeof(t_input[1]));
95         }
96         else
97 #endif
98         {
99             t_input[0] = *input++;
100             t_input[1] = *input++;
101         }
102
103         /* zero pad */
104         if (length < 8)
105             for (j = length; j <= 7; j++)
106                 *(t_in_p+j)= 0;
107
108 #ifdef DEBUG
109         if (des_debug)
110             des_debug_print("clear",length,t_input[0],t_input[1]);
111 #endif
112         /* do the xor for cbc into the temp */
113         t_input[0] ^= t_output[0] ;
114         t_input[1] ^= t_output[1] ;
115         /* encrypt */
116         (void) des_ecb_encrypt(t_input,t_output,key,1);
117 #ifdef DEBUG
118         if (des_debug) {
119             des_debug_print("xor'ed",i,t_input[0],t_input[1]);
120             des_debug_print("cipher",i,t_output[0],t_output[1]);
121         }
122 #else
123 #ifdef lint
124         i = i;
125 #endif
126 #endif
127     }
128     /* copy temp output and save it for checksum */
129 #ifdef MUSTALIGN
130     if ((afs_int32) output & 3) {
131         memcpy((char *)output++, (char *)&t_output[0], sizeof(t_output[0]));
132         memcpy((char *)output, (char *)&t_output[1], sizeof(t_output[1]));
133     }
134     else
135 #endif
136     {
137         *output++ = t_output[0];
138         *output = t_output[1];
139     }
140
141     return (afs_uint32) t_output[1];
142 }