xref: /openssl/crypto/cast/c_enc.c (revision 33388b44)
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 /*
11  * CAST low level APIs are deprecated for public use, but still ok for
12  * internal use.
13  */
14 #include "internal/deprecated.h"
15 
16 #include <openssl/cast.h>
17 #include "cast_local.h"
18 
CAST_encrypt(CAST_LONG * data,const CAST_KEY * key)19 void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key)
20 {
21     CAST_LONG l, r, t;
22     const CAST_LONG *k;
23 
24     k = &(key->data[0]);
25     l = data[0];
26     r = data[1];
27 
28     E_CAST(0, k, l, r, +, ^, -);
29     E_CAST(1, k, r, l, ^, -, +);
30     E_CAST(2, k, l, r, -, +, ^);
31     E_CAST(3, k, r, l, +, ^, -);
32     E_CAST(4, k, l, r, ^, -, +);
33     E_CAST(5, k, r, l, -, +, ^);
34     E_CAST(6, k, l, r, +, ^, -);
35     E_CAST(7, k, r, l, ^, -, +);
36     E_CAST(8, k, l, r, -, +, ^);
37     E_CAST(9, k, r, l, +, ^, -);
38     E_CAST(10, k, l, r, ^, -, +);
39     E_CAST(11, k, r, l, -, +, ^);
40     if (!key->short_key) {
41         E_CAST(12, k, l, r, +, ^, -);
42         E_CAST(13, k, r, l, ^, -, +);
43         E_CAST(14, k, l, r, -, +, ^);
44         E_CAST(15, k, r, l, +, ^, -);
45     }
46 
47     data[1] = l & 0xffffffffL;
48     data[0] = r & 0xffffffffL;
49 }
50 
CAST_decrypt(CAST_LONG * data,const CAST_KEY * key)51 void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key)
52 {
53     CAST_LONG l, r, t;
54     const CAST_LONG *k;
55 
56     k = &(key->data[0]);
57     l = data[0];
58     r = data[1];
59 
60     if (!key->short_key) {
61         E_CAST(15, k, l, r, +, ^, -);
62         E_CAST(14, k, r, l, -, +, ^);
63         E_CAST(13, k, l, r, ^, -, +);
64         E_CAST(12, k, r, l, +, ^, -);
65     }
66     E_CAST(11, k, l, r, -, +, ^);
67     E_CAST(10, k, r, l, ^, -, +);
68     E_CAST(9, k, l, r, +, ^, -);
69     E_CAST(8, k, r, l, -, +, ^);
70     E_CAST(7, k, l, r, ^, -, +);
71     E_CAST(6, k, r, l, +, ^, -);
72     E_CAST(5, k, l, r, -, +, ^);
73     E_CAST(4, k, r, l, ^, -, +);
74     E_CAST(3, k, l, r, +, ^, -);
75     E_CAST(2, k, r, l, -, +, ^);
76     E_CAST(1, k, l, r, ^, -, +);
77     E_CAST(0, k, r, l, +, ^, -);
78 
79     data[1] = l & 0xffffffffL;
80     data[0] = r & 0xffffffffL;
81 }
82 
CAST_cbc_encrypt(const unsigned char * in,unsigned char * out,long length,const CAST_KEY * ks,unsigned char * iv,int enc)83 void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out,
84                       long length, const CAST_KEY *ks, unsigned char *iv,
85                       int enc)
86 {
87     register CAST_LONG tin0, tin1;
88     register CAST_LONG tout0, tout1, xor0, xor1;
89     register long l = length;
90     CAST_LONG tin[2];
91 
92     if (enc) {
93         n2l(iv, tout0);
94         n2l(iv, tout1);
95         iv -= 8;
96         for (l -= 8; l >= 0; l -= 8) {
97             n2l(in, tin0);
98             n2l(in, tin1);
99             tin0 ^= tout0;
100             tin1 ^= tout1;
101             tin[0] = tin0;
102             tin[1] = tin1;
103             CAST_encrypt(tin, ks);
104             tout0 = tin[0];
105             tout1 = tin[1];
106             l2n(tout0, out);
107             l2n(tout1, out);
108         }
109         if (l != -8) {
110             n2ln(in, tin0, tin1, l + 8);
111             tin0 ^= tout0;
112             tin1 ^= tout1;
113             tin[0] = tin0;
114             tin[1] = tin1;
115             CAST_encrypt(tin, ks);
116             tout0 = tin[0];
117             tout1 = tin[1];
118             l2n(tout0, out);
119             l2n(tout1, out);
120         }
121         l2n(tout0, iv);
122         l2n(tout1, iv);
123     } else {
124         n2l(iv, xor0);
125         n2l(iv, xor1);
126         iv -= 8;
127         for (l -= 8; l >= 0; l -= 8) {
128             n2l(in, tin0);
129             n2l(in, tin1);
130             tin[0] = tin0;
131             tin[1] = tin1;
132             CAST_decrypt(tin, ks);
133             tout0 = tin[0] ^ xor0;
134             tout1 = tin[1] ^ xor1;
135             l2n(tout0, out);
136             l2n(tout1, out);
137             xor0 = tin0;
138             xor1 = tin1;
139         }
140         if (l != -8) {
141             n2l(in, tin0);
142             n2l(in, tin1);
143             tin[0] = tin0;
144             tin[1] = tin1;
145             CAST_decrypt(tin, ks);
146             tout0 = tin[0] ^ xor0;
147             tout1 = tin[1] ^ xor1;
148             l2nn(tout0, tout1, out, l + 8);
149             xor0 = tin0;
150             xor1 = tin1;
151         }
152         l2n(xor0, iv);
153         l2n(xor1, iv);
154     }
155     tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
156     tin[0] = tin[1] = 0;
157 }
158