diff options
author | jsing <> | 2025-07-27 13:26:24 +0000 |
---|---|---|
committer | jsing <> | 2025-07-27 13:26:24 +0000 |
commit | 74f6d5e90112cc06125b3c0d88b91c6448ef5d90 (patch) | |
tree | b7ee90b2d082264c7abf0b2f61589f17d8cce991 /src | |
parent | 0b679ca6cc6cbe197008159a735cdb15b13d3f8e (diff) | |
download | openbsd-74f6d5e90112cc06125b3c0d88b91c6448ef5d90.tar.gz openbsd-74f6d5e90112cc06125b3c0d88b91c6448ef5d90.tar.bz2 openbsd-74f6d5e90112cc06125b3c0d88b91c6448ef5d90.zip |
Rework DES encryption/decryption loops.
Use a slightly unrolled loop, which gets us half way between DES_UNROLL and
no DES_UNROLL. While we're not terribly concerned by DES performance, this
gets us a small gain on aarch64 and a small loss on arm. But above all, we
end up with simpler code.
ok tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libcrypto/des/des_enc.c | 121 | ||||
-rw-r--r-- | src/lib/libcrypto/des/des_fcrypt.c | 34 |
2 files changed, 31 insertions, 124 deletions
diff --git a/src/lib/libcrypto/des/des_enc.c b/src/lib/libcrypto/des/des_enc.c index deec50bffb..cb89784fb0 100644 --- a/src/lib/libcrypto/des/des_enc.c +++ b/src/lib/libcrypto/des/des_enc.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: des_enc.c,v 1.20 2024/08/31 16:17:13 jsing Exp $ */ | 1 | /* $OpenBSD: des_enc.c,v 1.21 2025/07/27 13:26:24 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -210,10 +210,8 @@ void | |||
210 | DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) | 210 | DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) |
211 | { | 211 | { |
212 | DES_LONG l, r, t, u; | 212 | DES_LONG l, r, t, u; |
213 | #ifndef DES_UNROLL | ||
214 | int i; | ||
215 | #endif | ||
216 | DES_LONG *s; | 213 | DES_LONG *s; |
214 | int i; | ||
217 | 215 | ||
218 | r = data[0]; | 216 | r = data[0]; |
219 | l = data[1]; | 217 | l = data[1]; |
@@ -231,56 +229,21 @@ DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc) | |||
231 | l = ROTATE(l, 29) & 0xffffffffL; | 229 | l = ROTATE(l, 29) & 0xffffffffL; |
232 | 230 | ||
233 | s = ks->ks->deslong; | 231 | s = ks->ks->deslong; |
234 | /* I don't know if it is worth the effort of loop unrolling the | 232 | |
235 | * inner loop */ | ||
236 | if (enc) { | 233 | if (enc) { |
237 | #ifdef DES_UNROLL | 234 | for (i = 0; i < 32; i += 8) { |
238 | D_ENCRYPT(l, r, 0); /* 1 */ | 235 | D_ENCRYPT(l, r, i + 0); |
239 | D_ENCRYPT(r, l, 2); /* 2 */ | 236 | D_ENCRYPT(r, l, i + 2); |
240 | D_ENCRYPT(l, r, 4); /* 3 */ | 237 | D_ENCRYPT(l, r, i + 4); |
241 | D_ENCRYPT(r, l, 6); /* 4 */ | 238 | D_ENCRYPT(r, l, i + 6); |
242 | D_ENCRYPT(l, r, 8); /* 5 */ | ||
243 | D_ENCRYPT(r, l, 10); /* 6 */ | ||
244 | D_ENCRYPT(l, r, 12); /* 7 */ | ||
245 | D_ENCRYPT(r, l, 14); /* 8 */ | ||
246 | D_ENCRYPT(l, r, 16); /* 9 */ | ||
247 | D_ENCRYPT(r, l, 18); /* 10 */ | ||
248 | D_ENCRYPT(l, r, 20); /* 11 */ | ||
249 | D_ENCRYPT(r, l, 22); /* 12 */ | ||
250 | D_ENCRYPT(l, r, 24); /* 13 */ | ||
251 | D_ENCRYPT(r, l, 26); /* 14 */ | ||
252 | D_ENCRYPT(l, r, 28); /* 15 */ | ||
253 | D_ENCRYPT(r, l, 30); /* 16 */ | ||
254 | #else | ||
255 | for (i = 0; i < 32; i += 4) { | ||
256 | D_ENCRYPT(l, r, i + 0); /* 1 */ | ||
257 | D_ENCRYPT(r, l, i + 2); /* 2 */ | ||
258 | } | 239 | } |
259 | #endif | ||
260 | } else { | 240 | } else { |
261 | #ifdef DES_UNROLL | 241 | for (i = 32; i > 0; i -= 8) { |
262 | D_ENCRYPT(l, r, 30); /* 16 */ | 242 | D_ENCRYPT(l, r, i - 2); |
263 | D_ENCRYPT(r, l, 28); /* 15 */ | 243 | D_ENCRYPT(r, l, i - 4); |
264 | D_ENCRYPT(l, r, 26); /* 14 */ | 244 | D_ENCRYPT(l, r, i - 6); |
265 | D_ENCRYPT(r, l, 24); /* 13 */ | 245 | D_ENCRYPT(r, l, i - 8); |
266 | D_ENCRYPT(l, r, 22); /* 12 */ | ||
267 | D_ENCRYPT(r, l, 20); /* 11 */ | ||
268 | D_ENCRYPT(l, r, 18); /* 10 */ | ||
269 | D_ENCRYPT(r, l, 16); /* 9 */ | ||
270 | D_ENCRYPT(l, r, 14); /* 8 */ | ||
271 | D_ENCRYPT(r, l, 12); /* 7 */ | ||
272 | D_ENCRYPT(l, r, 10); /* 6 */ | ||
273 | D_ENCRYPT(r, l, 8); /* 5 */ | ||
274 | D_ENCRYPT(l, r, 6); /* 4 */ | ||
275 | D_ENCRYPT(r, l, 4); /* 3 */ | ||
276 | D_ENCRYPT(l, r, 2); /* 2 */ | ||
277 | D_ENCRYPT(r, l, 0); /* 1 */ | ||
278 | #else | ||
279 | for (i = 30; i > 0; i -= 4) { | ||
280 | D_ENCRYPT(l, r, i - 0); /* 16 */ | ||
281 | D_ENCRYPT(r, l, i - 2); /* 15 */ | ||
282 | } | 246 | } |
283 | #endif | ||
284 | } | 247 | } |
285 | 248 | ||
286 | /* rotate and clear the top bits on machines with 8byte longs */ | 249 | /* rotate and clear the top bits on machines with 8byte longs */ |
@@ -298,10 +261,8 @@ void | |||
298 | DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc) | 261 | DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc) |
299 | { | 262 | { |
300 | DES_LONG l, r, t, u; | 263 | DES_LONG l, r, t, u; |
301 | #ifndef DES_UNROLL | ||
302 | int i; | ||
303 | #endif | ||
304 | DES_LONG *s; | 264 | DES_LONG *s; |
265 | int i; | ||
305 | 266 | ||
306 | r = data[0]; | 267 | r = data[0]; |
307 | l = data[1]; | 268 | l = data[1]; |
@@ -320,53 +281,19 @@ DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc) | |||
320 | /* I don't know if it is worth the effort of loop unrolling the | 281 | /* I don't know if it is worth the effort of loop unrolling the |
321 | * inner loop */ | 282 | * inner loop */ |
322 | if (enc) { | 283 | if (enc) { |
323 | #ifdef DES_UNROLL | 284 | for (i = 0; i < 32; i += 8) { |
324 | D_ENCRYPT(l, r, 0); /* 1 */ | 285 | D_ENCRYPT(l, r, i + 0); |
325 | D_ENCRYPT(r, l, 2); /* 2 */ | 286 | D_ENCRYPT(r, l, i + 2); |
326 | D_ENCRYPT(l, r, 4); /* 3 */ | 287 | D_ENCRYPT(l, r, i + 4); |
327 | D_ENCRYPT(r, l, 6); /* 4 */ | 288 | D_ENCRYPT(r, l, i + 6); |
328 | D_ENCRYPT(l, r, 8); /* 5 */ | ||
329 | D_ENCRYPT(r, l, 10); /* 6 */ | ||
330 | D_ENCRYPT(l, r, 12); /* 7 */ | ||
331 | D_ENCRYPT(r, l, 14); /* 8 */ | ||
332 | D_ENCRYPT(l, r, 16); /* 9 */ | ||
333 | D_ENCRYPT(r, l, 18); /* 10 */ | ||
334 | D_ENCRYPT(l, r, 20); /* 11 */ | ||
335 | D_ENCRYPT(r, l, 22); /* 12 */ | ||
336 | D_ENCRYPT(l, r, 24); /* 13 */ | ||
337 | D_ENCRYPT(r, l, 26); /* 14 */ | ||
338 | D_ENCRYPT(l, r, 28); /* 15 */ | ||
339 | D_ENCRYPT(r, l, 30); /* 16 */ | ||
340 | #else | ||
341 | for (i = 0; i < 32; i += 4) { | ||
342 | D_ENCRYPT(l, r, i + 0); /* 1 */ | ||
343 | D_ENCRYPT(r, l, i + 2); /* 2 */ | ||
344 | } | 289 | } |
345 | #endif | ||
346 | } else { | 290 | } else { |
347 | #ifdef DES_UNROLL | 291 | for (i = 32; i > 0; i -= 8) { |
348 | D_ENCRYPT(l, r, 30); /* 16 */ | 292 | D_ENCRYPT(l, r, i - 2); |
349 | D_ENCRYPT(r, l, 28); /* 15 */ | 293 | D_ENCRYPT(r, l, i - 4); |
350 | D_ENCRYPT(l, r, 26); /* 14 */ | 294 | D_ENCRYPT(l, r, i - 6); |
351 | D_ENCRYPT(r, l, 24); /* 13 */ | 295 | D_ENCRYPT(r, l, i - 8); |
352 | D_ENCRYPT(l, r, 22); /* 12 */ | ||
353 | D_ENCRYPT(r, l, 20); /* 11 */ | ||
354 | D_ENCRYPT(l, r, 18); /* 10 */ | ||
355 | D_ENCRYPT(r, l, 16); /* 9 */ | ||
356 | D_ENCRYPT(l, r, 14); /* 8 */ | ||
357 | D_ENCRYPT(r, l, 12); /* 7 */ | ||
358 | D_ENCRYPT(l, r, 10); /* 6 */ | ||
359 | D_ENCRYPT(r, l, 8); /* 5 */ | ||
360 | D_ENCRYPT(l, r, 6); /* 4 */ | ||
361 | D_ENCRYPT(r, l, 4); /* 3 */ | ||
362 | D_ENCRYPT(l, r, 2); /* 2 */ | ||
363 | D_ENCRYPT(r, l, 0); /* 1 */ | ||
364 | #else | ||
365 | for (i = 30; i > 0; i -= 4) { | ||
366 | D_ENCRYPT(l, r, i - 0); /* 16 */ | ||
367 | D_ENCRYPT(r, l, i - 2); /* 15 */ | ||
368 | } | 296 | } |
369 | #endif | ||
370 | } | 297 | } |
371 | /* rotate and clear the top bits on machines with 8byte longs */ | 298 | /* rotate and clear the top bits on machines with 8byte longs */ |
372 | data[0] = ROTATE(l, 3) & 0xffffffffL; | 299 | data[0] = ROTATE(l, 3) & 0xffffffffL; |
diff --git a/src/lib/libcrypto/des/des_fcrypt.c b/src/lib/libcrypto/des/des_fcrypt.c index b33b1240c2..2dd071f5d0 100644 --- a/src/lib/libcrypto/des/des_fcrypt.c +++ b/src/lib/libcrypto/des/des_fcrypt.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: des_fcrypt.c,v 1.4 2024/08/31 16:22:18 jsing Exp $ */ | 1 | /* $OpenBSD: des_fcrypt.c,v 1.5 2025/07/27 13:26:24 jsing Exp $ */ |
2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 | * All rights reserved. | 3 | * All rights reserved. |
4 | * | 4 | * |
@@ -90,8 +90,8 @@ fcrypt_body(DES_LONG *out, DES_key_schedule *ks, DES_LONG Eswap0, | |||
90 | { | 90 | { |
91 | DES_LONG l, r, t, u; | 91 | DES_LONG l, r, t, u; |
92 | DES_LONG *s; | 92 | DES_LONG *s; |
93 | int j; | ||
94 | DES_LONG E0, E1; | 93 | DES_LONG E0, E1; |
94 | int i, j; | ||
95 | 95 | ||
96 | l = 0; | 96 | l = 0; |
97 | r = 0; | 97 | r = 0; |
@@ -101,32 +101,12 @@ fcrypt_body(DES_LONG *out, DES_key_schedule *ks, DES_LONG Eswap0, | |||
101 | E1 = Eswap1; | 101 | E1 = Eswap1; |
102 | 102 | ||
103 | for (j = 0; j < 25; j++) { | 103 | for (j = 0; j < 25; j++) { |
104 | #ifndef DES_UNROLL | 104 | for (i = 0; i < 32; i += 8) { |
105 | int i; | 105 | D_ENCRYPT(l, r, i + 0); |
106 | 106 | D_ENCRYPT(r, l, i + 2); | |
107 | for (i = 0; i < 32; i += 4) { | 107 | D_ENCRYPT(l, r, i + 4); |
108 | D_ENCRYPT(l, r, i + 0); /* 1 */ | 108 | D_ENCRYPT(r, l, i + 6); |
109 | D_ENCRYPT(r, l, i + 2); /* 2 */ | ||
110 | } | 109 | } |
111 | #else | ||
112 | D_ENCRYPT(l, r, 0); /* 1 */ | ||
113 | D_ENCRYPT(r, l, 2); /* 2 */ | ||
114 | D_ENCRYPT(l, r, 4); /* 3 */ | ||
115 | D_ENCRYPT(r, l, 6); /* 4 */ | ||
116 | D_ENCRYPT(l, r, 8); /* 5 */ | ||
117 | D_ENCRYPT(r, l, 10); /* 6 */ | ||
118 | D_ENCRYPT(l, r, 12); /* 7 */ | ||
119 | D_ENCRYPT(r, l, 14); /* 8 */ | ||
120 | D_ENCRYPT(l, r, 16); /* 9 */ | ||
121 | D_ENCRYPT(r, l, 18); /* 10 */ | ||
122 | D_ENCRYPT(l, r, 20); /* 11 */ | ||
123 | D_ENCRYPT(r, l, 22); /* 12 */ | ||
124 | D_ENCRYPT(l, r, 24); /* 13 */ | ||
125 | D_ENCRYPT(r, l, 26); /* 14 */ | ||
126 | D_ENCRYPT(l, r, 28); /* 15 */ | ||
127 | D_ENCRYPT(r, l, 30); /* 16 */ | ||
128 | #endif | ||
129 | |||
130 | t = l; | 110 | t = l; |
131 | l = r; | 111 | l = r; |
132 | r = t; | 112 | r = t; |