e85add71030da932fedc160a7f5306edfe0b5e28
[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 <afs/param.h>
21 #include <mit-cpyright.h>
22 #include <stdio.h>
23 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
24 #include <string.h>
25 #else
26 #include <strings.h>
27 #endif
28
29 #include <des.h>
30 #include "des_internal.h"
31
32 #define XPRT_CKSUM
33
34 extern int des_debug;
35 extern int des_debug_print();
36 extern int des_ecb_encrypt();
37
38 /*
39  * This routine performs DES cipher-block-chaining checksum operation,
40  * a.k.a.  Message Authentication Code.  It ALWAYS encrypts from input
41  * to a single 64 bit output MAC checksum.
42  *
43  * The key schedule is passed as an arg, as well as the cleartext or
44  * ciphertext. The cleartext and ciphertext should be in host order.
45  *
46  * NOTE-- the output is ALWAYS 8 bytes long.  If not enough space was
47  * provided, your program will get trashed.
48  *
49  * The input is null padded, at the end (highest addr), to an integral
50  * multiple of eight bytes.
51  */
52
53 afs_uint32
54 des_cbc_cksum(in,out,length,key,iv)
55     des_cblock *in;             /* >= length bytes of inputtext */
56     des_cblock *out;            /* >= length bytes of outputtext */
57     register afs_int32 length;  /* in bytes */
58     des_key_schedule key;               /* precomputed key schedule */
59     des_cblock *iv;             /* 8 bytes of ivec */
60 {
61     register afs_uint32 *input = (afs_uint32 *) in;
62     register afs_uint32 *output = (afs_uint32 *) out;
63     afs_uint32 *ivec = (afs_uint32 *) iv;
64
65     afs_uint32 i,j;
66     afs_uint32 t_input[2];
67     afs_uint32 t_output[8];
68     unsigned char *t_in_p = (unsigned char *) t_input;
69
70 #ifdef MUSTALIGN
71     if ((afs_int32) ivec & 3) {
72         bcopy((char *)ivec++,(char *)&t_output[0],sizeof(t_output[0]));
73         bcopy((char *)ivec,(char *)&t_output[1],sizeof(t_output[1]));
74     }
75     else
76 #endif
77     {
78         t_output[0] = *ivec++;
79         t_output[1] = *ivec;
80     }
81
82     for (i = 0; length > 0; i++, length -= 8) {
83         /* get input */
84 #ifdef MUSTALIGN
85         if ((afs_int32) input & 3) {
86             bcopy((char *)input++,(char *)&t_input[0],sizeof(t_input[0]));
87             bcopy((char *)input++,(char *)&t_input[1],sizeof(t_input[1]));
88         }
89         else
90 #endif
91         {
92             t_input[0] = *input++;
93             t_input[1] = *input++;
94         }
95
96         /* zero pad */
97         if (length < 8)
98             for (j = length; j <= 7; j++)
99                 *(t_in_p+j)= 0;
100
101 #ifdef DEBUG
102         if (des_debug)
103             des_debug_print("clear",length,t_input[0],t_input[1]);
104 #endif
105         /* do the xor for cbc into the temp */
106         t_input[0] ^= t_output[0] ;
107         t_input[1] ^= t_output[1] ;
108         /* encrypt */
109         (void) des_ecb_encrypt(t_input,t_output,key,1);
110 #ifdef DEBUG
111         if (des_debug) {
112             des_debug_print("xor'ed",i,t_input[0],t_input[1]);
113             des_debug_print("cipher",i,t_output[0],t_output[1]);
114         }
115 #else
116 #ifdef lint
117         i = i;
118 #endif
119 #endif
120     }
121     /* copy temp output and save it for checksum */
122 #ifdef MUSTALIGN
123     if ((afs_int32) output & 3) {
124         bcopy((char *)&t_output[0],(char *)output++,sizeof(t_output[0]));
125         bcopy((char *)&t_output[1],(char *)output,sizeof(t_output[1]));
126     }
127     else
128 #endif
129     {
130         *output++ = t_output[0];
131         *output = t_output[1];
132     }
133
134     return (afs_uint32) t_output[1];
135 }