/src/openssl/crypto/des/des_enc.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 | | #include <openssl/crypto.h> |
11 | | #include "des_locl.h" |
12 | | #include "spr.h" |
13 | | |
14 | | void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) |
15 | 0 | { |
16 | 0 | register DES_LONG l, r, t, u; |
17 | 0 | register DES_LONG *s; |
18 | 0 |
|
19 | 0 | r = data[0]; |
20 | 0 | l = data[1]; |
21 | 0 |
|
22 | 0 | IP(r, l); |
23 | 0 | /* |
24 | 0 | * Things have been modified so that the initial rotate is done outside |
25 | 0 | * the loop. This required the DES_SPtrans values in sp.h to be rotated |
26 | 0 | * 1 bit to the right. One perl script later and things have a 5% speed |
27 | 0 | * up on a sparc2. Thanks to Richard Outerbridge for pointing this out. |
28 | 0 | */ |
29 | 0 | /* clear the top bits on machines with 8byte longs */ |
30 | 0 | /* shift left by 2 */ |
31 | 0 | r = ROTATE(r, 29) & 0xffffffffL; |
32 | 0 | l = ROTATE(l, 29) & 0xffffffffL; |
33 | 0 |
|
34 | 0 | s = ks->ks->deslong; |
35 | 0 | /* |
36 | 0 | * I don't know if it is worth the effort of loop unrolling the inner |
37 | 0 | * loop |
38 | 0 | */ |
39 | 0 | if (enc) { |
40 | 0 | D_ENCRYPT(l, r, 0); /* 1 */ |
41 | 0 | D_ENCRYPT(r, l, 2); /* 2 */ |
42 | 0 | D_ENCRYPT(l, r, 4); /* 3 */ |
43 | 0 | D_ENCRYPT(r, l, 6); /* 4 */ |
44 | 0 | D_ENCRYPT(l, r, 8); /* 5 */ |
45 | 0 | D_ENCRYPT(r, l, 10); /* 6 */ |
46 | 0 | D_ENCRYPT(l, r, 12); /* 7 */ |
47 | 0 | D_ENCRYPT(r, l, 14); /* 8 */ |
48 | 0 | D_ENCRYPT(l, r, 16); /* 9 */ |
49 | 0 | D_ENCRYPT(r, l, 18); /* 10 */ |
50 | 0 | D_ENCRYPT(l, r, 20); /* 11 */ |
51 | 0 | D_ENCRYPT(r, l, 22); /* 12 */ |
52 | 0 | D_ENCRYPT(l, r, 24); /* 13 */ |
53 | 0 | D_ENCRYPT(r, l, 26); /* 14 */ |
54 | 0 | D_ENCRYPT(l, r, 28); /* 15 */ |
55 | 0 | D_ENCRYPT(r, l, 30); /* 16 */ |
56 | 0 | } else { |
57 | 0 | D_ENCRYPT(l, r, 30); /* 16 */ |
58 | 0 | D_ENCRYPT(r, l, 28); /* 15 */ |
59 | 0 | D_ENCRYPT(l, r, 26); /* 14 */ |
60 | 0 | D_ENCRYPT(r, l, 24); /* 13 */ |
61 | 0 | D_ENCRYPT(l, r, 22); /* 12 */ |
62 | 0 | D_ENCRYPT(r, l, 20); /* 11 */ |
63 | 0 | D_ENCRYPT(l, r, 18); /* 10 */ |
64 | 0 | D_ENCRYPT(r, l, 16); /* 9 */ |
65 | 0 | D_ENCRYPT(l, r, 14); /* 8 */ |
66 | 0 | D_ENCRYPT(r, l, 12); /* 7 */ |
67 | 0 | D_ENCRYPT(l, r, 10); /* 6 */ |
68 | 0 | D_ENCRYPT(r, l, 8); /* 5 */ |
69 | 0 | D_ENCRYPT(l, r, 6); /* 4 */ |
70 | 0 | D_ENCRYPT(r, l, 4); /* 3 */ |
71 | 0 | D_ENCRYPT(l, r, 2); /* 2 */ |
72 | 0 | D_ENCRYPT(r, l, 0); /* 1 */ |
73 | 0 | } |
74 | 0 |
|
75 | 0 | /* rotate and clear the top bits on machines with 8byte longs */ |
76 | 0 | l = ROTATE(l, 3) & 0xffffffffL; |
77 | 0 | r = ROTATE(r, 3) & 0xffffffffL; |
78 | 0 |
|
79 | 0 | FP(r, l); |
80 | 0 | data[0] = l; |
81 | 0 | data[1] = r; |
82 | 0 | l = r = t = u = 0; |
83 | 0 | } |
84 | | |
85 | | void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc) |
86 | 0 | { |
87 | 0 | register DES_LONG l, r, t, u; |
88 | 0 | register DES_LONG *s; |
89 | 0 |
|
90 | 0 | r = data[0]; |
91 | 0 | l = data[1]; |
92 | 0 |
|
93 | 0 | /* |
94 | 0 | * Things have been modified so that the initial rotate is done outside |
95 | 0 | * the loop. This required the DES_SPtrans values in sp.h to be rotated |
96 | 0 | * 1 bit to the right. One perl script later and things have a 5% speed |
97 | 0 | * up on a sparc2. Thanks to Richard Outerbridge for pointing this out. |
98 | 0 | */ |
99 | 0 | /* clear the top bits on machines with 8byte longs */ |
100 | 0 | r = ROTATE(r, 29) & 0xffffffffL; |
101 | 0 | l = ROTATE(l, 29) & 0xffffffffL; |
102 | 0 |
|
103 | 0 | s = ks->ks->deslong; |
104 | 0 | /* |
105 | 0 | * I don't know if it is worth the effort of loop unrolling the inner |
106 | 0 | * loop |
107 | 0 | */ |
108 | 0 | if (enc) { |
109 | 0 | D_ENCRYPT(l, r, 0); /* 1 */ |
110 | 0 | D_ENCRYPT(r, l, 2); /* 2 */ |
111 | 0 | D_ENCRYPT(l, r, 4); /* 3 */ |
112 | 0 | D_ENCRYPT(r, l, 6); /* 4 */ |
113 | 0 | D_ENCRYPT(l, r, 8); /* 5 */ |
114 | 0 | D_ENCRYPT(r, l, 10); /* 6 */ |
115 | 0 | D_ENCRYPT(l, r, 12); /* 7 */ |
116 | 0 | D_ENCRYPT(r, l, 14); /* 8 */ |
117 | 0 | D_ENCRYPT(l, r, 16); /* 9 */ |
118 | 0 | D_ENCRYPT(r, l, 18); /* 10 */ |
119 | 0 | D_ENCRYPT(l, r, 20); /* 11 */ |
120 | 0 | D_ENCRYPT(r, l, 22); /* 12 */ |
121 | 0 | D_ENCRYPT(l, r, 24); /* 13 */ |
122 | 0 | D_ENCRYPT(r, l, 26); /* 14 */ |
123 | 0 | D_ENCRYPT(l, r, 28); /* 15 */ |
124 | 0 | D_ENCRYPT(r, l, 30); /* 16 */ |
125 | 0 | } else { |
126 | 0 | D_ENCRYPT(l, r, 30); /* 16 */ |
127 | 0 | D_ENCRYPT(r, l, 28); /* 15 */ |
128 | 0 | D_ENCRYPT(l, r, 26); /* 14 */ |
129 | 0 | D_ENCRYPT(r, l, 24); /* 13 */ |
130 | 0 | D_ENCRYPT(l, r, 22); /* 12 */ |
131 | 0 | D_ENCRYPT(r, l, 20); /* 11 */ |
132 | 0 | D_ENCRYPT(l, r, 18); /* 10 */ |
133 | 0 | D_ENCRYPT(r, l, 16); /* 9 */ |
134 | 0 | D_ENCRYPT(l, r, 14); /* 8 */ |
135 | 0 | D_ENCRYPT(r, l, 12); /* 7 */ |
136 | 0 | D_ENCRYPT(l, r, 10); /* 6 */ |
137 | 0 | D_ENCRYPT(r, l, 8); /* 5 */ |
138 | 0 | D_ENCRYPT(l, r, 6); /* 4 */ |
139 | 0 | D_ENCRYPT(r, l, 4); /* 3 */ |
140 | 0 | D_ENCRYPT(l, r, 2); /* 2 */ |
141 | 0 | D_ENCRYPT(r, l, 0); /* 1 */ |
142 | 0 | } |
143 | 0 | /* rotate and clear the top bits on machines with 8byte longs */ |
144 | 0 | data[0] = ROTATE(l, 3) & 0xffffffffL; |
145 | 0 | data[1] = ROTATE(r, 3) & 0xffffffffL; |
146 | 0 | l = r = t = u = 0; |
147 | 0 | } |
148 | | |
149 | | void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1, |
150 | | DES_key_schedule *ks2, DES_key_schedule *ks3) |
151 | 0 | { |
152 | 0 | register DES_LONG l, r; |
153 | 0 |
|
154 | 0 | l = data[0]; |
155 | 0 | r = data[1]; |
156 | 0 | IP(l, r); |
157 | 0 | data[0] = l; |
158 | 0 | data[1] = r; |
159 | 0 | DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT); |
160 | 0 | DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT); |
161 | 0 | DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT); |
162 | 0 | l = data[0]; |
163 | 0 | r = data[1]; |
164 | 0 | FP(r, l); |
165 | 0 | data[0] = l; |
166 | 0 | data[1] = r; |
167 | 0 | } |
168 | | |
169 | | void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1, |
170 | | DES_key_schedule *ks2, DES_key_schedule *ks3) |
171 | 0 | { |
172 | 0 | register DES_LONG l, r; |
173 | 0 |
|
174 | 0 | l = data[0]; |
175 | 0 | r = data[1]; |
176 | 0 | IP(l, r); |
177 | 0 | data[0] = l; |
178 | 0 | data[1] = r; |
179 | 0 | DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT); |
180 | 0 | DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT); |
181 | 0 | DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT); |
182 | 0 | l = data[0]; |
183 | 0 | r = data[1]; |
184 | 0 | FP(r, l); |
185 | 0 | data[0] = l; |
186 | 0 | data[1] = r; |
187 | 0 | } |
188 | | |
189 | | #ifndef DES_DEFAULT_OPTIONS |
190 | | |
191 | | # undef CBC_ENC_C__DONT_UPDATE_IV |
192 | | # include "ncbc_enc.c" /* DES_ncbc_encrypt */ |
193 | | |
194 | | void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, |
195 | | long length, DES_key_schedule *ks1, |
196 | | DES_key_schedule *ks2, DES_key_schedule *ks3, |
197 | | DES_cblock *ivec, int enc) |
198 | 0 | { |
199 | 0 | register DES_LONG tin0, tin1; |
200 | 0 | register DES_LONG tout0, tout1, xor0, xor1; |
201 | 0 | register const unsigned char *in; |
202 | 0 | unsigned char *out; |
203 | 0 | register long l = length; |
204 | 0 | DES_LONG tin[2]; |
205 | 0 | unsigned char *iv; |
206 | 0 |
|
207 | 0 | in = input; |
208 | 0 | out = output; |
209 | 0 | iv = &(*ivec)[0]; |
210 | 0 |
|
211 | 0 | if (enc) { |
212 | 0 | c2l(iv, tout0); |
213 | 0 | c2l(iv, tout1); |
214 | 0 | for (l -= 8; l >= 0; l -= 8) { |
215 | 0 | c2l(in, tin0); |
216 | 0 | c2l(in, tin1); |
217 | 0 | tin0 ^= tout0; |
218 | 0 | tin1 ^= tout1; |
219 | 0 |
|
220 | 0 | tin[0] = tin0; |
221 | 0 | tin[1] = tin1; |
222 | 0 | DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3); |
223 | 0 | tout0 = tin[0]; |
224 | 0 | tout1 = tin[1]; |
225 | 0 |
|
226 | 0 | l2c(tout0, out); |
227 | 0 | l2c(tout1, out); |
228 | 0 | } |
229 | 0 | if (l != -8) { |
230 | 0 | c2ln(in, tin0, tin1, l + 8); |
231 | 0 | tin0 ^= tout0; |
232 | 0 | tin1 ^= tout1; |
233 | 0 |
|
234 | 0 | tin[0] = tin0; |
235 | 0 | tin[1] = tin1; |
236 | 0 | DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3); |
237 | 0 | tout0 = tin[0]; |
238 | 0 | tout1 = tin[1]; |
239 | 0 |
|
240 | 0 | l2c(tout0, out); |
241 | 0 | l2c(tout1, out); |
242 | 0 | } |
243 | 0 | iv = &(*ivec)[0]; |
244 | 0 | l2c(tout0, iv); |
245 | 0 | l2c(tout1, iv); |
246 | 0 | } else { |
247 | 0 | register DES_LONG t0, t1; |
248 | 0 |
|
249 | 0 | c2l(iv, xor0); |
250 | 0 | c2l(iv, xor1); |
251 | 0 | for (l -= 8; l >= 0; l -= 8) { |
252 | 0 | c2l(in, tin0); |
253 | 0 | c2l(in, tin1); |
254 | 0 |
|
255 | 0 | t0 = tin0; |
256 | 0 | t1 = tin1; |
257 | 0 |
|
258 | 0 | tin[0] = tin0; |
259 | 0 | tin[1] = tin1; |
260 | 0 | DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3); |
261 | 0 | tout0 = tin[0]; |
262 | 0 | tout1 = tin[1]; |
263 | 0 |
|
264 | 0 | tout0 ^= xor0; |
265 | 0 | tout1 ^= xor1; |
266 | 0 | l2c(tout0, out); |
267 | 0 | l2c(tout1, out); |
268 | 0 | xor0 = t0; |
269 | 0 | xor1 = t1; |
270 | 0 | } |
271 | 0 | if (l != -8) { |
272 | 0 | c2l(in, tin0); |
273 | 0 | c2l(in, tin1); |
274 | 0 |
|
275 | 0 | t0 = tin0; |
276 | 0 | t1 = tin1; |
277 | 0 |
|
278 | 0 | tin[0] = tin0; |
279 | 0 | tin[1] = tin1; |
280 | 0 | DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3); |
281 | 0 | tout0 = tin[0]; |
282 | 0 | tout1 = tin[1]; |
283 | 0 |
|
284 | 0 | tout0 ^= xor0; |
285 | 0 | tout1 ^= xor1; |
286 | 0 | l2cn(tout0, tout1, out, l + 8); |
287 | 0 | xor0 = t0; |
288 | 0 | xor1 = t1; |
289 | 0 | } |
290 | 0 |
|
291 | 0 | iv = &(*ivec)[0]; |
292 | 0 | l2c(xor0, iv); |
293 | 0 | l2c(xor1, iv); |
294 | 0 | } |
295 | 0 | tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; |
296 | 0 | tin[0] = tin[1] = 0; |
297 | 0 | } |
298 | | |
299 | | #endif /* DES_DEFAULT_OPTIONS */ |