155714Skris/* crypto/bf/bf_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 <openssl/blowfish.h> 6055714Skris#include "bf_locl.h" 6155714Skris 62296465Sdelphij/* 63296465Sdelphij * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From 64296465Sdelphij * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE 65296465Sdelphij * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 6655714Skris */ 6755714Skris 6855714Skris#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 69296465Sdelphij# error If you set BF_ROUNDS to some value other than 16 or 20, you will have \ 7055714Skristo modify the code. 7155714Skris#endif 7255714Skris 7359191Skrisvoid BF_encrypt(BF_LONG *data, const BF_KEY *key) 74296465Sdelphij{ 7555714Skris#ifndef BF_PTR2 76296465Sdelphij register BF_LONG l, r; 77296465Sdelphij register const BF_LONG *p, *s; 7855714Skris 79296465Sdelphij p = key->P; 80296465Sdelphij s = &(key->S[0]); 81296465Sdelphij l = data[0]; 82296465Sdelphij r = data[1]; 8355714Skris 84296465Sdelphij l ^= p[0]; 85296465Sdelphij BF_ENC(r, l, s, p[1]); 86296465Sdelphij BF_ENC(l, r, s, p[2]); 87296465Sdelphij BF_ENC(r, l, s, p[3]); 88296465Sdelphij BF_ENC(l, r, s, p[4]); 89296465Sdelphij BF_ENC(r, l, s, p[5]); 90296465Sdelphij BF_ENC(l, r, s, p[6]); 91296465Sdelphij BF_ENC(r, l, s, p[7]); 92296465Sdelphij BF_ENC(l, r, s, p[8]); 93296465Sdelphij BF_ENC(r, l, s, p[9]); 94296465Sdelphij BF_ENC(l, r, s, p[10]); 95296465Sdelphij BF_ENC(r, l, s, p[11]); 96296465Sdelphij BF_ENC(l, r, s, p[12]); 97296465Sdelphij BF_ENC(r, l, s, p[13]); 98296465Sdelphij BF_ENC(l, r, s, p[14]); 99296465Sdelphij BF_ENC(r, l, s, p[15]); 100296465Sdelphij BF_ENC(l, r, s, p[16]); 101296465Sdelphij# if BF_ROUNDS == 20 102296465Sdelphij BF_ENC(r, l, s, p[17]); 103296465Sdelphij BF_ENC(l, r, s, p[18]); 104296465Sdelphij BF_ENC(r, l, s, p[19]); 105296465Sdelphij BF_ENC(l, r, s, p[20]); 106296465Sdelphij# endif 107296465Sdelphij r ^= p[BF_ROUNDS + 1]; 10855714Skris 109296465Sdelphij data[1] = l & 0xffffffffL; 110296465Sdelphij data[0] = r & 0xffffffffL; 11155714Skris#else 112296465Sdelphij register BF_LONG l, r, t, *k; 11355714Skris 114296465Sdelphij l = data[0]; 115296465Sdelphij r = data[1]; 116296465Sdelphij k = (BF_LONG *)key; 11755714Skris 118296465Sdelphij l ^= k[0]; 119296465Sdelphij BF_ENC(r, l, k, 1); 120296465Sdelphij BF_ENC(l, r, k, 2); 121296465Sdelphij BF_ENC(r, l, k, 3); 122296465Sdelphij BF_ENC(l, r, k, 4); 123296465Sdelphij BF_ENC(r, l, k, 5); 124296465Sdelphij BF_ENC(l, r, k, 6); 125296465Sdelphij BF_ENC(r, l, k, 7); 126296465Sdelphij BF_ENC(l, r, k, 8); 127296465Sdelphij BF_ENC(r, l, k, 9); 128296465Sdelphij BF_ENC(l, r, k, 10); 129296465Sdelphij BF_ENC(r, l, k, 11); 130296465Sdelphij BF_ENC(l, r, k, 12); 131296465Sdelphij BF_ENC(r, l, k, 13); 132296465Sdelphij BF_ENC(l, r, k, 14); 133296465Sdelphij BF_ENC(r, l, k, 15); 134296465Sdelphij BF_ENC(l, r, k, 16); 135296465Sdelphij# if BF_ROUNDS == 20 136296465Sdelphij BF_ENC(r, l, k, 17); 137296465Sdelphij BF_ENC(l, r, k, 18); 138296465Sdelphij BF_ENC(r, l, k, 19); 139296465Sdelphij BF_ENC(l, r, k, 20); 140296465Sdelphij# endif 141296465Sdelphij r ^= k[BF_ROUNDS + 1]; 14255714Skris 143296465Sdelphij data[1] = l & 0xffffffffL; 144296465Sdelphij data[0] = r & 0xffffffffL; 14555714Skris#endif 146296465Sdelphij} 14755714Skris 14855714Skris#ifndef BF_DEFAULT_OPTIONS 14955714Skris 15059191Skrisvoid BF_decrypt(BF_LONG *data, const BF_KEY *key) 151296465Sdelphij{ 152296465Sdelphij# ifndef BF_PTR2 153296465Sdelphij register BF_LONG l, r; 154296465Sdelphij register const BF_LONG *p, *s; 15555714Skris 156296465Sdelphij p = key->P; 157296465Sdelphij s = &(key->S[0]); 158296465Sdelphij l = data[0]; 159296465Sdelphij r = data[1]; 16055714Skris 161296465Sdelphij l ^= p[BF_ROUNDS + 1]; 162296465Sdelphij# if BF_ROUNDS == 20 163296465Sdelphij BF_ENC(r, l, s, p[20]); 164296465Sdelphij BF_ENC(l, r, s, p[19]); 165296465Sdelphij BF_ENC(r, l, s, p[18]); 166296465Sdelphij BF_ENC(l, r, s, p[17]); 167296465Sdelphij# endif 168296465Sdelphij BF_ENC(r, l, s, p[16]); 169296465Sdelphij BF_ENC(l, r, s, p[15]); 170296465Sdelphij BF_ENC(r, l, s, p[14]); 171296465Sdelphij BF_ENC(l, r, s, p[13]); 172296465Sdelphij BF_ENC(r, l, s, p[12]); 173296465Sdelphij BF_ENC(l, r, s, p[11]); 174296465Sdelphij BF_ENC(r, l, s, p[10]); 175296465Sdelphij BF_ENC(l, r, s, p[9]); 176296465Sdelphij BF_ENC(r, l, s, p[8]); 177296465Sdelphij BF_ENC(l, r, s, p[7]); 178296465Sdelphij BF_ENC(r, l, s, p[6]); 179296465Sdelphij BF_ENC(l, r, s, p[5]); 180296465Sdelphij BF_ENC(r, l, s, p[4]); 181296465Sdelphij BF_ENC(l, r, s, p[3]); 182296465Sdelphij BF_ENC(r, l, s, p[2]); 183296465Sdelphij BF_ENC(l, r, s, p[1]); 184296465Sdelphij r ^= p[0]; 18555714Skris 186296465Sdelphij data[1] = l & 0xffffffffL; 187296465Sdelphij data[0] = r & 0xffffffffL; 188296465Sdelphij# else 189296465Sdelphij register BF_LONG l, r, t, *k; 19055714Skris 191296465Sdelphij l = data[0]; 192296465Sdelphij r = data[1]; 193296465Sdelphij k = (BF_LONG *)key; 19455714Skris 195296465Sdelphij l ^= k[BF_ROUNDS + 1]; 196296465Sdelphij# if BF_ROUNDS == 20 197296465Sdelphij BF_ENC(r, l, k, 20); 198296465Sdelphij BF_ENC(l, r, k, 19); 199296465Sdelphij BF_ENC(r, l, k, 18); 200296465Sdelphij BF_ENC(l, r, k, 17); 201296465Sdelphij# endif 202296465Sdelphij BF_ENC(r, l, k, 16); 203296465Sdelphij BF_ENC(l, r, k, 15); 204296465Sdelphij BF_ENC(r, l, k, 14); 205296465Sdelphij BF_ENC(l, r, k, 13); 206296465Sdelphij BF_ENC(r, l, k, 12); 207296465Sdelphij BF_ENC(l, r, k, 11); 208296465Sdelphij BF_ENC(r, l, k, 10); 209296465Sdelphij BF_ENC(l, r, k, 9); 210296465Sdelphij BF_ENC(r, l, k, 8); 211296465Sdelphij BF_ENC(l, r, k, 7); 212296465Sdelphij BF_ENC(r, l, k, 6); 213296465Sdelphij BF_ENC(l, r, k, 5); 214296465Sdelphij BF_ENC(r, l, k, 4); 215296465Sdelphij BF_ENC(l, r, k, 3); 216296465Sdelphij BF_ENC(r, l, k, 2); 217296465Sdelphij BF_ENC(l, r, k, 1); 218296465Sdelphij r ^= k[0]; 21955714Skris 220296465Sdelphij data[1] = l & 0xffffffffL; 221296465Sdelphij data[0] = r & 0xffffffffL; 222296465Sdelphij# endif 223296465Sdelphij} 22455714Skris 22559191Skrisvoid BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 226296465Sdelphij const BF_KEY *schedule, unsigned char *ivec, int encrypt) 227296465Sdelphij{ 228296465Sdelphij register BF_LONG tin0, tin1; 229296465Sdelphij register BF_LONG tout0, tout1, xor0, xor1; 230296465Sdelphij register long l = length; 231296465Sdelphij BF_LONG tin[2]; 23255714Skris 233296465Sdelphij if (encrypt) { 234296465Sdelphij n2l(ivec, tout0); 235296465Sdelphij n2l(ivec, tout1); 236296465Sdelphij ivec -= 8; 237296465Sdelphij for (l -= 8; l >= 0; l -= 8) { 238296465Sdelphij n2l(in, tin0); 239296465Sdelphij n2l(in, tin1); 240296465Sdelphij tin0 ^= tout0; 241296465Sdelphij tin1 ^= tout1; 242296465Sdelphij tin[0] = tin0; 243296465Sdelphij tin[1] = tin1; 244296465Sdelphij BF_encrypt(tin, schedule); 245296465Sdelphij tout0 = tin[0]; 246296465Sdelphij tout1 = tin[1]; 247296465Sdelphij l2n(tout0, out); 248296465Sdelphij l2n(tout1, out); 249296465Sdelphij } 250296465Sdelphij if (l != -8) { 251296465Sdelphij n2ln(in, tin0, tin1, l + 8); 252296465Sdelphij tin0 ^= tout0; 253296465Sdelphij tin1 ^= tout1; 254296465Sdelphij tin[0] = tin0; 255296465Sdelphij tin[1] = tin1; 256296465Sdelphij BF_encrypt(tin, schedule); 257296465Sdelphij tout0 = tin[0]; 258296465Sdelphij tout1 = tin[1]; 259296465Sdelphij l2n(tout0, out); 260296465Sdelphij l2n(tout1, out); 261296465Sdelphij } 262296465Sdelphij l2n(tout0, ivec); 263296465Sdelphij l2n(tout1, ivec); 264296465Sdelphij } else { 265296465Sdelphij n2l(ivec, xor0); 266296465Sdelphij n2l(ivec, xor1); 267296465Sdelphij ivec -= 8; 268296465Sdelphij for (l -= 8; l >= 0; l -= 8) { 269296465Sdelphij n2l(in, tin0); 270296465Sdelphij n2l(in, tin1); 271296465Sdelphij tin[0] = tin0; 272296465Sdelphij tin[1] = tin1; 273296465Sdelphij BF_decrypt(tin, schedule); 274296465Sdelphij tout0 = tin[0] ^ xor0; 275296465Sdelphij tout1 = tin[1] ^ xor1; 276296465Sdelphij l2n(tout0, out); 277296465Sdelphij l2n(tout1, out); 278296465Sdelphij xor0 = tin0; 279296465Sdelphij xor1 = tin1; 280296465Sdelphij } 281296465Sdelphij if (l != -8) { 282296465Sdelphij n2l(in, tin0); 283296465Sdelphij n2l(in, tin1); 284296465Sdelphij tin[0] = tin0; 285296465Sdelphij tin[1] = tin1; 286296465Sdelphij BF_decrypt(tin, schedule); 287296465Sdelphij tout0 = tin[0] ^ xor0; 288296465Sdelphij tout1 = tin[1] ^ xor1; 289296465Sdelphij l2nn(tout0, tout1, out, l + 8); 290296465Sdelphij xor0 = tin0; 291296465Sdelphij xor1 = tin1; 292296465Sdelphij } 293296465Sdelphij l2n(xor0, ivec); 294296465Sdelphij l2n(xor1, ivec); 295296465Sdelphij } 296296465Sdelphij tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; 297296465Sdelphij tin[0] = tin[1] = 0; 298296465Sdelphij} 29955714Skris 30055714Skris#endif 301