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>
16 #include <afs/param.h>
22 #if defined(AFS_AIX_ENV) || defined(AFS_AUX_ENV) || defined(AFS_SUN5_ENV)
26 #if !defined(AFS_LINUX_ENV) && !defined(AFS_OBSD_ENV)
27 #include "netinet/in.h"
30 #include "afs/sysincludes.h"
33 #include <asm/byteorder.h>
46 #include <rx/rxkad_stats.h>
57 fc_keysched(struct ktc_encryptionKey *key, fc_KeySchedule schedule)
59 unsigned char *keychar = (unsigned char *)key;
65 /* first, flush the losing key parity bits. */
66 kword[0] = (*keychar++) >> 1;
68 kword[0] += (*keychar++) >> 1;
70 kword[0] += (*keychar++) >> 1;
72 kword[0] += (*keychar++) >> 1;
73 kword[1] = kword[0] >> 4; /* get top 24 bits for hi word */
76 kword[0] += (*keychar++) >> 1;
78 kword[0] += (*keychar++) >> 1;
80 kword[0] += (*keychar++) >> 1;
82 kword[0] += (*keychar) >> 1;
84 schedule[0] = kword[0];
85 for (i = 1; i < ROUNDS; i++) {
87 temp = kword[0] & ((1 << 11) - 1); /* get 11 lsb */
89 (kword[0] >> 11) | ((kword[1] & ((1 << 11) - 1)) << (32 - 11));
90 kword[1] = (kword[1] >> 11) | (temp << (56 - 32 - 11));
91 schedule[i] = kword[0];
93 INC_RXKAD_STATS(fc_key_scheds);
97 /* IN int encrypt; * 0 ==> decrypt, else encrypt */
99 fc_ecb_encrypt(void * clear, void * cipher,
100 const fc_KeySchedule schedule, int encrypt)
103 volatile afs_uint32 S, P;
104 volatile unsigned char *Pchar = (unsigned char *)&P;
105 volatile unsigned char *Schar = (unsigned char *)&S;
108 #ifndef WORDS_BIGENDIAN
120 L = ntohl(*((afs_uint32 *)clear));
121 R = ntohl(*((afs_uint32 *)clear + 1));
124 INC_RXKAD_STATS(fc_encrypts[ENCRYPT]);
125 for (i = 0; i < (ROUNDS / 2); i++) {
126 S = *schedule++ ^ R; /* xor R with key bits from schedule */
127 Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. */
128 Pchar[Byte3] = sbox1[Schar[Byte1]]; /* and permute the result */
129 Pchar[Byte1] = sbox2[Schar[Byte2]];
130 Pchar[Byte0] = sbox3[Schar[Byte3]];
131 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
132 L ^= P; /* we're done with L, so save there */
133 S = *schedule++ ^ L; /* this time xor with L */
134 Pchar[Byte2] = sbox0[Schar[Byte0]];
135 Pchar[Byte3] = sbox1[Schar[Byte1]];
136 Pchar[Byte1] = sbox2[Schar[Byte2]];
137 Pchar[Byte0] = sbox3[Schar[Byte3]];
138 P = (P >> 5) | ((P & ((1 << 5) - 1)) << (32 - 5)); /* right rot 5 bits */
142 INC_RXKAD_STATS(fc_encrypts[DECRYPT]);
143 schedule = &schedule[ROUNDS - 1]; /* start at end of key schedule */
144 for (i = 0; i < (ROUNDS / 2); i++) {
145 S = *schedule-- ^ L; /* xor R with key bits from schedule */
146 Pchar[Byte2] = sbox0[Schar[Byte0]]; /* do 8-bit S Box subst. and */
147 Pchar[Byte3] = sbox1[Schar[Byte1]]; /* 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 R ^= P; /* we're done with L, so save there */
152 S = *schedule-- ^ R; /* 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 */
161 *((afs_int32 *)cipher) = htonl(L);
162 *((afs_int32 *)cipher + 1) = htonl(R);
166 /* Crypting can be done in segments by recycling xor. All but the final segment must
167 * be multiples of 8 bytes.
168 * NOTE: fc_cbc_encrypt now modifies its 5th argument, to permit chaining over
169 * scatter/gather vectors.
172 afs_int32 length; * in bytes *
173 int encrypt; * 0 ==> decrypt, else encrypt *
174 fc_KeySchedule key; * precomputed key schedule *
175 afs_uint32 *xor; * 8 bytes of initialization vector *
178 fc_cbc_encrypt(void *input, void *output, afs_int32 length,
179 const fc_KeySchedule key, afs_uint32 * xor, int encrypt)
182 afs_uint32 t_input[2];
183 afs_uint32 t_output[2];
184 unsigned char *t_in_p = (unsigned char *)t_input;
187 for (i = 0; length > 0; i++, length -= 8) {
189 memcpy(t_input, input, sizeof(t_input));
190 input=((char *)input) + sizeof(t_input);
193 for (j = length; j <= 7; j++)
196 /* do the xor for cbc into the temp */
197 xor[0] ^= t_input[0];
198 xor[1] ^= t_input[1];
200 fc_ecb_encrypt(xor, t_output, key, encrypt);
202 /* copy temp output and save it for cbc */
203 memcpy(output, t_output, sizeof(t_output));
204 output=(char *)output + sizeof(t_output);
206 /* calculate xor value for next round from plain & cipher text */
207 xor[0] = t_input[0] ^ t_output[0];
208 xor[1] = t_input[1] ^ t_output[1];
216 for (i = 0; length > 0; i++, length -= 8) {
218 memcpy(t_input, input, sizeof(t_input));
219 input=((char *)input) + sizeof(t_input);
221 /* no padding for decrypt */
222 fc_ecb_encrypt(t_input, t_output, key, encrypt);
224 /* do the xor for cbc into the output */
225 t_output[0] ^= xor[0];
226 t_output[1] ^= xor[1];
228 /* copy temp output */
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];