2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
11 * scatter/gather vectors.
15 #include <afsconfig.h>
17 #include "afs/param.h"
19 #include <afs/param.h>
30 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_OBSD_ENV)
31 #include "netinet/in.h"
34 #include "afs/sysincludes.h"
37 #ifdef AFS_LINUX22_ENV
38 #include <asm/byteorder.h>
41 #include "afs/longc_procs.h"
46 #include <sys/types.h>
50 #include <netinet/in.h>
58 #include <des/stats.h>
69 fc_keysched(struct ktc_encryptionKey *key, fc_KeySchedule schedule)
71 unsigned char *keychar = (unsigned char *)key;
77 /* first, flush the losing key parity bits. */
78 kword[0] = (*keychar++) >> 1;
80 kword[0] += (*keychar++) >> 1;
82 kword[0] += (*keychar++) >> 1;
84 kword[0] += (*keychar++) >> 1;
85 kword[1] = kword[0] >> 4; /* get top 24 bits for hi word */
88 kword[0] += (*keychar++) >> 1;
90 kword[0] += (*keychar++) >> 1;
92 kword[0] += (*keychar++) >> 1;
94 kword[0] += (*keychar) >> 1;
96 schedule[0] = kword[0];
97 for (i = 1; i < ROUNDS; i++) {
99 temp = kword[0] & ((1 << 11) - 1); /* get 11 lsb */
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];
105 INC_RXKAD_STATS(fc_key_scheds);
109 /* IN int encrypt; * 0 ==> decrypt, else encrypt */
111 fc_ecb_encrypt(void * clear, void * cipher,
112 fc_KeySchedule schedule, int encrypt)
115 volatile afs_uint32 S, P;
116 volatile unsigned char *Pchar = (unsigned char *)&P;
117 volatile unsigned char *Schar = (unsigned char *)&S;
120 #if defined(vax) || (defined(mips) && defined(MIPSEL)) || defined(AFSLITTLE_ENDIAN)
133 memcpy(&L, clear, sizeof(afs_int32));
134 memcpy(&R, clear + 1, sizeof(afs_int32));
136 L = ntohl(*((afs_uint32 *)clear));
137 R = ntohl(*((afs_uint32 *)clear + 1));
141 INC_RXKAD_STATS(fc_encrypts[ENCRYPT]);
142 for (i = 0; i < (ROUNDS / 2); i++) {
143 S = *schedule++ ^ R; /* xor R with key bits from schedule */
144 Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. */
145 Pchar[Byte3] = sbox1[Schar[Byte1]]; /* and permute the result */
146 Pchar[Byte1] = sbox2[Schar[Byte2]];
147 Pchar[Byte0] = sbox3[Schar[Byte3]];
148 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
149 L ^= P; /* we're done with L, so save there */
150 S = *schedule++ ^ L; /* this time xor with L */
151 Pchar[Byte2] = sbox0[Schar[Byte0]];
152 Pchar[Byte3] = sbox1[Schar[Byte1]];
153 Pchar[Byte1] = sbox2[Schar[Byte2]];
154 Pchar[Byte0] = sbox3[Schar[Byte3]];
155 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
159 INC_RXKAD_STATS(fc_encrypts[DECRYPT]);
160 schedule = &schedule[ROUNDS - 1]; /* start at end of key schedule */
161 for (i = 0; i < (ROUNDS / 2); i++) {
162 S = *schedule-- ^ L; /* xor R with key bits from schedule */
163 Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. and */
164 Pchar[Byte3] = sbox1[Schar[Byte1]]; /* permute the result */
165 Pchar[Byte1] = sbox2[Schar[Byte2]];
166 Pchar[Byte0] = sbox3[Schar[Byte3]];
167 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
168 R ^= P; /* we're done with L, so save there */
169 S = *schedule-- ^ R; /* this time xor with L */
170 Pchar[Byte2] = sbox0[Schar[Byte0]];
171 Pchar[Byte3] = sbox1[Schar[Byte1]];
172 Pchar[Byte1] = sbox2[Schar[Byte2]];
173 Pchar[Byte0] = sbox3[Schar[Byte3]];
174 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
179 memcpy(cipher, &L, sizeof(afs_int32));
180 memcpy(cipher + 1, &R, sizeof(afs_int32));
182 *((afs_int32 *)cipher) = htonl(L);
183 *((afs_int32 *)cipher + 1) = htonl(R);
188 /* Crypting can be done in segments by recycling xor. All but the final segment must
189 * be multiples of 8 bytes.
190 * NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
191 * scatter/gather vectors.
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 *
200 fc_cbc_encrypt(void *input, void *output, afs_int32 length,
201 fc_KeySchedule key, afs_uint32 * xor, int encrypt)
204 afs_uint32 t_input[2];
205 afs_uint32 t_output[2];
206 unsigned char *t_in_p = (unsigned char *)t_input;
209 for (i = 0; length > 0; i++, length -= 8) {
211 memcpy(t_input, input, sizeof(t_input));
212 input=((char *)input) + sizeof(t_input);
215 for (j = length; j <= 7; j++)
218 /* do the xor for cbc into the temp */
219 xor[0] ^= t_input[0];
220 xor[1] ^= t_input[1];
222 fc_ecb_encrypt(xor, t_output, key, encrypt);
224 /* copy temp output and save it for cbc */
225 memcpy(output, t_output, sizeof(t_output));
226 output=(char *)output + sizeof(t_output);
228 /* calculate xor value for next round from plain & cipher text */
229 xor[0] = t_input[0] ^ t_output[0];
230 xor[1] = t_input[1] ^ t_output[1];
238 for (i = 0; length > 0; i++, length -= 8) {
240 memcpy(t_input, input, sizeof(t_input));
241 input=((char *)input) + sizeof(t_input);
243 /* no padding for decrypt */
244 fc_ecb_encrypt(t_input, t_output, key, encrypt);
246 /* do the xor for cbc into the output */
247 t_output[0] ^= xor[0];
248 t_output[1] ^= xor[1];
250 /* copy temp output */
251 memcpy(output, t_output, sizeof(t_output));
252 output=((char *)output) + sizeof(t_output);
254 /* calculate xor value for next round from plain & cipher text */
255 xor[0] = t_input[0] ^ t_output[0];
256 xor[1] = t_input[1] ^ t_output[1];