1 /*
2 * Copyright 1995-2021 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 * RC2 low level APIs are deprecated for public use, but still ok for internal
12 * use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <openssl/rc2.h>
17 #include "rc2_local.h"
18
19 static const unsigned char key_table[256] = {
20 0xd9,
21 0x78,
22 0xf9,
23 0xc4,
24 0x19,
25 0xdd,
26 0xb5,
27 0xed,
28 0x28,
29 0xe9,
30 0xfd,
31 0x79,
32 0x4a,
33 0xa0,
34 0xd8,
35 0x9d,
36 0xc6,
37 0x7e,
38 0x37,
39 0x83,
40 0x2b,
41 0x76,
42 0x53,
43 0x8e,
44 0x62,
45 0x4c,
46 0x64,
47 0x88,
48 0x44,
49 0x8b,
50 0xfb,
51 0xa2,
52 0x17,
53 0x9a,
54 0x59,
55 0xf5,
56 0x87,
57 0xb3,
58 0x4f,
59 0x13,
60 0x61,
61 0x45,
62 0x6d,
63 0x8d,
64 0x09,
65 0x81,
66 0x7d,
67 0x32,
68 0xbd,
69 0x8f,
70 0x40,
71 0xeb,
72 0x86,
73 0xb7,
74 0x7b,
75 0x0b,
76 0xf0,
77 0x95,
78 0x21,
79 0x22,
80 0x5c,
81 0x6b,
82 0x4e,
83 0x82,
84 0x54,
85 0xd6,
86 0x65,
87 0x93,
88 0xce,
89 0x60,
90 0xb2,
91 0x1c,
92 0x73,
93 0x56,
94 0xc0,
95 0x14,
96 0xa7,
97 0x8c,
98 0xf1,
99 0xdc,
100 0x12,
101 0x75,
102 0xca,
103 0x1f,
104 0x3b,
105 0xbe,
106 0xe4,
107 0xd1,
108 0x42,
109 0x3d,
110 0xd4,
111 0x30,
112 0xa3,
113 0x3c,
114 0xb6,
115 0x26,
116 0x6f,
117 0xbf,
118 0x0e,
119 0xda,
120 0x46,
121 0x69,
122 0x07,
123 0x57,
124 0x27,
125 0xf2,
126 0x1d,
127 0x9b,
128 0xbc,
129 0x94,
130 0x43,
131 0x03,
132 0xf8,
133 0x11,
134 0xc7,
135 0xf6,
136 0x90,
137 0xef,
138 0x3e,
139 0xe7,
140 0x06,
141 0xc3,
142 0xd5,
143 0x2f,
144 0xc8,
145 0x66,
146 0x1e,
147 0xd7,
148 0x08,
149 0xe8,
150 0xea,
151 0xde,
152 0x80,
153 0x52,
154 0xee,
155 0xf7,
156 0x84,
157 0xaa,
158 0x72,
159 0xac,
160 0x35,
161 0x4d,
162 0x6a,
163 0x2a,
164 0x96,
165 0x1a,
166 0xd2,
167 0x71,
168 0x5a,
169 0x15,
170 0x49,
171 0x74,
172 0x4b,
173 0x9f,
174 0xd0,
175 0x5e,
176 0x04,
177 0x18,
178 0xa4,
179 0xec,
180 0xc2,
181 0xe0,
182 0x41,
183 0x6e,
184 0x0f,
185 0x51,
186 0xcb,
187 0xcc,
188 0x24,
189 0x91,
190 0xaf,
191 0x50,
192 0xa1,
193 0xf4,
194 0x70,
195 0x39,
196 0x99,
197 0x7c,
198 0x3a,
199 0x85,
200 0x23,
201 0xb8,
202 0xb4,
203 0x7a,
204 0xfc,
205 0x02,
206 0x36,
207 0x5b,
208 0x25,
209 0x55,
210 0x97,
211 0x31,
212 0x2d,
213 0x5d,
214 0xfa,
215 0x98,
216 0xe3,
217 0x8a,
218 0x92,
219 0xae,
220 0x05,
221 0xdf,
222 0x29,
223 0x10,
224 0x67,
225 0x6c,
226 0xba,
227 0xc9,
228 0xd3,
229 0x00,
230 0xe6,
231 0xcf,
232 0xe1,
233 0x9e,
234 0xa8,
235 0x2c,
236 0x63,
237 0x16,
238 0x01,
239 0x3f,
240 0x58,
241 0xe2,
242 0x89,
243 0xa9,
244 0x0d,
245 0x38,
246 0x34,
247 0x1b,
248 0xab,
249 0x33,
250 0xff,
251 0xb0,
252 0xbb,
253 0x48,
254 0x0c,
255 0x5f,
256 0xb9,
257 0xb1,
258 0xcd,
259 0x2e,
260 0xc5,
261 0xf3,
262 0xdb,
263 0x47,
264 0xe5,
265 0xa5,
266 0x9c,
267 0x77,
268 0x0a,
269 0xa6,
270 0x20,
271 0x68,
272 0xfe,
273 0x7f,
274 0xc1,
275 0xad,
276 };
277
278 #if defined(_MSC_VER) && defined(_ARM_)
279 #pragma optimize("g", off)
280 #endif
281
282 /*
283 * It has come to my attention that there are 2 versions of the RC2 key
284 * schedule. One which is normal, and another which has a hook to use a
285 * reduced key length. BSAFE uses the latter version. What I previously
286 * shipped is the same as specifying 1024 for the 'bits' parameter. Bsafe
287 * uses a version where the bits parameter is the same as len*8
288 */
RC2_set_key(RC2_KEY * key,int len,const unsigned char * data,int bits)289 void RC2_set_key(RC2_KEY *key, int len, const unsigned char *data, int bits)
290 {
291 int i, j;
292 unsigned char *k;
293 RC2_INT *ki;
294 unsigned int c, d;
295
296 k = (unsigned char *)&(key->data[0]);
297 *k = 0; /* for if there is a zero length key */
298
299 if (len > 128)
300 len = 128;
301 if (bits <= 0)
302 bits = 1024;
303 if (bits > 1024)
304 bits = 1024;
305
306 for (i = 0; i < len; i++)
307 k[i] = data[i];
308
309 /* expand table */
310 d = k[len - 1];
311 j = 0;
312 for (i = len; i < 128; i++, j++) {
313 d = key_table[(k[j] + d) & 0xff];
314 k[i] = d;
315 }
316
317 /* hmm.... key reduction to 'bits' bits */
318
319 j = (bits + 7) >> 3;
320 i = 128 - j;
321 c = (0xff >> (-bits & 0x07));
322
323 d = key_table[k[i] & c];
324 k[i] = d;
325 while (i--) {
326 d = key_table[k[i + j] ^ d];
327 k[i] = d;
328 }
329
330 /* copy from bytes into RC2_INT's */
331 ki = &(key->data[63]);
332 for (i = 127; i >= 0; i -= 2)
333 *(ki--) = ((k[i] << 8) | k[i - 1]) & 0xffff;
334 }
335
336 #if defined(_MSC_VER)
337 #pragma optimize("", on)
338 #endif
339