crypt-take-voids2-20041009
[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 #include "h/types.h"
30 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_OBSD_ENV)
31 #include "netinet/in.h"
32 #endif
33 #else /* UKERNEL */
34 #include "afs/sysincludes.h"
35 #include "afs/stds.h"
36 #endif /* UKERNEL */
37 #ifdef AFS_LINUX22_ENV
38 #include <asm/byteorder.h>
39 #endif
40
41 #include "afs/longc_procs.h"
42
43 #else /* KERNEL */
44
45 #include <afs/stds.h>
46 #include <sys/types.h>
47 #ifdef AFS_NT40_ENV
48 #include <winsock2.h>
49 #else
50 #include <netinet/in.h>
51 #endif
52 #include <rx/rx.h>
53 #endif /* KERNEL */
54
55 #include "sboxes.h"
56 #include "fcrypt.h"
57 #include "rxkad.h"
58
59
60 #ifdef TCRYPT
61 int ROUNDS = 16;
62 #else
63 #define ROUNDS 16
64 #endif
65
66 #define XPRT_FCRYPT
67
68 int
69 fc_keysched(struct ktc_encryptionKey *key, fc_KeySchedule schedule)
70 {
71     unsigned char *keychar = (unsigned char *)key;
72     afs_uint32 kword[2];
73
74     unsigned int temp;
75     int i;
76
77     /* first, flush the losing key parity bits. */
78     kword[0] = (*keychar++) >> 1;
79     kword[0] <<= 7;
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[1] = kword[0] >> 4;   /* get top 24 bits for hi word */
86     kword[0] &= 0xf;
87     kword[0] <<= 7;
88     kword[0] += (*keychar++) >> 1;
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
96     schedule[0] = kword[0];
97     for (i = 1; i < ROUNDS; i++) {
98         /* rotate right 3 */
99         temp = kword[0] & ((1 << 11) - 1);      /* get 11 lsb */
100         kword[0] =
101             (kword[0] >> 11) | ((kword[1] & ((1 << 11) - 1)) << (32 - 11));
102         kword[1] = (kword[1] >> 11) | (temp << (56 - 32 - 11));
103         schedule[i] = kword[0];
104     }
105     LOCK_RXKAD_STATS;
106     rxkad_stats.fc_key_scheds++;
107     UNLOCK_RXKAD_STATS;
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                fc_KeySchedule schedule, int encrypt)
115 {
116     afs_uint32 L, R;
117     afs_uint32 S, P;
118     unsigned char *Pchar = (unsigned char *)&P;
119     unsigned char *Schar = (unsigned char *)&S;
120     int i;
121
122 #if defined(vax) || (defined(mips) && defined(MIPSEL)) || defined(AFSLITTLE_ENDIAN)
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         LOCK_RXKAD_STATS;
144         rxkad_stats.fc_encrypts[ENCRYPT]++;
145         UNLOCK_RXKAD_STATS;
146         for (i = 0; i < (ROUNDS / 2); i++) {
147             S = *schedule++ ^ R;        /* xor R with key bits from schedule */
148             Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. */
149             Pchar[Byte3] = sbox1[Schar[Byte1]]; /* and permute the result */
150             Pchar[Byte1] = sbox2[Schar[Byte2]];
151             Pchar[Byte0] = sbox3[Schar[Byte3]];
152             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
153             L ^= P;             /* we're done with L, so save there */
154             S = *schedule++ ^ L;        /* this time xor with L */
155             Pchar[Byte2] = sbox0[Schar[Byte0]];
156             Pchar[Byte3] = sbox1[Schar[Byte1]];
157             Pchar[Byte1] = sbox2[Schar[Byte2]];
158             Pchar[Byte0] = sbox3[Schar[Byte3]];
159             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
160             R ^= P;
161         }
162     } else {
163         LOCK_RXKAD_STATS;
164         rxkad_stats.fc_encrypts[DECRYPT]++;
165         UNLOCK_RXKAD_STATS;
166         schedule = &schedule[ROUNDS - 1];       /* start at end of key schedule */
167         for (i = 0; i < (ROUNDS / 2); i++) {
168             S = *schedule-- ^ L;        /* xor R with key bits from schedule */
169             Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. and */
170             Pchar[Byte3] = sbox1[Schar[Byte1]]; /* permute the result */
171             Pchar[Byte1] = sbox2[Schar[Byte2]];
172             Pchar[Byte0] = sbox3[Schar[Byte3]];
173             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
174             R ^= P;             /* we're done with L, so save there */
175             S = *schedule-- ^ R;        /* this time xor with L */
176             Pchar[Byte2] = sbox0[Schar[Byte0]];
177             Pchar[Byte3] = sbox1[Schar[Byte1]];
178             Pchar[Byte1] = sbox2[Schar[Byte2]];
179             Pchar[Byte0] = sbox3[Schar[Byte3]];
180             P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5));  /* right rot 5 bits */
181             L ^= P;
182         }
183     }
184 #if 0
185     memcpy(cipher, &L, sizeof(afs_int32));
186     memcpy(cipher + 1, &R, sizeof(afs_int32));
187 #else
188     *((afs_int32 *)cipher) = htonl(L);
189     *((afs_int32 *)cipher + 1) = htonl(R);
190 #endif
191     return 0;
192 }
193
194 /* Crypting can be done in segments by recycling xor.  All but the final segment must
195  * be multiples of 8 bytes.
196  * NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
197  * scatter/gather vectors.
198  */
199 /*
200   afs_int32 length; * in bytes *
201   int encrypt; * 0 ==> decrypt, else encrypt *
202   fc_KeySchedule key; * precomputed key schedule *
203   afs_uint32 *xor; * 8 bytes of initialization vector *
204 */
205 afs_int32
206 fc_cbc_encrypt(void *input, void *output, afs_int32 length,
207                fc_KeySchedule key, afs_uint32 * xor, int encrypt)
208 {
209     afs_uint32 i, j;
210     afs_uint32 t_input[2];
211     afs_uint32 t_output[2];
212     unsigned char *t_in_p = (unsigned char *)t_input;
213
214     if (encrypt) {
215         for (i = 0; length > 0; i++, length -= 8) {
216             /* get input */
217             memcpy(t_input, input, sizeof(t_input));
218             (char *)input += sizeof(t_input);
219
220             /* zero pad */
221             for (j = length; j <= 7; j++)
222                 *(t_in_p + j) = 0;
223
224             /* do the xor for cbc into the temp */
225             xor[0] ^= t_input[0];
226             xor[1] ^= t_input[1];
227             /* encrypt */
228             fc_ecb_encrypt(xor, t_output, key, encrypt);
229
230             /* copy temp output and save it for cbc */
231             memcpy(output, t_output, sizeof(t_output));
232             (char *)output += sizeof(t_output);
233
234             /* calculate xor value for next round from plain & cipher text */
235             xor[0] = t_input[0] ^ t_output[0];
236             xor[1] = t_input[1] ^ t_output[1];
237
238
239         }
240         t_output[0] = 0;
241         t_output[1] = 0;
242     } else {
243         /* decrypt */
244         for (i = 0; length > 0; i++, length -= 8) {
245             /* get input */
246             memcpy(t_input, input, sizeof(t_input));
247             (char *)input += sizeof(t_input);
248
249             /* no padding for decrypt */
250             fc_ecb_encrypt(t_input, t_output, key, encrypt);
251
252             /* do the xor for cbc into the output */
253             t_output[0] ^= xor[0];
254             t_output[1] ^= xor[1];
255
256             /* copy temp output */
257             memcpy(output, t_output, sizeof(t_output));
258             (char *)output += sizeof(t_output);
259
260             /* calculate xor value for next round from plain & cipher text */
261             xor[0] = t_input[0] ^ t_output[0];
262             xor[1] = t_input[1] ^ t_output[1];
263         }
264     }
265     return 0;
266 }