1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
58 #include <openssl/crypto.h>
62 void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
64 register DES_LONG l, r, t, u;
72 * Things have been modified so that the initial rotate is done outside
73 * the loop. This required the DES_SPtrans values in sp.h to be rotated
74 * 1 bit to the right. One perl script later and things have a 5% speed
75 * up on a sparc2. Thanks to Richard Outerbridge
76 * <71755.204@CompuServe.COM> for pointing this out.
78 /* clear the top bits on machines with 8byte longs */
80 r = ROTATE(r, 29) & 0xffffffffL;
81 l = ROTATE(l, 29) & 0xffffffffL;
85 * I don't know if it is worth the effort of loop unrolling the inner
89 D_ENCRYPT(l, r, 0); /* 1 */
90 D_ENCRYPT(r, l, 2); /* 2 */
91 D_ENCRYPT(l, r, 4); /* 3 */
92 D_ENCRYPT(r, l, 6); /* 4 */
93 D_ENCRYPT(l, r, 8); /* 5 */
94 D_ENCRYPT(r, l, 10); /* 6 */
95 D_ENCRYPT(l, r, 12); /* 7 */
96 D_ENCRYPT(r, l, 14); /* 8 */
97 D_ENCRYPT(l, r, 16); /* 9 */
98 D_ENCRYPT(r, l, 18); /* 10 */
99 D_ENCRYPT(l, r, 20); /* 11 */
100 D_ENCRYPT(r, l, 22); /* 12 */
101 D_ENCRYPT(l, r, 24); /* 13 */
102 D_ENCRYPT(r, l, 26); /* 14 */
103 D_ENCRYPT(l, r, 28); /* 15 */
104 D_ENCRYPT(r, l, 30); /* 16 */
106 D_ENCRYPT(l, r, 30); /* 16 */
107 D_ENCRYPT(r, l, 28); /* 15 */
108 D_ENCRYPT(l, r, 26); /* 14 */
109 D_ENCRYPT(r, l, 24); /* 13 */
110 D_ENCRYPT(l, r, 22); /* 12 */
111 D_ENCRYPT(r, l, 20); /* 11 */
112 D_ENCRYPT(l, r, 18); /* 10 */
113 D_ENCRYPT(r, l, 16); /* 9 */
114 D_ENCRYPT(l, r, 14); /* 8 */
115 D_ENCRYPT(r, l, 12); /* 7 */
116 D_ENCRYPT(l, r, 10); /* 6 */
117 D_ENCRYPT(r, l, 8); /* 5 */
118 D_ENCRYPT(l, r, 6); /* 4 */
119 D_ENCRYPT(r, l, 4); /* 3 */
120 D_ENCRYPT(l, r, 2); /* 2 */
121 D_ENCRYPT(r, l, 0); /* 1 */
124 /* rotate and clear the top bits on machines with 8byte longs */
125 l = ROTATE(l, 3) & 0xffffffffL;
126 r = ROTATE(r, 3) & 0xffffffffL;
134 void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
136 register DES_LONG l, r, t, u;
137 register DES_LONG *s;
143 * Things have been modified so that the initial rotate is done outside
144 * the loop. This required the DES_SPtrans values in sp.h to be rotated
145 * 1 bit to the right. One perl script later and things have a 5% speed
146 * up on a sparc2. Thanks to Richard Outerbridge
147 * <71755.204@CompuServe.COM> for pointing this out.
149 /* clear the top bits on machines with 8byte longs */
150 r = ROTATE(r, 29) & 0xffffffffL;
151 l = ROTATE(l, 29) & 0xffffffffL;
155 * I don't know if it is worth the effort of loop unrolling the inner
159 D_ENCRYPT(l, r, 0); /* 1 */
160 D_ENCRYPT(r, l, 2); /* 2 */
161 D_ENCRYPT(l, r, 4); /* 3 */
162 D_ENCRYPT(r, l, 6); /* 4 */
163 D_ENCRYPT(l, r, 8); /* 5 */
164 D_ENCRYPT(r, l, 10); /* 6 */
165 D_ENCRYPT(l, r, 12); /* 7 */
166 D_ENCRYPT(r, l, 14); /* 8 */
167 D_ENCRYPT(l, r, 16); /* 9 */
168 D_ENCRYPT(r, l, 18); /* 10 */
169 D_ENCRYPT(l, r, 20); /* 11 */
170 D_ENCRYPT(r, l, 22); /* 12 */
171 D_ENCRYPT(l, r, 24); /* 13 */
172 D_ENCRYPT(r, l, 26); /* 14 */
173 D_ENCRYPT(l, r, 28); /* 15 */
174 D_ENCRYPT(r, l, 30); /* 16 */
176 D_ENCRYPT(l, r, 30); /* 16 */
177 D_ENCRYPT(r, l, 28); /* 15 */
178 D_ENCRYPT(l, r, 26); /* 14 */
179 D_ENCRYPT(r, l, 24); /* 13 */
180 D_ENCRYPT(l, r, 22); /* 12 */
181 D_ENCRYPT(r, l, 20); /* 11 */
182 D_ENCRYPT(l, r, 18); /* 10 */
183 D_ENCRYPT(r, l, 16); /* 9 */
184 D_ENCRYPT(l, r, 14); /* 8 */
185 D_ENCRYPT(r, l, 12); /* 7 */
186 D_ENCRYPT(l, r, 10); /* 6 */
187 D_ENCRYPT(r, l, 8); /* 5 */
188 D_ENCRYPT(l, r, 6); /* 4 */
189 D_ENCRYPT(r, l, 4); /* 3 */
190 D_ENCRYPT(l, r, 2); /* 2 */
191 D_ENCRYPT(r, l, 0); /* 1 */
193 /* rotate and clear the top bits on machines with 8byte longs */
194 data[0] = ROTATE(l, 3) & 0xffffffffL;
195 data[1] = ROTATE(r, 3) & 0xffffffffL;
199 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
200 DES_key_schedule *ks2, DES_key_schedule *ks3)
202 register DES_LONG l, r;
209 DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT);
210 DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT);
211 DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT);
219 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
220 DES_key_schedule *ks2, DES_key_schedule *ks3)
222 register DES_LONG l, r;
229 DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT);
230 DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT);
231 DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT);
239 #ifndef DES_DEFAULT_OPTIONS
241 # undef CBC_ENC_C__DONT_UPDATE_IV
242 # include "ncbc_enc.c" /* DES_ncbc_encrypt */
244 void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
245 long length, DES_key_schedule *ks1,
246 DES_key_schedule *ks2, DES_key_schedule *ks3,
247 DES_cblock *ivec, int enc)
249 register DES_LONG tin0, tin1;
250 register DES_LONG tout0, tout1, xor0, xor1;
251 register const unsigned char *in;
253 register long l = length;
264 for (l -= 8; l >= 0; l -= 8) {
272 DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
280 c2ln(in, tin0, tin1, l + 8);
286 DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
297 register DES_LONG t0, t1;
301 for (l -= 8; l >= 0; l -= 8) {
310 DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
330 DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
336 l2cn(tout0, tout1, out, l + 8);
345 tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
349 #endif /* DES_DEFAULT_OPTIONS */