linux-warning-reduction-20090318
[openafs.git] / src / rxkad / domestic / fcrypt.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
11  * scatter/gather vectors.
12  */
13
14
15 #include <afsconfig.h>
16 #ifdef KERNEL
17 #include "afs/param.h"
18 #else
19 #include <afs/param.h>
20 #endif
21
22 RCSID
23     ("$Header$");
24
25 #define DEBUG 0
26 #ifdef KERNEL
27 #ifndef UKERNEL
28 #include "afs/stds.h"
29 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV) 
30 #include "h/systm.h"
31 #endif
32 #include "h/types.h"
33 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_OBSD_ENV)
34 #include "netinet/in.h"
35 #endif
36 #else /* UKERNEL */
37 #include "afs/sysincludes.h"
38 #include "afs/stds.h"
39 #endif /* UKERNEL */
40 #ifdef AFS_LINUX22_ENV
41 #include <asm/byteorder.h>
42 #endif
43
44 #else /* KERNEL */
45
46 #include <afs/stds.h>
47 #include <sys/types.h>
48 #include <string.h>
49 #ifdef AFS_NT40_ENV
50 #include <winsock2.h>
51 #else
52 #include <netinet/in.h>
53 #endif
54 #include <rx/rx.h>
55 #endif /* KERNEL */
56
57 #include "sboxes.h"
58 #include "fcrypt.h"
59 #include "rxkad.h"
60 #include <des/stats.h>
61
62 #ifdef TCRYPT
63 int ROUNDS = 16;
64 #else
65 #define ROUNDS 16
66 #endif
67
68 #define XPRT_FCRYPT
69
70 int
71 fc_keysched(struct ktc_encryptionKey *key, fc_KeySchedule schedule)
72 {
73     unsigned char *keychar = (unsigned char *)key;
74     afs_uint32 kword[2];
75
76     unsigned int temp;
77     int i;
78
79     /* first, flush the losing key parity bits. */
80     kword[0] = (*keychar++) >> 1;
81     kword[0] <<= 7;
82     kword[0] += (*keychar++) >> 1;
83     kword[0] <<= 7;
84     kword[0] += (*keychar++) >> 1;
85     kword[0] <<= 7;
86     kword[0] += (*keychar++) >> 1;
87     kword[1] = kword[0] >> 4;   /* get top 24 bits for hi word */
88     kword[0] &= 0xf;
89     kword[0] <<= 7;
90     kword[0] += (*keychar++) >> 1;
91     kword[0] <<= 7;
92     kword[0] += (*keychar++) >> 1;
93     kword[0] <<= 7;
94     kword[0] += (*keychar++) >> 1;
95     kword[0] <<= 7;
96     kword[0] += (*keychar) >> 1;
97
98     schedule[0] = kword[0];
99     for (i = 1; i < ROUNDS; i++) {
100         /* rotate right 3 */
101         temp = kword[0] & ((1 << 11) - 1);      /* get 11 lsb */
102         kword[0] =
103             (kword[0] >> 11) | ((kword[1] & ((1 << 11) - 1)) << (32 - 11));
104         kword[1] = (kword[1] >> 11) | (temp << (56 - 32 - 11));
105         schedule[i] = kword[0];
106     }
107     INC_RXKAD_STATS(fc_key_scheds);
108     return 0;
109 }
110
111 /* IN int encrypt; * 0 ==> decrypt, else encrypt */
112 afs_int32
113 fc_ecb_encrypt(void * clear, void * cipher,
114                const fc_KeySchedule schedule, int encrypt)
115 {
116     afs_uint32 L, R;
117     volatile afs_uint32 S, P;
118     volatile unsigned char *Pchar = (unsigned char *)&P;
119     volatile unsigned char *Schar = (unsigned char *)&S;
120     int i;
121
122 #ifndef WORDS_BIGENDIAN
123 #define Byte0 3
124 #define Byte1 2
125 #define Byte2 1
126 #define Byte3 0
127 #else
128 #define Byte0 0
129 #define Byte1 1
130 #define Byte2 2
131 #define Byte3 3
132 #endif
133
134 #if 0
135     memcpy(&L, clear, sizeof(afs_int32));
136     memcpy(&R, clear + 1, sizeof(afs_int32));
137 #else
138     L = ntohl(*((afs_uint32 *)clear));
139     R = ntohl(*((afs_uint32 *)clear + 1));
140 #endif
141
142     if (encrypt) {
143         INC_RXKAD_STATS(fc_encrypts[ENCRYPT]);
144         for (i = 0; i < (ROUNDS / 2); i++) {
145             S = *schedule++ ^ R;        /* xor R with key bits from schedule */
146             Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. */
147             Pchar[Byte3] = sbox1[Schar[Byte1]]; /* and permute the result */
148             Pchar[Byte1] = sbox2[Schar[Byte2]];
149             Pchar[Byte0] = sbox3[Schar[Byte3]];
150             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
151             L ^= P;             /* we're done with L, so save there */
152             S = *schedule++ ^ L;        /* this time xor with L */
153             Pchar[Byte2] = sbox0[Schar[Byte0]];
154             Pchar[Byte3] = sbox1[Schar[Byte1]];
155             Pchar[Byte1] = sbox2[Schar[Byte2]];
156             Pchar[Byte0] = sbox3[Schar[Byte3]];
157             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
158             R ^= P;
159         }
160     } else {
161         INC_RXKAD_STATS(fc_encrypts[DECRYPT]);
162         schedule = &schedule[ROUNDS - 1];       /* start at end of key schedule */
163         for (i = 0; i < (ROUNDS / 2); i++) {
164             S = *schedule-- ^ L;        /* xor R with key bits from schedule */
165             Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. and */
166             Pchar[Byte3] = sbox1[Schar[Byte1]]; /* permute the result */
167             Pchar[Byte1] = sbox2[Schar[Byte2]];
168             Pchar[Byte0] = sbox3[Schar[Byte3]];
169             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
170             R ^= P;             /* we're done with L, so save there */
171             S = *schedule-- ^ R;        /* this time xor with L */
172             Pchar[Byte2] = sbox0[Schar[Byte0]];
173             Pchar[Byte3] = sbox1[Schar[Byte1]];
174             Pchar[Byte1] = sbox2[Schar[Byte2]];
175             Pchar[Byte0] = sbox3[Schar[Byte3]];
176             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
177             L ^= P;
178         }
179     }
180 #if 0
181     memcpy(cipher, &L, sizeof(afs_int32));
182     memcpy(cipher + 1, &R, sizeof(afs_int32));
183 #else
184     *((afs_int32 *)cipher) = htonl(L);
185     *((afs_int32 *)cipher + 1) = htonl(R);
186 #endif
187     return 0;
188 }
189
190 /* Crypting can be done in segments by recycling xor.  All but the final segment must
191  * be multiples of 8 bytes.
192  * NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
193  * scatter/gather vectors.
194  */
195 /*
196   afs_int32 length; * in bytes *
197   int encrypt; * 0 ==> decrypt, else encrypt *
198   fc_KeySchedule key; * precomputed key schedule *
199   afs_uint32 *xor; * 8 bytes of initialization vector *
200 */
201 afs_int32
202 fc_cbc_encrypt(void *input, void *output, afs_int32 length,
203                const fc_KeySchedule key, afs_uint32 * xor, int encrypt)
204 {
205     afs_uint32 i, j;
206     afs_uint32 t_input[2];
207     afs_uint32 t_output[2];
208     unsigned char *t_in_p = (unsigned char *)t_input;
209
210     if (encrypt) {
211         for (i = 0; length > 0; i++, length -= 8) {
212             /* get input */
213             memcpy(t_input, input, sizeof(t_input));
214             input=((char *)input) + sizeof(t_input);
215
216             /* zero pad */
217             for (j = length; j <= 7; j++)
218                 *(t_in_p + j) = 0;
219
220             /* do the xor for cbc into the temp */
221             xor[0] ^= t_input[0];
222             xor[1] ^= t_input[1];
223             /* encrypt */
224             fc_ecb_encrypt(xor, t_output, key, encrypt);
225
226             /* copy temp output and save it for cbc */
227             memcpy(output, t_output, sizeof(t_output));
228             output=(char *)output + sizeof(t_output);
229
230             /* calculate xor value for next round from plain & cipher text */
231             xor[0] = t_input[0] ^ t_output[0];
232             xor[1] = t_input[1] ^ t_output[1];
233
234
235         }
236         t_output[0] = 0;
237         t_output[1] = 0;
238     } else {
239         /* decrypt */
240         for (i = 0; length > 0; i++, length -= 8) {
241             /* get input */
242             memcpy(t_input, input, sizeof(t_input));
243             input=((char *)input) + sizeof(t_input);
244
245             /* no padding for decrypt */
246             fc_ecb_encrypt(t_input, t_output, key, encrypt);
247
248             /* do the xor for cbc into the output */
249             t_output[0] ^= xor[0];
250             t_output[1] ^= xor[1];
251
252             /* copy temp output */
253             memcpy(output, t_output, sizeof(t_output));
254             output=((char *)output) + sizeof(t_output);
255
256             /* calculate xor value for next round from plain & cipher text */
257             xor[0] = t_input[0] ^ t_output[0];
258             xor[1] = t_input[1] ^ t_output[1];
259         }
260     }
261     return 0;
262 }