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>
29 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
33 #if !defined(AFS_LINUX20_ENV) && !defined(AFS_OBSD_ENV)
34 #include "netinet/in.h"
37 #include "afs/sysincludes.h"
40 #ifdef AFS_LINUX22_ENV
41 #include <asm/byteorder.h>
44 #include "afs/longc_procs.h"
49 #include <sys/types.h>
54 #include <netinet/in.h>
62 #include <des/stats.h>
73 fc_keysched(struct ktc_encryptionKey *key, fc_KeySchedule schedule)
75 unsigned char *keychar = (unsigned char *)key;
81 /* first, flush the losing key parity bits. */
82 kword[0] = (*keychar++) >> 1;
84 kword[0] += (*keychar++) >> 1;
86 kword[0] += (*keychar++) >> 1;
88 kword[0] += (*keychar++) >> 1;
89 kword[1] = kword[0] >> 4; /* get top 24 bits for hi word */
92 kword[0] += (*keychar++) >> 1;
94 kword[0] += (*keychar++) >> 1;
96 kword[0] += (*keychar++) >> 1;
98 kword[0] += (*keychar) >> 1;
100 schedule[0] = kword[0];
101 for (i = 1; i < ROUNDS; i++) {
103 temp = kword[0] & ((1 << 11) - 1); /* get 11 lsb */
105 (kword[0] >> 11) | ((kword[1] & ((1 << 11) - 1)) << (32 - 11));
106 kword[1] = (kword[1] >> 11) | (temp << (56 - 32 - 11));
107 schedule[i] = kword[0];
109 INC_RXKAD_STATS(fc_key_scheds);
113 /* IN int encrypt; * 0 ==> decrypt, else encrypt */
115 fc_ecb_encrypt(void * clear, void * cipher,
116 fc_KeySchedule schedule, int encrypt)
119 volatile afs_uint32 S, P;
120 volatile unsigned char *Pchar = (unsigned char *)&P;
121 volatile unsigned char *Schar = (unsigned char *)&S;
124 #ifndef WORDS_BIGENDIAN
137 memcpy(&L, clear, sizeof(afs_int32));
138 memcpy(&R, clear + 1, sizeof(afs_int32));
140 L = ntohl(*((afs_uint32 *)clear));
141 R = ntohl(*((afs_uint32 *)clear + 1));
145 INC_RXKAD_STATS(fc_encrypts[ENCRYPT]);
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 */
163 INC_RXKAD_STATS(fc_encrypts[DECRYPT]);
164 schedule = &schedule[ROUNDS - 1]; /* start at end of key schedule */
165 for (i = 0; i < (ROUNDS / 2); i++) {
166 S = *schedule-- ^ L; /* xor R with key bits from schedule */
167 Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. and */
168 Pchar[Byte3] = sbox1[Schar[Byte1]]; /* permute the result */
169 Pchar[Byte1] = sbox2[Schar[Byte2]];
170 Pchar[Byte0] = sbox3[Schar[Byte3]];
171 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
172 R ^= P; /* we're done with L, so save there */
173 S = *schedule-- ^ R; /* this time xor with L */
174 Pchar[Byte2] = sbox0[Schar[Byte0]];
175 Pchar[Byte3] = sbox1[Schar[Byte1]];
176 Pchar[Byte1] = sbox2[Schar[Byte2]];
177 Pchar[Byte0] = sbox3[Schar[Byte3]];
178 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
183 memcpy(cipher, &L, sizeof(afs_int32));
184 memcpy(cipher + 1, &R, sizeof(afs_int32));
186 *((afs_int32 *)cipher) = htonl(L);
187 *((afs_int32 *)cipher + 1) = htonl(R);
192 /* Crypting can be done in segments by recycling xor. All but the final segment must
193 * be multiples of 8 bytes.
194 * NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
195 * scatter/gather vectors.
198 afs_int32 length; * in bytes *
199 int encrypt; * 0 ==> decrypt, else encrypt *
200 fc_KeySchedule key; * precomputed key schedule *
201 afs_uint32 *xor; * 8 bytes of initialization vector *
204 fc_cbc_encrypt(void *input, void *output, afs_int32 length,
205 fc_KeySchedule key, afs_uint32 * xor, int encrypt)
208 afs_uint32 t_input[2];
209 afs_uint32 t_output[2];
210 unsigned char *t_in_p = (unsigned char *)t_input;
213 for (i = 0; length > 0; i++, length -= 8) {
215 memcpy(t_input, input, sizeof(t_input));
216 input=((char *)input) + sizeof(t_input);
219 for (j = length; j <= 7; j++)
222 /* do the xor for cbc into the temp */
223 xor[0] ^= t_input[0];
224 xor[1] ^= t_input[1];
226 fc_ecb_encrypt(xor, t_output, key, encrypt);
228 /* copy temp output and save it for cbc */
229 memcpy(output, t_output, sizeof(t_output));
230 output=(char *)output + sizeof(t_output);
232 /* calculate xor value for next round from plain & cipher text */
233 xor[0] = t_input[0] ^ t_output[0];
234 xor[1] = t_input[1] ^ t_output[1];
242 for (i = 0; length > 0; i++, length -= 8) {
244 memcpy(t_input, input, sizeof(t_input));
245 input=((char *)input) + sizeof(t_input);
247 /* no padding for decrypt */
248 fc_ecb_encrypt(t_input, t_output, key, encrypt);
250 /* do the xor for cbc into the output */
251 t_output[0] ^= xor[0];
252 t_output[1] ^= xor[1];
254 /* copy temp output */
255 memcpy(output, t_output, sizeof(t_output));
256 output=((char *)output) + sizeof(t_output);
258 /* calculate xor value for next round from plain & cipher text */
259 xor[0] = t_input[0] ^ t_output[0];
260 xor[1] = t_input[1] ^ t_output[1];