Always build library object files with shared library cflags
[openssl.git] / crypto / des / des_enc.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
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.
7  *
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).
14  *
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.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
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)"
39  *
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
50  * SUCH DAMAGE.
51  *
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.]
56  */
57
58 #include <openssl/crypto.h>
59 #include "des_locl.h"
60 #include "spr.h"
61
62 void DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
63 {
64     register DES_LONG l, r, t, u;
65     register DES_LONG *s;
66
67     r = data[0];
68     l = data[1];
69
70     IP(r, l);
71     /*
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.
77      */
78     /* clear the top bits on machines with 8byte longs */
79     /* shift left by 2 */
80     r = ROTATE(r, 29) & 0xffffffffL;
81     l = ROTATE(l, 29) & 0xffffffffL;
82
83     s = ks->ks->deslong;
84     /*
85      * I don't know if it is worth the effort of loop unrolling the inner
86      * loop
87      */
88     if (enc) {
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 */
105     } else {
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 */
122     }
123
124     /* rotate and clear the top bits on machines with 8byte longs */
125     l = ROTATE(l, 3) & 0xffffffffL;
126     r = ROTATE(r, 3) & 0xffffffffL;
127
128     FP(r, l);
129     data[0] = l;
130     data[1] = r;
131     l = r = t = u = 0;
132 }
133
134 void DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
135 {
136     register DES_LONG l, r, t, u;
137     register DES_LONG *s;
138
139     r = data[0];
140     l = data[1];
141
142     /*
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.
148      */
149     /* clear the top bits on machines with 8byte longs */
150     r = ROTATE(r, 29) & 0xffffffffL;
151     l = ROTATE(l, 29) & 0xffffffffL;
152
153     s = ks->ks->deslong;
154     /*
155      * I don't know if it is worth the effort of loop unrolling the inner
156      * loop
157      */
158     if (enc) {
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 */
175     } else {
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 */
192     }
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;
196     l = r = t = u = 0;
197 }
198
199 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
200                   DES_key_schedule *ks2, DES_key_schedule *ks3)
201 {
202     register DES_LONG l, r;
203
204     l = data[0];
205     r = data[1];
206     IP(l, r);
207     data[0] = l;
208     data[1] = 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);
212     l = data[0];
213     r = data[1];
214     FP(r, l);
215     data[0] = l;
216     data[1] = r;
217 }
218
219 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
220                   DES_key_schedule *ks2, DES_key_schedule *ks3)
221 {
222     register DES_LONG l, r;
223
224     l = data[0];
225     r = data[1];
226     IP(l, r);
227     data[0] = l;
228     data[1] = 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);
232     l = data[0];
233     r = data[1];
234     FP(r, l);
235     data[0] = l;
236     data[1] = r;
237 }
238
239 #ifndef DES_DEFAULT_OPTIONS
240
241 # undef CBC_ENC_C__DONT_UPDATE_IV
242 # include "ncbc_enc.c"          /* DES_ncbc_encrypt */
243
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)
248 {
249     register DES_LONG tin0, tin1;
250     register DES_LONG tout0, tout1, xor0, xor1;
251     register const unsigned char *in;
252     unsigned char *out;
253     register long l = length;
254     DES_LONG tin[2];
255     unsigned char *iv;
256
257     in = input;
258     out = output;
259     iv = &(*ivec)[0];
260
261     if (enc) {
262         c2l(iv, tout0);
263         c2l(iv, tout1);
264         for (l -= 8; l >= 0; l -= 8) {
265             c2l(in, tin0);
266             c2l(in, tin1);
267             tin0 ^= tout0;
268             tin1 ^= tout1;
269
270             tin[0] = tin0;
271             tin[1] = tin1;
272             DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
273             tout0 = tin[0];
274             tout1 = tin[1];
275
276             l2c(tout0, out);
277             l2c(tout1, out);
278         }
279         if (l != -8) {
280             c2ln(in, tin0, tin1, l + 8);
281             tin0 ^= tout0;
282             tin1 ^= tout1;
283
284             tin[0] = tin0;
285             tin[1] = tin1;
286             DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
287             tout0 = tin[0];
288             tout1 = tin[1];
289
290             l2c(tout0, out);
291             l2c(tout1, out);
292         }
293         iv = &(*ivec)[0];
294         l2c(tout0, iv);
295         l2c(tout1, iv);
296     } else {
297         register DES_LONG t0, t1;
298
299         c2l(iv, xor0);
300         c2l(iv, xor1);
301         for (l -= 8; l >= 0; l -= 8) {
302             c2l(in, tin0);
303             c2l(in, tin1);
304
305             t0 = tin0;
306             t1 = tin1;
307
308             tin[0] = tin0;
309             tin[1] = tin1;
310             DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
311             tout0 = tin[0];
312             tout1 = tin[1];
313
314             tout0 ^= xor0;
315             tout1 ^= xor1;
316             l2c(tout0, out);
317             l2c(tout1, out);
318             xor0 = t0;
319             xor1 = t1;
320         }
321         if (l != -8) {
322             c2l(in, tin0);
323             c2l(in, tin1);
324
325             t0 = tin0;
326             t1 = tin1;
327
328             tin[0] = tin0;
329             tin[1] = tin1;
330             DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
331             tout0 = tin[0];
332             tout1 = tin[1];
333
334             tout0 ^= xor0;
335             tout1 ^= xor1;
336             l2cn(tout0, tout1, out, l + 8);
337             xor0 = t0;
338             xor1 = t1;
339         }
340
341         iv = &(*ivec)[0];
342         l2c(xor0, iv);
343         l2c(xor1, iv);
344     }
345     tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
346     tin[0] = tin[1] = 0;
347 }
348
349 #endif                          /* DES_DEFAULT_OPTIONS */