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