xref: /aosp_15_r20/external/boringssl/src/decrepit/cast/cast.c (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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 #include <openssl/cipher.h>
59 #include <openssl/obj.h>
60 
61 #if defined(OPENSSL_WINDOWS)
62 OPENSSL_MSVC_PRAGMA(warning(push, 3))
63 #include <intrin.h>
OPENSSL_MSVC_PRAGMA(warning (pop))64 OPENSSL_MSVC_PRAGMA(warning(pop))
65 #endif
66 
67 #include "../../crypto/fipsmodule/cipher/internal.h"
68 #include "../../crypto/internal.h"
69 #include "internal.h"
70 #include "../macros.h"
71 
72 
73 void CAST_ecb_encrypt(const uint8_t *in, uint8_t *out, const CAST_KEY *ks,
74                       int enc) {
75   uint32_t d[2];
76 
77   n2l(in, d[0]);
78   n2l(in, d[1]);
79   if (enc) {
80     CAST_encrypt(d, ks);
81   } else {
82     CAST_decrypt(d, ks);
83   }
84   l2n(d[0], out);
85   l2n(d[1], out);
86 }
87 
88 #define E_CAST(n, key, L, R, OP1, OP2, OP3)                                    \
89   {                                                                            \
90     uint32_t a, b, c, d;                                                       \
91     t = (key[n * 2] OP1 R) & 0xffffffff;                                       \
92     t = CRYPTO_rotl_u32(t, (key[n * 2 + 1]));                                  \
93     a = CAST_S_table0[(t >> 8) & 0xff];                                        \
94     b = CAST_S_table1[(t)&0xff];                                               \
95     c = CAST_S_table2[(t >> 24) & 0xff];                                       \
96     d = CAST_S_table3[(t >> 16) & 0xff];                                       \
97     L ^= (((((a OP2 b)&0xffffffffL)OP3 c) & 0xffffffffL) OP1 d) & 0xffffffffL; \
98   }
99 
CAST_encrypt(uint32_t * data,const CAST_KEY * key)100 void CAST_encrypt(uint32_t *data, const CAST_KEY *key) {
101   uint32_t l, r, t;
102   const uint32_t *k;
103 
104   k = &key->data[0];
105   l = data[0];
106   r = data[1];
107 
108   E_CAST(0, k, l, r, +, ^, -);
109   E_CAST(1, k, r, l, ^, -, +);
110   E_CAST(2, k, l, r, -, +, ^);
111   E_CAST(3, k, r, l, +, ^, -);
112   E_CAST(4, k, l, r, ^, -, +);
113   E_CAST(5, k, r, l, -, +, ^);
114   E_CAST(6, k, l, r, +, ^, -);
115   E_CAST(7, k, r, l, ^, -, +);
116   E_CAST(8, k, l, r, -, +, ^);
117   E_CAST(9, k, r, l, +, ^, -);
118   E_CAST(10, k, l, r, ^, -, +);
119   E_CAST(11, k, r, l, -, +, ^);
120 
121   if (!key->short_key) {
122     E_CAST(12, k, l, r, +, ^, -);
123     E_CAST(13, k, r, l, ^, -, +);
124     E_CAST(14, k, l, r, -, +, ^);
125     E_CAST(15, k, r, l, +, ^, -);
126   }
127 
128   data[1] = l & 0xffffffffL;
129   data[0] = r & 0xffffffffL;
130 }
131 
CAST_decrypt(uint32_t * data,const CAST_KEY * key)132 void CAST_decrypt(uint32_t *data, const CAST_KEY *key) {
133   uint32_t l, r, t;
134   const uint32_t *k;
135 
136   k = &key->data[0];
137   l = data[0];
138   r = data[1];
139 
140   if (!key->short_key) {
141     E_CAST(15, k, l, r, +, ^, -);
142     E_CAST(14, k, r, l, -, +, ^);
143     E_CAST(13, k, l, r, ^, -, +);
144     E_CAST(12, k, r, l, +, ^, -);
145   }
146 
147   E_CAST(11, k, l, r, -, +, ^);
148   E_CAST(10, k, r, l, ^, -, +);
149   E_CAST(9, k, l, r, +, ^, -);
150   E_CAST(8, k, r, l, -, +, ^);
151   E_CAST(7, k, l, r, ^, -, +);
152   E_CAST(6, k, r, l, +, ^, -);
153   E_CAST(5, k, l, r, -, +, ^);
154   E_CAST(4, k, r, l, ^, -, +);
155   E_CAST(3, k, l, r, +, ^, -);
156   E_CAST(2, k, r, l, -, +, ^);
157   E_CAST(1, k, l, r, ^, -, +);
158   E_CAST(0, k, r, l, +, ^, -);
159 
160   data[1] = l & 0xffffffffL;
161   data[0] = r & 0xffffffffL;
162 }
163 
CAST_cbc_encrypt(const uint8_t * in,uint8_t * out,size_t length,const CAST_KEY * ks,uint8_t * iv,int enc)164 void CAST_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
165                       const CAST_KEY *ks, uint8_t *iv, int enc) {
166   uint32_t tin0, tin1;
167   uint32_t tout0, tout1, xor0, xor1;
168   size_t l = length;
169   uint32_t tin[2];
170 
171   if (enc) {
172     n2l(iv, tout0);
173     n2l(iv, tout1);
174     iv -= 8;
175     while (l >= 8) {
176       n2l(in, tin0);
177       n2l(in, tin1);
178       tin0 ^= tout0;
179       tin1 ^= tout1;
180       tin[0] = tin0;
181       tin[1] = tin1;
182       CAST_encrypt(tin, ks);
183       tout0 = tin[0];
184       tout1 = tin[1];
185       l2n(tout0, out);
186       l2n(tout1, out);
187       l -= 8;
188     }
189     if (l != 0) {
190       n2ln(in, tin0, tin1, l);
191       tin0 ^= tout0;
192       tin1 ^= tout1;
193       tin[0] = tin0;
194       tin[1] = tin1;
195       CAST_encrypt(tin, ks);
196       tout0 = tin[0];
197       tout1 = tin[1];
198       l2n(tout0, out);
199       l2n(tout1, out);
200     }
201     l2n(tout0, iv);
202     l2n(tout1, iv);
203   } else {
204     n2l(iv, xor0);
205     n2l(iv, xor1);
206     iv -= 8;
207     while (l >= 8) {
208       n2l(in, tin0);
209       n2l(in, tin1);
210       tin[0] = tin0;
211       tin[1] = tin1;
212       CAST_decrypt(tin, ks);
213       tout0 = tin[0] ^ xor0;
214       tout1 = tin[1] ^ xor1;
215       l2n(tout0, out);
216       l2n(tout1, out);
217       xor0 = tin0;
218       xor1 = tin1;
219       l -= 8;
220     }
221     if (l != 0) {
222       n2l(in, tin0);
223       n2l(in, tin1);
224       tin[0] = tin0;
225       tin[1] = tin1;
226       CAST_decrypt(tin, ks);
227       tout0 = tin[0] ^ xor0;
228       tout1 = tin[1] ^ xor1;
229       l2nn(tout0, tout1, out, l);
230       xor0 = tin0;
231       xor1 = tin1;
232     }
233     l2n(xor0, iv);
234     l2n(xor1, iv);
235   }
236   tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
237   tin[0] = tin[1] = 0;
238 }
239 
240 #define CAST_exp(l, A, a, n)   \
241   A[n / 4] = l;                \
242   a[n + 3] = (l)&0xff;         \
243   a[n + 2] = (l >> 8) & 0xff;  \
244   a[n + 1] = (l >> 16) & 0xff; \
245   a[n + 0] = (l >> 24) & 0xff;
246 #define S4 CAST_S_table4
247 #define S5 CAST_S_table5
248 #define S6 CAST_S_table6
249 #define S7 CAST_S_table7
250 
CAST_set_key(CAST_KEY * key,size_t len,const uint8_t * data)251 void CAST_set_key(CAST_KEY *key, size_t len, const uint8_t *data) {
252   uint32_t x[16];
253   uint32_t z[16];
254   uint32_t k[32];
255   uint32_t X[4], Z[4];
256   uint32_t l, *K;
257   size_t i;
258 
259   for (i = 0; i < 16; i++) {
260     x[i] = 0;
261   }
262 
263   if (len > 16) {
264     len = 16;
265   }
266 
267   for (i = 0; i < len; i++) {
268     x[i] = data[i];
269   }
270 
271   if (len <= 10) {
272     key->short_key = 1;
273   } else {
274     key->short_key = 0;
275   }
276 
277   K = &k[0];
278   X[0] = ((x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]) & 0xffffffffL;
279   X[1] = ((x[4] << 24) | (x[5] << 16) | (x[6] << 8) | x[7]) & 0xffffffffL;
280   X[2] = ((x[8] << 24) | (x[9] << 16) | (x[10] << 8) | x[11]) & 0xffffffffL;
281   X[3] = ((x[12] << 24) | (x[13] << 16) | (x[14] << 8) | x[15]) & 0xffffffffL;
282 
283   for (;;) {
284     l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
285     CAST_exp(l, Z, z, 0);
286     l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
287     CAST_exp(l, Z, z, 4);
288     l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
289     CAST_exp(l, Z, z, 8);
290     l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
291     CAST_exp(l, Z, z, 12);
292 
293     K[0] = S4[z[8]] ^ S5[z[9]] ^ S6[z[7]] ^ S7[z[6]] ^ S4[z[2]];
294     K[1] = S4[z[10]] ^ S5[z[11]] ^ S6[z[5]] ^ S7[z[4]] ^ S5[z[6]];
295     K[2] = S4[z[12]] ^ S5[z[13]] ^ S6[z[3]] ^ S7[z[2]] ^ S6[z[9]];
296     K[3] = S4[z[14]] ^ S5[z[15]] ^ S6[z[1]] ^ S7[z[0]] ^ S7[z[12]];
297 
298     l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
299     CAST_exp(l, X, x, 0);
300     l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
301     CAST_exp(l, X, x, 4);
302     l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
303     CAST_exp(l, X, x, 8);
304     l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
305     CAST_exp(l, X, x, 12);
306 
307     K[4] = S4[x[3]] ^ S5[x[2]] ^ S6[x[12]] ^ S7[x[13]] ^ S4[x[8]];
308     K[5] = S4[x[1]] ^ S5[x[0]] ^ S6[x[14]] ^ S7[x[15]] ^ S5[x[13]];
309     K[6] = S4[x[7]] ^ S5[x[6]] ^ S6[x[8]] ^ S7[x[9]] ^ S6[x[3]];
310     K[7] = S4[x[5]] ^ S5[x[4]] ^ S6[x[10]] ^ S7[x[11]] ^ S7[x[7]];
311 
312     l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
313     CAST_exp(l, Z, z, 0);
314     l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
315     CAST_exp(l, Z, z, 4);
316     l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
317     CAST_exp(l, Z, z, 8);
318     l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
319     CAST_exp(l, Z, z, 12);
320 
321     K[8] = S4[z[3]] ^ S5[z[2]] ^ S6[z[12]] ^ S7[z[13]] ^ S4[z[9]];
322     K[9] = S4[z[1]] ^ S5[z[0]] ^ S6[z[14]] ^ S7[z[15]] ^ S5[z[12]];
323     K[10] = S4[z[7]] ^ S5[z[6]] ^ S6[z[8]] ^ S7[z[9]] ^ S6[z[2]];
324     K[11] = S4[z[5]] ^ S5[z[4]] ^ S6[z[10]] ^ S7[z[11]] ^ S7[z[6]];
325 
326     l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
327     CAST_exp(l, X, x, 0);
328     l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
329     CAST_exp(l, X, x, 4);
330     l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
331     CAST_exp(l, X, x, 8);
332     l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
333     CAST_exp(l, X, x, 12);
334 
335     K[12] = S4[x[8]] ^ S5[x[9]] ^ S6[x[7]] ^ S7[x[6]] ^ S4[x[3]];
336     K[13] = S4[x[10]] ^ S5[x[11]] ^ S6[x[5]] ^ S7[x[4]] ^ S5[x[7]];
337     K[14] = S4[x[12]] ^ S5[x[13]] ^ S6[x[3]] ^ S7[x[2]] ^ S6[x[8]];
338     K[15] = S4[x[14]] ^ S5[x[15]] ^ S6[x[1]] ^ S7[x[0]] ^ S7[x[13]];
339     if (K != k) {
340       break;
341     }
342     K += 16;
343   }
344 
345   for (i = 0; i < 16; i++) {
346     key->data[i * 2] = k[i];
347     key->data[i * 2 + 1] = ((k[i + 16]) + 16) & 0x1f;
348   }
349 }
350 
351 // The input and output encrypted as though 64bit cfb mode is being used. The
352 // extra state information to record how much of the 64bit block we have used
353 // is contained in *num.
CAST_cfb64_encrypt(const uint8_t * in,uint8_t * out,size_t length,const CAST_KEY * schedule,uint8_t * ivec,int * num,int enc)354 void CAST_cfb64_encrypt(const uint8_t *in, uint8_t *out, size_t length,
355                         const CAST_KEY *schedule, uint8_t *ivec, int *num,
356                         int enc) {
357   uint32_t v0, v1, t;
358   int n = *num;
359   size_t l = length;
360   uint32_t ti[2];
361   uint8_t *iv, c, cc;
362 
363   iv = ivec;
364   if (enc) {
365     while (l--) {
366       if (n == 0) {
367         n2l(iv, v0);
368         ti[0] = v0;
369         n2l(iv, v1);
370         ti[1] = v1;
371         CAST_encrypt((uint32_t *)ti, schedule);
372         iv = ivec;
373         t = ti[0];
374         l2n(t, iv);
375         t = ti[1];
376         l2n(t, iv);
377         iv = ivec;
378       }
379       c = *(in++) ^ iv[n];
380       *(out++) = c;
381       iv[n] = c;
382       n = (n + 1) & 0x07;
383     }
384   } else {
385     while (l--) {
386       if (n == 0) {
387         n2l(iv, v0);
388         ti[0] = v0;
389         n2l(iv, v1);
390         ti[1] = v1;
391         CAST_encrypt((uint32_t *)ti, schedule);
392         iv = ivec;
393         t = ti[0];
394         l2n(t, iv);
395         t = ti[1];
396         l2n(t, iv);
397         iv = ivec;
398       }
399       cc = *(in++);
400       c = iv[n];
401       iv[n] = cc;
402       *(out++) = c ^ cc;
403       n = (n + 1) & 0x07;
404     }
405   }
406   v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
407   *num = n;
408 }
409 
cast_init_key(EVP_CIPHER_CTX * ctx,const uint8_t * key,const uint8_t * iv,int enc)410 static int cast_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
411                          const uint8_t *iv, int enc) {
412   CAST_KEY *cast_key = ctx->cipher_data;
413   CAST_set_key(cast_key, ctx->key_len, key);
414   return 1;
415 }
416 
cast_ecb_cipher(EVP_CIPHER_CTX * ctx,uint8_t * out,const uint8_t * in,size_t len)417 static int cast_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
418                            size_t len) {
419   CAST_KEY *cast_key = ctx->cipher_data;
420 
421   while (len >= CAST_BLOCK) {
422     CAST_ecb_encrypt(in, out, cast_key, ctx->encrypt);
423     in += CAST_BLOCK;
424     out += CAST_BLOCK;
425     len -= CAST_BLOCK;
426   }
427   assert(len == 0);
428 
429   return 1;
430 }
431 
cast_cbc_cipher(EVP_CIPHER_CTX * ctx,uint8_t * out,const uint8_t * in,size_t len)432 static int cast_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
433                            size_t len) {
434   CAST_KEY *cast_key = ctx->cipher_data;
435   CAST_cbc_encrypt(in, out, len, cast_key, ctx->iv, ctx->encrypt);
436   return 1;
437 }
438 
439 static const EVP_CIPHER cast5_ecb = {
440     .nid = NID_cast5_ecb,
441     .block_size = CAST_BLOCK,
442     .key_len = CAST_KEY_LENGTH,
443     .iv_len = CAST_BLOCK,
444     .ctx_size = sizeof(CAST_KEY),
445     .flags = EVP_CIPH_ECB_MODE | EVP_CIPH_VARIABLE_LENGTH,
446     .init = cast_init_key,
447     .cipher = cast_ecb_cipher,
448 };
449 
450 static const EVP_CIPHER cast5_cbc = {
451     .nid = NID_cast5_cbc,
452     .block_size = CAST_BLOCK,
453     .key_len = CAST_KEY_LENGTH,
454     .iv_len = CAST_BLOCK,
455     .ctx_size = sizeof(CAST_KEY),
456     .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH,
457     .init = cast_init_key,
458     .cipher = cast_cbc_cipher,
459 };
460 
EVP_cast5_ecb(void)461 const EVP_CIPHER *EVP_cast5_ecb(void) { return &cast5_ecb; }
462 
EVP_cast5_cbc(void)463 const EVP_CIPHER *EVP_cast5_cbc(void) { return &cast5_cbc; }
464