155714Skris/* crypto/des/des_enc.c */
255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
8296465Sdelphij *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15296465Sdelphij *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
22296465Sdelphij *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
37296465Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40296465Sdelphij *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
52296465Sdelphij *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
5955714Skris#include "des_locl.h"
6055714Skris
61109998Smarkmvoid DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc)
62296465Sdelphij{
63296465Sdelphij    register DES_LONG l, r, t, u;
6455714Skris#ifdef DES_PTR
65296465Sdelphij    register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans;
6655714Skris#endif
6755714Skris#ifndef DES_UNROLL
68296465Sdelphij    register int i;
6955714Skris#endif
70296465Sdelphij    register DES_LONG *s;
7155714Skris
72296465Sdelphij    r = data[0];
73296465Sdelphij    l = data[1];
7455714Skris
75296465Sdelphij    IP(r, l);
76296465Sdelphij    /*
77296465Sdelphij     * Things have been modified so that the initial rotate is done outside
78296465Sdelphij     * the loop.  This required the DES_SPtrans values in sp.h to be rotated
79296465Sdelphij     * 1 bit to the right. One perl script later and things have a 5% speed
80296465Sdelphij     * up on a sparc2. Thanks to Richard Outerbridge
81296465Sdelphij     * <71755.204@CompuServe.COM> for pointing this out.
82296465Sdelphij     */
83296465Sdelphij    /* clear the top bits on machines with 8byte longs */
84296465Sdelphij    /* shift left by 2 */
85296465Sdelphij    r = ROTATE(r, 29) & 0xffffffffL;
86296465Sdelphij    l = ROTATE(l, 29) & 0xffffffffL;
8755714Skris
88296465Sdelphij    s = ks->ks->deslong;
89296465Sdelphij    /*
90296465Sdelphij     * I don't know if it is worth the effort of loop unrolling the inner
91296465Sdelphij     * loop
92296465Sdelphij     */
93296465Sdelphij    if (enc) {
9455714Skris#ifdef DES_UNROLL
95296465Sdelphij        D_ENCRYPT(l, r, 0);     /* 1 */
96296465Sdelphij        D_ENCRYPT(r, l, 2);     /* 2 */
97296465Sdelphij        D_ENCRYPT(l, r, 4);     /* 3 */
98296465Sdelphij        D_ENCRYPT(r, l, 6);     /* 4 */
99296465Sdelphij        D_ENCRYPT(l, r, 8);     /* 5 */
100296465Sdelphij        D_ENCRYPT(r, l, 10);    /* 6 */
101296465Sdelphij        D_ENCRYPT(l, r, 12);    /* 7 */
102296465Sdelphij        D_ENCRYPT(r, l, 14);    /* 8 */
103296465Sdelphij        D_ENCRYPT(l, r, 16);    /* 9 */
104296465Sdelphij        D_ENCRYPT(r, l, 18);    /* 10 */
105296465Sdelphij        D_ENCRYPT(l, r, 20);    /* 11 */
106296465Sdelphij        D_ENCRYPT(r, l, 22);    /* 12 */
107296465Sdelphij        D_ENCRYPT(l, r, 24);    /* 13 */
108296465Sdelphij        D_ENCRYPT(r, l, 26);    /* 14 */
109296465Sdelphij        D_ENCRYPT(l, r, 28);    /* 15 */
110296465Sdelphij        D_ENCRYPT(r, l, 30);    /* 16 */
11155714Skris#else
112296465Sdelphij        for (i = 0; i < 32; i += 8) {
113296465Sdelphij            D_ENCRYPT(l, r, i + 0); /* 1 */
114296465Sdelphij            D_ENCRYPT(r, l, i + 2); /* 2 */
115296465Sdelphij            D_ENCRYPT(l, r, i + 4); /* 3 */
116296465Sdelphij            D_ENCRYPT(r, l, i + 6); /* 4 */
117296465Sdelphij        }
11855714Skris#endif
119296465Sdelphij    } else {
12055714Skris#ifdef DES_UNROLL
121296465Sdelphij        D_ENCRYPT(l, r, 30);    /* 16 */
122296465Sdelphij        D_ENCRYPT(r, l, 28);    /* 15 */
123296465Sdelphij        D_ENCRYPT(l, r, 26);    /* 14 */
124296465Sdelphij        D_ENCRYPT(r, l, 24);    /* 13 */
125296465Sdelphij        D_ENCRYPT(l, r, 22);    /* 12 */
126296465Sdelphij        D_ENCRYPT(r, l, 20);    /* 11 */
127296465Sdelphij        D_ENCRYPT(l, r, 18);    /* 10 */
128296465Sdelphij        D_ENCRYPT(r, l, 16);    /* 9 */
129296465Sdelphij        D_ENCRYPT(l, r, 14);    /* 8 */
130296465Sdelphij        D_ENCRYPT(r, l, 12);    /* 7 */
131296465Sdelphij        D_ENCRYPT(l, r, 10);    /* 6 */
132296465Sdelphij        D_ENCRYPT(r, l, 8);     /* 5 */
133296465Sdelphij        D_ENCRYPT(l, r, 6);     /* 4 */
134296465Sdelphij        D_ENCRYPT(r, l, 4);     /* 3 */
135296465Sdelphij        D_ENCRYPT(l, r, 2);     /* 2 */
136296465Sdelphij        D_ENCRYPT(r, l, 0);     /* 1 */
13755714Skris#else
138296465Sdelphij        for (i = 30; i > 0; i -= 8) {
139296465Sdelphij            D_ENCRYPT(l, r, i - 0); /* 16 */
140296465Sdelphij            D_ENCRYPT(r, l, i - 2); /* 15 */
141296465Sdelphij            D_ENCRYPT(l, r, i - 4); /* 14 */
142296465Sdelphij            D_ENCRYPT(r, l, i - 6); /* 13 */
143296465Sdelphij        }
14455714Skris#endif
145296465Sdelphij    }
14655714Skris
147296465Sdelphij    /* rotate and clear the top bits on machines with 8byte longs */
148296465Sdelphij    l = ROTATE(l, 3) & 0xffffffffL;
149296465Sdelphij    r = ROTATE(r, 3) & 0xffffffffL;
15055714Skris
151296465Sdelphij    FP(r, l);
152296465Sdelphij    data[0] = l;
153296465Sdelphij    data[1] = r;
154296465Sdelphij    l = r = t = u = 0;
155296465Sdelphij}
15655714Skris
157109998Smarkmvoid DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc)
158296465Sdelphij{
159296465Sdelphij    register DES_LONG l, r, t, u;
16055714Skris#ifdef DES_PTR
161296465Sdelphij    register const unsigned char *des_SP = (const unsigned char *)DES_SPtrans;
16255714Skris#endif
16355714Skris#ifndef DES_UNROLL
164296465Sdelphij    register int i;
16555714Skris#endif
166296465Sdelphij    register DES_LONG *s;
16755714Skris
168296465Sdelphij    r = data[0];
169296465Sdelphij    l = data[1];
17055714Skris
171296465Sdelphij    /*
172296465Sdelphij     * Things have been modified so that the initial rotate is done outside
173296465Sdelphij     * the loop.  This required the DES_SPtrans values in sp.h to be rotated
174296465Sdelphij     * 1 bit to the right. One perl script later and things have a 5% speed
175296465Sdelphij     * up on a sparc2. Thanks to Richard Outerbridge
176296465Sdelphij     * <71755.204@CompuServe.COM> for pointing this out.
177296465Sdelphij     */
178296465Sdelphij    /* clear the top bits on machines with 8byte longs */
179296465Sdelphij    r = ROTATE(r, 29) & 0xffffffffL;
180296465Sdelphij    l = ROTATE(l, 29) & 0xffffffffL;
18155714Skris
182296465Sdelphij    s = ks->ks->deslong;
183296465Sdelphij    /*
184296465Sdelphij     * I don't know if it is worth the effort of loop unrolling the inner
185296465Sdelphij     * loop
186296465Sdelphij     */
187296465Sdelphij    if (enc) {
18855714Skris#ifdef DES_UNROLL
189296465Sdelphij        D_ENCRYPT(l, r, 0);     /* 1 */
190296465Sdelphij        D_ENCRYPT(r, l, 2);     /* 2 */
191296465Sdelphij        D_ENCRYPT(l, r, 4);     /* 3 */
192296465Sdelphij        D_ENCRYPT(r, l, 6);     /* 4 */
193296465Sdelphij        D_ENCRYPT(l, r, 8);     /* 5 */
194296465Sdelphij        D_ENCRYPT(r, l, 10);    /* 6 */
195296465Sdelphij        D_ENCRYPT(l, r, 12);    /* 7 */
196296465Sdelphij        D_ENCRYPT(r, l, 14);    /* 8 */
197296465Sdelphij        D_ENCRYPT(l, r, 16);    /* 9 */
198296465Sdelphij        D_ENCRYPT(r, l, 18);    /* 10 */
199296465Sdelphij        D_ENCRYPT(l, r, 20);    /* 11 */
200296465Sdelphij        D_ENCRYPT(r, l, 22);    /* 12 */
201296465Sdelphij        D_ENCRYPT(l, r, 24);    /* 13 */
202296465Sdelphij        D_ENCRYPT(r, l, 26);    /* 14 */
203296465Sdelphij        D_ENCRYPT(l, r, 28);    /* 15 */
204296465Sdelphij        D_ENCRYPT(r, l, 30);    /* 16 */
20555714Skris#else
206296465Sdelphij        for (i = 0; i < 32; i += 8) {
207296465Sdelphij            D_ENCRYPT(l, r, i + 0); /* 1 */
208296465Sdelphij            D_ENCRYPT(r, l, i + 2); /* 2 */
209296465Sdelphij            D_ENCRYPT(l, r, i + 4); /* 3 */
210296465Sdelphij            D_ENCRYPT(r, l, i + 6); /* 4 */
211296465Sdelphij        }
21255714Skris#endif
213296465Sdelphij    } else {
21455714Skris#ifdef DES_UNROLL
215296465Sdelphij        D_ENCRYPT(l, r, 30);    /* 16 */
216296465Sdelphij        D_ENCRYPT(r, l, 28);    /* 15 */
217296465Sdelphij        D_ENCRYPT(l, r, 26);    /* 14 */
218296465Sdelphij        D_ENCRYPT(r, l, 24);    /* 13 */
219296465Sdelphij        D_ENCRYPT(l, r, 22);    /* 12 */
220296465Sdelphij        D_ENCRYPT(r, l, 20);    /* 11 */
221296465Sdelphij        D_ENCRYPT(l, r, 18);    /* 10 */
222296465Sdelphij        D_ENCRYPT(r, l, 16);    /* 9 */
223296465Sdelphij        D_ENCRYPT(l, r, 14);    /* 8 */
224296465Sdelphij        D_ENCRYPT(r, l, 12);    /* 7 */
225296465Sdelphij        D_ENCRYPT(l, r, 10);    /* 6 */
226296465Sdelphij        D_ENCRYPT(r, l, 8);     /* 5 */
227296465Sdelphij        D_ENCRYPT(l, r, 6);     /* 4 */
228296465Sdelphij        D_ENCRYPT(r, l, 4);     /* 3 */
229296465Sdelphij        D_ENCRYPT(l, r, 2);     /* 2 */
230296465Sdelphij        D_ENCRYPT(r, l, 0);     /* 1 */
23155714Skris#else
232296465Sdelphij        for (i = 30; i > 0; i -= 8) {
233296465Sdelphij            D_ENCRYPT(l, r, i - 0); /* 16 */
234296465Sdelphij            D_ENCRYPT(r, l, i - 2); /* 15 */
235296465Sdelphij            D_ENCRYPT(l, r, i - 4); /* 14 */
236296465Sdelphij            D_ENCRYPT(r, l, i - 6); /* 13 */
237296465Sdelphij        }
23855714Skris#endif
239296465Sdelphij    }
240296465Sdelphij    /* rotate and clear the top bits on machines with 8byte longs */
241296465Sdelphij    data[0] = ROTATE(l, 3) & 0xffffffffL;
242296465Sdelphij    data[1] = ROTATE(r, 3) & 0xffffffffL;
243296465Sdelphij    l = r = t = u = 0;
244296465Sdelphij}
24555714Skris
246109998Smarkmvoid DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
247296465Sdelphij                  DES_key_schedule *ks2, DES_key_schedule *ks3)
248296465Sdelphij{
249296465Sdelphij    register DES_LONG l, r;
25055714Skris
251296465Sdelphij    l = data[0];
252296465Sdelphij    r = data[1];
253296465Sdelphij    IP(l, r);
254296465Sdelphij    data[0] = l;
255296465Sdelphij    data[1] = r;
256296465Sdelphij    DES_encrypt2((DES_LONG *)data, ks1, DES_ENCRYPT);
257296465Sdelphij    DES_encrypt2((DES_LONG *)data, ks2, DES_DECRYPT);
258296465Sdelphij    DES_encrypt2((DES_LONG *)data, ks3, DES_ENCRYPT);
259296465Sdelphij    l = data[0];
260296465Sdelphij    r = data[1];
261296465Sdelphij    FP(r, l);
262296465Sdelphij    data[0] = l;
263296465Sdelphij    data[1] = r;
264296465Sdelphij}
26555714Skris
266109998Smarkmvoid DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
267296465Sdelphij                  DES_key_schedule *ks2, DES_key_schedule *ks3)
268296465Sdelphij{
269296465Sdelphij    register DES_LONG l, r;
27055714Skris
271296465Sdelphij    l = data[0];
272296465Sdelphij    r = data[1];
273296465Sdelphij    IP(l, r);
274296465Sdelphij    data[0] = l;
275296465Sdelphij    data[1] = r;
276296465Sdelphij    DES_encrypt2((DES_LONG *)data, ks3, DES_DECRYPT);
277296465Sdelphij    DES_encrypt2((DES_LONG *)data, ks2, DES_ENCRYPT);
278296465Sdelphij    DES_encrypt2((DES_LONG *)data, ks1, DES_DECRYPT);
279296465Sdelphij    l = data[0];
280296465Sdelphij    r = data[1];
281296465Sdelphij    FP(r, l);
282296465Sdelphij    data[0] = l;
283296465Sdelphij    data[1] = r;
284296465Sdelphij}
28555714Skris
28655714Skris#ifndef DES_DEFAULT_OPTIONS
28755714Skris
288296465Sdelphij# if !defined(OPENSSL_FIPS_DES_ASM)
289194206Ssimon
290296465Sdelphij#  undef CBC_ENC_C__DONT_UPDATE_IV
291296465Sdelphij#  include "ncbc_enc.c"         /* DES_ncbc_encrypt */
29255714Skris
293109998Smarkmvoid DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
294296465Sdelphij                          long length, DES_key_schedule *ks1,
295296465Sdelphij                          DES_key_schedule *ks2, DES_key_schedule *ks3,
296296465Sdelphij                          DES_cblock *ivec, int enc)
297296465Sdelphij{
298296465Sdelphij    register DES_LONG tin0, tin1;
299296465Sdelphij    register DES_LONG tout0, tout1, xor0, xor1;
300296465Sdelphij    register const unsigned char *in;
301296465Sdelphij    unsigned char *out;
302296465Sdelphij    register long l = length;
303296465Sdelphij    DES_LONG tin[2];
304296465Sdelphij    unsigned char *iv;
30555714Skris
306296465Sdelphij    in = input;
307296465Sdelphij    out = output;
308296465Sdelphij    iv = &(*ivec)[0];
30955714Skris
310296465Sdelphij    if (enc) {
311296465Sdelphij        c2l(iv, tout0);
312296465Sdelphij        c2l(iv, tout1);
313296465Sdelphij        for (l -= 8; l >= 0; l -= 8) {
314296465Sdelphij            c2l(in, tin0);
315296465Sdelphij            c2l(in, tin1);
316296465Sdelphij            tin0 ^= tout0;
317296465Sdelphij            tin1 ^= tout1;
31855714Skris
319296465Sdelphij            tin[0] = tin0;
320296465Sdelphij            tin[1] = tin1;
321296465Sdelphij            DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
322296465Sdelphij            tout0 = tin[0];
323296465Sdelphij            tout1 = tin[1];
32455714Skris
325296465Sdelphij            l2c(tout0, out);
326296465Sdelphij            l2c(tout1, out);
327296465Sdelphij        }
328296465Sdelphij        if (l != -8) {
329296465Sdelphij            c2ln(in, tin0, tin1, l + 8);
330296465Sdelphij            tin0 ^= tout0;
331296465Sdelphij            tin1 ^= tout1;
33255714Skris
333296465Sdelphij            tin[0] = tin0;
334296465Sdelphij            tin[1] = tin1;
335296465Sdelphij            DES_encrypt3((DES_LONG *)tin, ks1, ks2, ks3);
336296465Sdelphij            tout0 = tin[0];
337296465Sdelphij            tout1 = tin[1];
33855714Skris
339296465Sdelphij            l2c(tout0, out);
340296465Sdelphij            l2c(tout1, out);
341296465Sdelphij        }
342296465Sdelphij        iv = &(*ivec)[0];
343296465Sdelphij        l2c(tout0, iv);
344296465Sdelphij        l2c(tout1, iv);
345296465Sdelphij    } else {
346296465Sdelphij        register DES_LONG t0, t1;
34755714Skris
348296465Sdelphij        c2l(iv, xor0);
349296465Sdelphij        c2l(iv, xor1);
350296465Sdelphij        for (l -= 8; l >= 0; l -= 8) {
351296465Sdelphij            c2l(in, tin0);
352296465Sdelphij            c2l(in, tin1);
35355714Skris
354296465Sdelphij            t0 = tin0;
355296465Sdelphij            t1 = tin1;
35655714Skris
357296465Sdelphij            tin[0] = tin0;
358296465Sdelphij            tin[1] = tin1;
359296465Sdelphij            DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
360296465Sdelphij            tout0 = tin[0];
361296465Sdelphij            tout1 = tin[1];
36255714Skris
363296465Sdelphij            tout0 ^= xor0;
364296465Sdelphij            tout1 ^= xor1;
365296465Sdelphij            l2c(tout0, out);
366296465Sdelphij            l2c(tout1, out);
367296465Sdelphij            xor0 = t0;
368296465Sdelphij            xor1 = t1;
369296465Sdelphij        }
370296465Sdelphij        if (l != -8) {
371296465Sdelphij            c2l(in, tin0);
372296465Sdelphij            c2l(in, tin1);
37355714Skris
374296465Sdelphij            t0 = tin0;
375296465Sdelphij            t1 = tin1;
37655714Skris
377296465Sdelphij            tin[0] = tin0;
378296465Sdelphij            tin[1] = tin1;
379296465Sdelphij            DES_decrypt3((DES_LONG *)tin, ks1, ks2, ks3);
380296465Sdelphij            tout0 = tin[0];
381296465Sdelphij            tout1 = tin[1];
38255714Skris
383296465Sdelphij            tout0 ^= xor0;
384296465Sdelphij            tout1 ^= xor1;
385296465Sdelphij            l2cn(tout0, tout1, out, l + 8);
386296465Sdelphij            xor0 = t0;
387296465Sdelphij            xor1 = t1;
388296465Sdelphij        }
389194206Ssimon
390296465Sdelphij        iv = &(*ivec)[0];
391296465Sdelphij        l2c(xor0, iv);
392296465Sdelphij        l2c(xor1, iv);
393296465Sdelphij    }
394296465Sdelphij    tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
395296465Sdelphij    tin[0] = tin[1] = 0;
396296465Sdelphij}
397296465Sdelphij
398296465Sdelphij# endif
399296465Sdelphij
400296465Sdelphij#endif                          /* DES_DEFAULT_OPTIONS */
401