xref: /openbsd/lib/libcrypto/rc2/rc2_cbc.c (revision 4a925a6a)
1*4a925a6aSbeck /* $OpenBSD: rc2_cbc.c,v 1.8 2023/07/07 13:40:44 beck Exp $ */
25b37fcf3Sryker /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
35b37fcf3Sryker  * All rights reserved.
45b37fcf3Sryker  *
55b37fcf3Sryker  * This package is an SSL implementation written
65b37fcf3Sryker  * by Eric Young (eay@cryptsoft.com).
75b37fcf3Sryker  * The implementation was written so as to conform with Netscapes SSL.
85b37fcf3Sryker  *
95b37fcf3Sryker  * This library is free for commercial and non-commercial use as long as
105b37fcf3Sryker  * the following conditions are aheared to.  The following conditions
115b37fcf3Sryker  * apply to all code found in this distribution, be it the RC4, RSA,
125b37fcf3Sryker  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
135b37fcf3Sryker  * included with this distribution is covered by the same copyright terms
145b37fcf3Sryker  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
155b37fcf3Sryker  *
165b37fcf3Sryker  * Copyright remains Eric Young's, and as such any Copyright notices in
175b37fcf3Sryker  * the code are not to be removed.
185b37fcf3Sryker  * If this package is used in a product, Eric Young should be given attribution
195b37fcf3Sryker  * as the author of the parts of the library used.
205b37fcf3Sryker  * This can be in the form of a textual message at program startup or
215b37fcf3Sryker  * in documentation (online or textual) provided with the package.
225b37fcf3Sryker  *
235b37fcf3Sryker  * Redistribution and use in source and binary forms, with or without
245b37fcf3Sryker  * modification, are permitted provided that the following conditions
255b37fcf3Sryker  * are met:
265b37fcf3Sryker  * 1. Redistributions of source code must retain the copyright
275b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer.
285b37fcf3Sryker  * 2. Redistributions in binary form must reproduce the above copyright
295b37fcf3Sryker  *    notice, this list of conditions and the following disclaimer in the
305b37fcf3Sryker  *    documentation and/or other materials provided with the distribution.
315b37fcf3Sryker  * 3. All advertising materials mentioning features or use of this software
325b37fcf3Sryker  *    must display the following acknowledgement:
335b37fcf3Sryker  *    "This product includes cryptographic software written by
345b37fcf3Sryker  *     Eric Young (eay@cryptsoft.com)"
355b37fcf3Sryker  *    The word 'cryptographic' can be left out if the rouines from the library
365b37fcf3Sryker  *    being used are not cryptographic related :-).
375b37fcf3Sryker  * 4. If you include any Windows specific code (or a derivative thereof) from
385b37fcf3Sryker  *    the apps directory (application code) you must include an acknowledgement:
395b37fcf3Sryker  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
405b37fcf3Sryker  *
415b37fcf3Sryker  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
425b37fcf3Sryker  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
435b37fcf3Sryker  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
445b37fcf3Sryker  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
455b37fcf3Sryker  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
465b37fcf3Sryker  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
475b37fcf3Sryker  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
485b37fcf3Sryker  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
495b37fcf3Sryker  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
505b37fcf3Sryker  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
515b37fcf3Sryker  * SUCH DAMAGE.
525b37fcf3Sryker  *
535b37fcf3Sryker  * The licence and distribution terms for any publically available version or
545b37fcf3Sryker  * derivative of this code cannot be changed.  i.e. this code cannot simply be
555b37fcf3Sryker  * copied and put under another distribution licence
565b37fcf3Sryker  * [including the GNU Public Licence.]
575b37fcf3Sryker  */
585b37fcf3Sryker 
59913ec974Sbeck #include <openssl/rc2.h>
60c9675a23Stb #include "rc2_local.h"
615b37fcf3Sryker 
62aa1fb016Sbeck void
RC2_cbc_encrypt(const unsigned char * in,unsigned char * out,long length,RC2_KEY * ks,unsigned char * iv,int encrypt)63aa1fb016Sbeck RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
64913ec974Sbeck     RC2_KEY *ks, unsigned char *iv, int encrypt)
655b37fcf3Sryker {
6698acf185Sjsg 	unsigned long tin0, tin1;
6798acf185Sjsg 	unsigned long tout0, tout1, xor0, xor1;
6898acf185Sjsg 	long l = length;
695b37fcf3Sryker 	unsigned long tin[2];
705b37fcf3Sryker 
71aa1fb016Sbeck 	if (encrypt) {
725b37fcf3Sryker 		c2l(iv, tout0);
735b37fcf3Sryker 		c2l(iv, tout1);
745b37fcf3Sryker 		iv -= 8;
755b37fcf3Sryker 		for (l -= 8; l >= 0; l -= 8)
765b37fcf3Sryker 		{
775b37fcf3Sryker 			c2l(in, tin0);
785b37fcf3Sryker 			c2l(in, tin1);
795b37fcf3Sryker 			tin0 ^= tout0;
805b37fcf3Sryker 			tin1 ^= tout1;
815b37fcf3Sryker 			tin[0] = tin0;
825b37fcf3Sryker 			tin[1] = tin1;
835b37fcf3Sryker 			RC2_encrypt(tin, ks);
84aa1fb016Sbeck 			tout0 = tin[0];
85aa1fb016Sbeck 			l2c(tout0, out);
86aa1fb016Sbeck 			tout1 = tin[1];
87aa1fb016Sbeck 			l2c(tout1, out);
885b37fcf3Sryker 		}
89aa1fb016Sbeck 		if (l != -8) {
905b37fcf3Sryker 			c2ln(in, tin0, tin1, l + 8);
915b37fcf3Sryker 			tin0 ^= tout0;
925b37fcf3Sryker 			tin1 ^= tout1;
935b37fcf3Sryker 			tin[0] = tin0;
945b37fcf3Sryker 			tin[1] = tin1;
955b37fcf3Sryker 			RC2_encrypt(tin, ks);
96aa1fb016Sbeck 			tout0 = tin[0];
97aa1fb016Sbeck 			l2c(tout0, out);
98aa1fb016Sbeck 			tout1 = tin[1];
99aa1fb016Sbeck 			l2c(tout1, out);
1005b37fcf3Sryker 		}
1015b37fcf3Sryker 		l2c(tout0, iv);
1025b37fcf3Sryker 		l2c(tout1, iv);
103aa1fb016Sbeck 	} else {
1045b37fcf3Sryker 		c2l(iv, xor0);
1055b37fcf3Sryker 		c2l(iv, xor1);
1065b37fcf3Sryker 		iv -= 8;
1075b37fcf3Sryker 		for (l -= 8; l >= 0; l -= 8)
1085b37fcf3Sryker 		{
109aa1fb016Sbeck 			c2l(in, tin0);
110aa1fb016Sbeck 			tin[0] = tin0;
111aa1fb016Sbeck 			c2l(in, tin1);
112aa1fb016Sbeck 			tin[1] = tin1;
1135b37fcf3Sryker 			RC2_decrypt(tin, ks);
1145b37fcf3Sryker 			tout0 = tin[0] ^ xor0;
1155b37fcf3Sryker 			tout1 = tin[1] ^ xor1;
1165b37fcf3Sryker 			l2c(tout0, out);
1175b37fcf3Sryker 			l2c(tout1, out);
1185b37fcf3Sryker 			xor0 = tin0;
1195b37fcf3Sryker 			xor1 = tin1;
1205b37fcf3Sryker 		}
121aa1fb016Sbeck 		if (l != -8) {
122aa1fb016Sbeck 			c2l(in, tin0);
123aa1fb016Sbeck 			tin[0] = tin0;
124aa1fb016Sbeck 			c2l(in, tin1);
125aa1fb016Sbeck 			tin[1] = tin1;
1265b37fcf3Sryker 			RC2_decrypt(tin, ks);
1275b37fcf3Sryker 			tout0 = tin[0] ^ xor0;
1285b37fcf3Sryker 			tout1 = tin[1] ^ xor1;
1295b37fcf3Sryker 			l2cn(tout0, tout1, out, l + 8);
1305b37fcf3Sryker 			xor0 = tin0;
1315b37fcf3Sryker 			xor1 = tin1;
1325b37fcf3Sryker 		}
1335b37fcf3Sryker 		l2c(xor0, iv);
1345b37fcf3Sryker 		l2c(xor1, iv);
1355b37fcf3Sryker 	}
1365b37fcf3Sryker 	tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
1375b37fcf3Sryker 	tin[0] = tin[1] = 0;
1385b37fcf3Sryker }
139*4a925a6aSbeck LCRYPTO_ALIAS(RC2_cbc_encrypt);
1405b37fcf3Sryker 
141aa1fb016Sbeck void
RC2_encrypt(unsigned long * d,RC2_KEY * key)142aa1fb016Sbeck RC2_encrypt(unsigned long *d, RC2_KEY *key)
1435b37fcf3Sryker {
1445b37fcf3Sryker 	int i, n;
14598acf185Sjsg 	RC2_INT *p0, *p1;
14698acf185Sjsg 	RC2_INT x0, x1, x2, x3, t;
1475b37fcf3Sryker 	unsigned long l;
1485b37fcf3Sryker 
1495b37fcf3Sryker 	l = d[0];
1505b37fcf3Sryker 	x0 = (RC2_INT)l & 0xffff;
1515b37fcf3Sryker 	x1 = (RC2_INT)(l >> 16L);
1525b37fcf3Sryker 	l = d[1];
1535b37fcf3Sryker 	x2 = (RC2_INT)l & 0xffff;
1545b37fcf3Sryker 	x3 = (RC2_INT)(l >> 16L);
1555b37fcf3Sryker 
1565b37fcf3Sryker 	n = 3;
1575b37fcf3Sryker 	i = 5;
1585b37fcf3Sryker 
1595b37fcf3Sryker 	p0 = p1 = &(key->data[0]);
160aa1fb016Sbeck 	for (;;) {
1615b37fcf3Sryker 		t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff;
1625b37fcf3Sryker 		x0 = (t << 1)|(t >> 15);
1635b37fcf3Sryker 		t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff;
1645b37fcf3Sryker 		x1 = (t << 2)|(t >> 14);
1655b37fcf3Sryker 		t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff;
1665b37fcf3Sryker 		x2 = (t << 3)|(t >> 13);
1675b37fcf3Sryker 		t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff;
1685b37fcf3Sryker 		x3 = (t << 5)|(t >> 11);
1695b37fcf3Sryker 
170aa1fb016Sbeck 		if (--i == 0) {
171aa1fb016Sbeck 			if (--n == 0)
172aa1fb016Sbeck 				break;
1735b37fcf3Sryker 			i = (n == 2) ? 6 : 5;
1745b37fcf3Sryker 
1755b37fcf3Sryker 			x0 += p1[x3 & 0x3f];
1765b37fcf3Sryker 			x1 += p1[x0 & 0x3f];
1775b37fcf3Sryker 			x2 += p1[x1 & 0x3f];
1785b37fcf3Sryker 			x3 += p1[x2 & 0x3f];
1795b37fcf3Sryker 		}
1805b37fcf3Sryker 	}
1815b37fcf3Sryker 
182aa1fb016Sbeck 	d[0] = (unsigned long)(x0 & 0xffff)|((unsigned long)(x1 & 0xffff) <<
183aa1fb016Sbeck 	    16L);
184aa1fb016Sbeck 	d[1] = (unsigned long)(x2 & 0xffff)|((unsigned long)(x3 & 0xffff) <<
185aa1fb016Sbeck 	    16L);
1865b37fcf3Sryker }
187*4a925a6aSbeck LCRYPTO_ALIAS(RC2_encrypt);
1885b37fcf3Sryker 
189aa1fb016Sbeck void
RC2_decrypt(unsigned long * d,RC2_KEY * key)190aa1fb016Sbeck RC2_decrypt(unsigned long *d, RC2_KEY *key)
1915b37fcf3Sryker {
1925b37fcf3Sryker 	int i, n;
19398acf185Sjsg 	RC2_INT *p0, *p1;
19498acf185Sjsg 	RC2_INT x0, x1, x2, x3, t;
1955b37fcf3Sryker 	unsigned long l;
1965b37fcf3Sryker 
1975b37fcf3Sryker 	l = d[0];
1985b37fcf3Sryker 	x0 = (RC2_INT)l & 0xffff;
1995b37fcf3Sryker 	x1 = (RC2_INT)(l >> 16L);
2005b37fcf3Sryker 	l = d[1];
2015b37fcf3Sryker 	x2 = (RC2_INT)l & 0xffff;
2025b37fcf3Sryker 	x3 = (RC2_INT)(l >> 16L);
2035b37fcf3Sryker 
2045b37fcf3Sryker 	n = 3;
2055b37fcf3Sryker 	i = 5;
2065b37fcf3Sryker 
2075b37fcf3Sryker 	p0 = &(key->data[63]);
2085b37fcf3Sryker 	p1 = &(key->data[0]);
209aa1fb016Sbeck 	for (;;) {
2105b37fcf3Sryker 		t = ((x3 << 11)|(x3 >> 5)) & 0xffff;
2115b37fcf3Sryker 		x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff;
2125b37fcf3Sryker 		t = ((x2 << 13)|(x2 >> 3)) & 0xffff;
2135b37fcf3Sryker 		x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff;
2145b37fcf3Sryker 		t = ((x1 << 14)|(x1 >> 2)) & 0xffff;
2155b37fcf3Sryker 		x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff;
2165b37fcf3Sryker 		t = ((x0 << 15)|(x0 >> 1)) & 0xffff;
2175b37fcf3Sryker 		x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff;
2185b37fcf3Sryker 
219aa1fb016Sbeck 		if (--i == 0) {
220aa1fb016Sbeck 			if (--n == 0)
221aa1fb016Sbeck 				break;
2225b37fcf3Sryker 			i = (n == 2) ? 6 : 5;
2235b37fcf3Sryker 
2245b37fcf3Sryker 			x3 = (x3 - p1[x2 & 0x3f]) & 0xffff;
2255b37fcf3Sryker 			x2 = (x2 - p1[x1 & 0x3f]) & 0xffff;
2265b37fcf3Sryker 			x1 = (x1 - p1[x0 & 0x3f]) & 0xffff;
2275b37fcf3Sryker 			x0 = (x0 - p1[x3 & 0x3f]) & 0xffff;
2285b37fcf3Sryker 		}
2295b37fcf3Sryker 	}
2305b37fcf3Sryker 
231aa1fb016Sbeck 	d[0] = (unsigned long)(x0 & 0xffff)|((unsigned long)(x1 & 0xffff) <<
232aa1fb016Sbeck 	    16L);
233aa1fb016Sbeck 	d[1] = (unsigned long)(x2 & 0xffff)|((unsigned long)(x3 & 0xffff) <<
234aa1fb016Sbeck 	    16L);
2355b37fcf3Sryker }
236*4a925a6aSbeck LCRYPTO_ALIAS(RC2_decrypt);
237