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