1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]. */
56 
57 #include <openssl/cast.h>
58 
59 #if defined(OPENSSL_WINDOWS)
60 #pragma warning(push, 3)
61 #include <intrin.h>
62 #pragma warning(pop)
63 #endif
64 
65 #include "../macros.h"
66 
67 
CAST_ecb_encrypt(const uint8_t * in,uint8_t * out,const CAST_KEY * ks,int enc)68 void CAST_ecb_encrypt(const uint8_t *in, uint8_t *out, const CAST_KEY *ks,
69                       int enc) {
70   uint32_t d[2];
71 
72   n2l(in, d[0]);
73   n2l(in, d[1]);
74   if (enc) {
75     CAST_encrypt(d, ks);
76   } else {
77     CAST_decrypt(d, ks);
78   }
79   l2n(d[0], out);
80   l2n(d[1], out);
81 }
82 
83 extern const uint32_t CAST_S_table0[256];
84 extern const uint32_t CAST_S_table1[256];
85 extern const uint32_t CAST_S_table2[256];
86 extern const uint32_t CAST_S_table3[256];
87 extern const uint32_t CAST_S_table4[256];
88 extern const uint32_t CAST_S_table5[256];
89 extern const uint32_t CAST_S_table6[256];
90 extern const uint32_t CAST_S_table7[256];
91 
92 #if defined(OPENSSL_WINDOWS) && defined(_MSC_VER)
93 #define ROTL(a, n) (_lrotl(a, n))
94 #else
95 #define ROTL(a, n) ((((a) << (n)) | ((a) >> ((-(n))&31))) & 0xffffffffL)
96 #endif
97 
98 #define E_CAST(n, key, L, R, OP1, OP2, OP3)                                   \
99   {                                                                           \
100     uint32_t a, b, c, d;                                                      \
101     t = (key[n * 2] OP1 R) & 0xffffffff;                                      \
102     t = ROTL(t, (key[n * 2 + 1]));                                            \
103     a = CAST_S_table0[(t >> 8) & 0xff];                                       \
104     b = CAST_S_table1[(t)&0xff];                                              \
105     c = CAST_S_table2[(t >> 24) & 0xff];                                      \
106     d = CAST_S_table3[(t >> 16) & 0xff];                                      \
107     L ^= (((((a OP2 b)&0xffffffffL)OP3 c) & 0xffffffffL)OP1 d) & 0xffffffffL; \
108   }
109 
CAST_encrypt(uint32_t * data,const CAST_KEY * key)110 void CAST_encrypt(uint32_t *data, const CAST_KEY *key) {
111   uint32_t l, r, t;
112   const uint32_t *k;
113 
114   k = &key->data[0];
115   l = data[0];
116   r = data[1];
117 
118   E_CAST(0, k, l, r, +, ^, -);
119   E_CAST(1, k, r, l, ^, -, +);
120   E_CAST(2, k, l, r, -, +, ^);
121   E_CAST(3, k, r, l, +, ^, -);
122   E_CAST(4, k, l, r, ^, -, +);
123   E_CAST(5, k, r, l, -, +, ^);
124   E_CAST(6, k, l, r, +, ^, -);
125   E_CAST(7, k, r, l, ^, -, +);
126   E_CAST(8, k, l, r, -, +, ^);
127   E_CAST(9, k, r, l, +, ^, -);
128   E_CAST(10, k, l, r, ^, -, +);
129   E_CAST(11, k, r, l, -, +, ^);
130 
131   if (!key->short_key) {
132     E_CAST(12, k, l, r, +, ^, -);
133     E_CAST(13, k, r, l, ^, -, +);
134     E_CAST(14, k, l, r, -, +, ^);
135     E_CAST(15, k, r, l, +, ^, -);
136   }
137 
138   data[1] = l & 0xffffffffL;
139   data[0] = r & 0xffffffffL;
140 }
141 
CAST_decrypt(uint32_t * data,const CAST_KEY * key)142 void CAST_decrypt(uint32_t *data, const CAST_KEY *key) {
143   uint32_t l, r, t;
144   const uint32_t *k;
145 
146   k = &key->data[0];
147   l = data[0];
148   r = data[1];
149 
150   if (!key->short_key) {
151     E_CAST(15, k, l, r, +, ^, -);
152     E_CAST(14, k, r, l, -, +, ^);
153     E_CAST(13, k, l, r, ^, -, +);
154     E_CAST(12, k, r, l, +, ^, -);
155   }
156 
157   E_CAST(11, k, l, r, -, +, ^);
158   E_CAST(10, k, r, l, ^, -, +);
159   E_CAST(9, k, l, r, +, ^, -);
160   E_CAST(8, k, r, l, -, +, ^);
161   E_CAST(7, k, l, r, ^, -, +);
162   E_CAST(6, k, r, l, +, ^, -);
163   E_CAST(5, k, l, r, -, +, ^);
164   E_CAST(4, k, r, l, ^, -, +);
165   E_CAST(3, k, l, r, +, ^, -);
166   E_CAST(2, k, r, l, -, +, ^);
167   E_CAST(1, k, l, r, ^, -, +);
168   E_CAST(0, k, r, l, +, ^, -);
169 
170   data[1] = l & 0xffffffffL;
171   data[0] = r & 0xffffffffL;
172 }
173 
CAST_cbc_encrypt(const uint8_t * in,uint8_t * out,long length,const CAST_KEY * ks,uint8_t * iv,int enc)174 void CAST_cbc_encrypt(const uint8_t *in, uint8_t *out, long length,
175                       const CAST_KEY *ks, uint8_t *iv, int enc) {
176   uint32_t tin0, tin1;
177   uint32_t tout0, tout1, xor0, xor1;
178   long l = length;
179   uint32_t tin[2];
180 
181   if (enc) {
182     n2l(iv, tout0);
183     n2l(iv, tout1);
184     iv -= 8;
185     for (l -= 8; l >= 0; l -= 8) {
186       n2l(in, tin0);
187       n2l(in, tin1);
188       tin0 ^= tout0;
189       tin1 ^= tout1;
190       tin[0] = tin0;
191       tin[1] = tin1;
192       CAST_encrypt(tin, ks);
193       tout0 = tin[0];
194       tout1 = tin[1];
195       l2n(tout0, out);
196       l2n(tout1, out);
197     }
198     if (l != -8) {
199       n2ln(in, tin0, tin1, l + 8);
200       tin0 ^= tout0;
201       tin1 ^= tout1;
202       tin[0] = tin0;
203       tin[1] = tin1;
204       CAST_encrypt(tin, ks);
205       tout0 = tin[0];
206       tout1 = tin[1];
207       l2n(tout0, out);
208       l2n(tout1, out);
209     }
210     l2n(tout0, iv);
211     l2n(tout1, iv);
212   } else {
213     n2l(iv, xor0);
214     n2l(iv, xor1);
215     iv -= 8;
216     for (l -= 8; l >= 0; l -= 8) {
217       n2l(in, tin0);
218       n2l(in, tin1);
219       tin[0] = tin0;
220       tin[1] = tin1;
221       CAST_decrypt(tin, ks);
222       tout0 = tin[0] ^ xor0;
223       tout1 = tin[1] ^ xor1;
224       l2n(tout0, out);
225       l2n(tout1, out);
226       xor0 = tin0;
227       xor1 = tin1;
228     }
229     if (l != -8) {
230       n2l(in, tin0);
231       n2l(in, tin1);
232       tin[0] = tin0;
233       tin[1] = tin1;
234       CAST_decrypt(tin, ks);
235       tout0 = tin[0] ^ xor0;
236       tout1 = tin[1] ^ xor1;
237       l2nn(tout0, tout1, out, l + 8);
238       xor0 = tin0;
239       xor1 = tin1;
240     }
241     l2n(xor0, iv);
242     l2n(xor1, iv);
243   }
244   tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
245   tin[0] = tin[1] = 0;
246 }
247 
248 #define CAST_exp(l, A, a, n)   \
249   A[n / 4] = l;                \
250   a[n + 3] = (l)&0xff;         \
251   a[n + 2] = (l >> 8) & 0xff;  \
252   a[n + 1] = (l >> 16) & 0xff; \
253   a[n + 0] = (l >> 24) & 0xff;
254 #define S4 CAST_S_table4
255 #define S5 CAST_S_table5
256 #define S6 CAST_S_table6
257 #define S7 CAST_S_table7
258 
CAST_set_key(CAST_KEY * key,size_t len,const uint8_t * data)259 void CAST_set_key(CAST_KEY *key, size_t len, const uint8_t *data) {
260   uint32_t x[16];
261   uint32_t z[16];
262   uint32_t k[32];
263   uint32_t X[4], Z[4];
264   uint32_t l, *K;
265   size_t i;
266 
267   for (i = 0; i < 16; i++) {
268     x[i] = 0;
269   }
270 
271   if (len > 16) {
272     len = 16;
273   }
274 
275   for (i = 0; i < len; i++) {
276     x[i] = data[i];
277   }
278 
279   if (len <= 10) {
280     key->short_key = 1;
281   } else {
282     key->short_key = 0;
283   }
284 
285   K = &k[0];
286   X[0] = ((x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]) & 0xffffffffL;
287   X[1] = ((x[4] << 24) | (x[5] << 16) | (x[6] << 8) | x[7]) & 0xffffffffL;
288   X[2] = ((x[8] << 24) | (x[9] << 16) | (x[10] << 8) | x[11]) & 0xffffffffL;
289   X[3] = ((x[12] << 24) | (x[13] << 16) | (x[14] << 8) | x[15]) & 0xffffffffL;
290 
291   for (;;) {
292     l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
293     CAST_exp(l, Z, z, 0);
294     l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
295     CAST_exp(l, Z, z, 4);
296     l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
297     CAST_exp(l, Z, z, 8);
298     l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
299     CAST_exp(l, Z, z, 12);
300 
301     K[0] = S4[z[8]] ^ S5[z[9]] ^ S6[z[7]] ^ S7[z[6]] ^ S4[z[2]];
302     K[1] = S4[z[10]] ^ S5[z[11]] ^ S6[z[5]] ^ S7[z[4]] ^ S5[z[6]];
303     K[2] = S4[z[12]] ^ S5[z[13]] ^ S6[z[3]] ^ S7[z[2]] ^ S6[z[9]];
304     K[3] = S4[z[14]] ^ S5[z[15]] ^ S6[z[1]] ^ S7[z[0]] ^ S7[z[12]];
305 
306     l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
307     CAST_exp(l, X, x, 0);
308     l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
309     CAST_exp(l, X, x, 4);
310     l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
311     CAST_exp(l, X, x, 8);
312     l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
313     CAST_exp(l, X, x, 12);
314 
315     K[4] = S4[x[3]] ^ S5[x[2]] ^ S6[x[12]] ^ S7[x[13]] ^ S4[x[8]];
316     K[5] = S4[x[1]] ^ S5[x[0]] ^ S6[x[14]] ^ S7[x[15]] ^ S5[x[13]];
317     K[6] = S4[x[7]] ^ S5[x[6]] ^ S6[x[8]] ^ S7[x[9]] ^ S6[x[3]];
318     K[7] = S4[x[5]] ^ S5[x[4]] ^ S6[x[10]] ^ S7[x[11]] ^ S7[x[7]];
319 
320     l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
321     CAST_exp(l, Z, z, 0);
322     l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
323     CAST_exp(l, Z, z, 4);
324     l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
325     CAST_exp(l, Z, z, 8);
326     l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
327     CAST_exp(l, Z, z, 12);
328 
329     K[8] = S4[z[3]] ^ S5[z[2]] ^ S6[z[12]] ^ S7[z[13]] ^ S4[z[9]];
330     K[9] = S4[z[1]] ^ S5[z[0]] ^ S6[z[14]] ^ S7[z[15]] ^ S5[z[12]];
331     K[10] = S4[z[7]] ^ S5[z[6]] ^ S6[z[8]] ^ S7[z[9]] ^ S6[z[2]];
332     K[11] = S4[z[5]] ^ S5[z[4]] ^ S6[z[10]] ^ S7[z[11]] ^ S7[z[6]];
333 
334     l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
335     CAST_exp(l, X, x, 0);
336     l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
337     CAST_exp(l, X, x, 4);
338     l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
339     CAST_exp(l, X, x, 8);
340     l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
341     CAST_exp(l, X, x, 12);
342 
343     K[12] = S4[x[8]] ^ S5[x[9]] ^ S6[x[7]] ^ S7[x[6]] ^ S4[x[3]];
344     K[13] = S4[x[10]] ^ S5[x[11]] ^ S6[x[5]] ^ S7[x[4]] ^ S5[x[7]];
345     K[14] = S4[x[12]] ^ S5[x[13]] ^ S6[x[3]] ^ S7[x[2]] ^ S6[x[8]];
346     K[15] = S4[x[14]] ^ S5[x[15]] ^ S6[x[1]] ^ S7[x[0]] ^ S7[x[13]];
347     if (K != k) {
348       break;
349     }
350     K += 16;
351   }
352 
353   for (i = 0; i < 16; i++) {
354     key->data[i * 2] = k[i];
355     key->data[i * 2 + 1] = ((k[i + 16]) + 16) & 0x1f;
356   }
357 }
358 
359 /* The input and output encrypted as though 64bit cfb mode is being used. The
360  * extra state information to record how much of the 64bit block we have used
361  * is contained in *num. */
CAST_cfb64_encrypt(const uint8_t * in,uint8_t * out,long length,const CAST_KEY * schedule,uint8_t * ivec,int * num,int enc)362 void CAST_cfb64_encrypt(const uint8_t *in, uint8_t *out, long length,
363                         const CAST_KEY *schedule, uint8_t *ivec, int *num,
364                         int enc) {
365   uint32_t v0, v1, t;
366   int n = *num;
367   long l = length;
368   uint32_t ti[2];
369   uint8_t *iv, c, cc;
370 
371   iv = ivec;
372   if (enc) {
373     while (l--) {
374       if (n == 0) {
375         n2l(iv, v0);
376         ti[0] = v0;
377         n2l(iv, v1);
378         ti[1] = v1;
379         CAST_encrypt((uint32_t *)ti, schedule);
380         iv = ivec;
381         t = ti[0];
382         l2n(t, iv);
383         t = ti[1];
384         l2n(t, iv);
385         iv = ivec;
386       }
387       c = *(in++) ^ iv[n];
388       *(out++) = c;
389       iv[n] = c;
390       n = (n + 1) & 0x07;
391     }
392   } else {
393     while (l--) {
394       if (n == 0) {
395         n2l(iv, v0);
396         ti[0] = v0;
397         n2l(iv, v1);
398         ti[1] = v1;
399         CAST_encrypt((uint32_t *)ti, schedule);
400         iv = ivec;
401         t = ti[0];
402         l2n(t, iv);
403         t = ti[1];
404         l2n(t, iv);
405         iv = ivec;
406       }
407       cc = *(in++);
408       c = iv[n];
409       iv[n] = cc;
410       *(out++) = c ^ cc;
411       n = (n + 1) & 0x07;
412     }
413   }
414   v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
415   *num = n;
416 }
417