155714Skris/* crypto/rc2/rc2_cbc.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 <openssl/rc2.h>
6055714Skris#include "rc2_locl.h"
6155714Skris
6268651Skrisvoid RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
63296465Sdelphij                     RC2_KEY *ks, unsigned char *iv, int encrypt)
64296465Sdelphij{
65296465Sdelphij    register unsigned long tin0, tin1;
66296465Sdelphij    register unsigned long tout0, tout1, xor0, xor1;
67296465Sdelphij    register long l = length;
68296465Sdelphij    unsigned long tin[2];
6955714Skris
70296465Sdelphij    if (encrypt) {
71296465Sdelphij        c2l(iv, tout0);
72296465Sdelphij        c2l(iv, tout1);
73296465Sdelphij        iv -= 8;
74296465Sdelphij        for (l -= 8; l >= 0; l -= 8) {
75296465Sdelphij            c2l(in, tin0);
76296465Sdelphij            c2l(in, tin1);
77296465Sdelphij            tin0 ^= tout0;
78296465Sdelphij            tin1 ^= tout1;
79296465Sdelphij            tin[0] = tin0;
80296465Sdelphij            tin[1] = tin1;
81296465Sdelphij            RC2_encrypt(tin, ks);
82296465Sdelphij            tout0 = tin[0];
83296465Sdelphij            l2c(tout0, out);
84296465Sdelphij            tout1 = tin[1];
85296465Sdelphij            l2c(tout1, out);
86296465Sdelphij        }
87296465Sdelphij        if (l != -8) {
88296465Sdelphij            c2ln(in, tin0, tin1, l + 8);
89296465Sdelphij            tin0 ^= tout0;
90296465Sdelphij            tin1 ^= tout1;
91296465Sdelphij            tin[0] = tin0;
92296465Sdelphij            tin[1] = tin1;
93296465Sdelphij            RC2_encrypt(tin, ks);
94296465Sdelphij            tout0 = tin[0];
95296465Sdelphij            l2c(tout0, out);
96296465Sdelphij            tout1 = tin[1];
97296465Sdelphij            l2c(tout1, out);
98296465Sdelphij        }
99296465Sdelphij        l2c(tout0, iv);
100296465Sdelphij        l2c(tout1, iv);
101296465Sdelphij    } else {
102296465Sdelphij        c2l(iv, xor0);
103296465Sdelphij        c2l(iv, xor1);
104296465Sdelphij        iv -= 8;
105296465Sdelphij        for (l -= 8; l >= 0; l -= 8) {
106296465Sdelphij            c2l(in, tin0);
107296465Sdelphij            tin[0] = tin0;
108296465Sdelphij            c2l(in, tin1);
109296465Sdelphij            tin[1] = tin1;
110296465Sdelphij            RC2_decrypt(tin, ks);
111296465Sdelphij            tout0 = tin[0] ^ xor0;
112296465Sdelphij            tout1 = tin[1] ^ xor1;
113296465Sdelphij            l2c(tout0, out);
114296465Sdelphij            l2c(tout1, out);
115296465Sdelphij            xor0 = tin0;
116296465Sdelphij            xor1 = tin1;
117296465Sdelphij        }
118296465Sdelphij        if (l != -8) {
119296465Sdelphij            c2l(in, tin0);
120296465Sdelphij            tin[0] = tin0;
121296465Sdelphij            c2l(in, tin1);
122296465Sdelphij            tin[1] = tin1;
123296465Sdelphij            RC2_decrypt(tin, ks);
124296465Sdelphij            tout0 = tin[0] ^ xor0;
125296465Sdelphij            tout1 = tin[1] ^ xor1;
126296465Sdelphij            l2cn(tout0, tout1, out, l + 8);
127296465Sdelphij            xor0 = tin0;
128296465Sdelphij            xor1 = tin1;
129296465Sdelphij        }
130296465Sdelphij        l2c(xor0, iv);
131296465Sdelphij        l2c(xor1, iv);
132296465Sdelphij    }
133296465Sdelphij    tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
134296465Sdelphij    tin[0] = tin[1] = 0;
135296465Sdelphij}
13655714Skris
13755714Skrisvoid RC2_encrypt(unsigned long *d, RC2_KEY *key)
138296465Sdelphij{
139296465Sdelphij    int i, n;
140296465Sdelphij    register RC2_INT *p0, *p1;
141296465Sdelphij    register RC2_INT x0, x1, x2, x3, t;
142296465Sdelphij    unsigned long l;
14355714Skris
144296465Sdelphij    l = d[0];
145296465Sdelphij    x0 = (RC2_INT) l & 0xffff;
146296465Sdelphij    x1 = (RC2_INT) (l >> 16L);
147296465Sdelphij    l = d[1];
148296465Sdelphij    x2 = (RC2_INT) l & 0xffff;
149296465Sdelphij    x3 = (RC2_INT) (l >> 16L);
15055714Skris
151296465Sdelphij    n = 3;
152296465Sdelphij    i = 5;
15355714Skris
154296465Sdelphij    p0 = p1 = &(key->data[0]);
155296465Sdelphij    for (;;) {
156296465Sdelphij        t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff;
157296465Sdelphij        x0 = (t << 1) | (t >> 15);
158296465Sdelphij        t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff;
159296465Sdelphij        x1 = (t << 2) | (t >> 14);
160296465Sdelphij        t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff;
161296465Sdelphij        x2 = (t << 3) | (t >> 13);
162296465Sdelphij        t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff;
163296465Sdelphij        x3 = (t << 5) | (t >> 11);
16455714Skris
165296465Sdelphij        if (--i == 0) {
166296465Sdelphij            if (--n == 0)
167296465Sdelphij                break;
168296465Sdelphij            i = (n == 2) ? 6 : 5;
16955714Skris
170296465Sdelphij            x0 += p1[x3 & 0x3f];
171296465Sdelphij            x1 += p1[x0 & 0x3f];
172296465Sdelphij            x2 += p1[x1 & 0x3f];
173296465Sdelphij            x3 += p1[x2 & 0x3f];
174296465Sdelphij        }
175296465Sdelphij    }
17655714Skris
177296465Sdelphij    d[0] =
178296465Sdelphij        (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
179296465Sdelphij    d[1] =
180296465Sdelphij        (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
181296465Sdelphij}
18255714Skris
18355714Skrisvoid RC2_decrypt(unsigned long *d, RC2_KEY *key)
184296465Sdelphij{
185296465Sdelphij    int i, n;
186296465Sdelphij    register RC2_INT *p0, *p1;
187296465Sdelphij    register RC2_INT x0, x1, x2, x3, t;
188296465Sdelphij    unsigned long l;
18955714Skris
190296465Sdelphij    l = d[0];
191296465Sdelphij    x0 = (RC2_INT) l & 0xffff;
192296465Sdelphij    x1 = (RC2_INT) (l >> 16L);
193296465Sdelphij    l = d[1];
194296465Sdelphij    x2 = (RC2_INT) l & 0xffff;
195296465Sdelphij    x3 = (RC2_INT) (l >> 16L);
19655714Skris
197296465Sdelphij    n = 3;
198296465Sdelphij    i = 5;
19955714Skris
200296465Sdelphij    p0 = &(key->data[63]);
201296465Sdelphij    p1 = &(key->data[0]);
202296465Sdelphij    for (;;) {
203296465Sdelphij        t = ((x3 << 11) | (x3 >> 5)) & 0xffff;
204296465Sdelphij        x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff;
205296465Sdelphij        t = ((x2 << 13) | (x2 >> 3)) & 0xffff;
206296465Sdelphij        x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff;
207296465Sdelphij        t = ((x1 << 14) | (x1 >> 2)) & 0xffff;
208296465Sdelphij        x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff;
209296465Sdelphij        t = ((x0 << 15) | (x0 >> 1)) & 0xffff;
210296465Sdelphij        x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff;
21155714Skris
212296465Sdelphij        if (--i == 0) {
213296465Sdelphij            if (--n == 0)
214296465Sdelphij                break;
215296465Sdelphij            i = (n == 2) ? 6 : 5;
21655714Skris
217296465Sdelphij            x3 = (x3 - p1[x2 & 0x3f]) & 0xffff;
218296465Sdelphij            x2 = (x2 - p1[x1 & 0x3f]) & 0xffff;
219296465Sdelphij            x1 = (x1 - p1[x0 & 0x3f]) & 0xffff;
220296465Sdelphij            x0 = (x0 - p1[x3 & 0x3f]) & 0xffff;
221296465Sdelphij        }
222296465Sdelphij    }
22355714Skris
224296465Sdelphij    d[0] =
225296465Sdelphij        (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
226296465Sdelphij    d[1] =
227296465Sdelphij        (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
228296465Sdelphij}
229