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/rc2.h>
13 void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
14 RC2_KEY *ks, unsigned char *iv, int encrypt)
16 register unsigned long tin0, tin1;
17 register unsigned long tout0, tout1, xor0, xor1;
18 register long l = length;
25 for (l -= 8; l >= 0; l -= 8) {
39 c2ln(in, tin0, tin1, l + 8);
56 for (l -= 8; l >= 0; l -= 8) {
62 tout0 = tin[0] ^ xor0;
63 tout1 = tin[1] ^ xor1;
75 tout0 = tin[0] ^ xor0;
76 tout1 = tin[1] ^ xor1;
77 l2cn(tout0, tout1, out, l + 8);
84 tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
88 void RC2_encrypt(unsigned long *d, RC2_KEY *key)
91 register RC2_INT *p0, *p1;
92 register RC2_INT x0, x1, x2, x3, t;
96 x0 = (RC2_INT) l & 0xffff;
97 x1 = (RC2_INT) (l >> 16L);
99 x2 = (RC2_INT) l & 0xffff;
100 x3 = (RC2_INT) (l >> 16L);
105 p0 = p1 = &(key->data[0]);
107 t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff;
108 x0 = (t << 1) | (t >> 15);
109 t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff;
110 x1 = (t << 2) | (t >> 14);
111 t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff;
112 x2 = (t << 3) | (t >> 13);
113 t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff;
114 x3 = (t << 5) | (t >> 11);
119 i = (n == 2) ? 6 : 5;
129 (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
131 (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
134 void RC2_decrypt(unsigned long *d, RC2_KEY *key)
137 register RC2_INT *p0, *p1;
138 register RC2_INT x0, x1, x2, x3, t;
142 x0 = (RC2_INT) l & 0xffff;
143 x1 = (RC2_INT) (l >> 16L);
145 x2 = (RC2_INT) l & 0xffff;
146 x3 = (RC2_INT) (l >> 16L);
151 p0 = &(key->data[63]);
152 p1 = &(key->data[0]);
154 t = ((x3 << 11) | (x3 >> 5)) & 0xffff;
155 x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff;
156 t = ((x2 << 13) | (x2 >> 3)) & 0xffff;
157 x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff;
158 t = ((x1 << 14) | (x1 >> 2)) & 0xffff;
159 x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff;
160 t = ((x0 << 15) | (x0 >> 1)) & 0xffff;
161 x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff;
166 i = (n == 2) ? 6 : 5;
168 x3 = (x3 - p1[x2 & 0x3f]) & 0xffff;
169 x2 = (x2 - p1[x1 & 0x3f]) & 0xffff;
170 x1 = (x1 - p1[x0 & 0x3f]) & 0xffff;
171 x0 = (x0 - p1[x3 & 0x3f]) & 0xffff;
176 (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
178 (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);