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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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, 63280304Sjkim RC2_KEY *ks, unsigned char *iv, int encrypt) 64280304Sjkim{ 65280304Sjkim register unsigned long tin0, tin1; 66280304Sjkim register unsigned long tout0, tout1, xor0, xor1; 67280304Sjkim register long l = length; 68280304Sjkim unsigned long tin[2]; 6955714Skris 70280304Sjkim if (encrypt) { 71280304Sjkim c2l(iv, tout0); 72280304Sjkim c2l(iv, tout1); 73280304Sjkim iv -= 8; 74280304Sjkim for (l -= 8; l >= 0; l -= 8) { 75280304Sjkim c2l(in, tin0); 76280304Sjkim c2l(in, tin1); 77280304Sjkim tin0 ^= tout0; 78280304Sjkim tin1 ^= tout1; 79280304Sjkim tin[0] = tin0; 80280304Sjkim tin[1] = tin1; 81280304Sjkim RC2_encrypt(tin, ks); 82280304Sjkim tout0 = tin[0]; 83280304Sjkim l2c(tout0, out); 84280304Sjkim tout1 = tin[1]; 85280304Sjkim l2c(tout1, out); 86280304Sjkim } 87280304Sjkim if (l != -8) { 88280304Sjkim c2ln(in, tin0, tin1, l + 8); 89280304Sjkim tin0 ^= tout0; 90280304Sjkim tin1 ^= tout1; 91280304Sjkim tin[0] = tin0; 92280304Sjkim tin[1] = tin1; 93280304Sjkim RC2_encrypt(tin, ks); 94280304Sjkim tout0 = tin[0]; 95280304Sjkim l2c(tout0, out); 96280304Sjkim tout1 = tin[1]; 97280304Sjkim l2c(tout1, out); 98280304Sjkim } 99280304Sjkim l2c(tout0, iv); 100280304Sjkim l2c(tout1, iv); 101280304Sjkim } else { 102280304Sjkim c2l(iv, xor0); 103280304Sjkim c2l(iv, xor1); 104280304Sjkim iv -= 8; 105280304Sjkim for (l -= 8; l >= 0; l -= 8) { 106280304Sjkim c2l(in, tin0); 107280304Sjkim tin[0] = tin0; 108280304Sjkim c2l(in, tin1); 109280304Sjkim tin[1] = tin1; 110280304Sjkim RC2_decrypt(tin, ks); 111280304Sjkim tout0 = tin[0] ^ xor0; 112280304Sjkim tout1 = tin[1] ^ xor1; 113280304Sjkim l2c(tout0, out); 114280304Sjkim l2c(tout1, out); 115280304Sjkim xor0 = tin0; 116280304Sjkim xor1 = tin1; 117280304Sjkim } 118280304Sjkim if (l != -8) { 119280304Sjkim c2l(in, tin0); 120280304Sjkim tin[0] = tin0; 121280304Sjkim c2l(in, tin1); 122280304Sjkim tin[1] = tin1; 123280304Sjkim RC2_decrypt(tin, ks); 124280304Sjkim tout0 = tin[0] ^ xor0; 125280304Sjkim tout1 = tin[1] ^ xor1; 126280304Sjkim l2cn(tout0, tout1, out, l + 8); 127280304Sjkim xor0 = tin0; 128280304Sjkim xor1 = tin1; 129280304Sjkim } 130280304Sjkim l2c(xor0, iv); 131280304Sjkim l2c(xor1, iv); 132280304Sjkim } 133280304Sjkim tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; 134280304Sjkim tin[0] = tin[1] = 0; 135280304Sjkim} 13655714Skris 13755714Skrisvoid RC2_encrypt(unsigned long *d, RC2_KEY *key) 138280304Sjkim{ 139280304Sjkim int i, n; 140280304Sjkim register RC2_INT *p0, *p1; 141280304Sjkim register RC2_INT x0, x1, x2, x3, t; 142280304Sjkim unsigned long l; 14355714Skris 144280304Sjkim l = d[0]; 145280304Sjkim x0 = (RC2_INT) l & 0xffff; 146280304Sjkim x1 = (RC2_INT) (l >> 16L); 147280304Sjkim l = d[1]; 148280304Sjkim x2 = (RC2_INT) l & 0xffff; 149280304Sjkim x3 = (RC2_INT) (l >> 16L); 15055714Skris 151280304Sjkim n = 3; 152280304Sjkim i = 5; 15355714Skris 154280304Sjkim p0 = p1 = &(key->data[0]); 155280304Sjkim for (;;) { 156280304Sjkim t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff; 157280304Sjkim x0 = (t << 1) | (t >> 15); 158280304Sjkim t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff; 159280304Sjkim x1 = (t << 2) | (t >> 14); 160280304Sjkim t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff; 161280304Sjkim x2 = (t << 3) | (t >> 13); 162280304Sjkim t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff; 163280304Sjkim x3 = (t << 5) | (t >> 11); 16455714Skris 165280304Sjkim if (--i == 0) { 166280304Sjkim if (--n == 0) 167280304Sjkim break; 168280304Sjkim i = (n == 2) ? 6 : 5; 16955714Skris 170280304Sjkim x0 += p1[x3 & 0x3f]; 171280304Sjkim x1 += p1[x0 & 0x3f]; 172280304Sjkim x2 += p1[x1 & 0x3f]; 173280304Sjkim x3 += p1[x2 & 0x3f]; 174280304Sjkim } 175280304Sjkim } 17655714Skris 177280304Sjkim d[0] = 178280304Sjkim (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L); 179280304Sjkim d[1] = 180280304Sjkim (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L); 181280304Sjkim} 18255714Skris 18355714Skrisvoid RC2_decrypt(unsigned long *d, RC2_KEY *key) 184280304Sjkim{ 185280304Sjkim int i, n; 186280304Sjkim register RC2_INT *p0, *p1; 187280304Sjkim register RC2_INT x0, x1, x2, x3, t; 188280304Sjkim unsigned long l; 18955714Skris 190280304Sjkim l = d[0]; 191280304Sjkim x0 = (RC2_INT) l & 0xffff; 192280304Sjkim x1 = (RC2_INT) (l >> 16L); 193280304Sjkim l = d[1]; 194280304Sjkim x2 = (RC2_INT) l & 0xffff; 195280304Sjkim x3 = (RC2_INT) (l >> 16L); 19655714Skris 197280304Sjkim n = 3; 198280304Sjkim i = 5; 19955714Skris 200280304Sjkim p0 = &(key->data[63]); 201280304Sjkim p1 = &(key->data[0]); 202280304Sjkim for (;;) { 203280304Sjkim t = ((x3 << 11) | (x3 >> 5)) & 0xffff; 204280304Sjkim x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff; 205280304Sjkim t = ((x2 << 13) | (x2 >> 3)) & 0xffff; 206280304Sjkim x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff; 207280304Sjkim t = ((x1 << 14) | (x1 >> 2)) & 0xffff; 208280304Sjkim x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff; 209280304Sjkim t = ((x0 << 15) | (x0 >> 1)) & 0xffff; 210280304Sjkim x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff; 21155714Skris 212280304Sjkim if (--i == 0) { 213280304Sjkim if (--n == 0) 214280304Sjkim break; 215280304Sjkim i = (n == 2) ? 6 : 5; 21655714Skris 217280304Sjkim x3 = (x3 - p1[x2 & 0x3f]) & 0xffff; 218280304Sjkim x2 = (x2 - p1[x1 & 0x3f]) & 0xffff; 219280304Sjkim x1 = (x1 - p1[x0 & 0x3f]) & 0xffff; 220280304Sjkim x0 = (x0 - p1[x3 & 0x3f]) & 0xffff; 221280304Sjkim } 222280304Sjkim } 22355714Skris 224280304Sjkim d[0] = 225280304Sjkim (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L); 226280304Sjkim d[1] = 227280304Sjkim (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L); 228280304Sjkim} 229