]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/des/des_enc.c
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/crypto.h>
14 void DES_encrypt1(DES_LONG
*data
, DES_key_schedule
*ks
, int enc
)
16 register DES_LONG l
, r
, t
, u
;
24 * Things have been modified so that the initial rotate is done outside
25 * the loop. This required the DES_SPtrans values in sp.h to be rotated
26 * 1 bit to the right. One perl script later and things have a 5% speed
27 * up on a sparc2. Thanks to Richard Outerbridge
28 * <71755.204@CompuServe.COM> for pointing this out.
30 /* clear the top bits on machines with 8byte longs */
32 r
= ROTATE(r
, 29) & 0xffffffffL
;
33 l
= ROTATE(l
, 29) & 0xffffffffL
;
37 * I don't know if it is worth the effort of loop unrolling the inner
41 D_ENCRYPT(l
, r
, 0); /* 1 */
42 D_ENCRYPT(r
, l
, 2); /* 2 */
43 D_ENCRYPT(l
, r
, 4); /* 3 */
44 D_ENCRYPT(r
, l
, 6); /* 4 */
45 D_ENCRYPT(l
, r
, 8); /* 5 */
46 D_ENCRYPT(r
, l
, 10); /* 6 */
47 D_ENCRYPT(l
, r
, 12); /* 7 */
48 D_ENCRYPT(r
, l
, 14); /* 8 */
49 D_ENCRYPT(l
, r
, 16); /* 9 */
50 D_ENCRYPT(r
, l
, 18); /* 10 */
51 D_ENCRYPT(l
, r
, 20); /* 11 */
52 D_ENCRYPT(r
, l
, 22); /* 12 */
53 D_ENCRYPT(l
, r
, 24); /* 13 */
54 D_ENCRYPT(r
, l
, 26); /* 14 */
55 D_ENCRYPT(l
, r
, 28); /* 15 */
56 D_ENCRYPT(r
, l
, 30); /* 16 */
58 D_ENCRYPT(l
, r
, 30); /* 16 */
59 D_ENCRYPT(r
, l
, 28); /* 15 */
60 D_ENCRYPT(l
, r
, 26); /* 14 */
61 D_ENCRYPT(r
, l
, 24); /* 13 */
62 D_ENCRYPT(l
, r
, 22); /* 12 */
63 D_ENCRYPT(r
, l
, 20); /* 11 */
64 D_ENCRYPT(l
, r
, 18); /* 10 */
65 D_ENCRYPT(r
, l
, 16); /* 9 */
66 D_ENCRYPT(l
, r
, 14); /* 8 */
67 D_ENCRYPT(r
, l
, 12); /* 7 */
68 D_ENCRYPT(l
, r
, 10); /* 6 */
69 D_ENCRYPT(r
, l
, 8); /* 5 */
70 D_ENCRYPT(l
, r
, 6); /* 4 */
71 D_ENCRYPT(r
, l
, 4); /* 3 */
72 D_ENCRYPT(l
, r
, 2); /* 2 */
73 D_ENCRYPT(r
, l
, 0); /* 1 */
76 /* rotate and clear the top bits on machines with 8byte longs */
77 l
= ROTATE(l
, 3) & 0xffffffffL
;
78 r
= ROTATE(r
, 3) & 0xffffffffL
;
86 void DES_encrypt2(DES_LONG
*data
, DES_key_schedule
*ks
, int enc
)
88 register DES_LONG l
, r
, t
, u
;
95 * Things have been modified so that the initial rotate is done outside
96 * the loop. This required the DES_SPtrans values in sp.h to be rotated
97 * 1 bit to the right. One perl script later and things have a 5% speed
98 * up on a sparc2. Thanks to Richard Outerbridge
99 * <71755.204@CompuServe.COM> for pointing this out.
101 /* clear the top bits on machines with 8byte longs */
102 r
= ROTATE(r
, 29) & 0xffffffffL
;
103 l
= ROTATE(l
, 29) & 0xffffffffL
;
107 * I don't know if it is worth the effort of loop unrolling the inner
111 D_ENCRYPT(l
, r
, 0); /* 1 */
112 D_ENCRYPT(r
, l
, 2); /* 2 */
113 D_ENCRYPT(l
, r
, 4); /* 3 */
114 D_ENCRYPT(r
, l
, 6); /* 4 */
115 D_ENCRYPT(l
, r
, 8); /* 5 */
116 D_ENCRYPT(r
, l
, 10); /* 6 */
117 D_ENCRYPT(l
, r
, 12); /* 7 */
118 D_ENCRYPT(r
, l
, 14); /* 8 */
119 D_ENCRYPT(l
, r
, 16); /* 9 */
120 D_ENCRYPT(r
, l
, 18); /* 10 */
121 D_ENCRYPT(l
, r
, 20); /* 11 */
122 D_ENCRYPT(r
, l
, 22); /* 12 */
123 D_ENCRYPT(l
, r
, 24); /* 13 */
124 D_ENCRYPT(r
, l
, 26); /* 14 */
125 D_ENCRYPT(l
, r
, 28); /* 15 */
126 D_ENCRYPT(r
, l
, 30); /* 16 */
128 D_ENCRYPT(l
, r
, 30); /* 16 */
129 D_ENCRYPT(r
, l
, 28); /* 15 */
130 D_ENCRYPT(l
, r
, 26); /* 14 */
131 D_ENCRYPT(r
, l
, 24); /* 13 */
132 D_ENCRYPT(l
, r
, 22); /* 12 */
133 D_ENCRYPT(r
, l
, 20); /* 11 */
134 D_ENCRYPT(l
, r
, 18); /* 10 */
135 D_ENCRYPT(r
, l
, 16); /* 9 */
136 D_ENCRYPT(l
, r
, 14); /* 8 */
137 D_ENCRYPT(r
, l
, 12); /* 7 */
138 D_ENCRYPT(l
, r
, 10); /* 6 */
139 D_ENCRYPT(r
, l
, 8); /* 5 */
140 D_ENCRYPT(l
, r
, 6); /* 4 */
141 D_ENCRYPT(r
, l
, 4); /* 3 */
142 D_ENCRYPT(l
, r
, 2); /* 2 */
143 D_ENCRYPT(r
, l
, 0); /* 1 */
145 /* rotate and clear the top bits on machines with 8byte longs */
146 data
[0] = ROTATE(l
, 3) & 0xffffffffL
;
147 data
[1] = ROTATE(r
, 3) & 0xffffffffL
;
151 void DES_encrypt3(DES_LONG
*data
, DES_key_schedule
*ks1
,
152 DES_key_schedule
*ks2
, DES_key_schedule
*ks3
)
154 register DES_LONG l
, r
;
161 DES_encrypt2((DES_LONG
*)data
, ks1
, DES_ENCRYPT
);
162 DES_encrypt2((DES_LONG
*)data
, ks2
, DES_DECRYPT
);
163 DES_encrypt2((DES_LONG
*)data
, ks3
, DES_ENCRYPT
);
171 void DES_decrypt3(DES_LONG
*data
, DES_key_schedule
*ks1
,
172 DES_key_schedule
*ks2
, DES_key_schedule
*ks3
)
174 register DES_LONG l
, r
;
181 DES_encrypt2((DES_LONG
*)data
, ks3
, DES_DECRYPT
);
182 DES_encrypt2((DES_LONG
*)data
, ks2
, DES_ENCRYPT
);
183 DES_encrypt2((DES_LONG
*)data
, ks1
, DES_DECRYPT
);
191 #ifndef DES_DEFAULT_OPTIONS
193 # undef CBC_ENC_C__DONT_UPDATE_IV
194 # include "ncbc_enc.c" /* DES_ncbc_encrypt */
196 void DES_ede3_cbc_encrypt(const unsigned char *input
, unsigned char *output
,
197 long length
, DES_key_schedule
*ks1
,
198 DES_key_schedule
*ks2
, DES_key_schedule
*ks3
,
199 DES_cblock
*ivec
, int enc
)
201 register DES_LONG tin0
, tin1
;
202 register DES_LONG tout0
, tout1
, xor0
, xor1
;
203 register const unsigned char *in
;
205 register long l
= length
;
216 for (l
-= 8; l
>= 0; l
-= 8) {
224 DES_encrypt3((DES_LONG
*)tin
, ks1
, ks2
, ks3
);
232 c2ln(in
, tin0
, tin1
, l
+ 8);
238 DES_encrypt3((DES_LONG
*)tin
, ks1
, ks2
, ks3
);
249 register DES_LONG t0
, t1
;
253 for (l
-= 8; l
>= 0; l
-= 8) {
262 DES_decrypt3((DES_LONG
*)tin
, ks1
, ks2
, ks3
);
282 DES_decrypt3((DES_LONG
*)tin
, ks1
, ks2
, ks3
);
288 l2cn(tout0
, tout1
, out
, l
+ 8);
297 tin0
= tin1
= tout0
= tout1
= xor0
= xor1
= 0;
301 #endif /* DES_DEFAULT_OPTIONS */