Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/decrepit/cast/cast.c

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected])
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/cipher.h>
5 #include <openssl/obj.h>
6
7 #if defined(OPENSSL_WINDOWS)
8 OPENSSL_MSVC_PRAGMA(warning(push, 3))
9 #include <intrin.h>
10 OPENSSL_MSVC_PRAGMA(warning(pop))
11 #endif
12
13 #include "../../../crypto/internal.h"
14 #include "../../fipsmodule/cipher/internal.h"
15 #include "../macros.h"
16 #include "internal.h"
17
18
19 void CAST_ecb_encrypt(const uint8_t *in, uint8_t *out, const CAST_KEY *ks,
20 int enc) {
21 uint32_t d[2];
22
23 n2l(in, d[0]);
24 n2l(in, d[1]);
25 if (enc) {
26 CAST_encrypt(d, ks);
27 } else {
28 CAST_decrypt(d, ks);
29 }
30 l2n(d[0], out);
31 l2n(d[1], out);
32 }
33
34 #if defined(OPENSSL_WINDOWS) && defined(_MSC_VER)
35 #define ROTL(a, n) (_lrotl(a, n))
36 #else
37 #define ROTL(a, n) ((((a) << (n)) | ((a) >> ((-(n))&31))) & 0xffffffffL)
38 #endif
39
40 #define E_CAST(n, key, L, R, OP1, OP2, OP3) \
41 { \
42 uint32_t a, b, c, d; \
43 t = (key[n * 2] OP1 R) & 0xffffffff; \
44 t = ROTL(t, (key[n * 2 + 1])); \
45 a = CAST_S_table0[(t >> 8) & 0xff]; \
46 b = CAST_S_table1[(t)&0xff]; \
47 c = CAST_S_table2[(t >> 24) & 0xff]; \
48 d = CAST_S_table3[(t >> 16) & 0xff]; \
49 L ^= (((((a OP2 b)&0xffffffffL)OP3 c) & 0xffffffffL)OP1 d) & 0xffffffffL; \
50 }
51
52 void CAST_encrypt(uint32_t *data, const CAST_KEY *key) {
53 uint32_t l, r, t;
54 const uint32_t *k;
55
56 k = &key->data[0];
57 l = data[0];
58 r = data[1];
59
60 E_CAST(0, k, l, r, +, ^, -);
61 E_CAST(1, k, r, l, ^, -, +);
62 E_CAST(2, k, l, r, -, +, ^);
63 E_CAST(3, k, r, l, +, ^, -);
64 E_CAST(4, k, l, r, ^, -, +);
65 E_CAST(5, k, r, l, -, +, ^);
66 E_CAST(6, k, l, r, +, ^, -);
67 E_CAST(7, k, r, l, ^, -, +);
68 E_CAST(8, k, l, r, -, +, ^);
69 E_CAST(9, k, r, l, +, ^, -);
70 E_CAST(10, k, l, r, ^, -, +);
71 E_CAST(11, k, r, l, -, +, ^);
72
73 if (!key->short_key) {
74 E_CAST(12, k, l, r, +, ^, -);
75 E_CAST(13, k, r, l, ^, -, +);
76 E_CAST(14, k, l, r, -, +, ^);
77 E_CAST(15, k, r, l, +, ^, -);
78 }
79
80 data[1] = l & 0xffffffffL;
81 data[0] = r & 0xffffffffL;
82 }
83
84 void CAST_decrypt(uint32_t *data, const CAST_KEY *key) {
85 uint32_t l, r, t;
86 const uint32_t *k;
87
88 k = &key->data[0];
89 l = data[0];
90 r = data[1];
91
92 if (!key->short_key) {
93 E_CAST(15, k, l, r, +, ^, -);
94 E_CAST(14, k, r, l, -, +, ^);
95 E_CAST(13, k, l, r, ^, -, +);
96 E_CAST(12, k, r, l, +, ^, -);
97 }
98
99 E_CAST(11, k, l, r, -, +, ^);
100 E_CAST(10, k, r, l, ^, -, +);
101 E_CAST(9, k, l, r, +, ^, -);
102 E_CAST(8, k, r, l, -, +, ^);
103 E_CAST(7, k, l, r, ^, -, +);
104 E_CAST(6, k, r, l, +, ^, -);
105 E_CAST(5, k, l, r, -, +, ^);
106 E_CAST(4, k, r, l, ^, -, +);
107 E_CAST(3, k, l, r, +, ^, -);
108 E_CAST(2, k, r, l, -, +, ^);
109 E_CAST(1, k, l, r, ^, -, +);
110 E_CAST(0, k, r, l, +, ^, -);
111
112 data[1] = l & 0xffffffffL;
113 data[0] = r & 0xffffffffL;
114 }
115
116 void CAST_cbc_encrypt(const uint8_t *in, uint8_t *out, size_t length,
117 const CAST_KEY *ks, uint8_t *iv, int enc) {
118 uint32_t tin0, tin1;
119 uint32_t tout0, tout1, xor0, xor1;
120 size_t l = length;
121 uint32_t tin[2];
122
123 if (enc) {
124 n2l(iv, tout0);
125 n2l(iv, tout1);
126 iv -= 8;
127 while (l >= 8) {
128 n2l(in, tin0);
129 n2l(in, tin1);
130 tin0 ^= tout0;
131 tin1 ^= tout1;
132 tin[0] = tin0;
133 tin[1] = tin1;
134 CAST_encrypt(tin, ks);
135 tout0 = tin[0];
136 tout1 = tin[1];
137 l2n(tout0, out);
138 l2n(tout1, out);
139 l -= 8;
140 }
141 if (l != 0) {
142 n2ln(in, tin0, tin1, l);
143 tin0 ^= tout0;
144 tin1 ^= tout1;
145 tin[0] = tin0;
146 tin[1] = tin1;
147 CAST_encrypt(tin, ks);
148 tout0 = tin[0];
149 tout1 = tin[1];
150 l2n(tout0, out);
151 l2n(tout1, out);
152 }
153 l2n(tout0, iv);
154 l2n(tout1, iv);
155 } else {
156 n2l(iv, xor0);
157 n2l(iv, xor1);
158 iv -= 8;
159 while (l >= 8) {
160 n2l(in, tin0);
161 n2l(in, tin1);
162 tin[0] = tin0;
163 tin[1] = tin1;
164 CAST_decrypt(tin, ks);
165 tout0 = tin[0] ^ xor0;
166 tout1 = tin[1] ^ xor1;
167 l2n(tout0, out);
168 l2n(tout1, out);
169 xor0 = tin0;
170 xor1 = tin1;
171 l -= 8;
172 }
173 if (l != 0) {
174 n2l(in, tin0);
175 n2l(in, tin1);
176 tin[0] = tin0;
177 tin[1] = tin1;
178 CAST_decrypt(tin, ks);
179 tout0 = tin[0] ^ xor0;
180 tout1 = tin[1] ^ xor1;
181 l2nn(tout0, tout1, out, l);
182 xor0 = tin0;
183 xor1 = tin1;
184 }
185 l2n(xor0, iv);
186 l2n(xor1, iv);
187 }
188 OPENSSL_cleanse(&tin0, sizeof(tin0));
189 OPENSSL_cleanse(&tin1, sizeof(tin1));
190 OPENSSL_cleanse(&tout0, sizeof(tout0));
191 OPENSSL_cleanse(&tout1, sizeof(tout1));
192 OPENSSL_cleanse(&xor0, sizeof(xor0));
193 OPENSSL_cleanse(&xor1, sizeof(xor1));
194 OPENSSL_cleanse(&tin, sizeof(tin));
195 }
196
197 #define CAST_exp(l, A, a, n) \
198 A[n / 4] = l; \
199 a[n + 3] = (l)&0xff; \
200 a[n + 2] = (l >> 8) & 0xff; \
201 a[n + 1] = (l >> 16) & 0xff; \
202 a[n + 0] = (l >> 24) & 0xff;
203 #define S4 CAST_S_table4
204 #define S5 CAST_S_table5
205 #define S6 CAST_S_table6
206 #define S7 CAST_S_table7
207
208 void CAST_set_key(CAST_KEY *key, size_t len, const uint8_t *data) {
209 uint32_t x[16];
210 uint32_t z[16];
211 uint32_t k[32];
212 uint32_t X[4], Z[4];
213 uint32_t l, *K;
214 size_t i;
215
216 for (i = 0; i < 16; i++) {
217 x[i] = 0;
218 }
219
220 if (len > 16) {
221 len = 16;
222 }
223
224 for (i = 0; i < len; i++) {
225 x[i] = data[i];
226 }
227
228 if (len <= 10) {
229 key->short_key = 1;
230 } else {
231 key->short_key = 0;
232 }
233
234 K = &k[0];
235 X[0] = ((x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3]) & 0xffffffffL;
236 X[1] = ((x[4] << 24) | (x[5] << 16) | (x[6] << 8) | x[7]) & 0xffffffffL;
237 X[2] = ((x[8] << 24) | (x[9] << 16) | (x[10] << 8) | x[11]) & 0xffffffffL;
238 X[3] = ((x[12] << 24) | (x[13] << 16) | (x[14] << 8) | x[15]) & 0xffffffffL;
239
240 for (;;) {
241 l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
242 CAST_exp(l, Z, z, 0);
243 l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
244 CAST_exp(l, Z, z, 4);
245 l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
246 CAST_exp(l, Z, z, 8);
247 l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
248 CAST_exp(l, Z, z, 12);
249
250 K[0] = S4[z[8]] ^ S5[z[9]] ^ S6[z[7]] ^ S7[z[6]] ^ S4[z[2]];
251 K[1] = S4[z[10]] ^ S5[z[11]] ^ S6[z[5]] ^ S7[z[4]] ^ S5[z[6]];
252 K[2] = S4[z[12]] ^ S5[z[13]] ^ S6[z[3]] ^ S7[z[2]] ^ S6[z[9]];
253 K[3] = S4[z[14]] ^ S5[z[15]] ^ S6[z[1]] ^ S7[z[0]] ^ S7[z[12]];
254
255 l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
256 CAST_exp(l, X, x, 0);
257 l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
258 CAST_exp(l, X, x, 4);
259 l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
260 CAST_exp(l, X, x, 8);
261 l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
262 CAST_exp(l, X, x, 12);
263
264 K[4] = S4[x[3]] ^ S5[x[2]] ^ S6[x[12]] ^ S7[x[13]] ^ S4[x[8]];
265 K[5] = S4[x[1]] ^ S5[x[0]] ^ S6[x[14]] ^ S7[x[15]] ^ S5[x[13]];
266 K[6] = S4[x[7]] ^ S5[x[6]] ^ S6[x[8]] ^ S7[x[9]] ^ S6[x[3]];
267 K[7] = S4[x[5]] ^ S5[x[4]] ^ S6[x[10]] ^ S7[x[11]] ^ S7[x[7]];
268
269 l = X[0] ^ S4[x[13]] ^ S5[x[15]] ^ S6[x[12]] ^ S7[x[14]] ^ S6[x[8]];
270 CAST_exp(l, Z, z, 0);
271 l = X[2] ^ S4[z[0]] ^ S5[z[2]] ^ S6[z[1]] ^ S7[z[3]] ^ S7[x[10]];
272 CAST_exp(l, Z, z, 4);
273 l = X[3] ^ S4[z[7]] ^ S5[z[6]] ^ S6[z[5]] ^ S7[z[4]] ^ S4[x[9]];
274 CAST_exp(l, Z, z, 8);
275 l = X[1] ^ S4[z[10]] ^ S5[z[9]] ^ S6[z[11]] ^ S7[z[8]] ^ S5[x[11]];
276 CAST_exp(l, Z, z, 12);
277
278 K[8] = S4[z[3]] ^ S5[z[2]] ^ S6[z[12]] ^ S7[z[13]] ^ S4[z[9]];
279 K[9] = S4[z[1]] ^ S5[z[0]] ^ S6[z[14]] ^ S7[z[15]] ^ S5[z[12]];
280 K[10] = S4[z[7]] ^ S5[z[6]] ^ S6[z[8]] ^ S7[z[9]] ^ S6[z[2]];
281 K[11] = S4[z[5]] ^ S5[z[4]] ^ S6[z[10]] ^ S7[z[11]] ^ S7[z[6]];
282
283 l = Z[2] ^ S4[z[5]] ^ S5[z[7]] ^ S6[z[4]] ^ S7[z[6]] ^ S6[z[0]];
284 CAST_exp(l, X, x, 0);
285 l = Z[0] ^ S4[x[0]] ^ S5[x[2]] ^ S6[x[1]] ^ S7[x[3]] ^ S7[z[2]];
286 CAST_exp(l, X, x, 4);
287 l = Z[1] ^ S4[x[7]] ^ S5[x[6]] ^ S6[x[5]] ^ S7[x[4]] ^ S4[z[1]];
288 CAST_exp(l, X, x, 8);
289 l = Z[3] ^ S4[x[10]] ^ S5[x[9]] ^ S6[x[11]] ^ S7[x[8]] ^ S5[z[3]];
290 CAST_exp(l, X, x, 12);
291
292 K[12] = S4[x[8]] ^ S5[x[9]] ^ S6[x[7]] ^ S7[x[6]] ^ S4[x[3]];
293 K[13] = S4[x[10]] ^ S5[x[11]] ^ S6[x[5]] ^ S7[x[4]] ^ S5[x[7]];
294 K[14] = S4[x[12]] ^ S5[x[13]] ^ S6[x[3]] ^ S7[x[2]] ^ S6[x[8]];
295 K[15] = S4[x[14]] ^ S5[x[15]] ^ S6[x[1]] ^ S7[x[0]] ^ S7[x[13]];
296 if (K != k) {
297 break;
298 }
299 K += 16;
300 }
301
302 for (i = 0; i < 16; i++) {
303 key->data[i * 2] = k[i];
304 key->data[i * 2 + 1] = ((k[i + 16]) + 16) & 0x1f;
305 }
306 }
307
308 static int cast_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
309 const uint8_t *iv, int enc) {
310 CAST_KEY *cast_key = ctx->cipher_data;
311 CAST_set_key(cast_key, ctx->key_len, key);
312 return 1;
313 }
314
315 static int cast_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
316 size_t len) {
317 CAST_KEY *cast_key = ctx->cipher_data;
318 assert(len % CAST_BLOCK == 0);
319
320 while (len >= CAST_BLOCK) {
321 CAST_ecb_encrypt(in, out, cast_key, ctx->encrypt);
322 in += CAST_BLOCK;
323 out += CAST_BLOCK;
324 len -= CAST_BLOCK;
325 }
326 assert(len == 0);
327
328 return 1;
329 }
330
331 static int cast_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
332 size_t len) {
333 CAST_KEY *cast_key = ctx->cipher_data;
334 CAST_cbc_encrypt(in, out, len, cast_key, ctx->iv, ctx->encrypt);
335 return 1;
336 }
337
338 static const EVP_CIPHER cast5_ecb = {
339 .nid = NID_cast5_ecb,
340 .block_size = CAST_BLOCK,
341 .key_len = CAST_KEY_LENGTH,
342 .iv_len = CAST_BLOCK,
343 .ctx_size = sizeof(CAST_KEY),
344 .flags = EVP_CIPH_ECB_MODE | EVP_CIPH_VARIABLE_LENGTH,
345 .init = cast_init_key,
346 .cipher = cast_ecb_cipher,
347 };
348
349 static const EVP_CIPHER cast5_cbc = {
350 .nid = NID_cast5_cbc,
351 .block_size = CAST_BLOCK,
352 .key_len = CAST_KEY_LENGTH,
353 .iv_len = CAST_BLOCK,
354 .ctx_size = sizeof(CAST_KEY),
355 .flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH,
356 .init = cast_init_key,
357 .cipher = cast_cbc_cipher,
358 };
359
360 const EVP_CIPHER *EVP_cast5_ecb(void) { return &cast5_ecb; }
361
362 const EVP_CIPHER *EVP_cast5_cbc(void) { return &cast5_cbc; }