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. 8296341Sdelphij * 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). 15296341Sdelphij * 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. 22296341Sdelphij * 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 :-). 37296341Sdelphij * 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)" 40296341Sdelphij * 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. 52296341Sdelphij * 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 62296341Sdelphij/* 63296341Sdelphij * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From 64296341Sdelphij * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE 65296341Sdelphij * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 6655714Skris */ 6755714Skris 6855714Skris#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 69296341Sdelphij# 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) 74296341Sdelphij{ 7555714Skris#ifndef BF_PTR2 76296341Sdelphij register BF_LONG l, r; 77296341Sdelphij register const BF_LONG *p, *s; 7855714Skris 79296341Sdelphij p = key->P; 80296341Sdelphij s = &(key->S[0]); 81296341Sdelphij l = data[0]; 82296341Sdelphij r = data[1]; 8355714Skris 84296341Sdelphij l ^= p[0]; 85296341Sdelphij BF_ENC(r, l, s, p[1]); 86296341Sdelphij BF_ENC(l, r, s, p[2]); 87296341Sdelphij BF_ENC(r, l, s, p[3]); 88296341Sdelphij BF_ENC(l, r, s, p[4]); 89296341Sdelphij BF_ENC(r, l, s, p[5]); 90296341Sdelphij BF_ENC(l, r, s, p[6]); 91296341Sdelphij BF_ENC(r, l, s, p[7]); 92296341Sdelphij BF_ENC(l, r, s, p[8]); 93296341Sdelphij BF_ENC(r, l, s, p[9]); 94296341Sdelphij BF_ENC(l, r, s, p[10]); 95296341Sdelphij BF_ENC(r, l, s, p[11]); 96296341Sdelphij BF_ENC(l, r, s, p[12]); 97296341Sdelphij BF_ENC(r, l, s, p[13]); 98296341Sdelphij BF_ENC(l, r, s, p[14]); 99296341Sdelphij BF_ENC(r, l, s, p[15]); 100296341Sdelphij BF_ENC(l, r, s, p[16]); 101296341Sdelphij# if BF_ROUNDS == 20 102296341Sdelphij BF_ENC(r, l, s, p[17]); 103296341Sdelphij BF_ENC(l, r, s, p[18]); 104296341Sdelphij BF_ENC(r, l, s, p[19]); 105296341Sdelphij BF_ENC(l, r, s, p[20]); 106296341Sdelphij# endif 107296341Sdelphij r ^= p[BF_ROUNDS + 1]; 10855714Skris 109296341Sdelphij data[1] = l & 0xffffffffL; 110296341Sdelphij data[0] = r & 0xffffffffL; 11155714Skris#else 112296341Sdelphij register BF_LONG l, r, t, *k; 11355714Skris 114296341Sdelphij l = data[0]; 115296341Sdelphij r = data[1]; 116296341Sdelphij k = (BF_LONG *)key; 11755714Skris 118296341Sdelphij l ^= k[0]; 119296341Sdelphij BF_ENC(r, l, k, 1); 120296341Sdelphij BF_ENC(l, r, k, 2); 121296341Sdelphij BF_ENC(r, l, k, 3); 122296341Sdelphij BF_ENC(l, r, k, 4); 123296341Sdelphij BF_ENC(r, l, k, 5); 124296341Sdelphij BF_ENC(l, r, k, 6); 125296341Sdelphij BF_ENC(r, l, k, 7); 126296341Sdelphij BF_ENC(l, r, k, 8); 127296341Sdelphij BF_ENC(r, l, k, 9); 128296341Sdelphij BF_ENC(l, r, k, 10); 129296341Sdelphij BF_ENC(r, l, k, 11); 130296341Sdelphij BF_ENC(l, r, k, 12); 131296341Sdelphij BF_ENC(r, l, k, 13); 132296341Sdelphij BF_ENC(l, r, k, 14); 133296341Sdelphij BF_ENC(r, l, k, 15); 134296341Sdelphij BF_ENC(l, r, k, 16); 135296341Sdelphij# if BF_ROUNDS == 20 136296341Sdelphij BF_ENC(r, l, k, 17); 137296341Sdelphij BF_ENC(l, r, k, 18); 138296341Sdelphij BF_ENC(r, l, k, 19); 139296341Sdelphij BF_ENC(l, r, k, 20); 140296341Sdelphij# endif 141296341Sdelphij r ^= k[BF_ROUNDS + 1]; 14255714Skris 143296341Sdelphij data[1] = l & 0xffffffffL; 144296341Sdelphij data[0] = r & 0xffffffffL; 14555714Skris#endif 146296341Sdelphij} 14755714Skris 14855714Skris#ifndef BF_DEFAULT_OPTIONS 14955714Skris 15059191Skrisvoid BF_decrypt(BF_LONG *data, const BF_KEY *key) 151296341Sdelphij{ 152296341Sdelphij# ifndef BF_PTR2 153296341Sdelphij register BF_LONG l, r; 154296341Sdelphij register const BF_LONG *p, *s; 15555714Skris 156296341Sdelphij p = key->P; 157296341Sdelphij s = &(key->S[0]); 158296341Sdelphij l = data[0]; 159296341Sdelphij r = data[1]; 16055714Skris 161296341Sdelphij l ^= p[BF_ROUNDS + 1]; 162296341Sdelphij# if BF_ROUNDS == 20 163296341Sdelphij BF_ENC(r, l, s, p[20]); 164296341Sdelphij BF_ENC(l, r, s, p[19]); 165296341Sdelphij BF_ENC(r, l, s, p[18]); 166296341Sdelphij BF_ENC(l, r, s, p[17]); 167296341Sdelphij# endif 168296341Sdelphij BF_ENC(r, l, s, p[16]); 169296341Sdelphij BF_ENC(l, r, s, p[15]); 170296341Sdelphij BF_ENC(r, l, s, p[14]); 171296341Sdelphij BF_ENC(l, r, s, p[13]); 172296341Sdelphij BF_ENC(r, l, s, p[12]); 173296341Sdelphij BF_ENC(l, r, s, p[11]); 174296341Sdelphij BF_ENC(r, l, s, p[10]); 175296341Sdelphij BF_ENC(l, r, s, p[9]); 176296341Sdelphij BF_ENC(r, l, s, p[8]); 177296341Sdelphij BF_ENC(l, r, s, p[7]); 178296341Sdelphij BF_ENC(r, l, s, p[6]); 179296341Sdelphij BF_ENC(l, r, s, p[5]); 180296341Sdelphij BF_ENC(r, l, s, p[4]); 181296341Sdelphij BF_ENC(l, r, s, p[3]); 182296341Sdelphij BF_ENC(r, l, s, p[2]); 183296341Sdelphij BF_ENC(l, r, s, p[1]); 184296341Sdelphij r ^= p[0]; 18555714Skris 186296341Sdelphij data[1] = l & 0xffffffffL; 187296341Sdelphij data[0] = r & 0xffffffffL; 188296341Sdelphij# else 189296341Sdelphij register BF_LONG l, r, t, *k; 19055714Skris 191296341Sdelphij l = data[0]; 192296341Sdelphij r = data[1]; 193296341Sdelphij k = (BF_LONG *)key; 19455714Skris 195296341Sdelphij l ^= k[BF_ROUNDS + 1]; 196296341Sdelphij# if BF_ROUNDS == 20 197296341Sdelphij BF_ENC(r, l, k, 20); 198296341Sdelphij BF_ENC(l, r, k, 19); 199296341Sdelphij BF_ENC(r, l, k, 18); 200296341Sdelphij BF_ENC(l, r, k, 17); 201296341Sdelphij# endif 202296341Sdelphij BF_ENC(r, l, k, 16); 203296341Sdelphij BF_ENC(l, r, k, 15); 204296341Sdelphij BF_ENC(r, l, k, 14); 205296341Sdelphij BF_ENC(l, r, k, 13); 206296341Sdelphij BF_ENC(r, l, k, 12); 207296341Sdelphij BF_ENC(l, r, k, 11); 208296341Sdelphij BF_ENC(r, l, k, 10); 209296341Sdelphij BF_ENC(l, r, k, 9); 210296341Sdelphij BF_ENC(r, l, k, 8); 211296341Sdelphij BF_ENC(l, r, k, 7); 212296341Sdelphij BF_ENC(r, l, k, 6); 213296341Sdelphij BF_ENC(l, r, k, 5); 214296341Sdelphij BF_ENC(r, l, k, 4); 215296341Sdelphij BF_ENC(l, r, k, 3); 216296341Sdelphij BF_ENC(r, l, k, 2); 217296341Sdelphij BF_ENC(l, r, k, 1); 218296341Sdelphij r ^= k[0]; 21955714Skris 220296341Sdelphij data[1] = l & 0xffffffffL; 221296341Sdelphij data[0] = r & 0xffffffffL; 222296341Sdelphij# endif 223296341Sdelphij} 22455714Skris 22559191Skrisvoid BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 226296341Sdelphij const BF_KEY *schedule, unsigned char *ivec, int encrypt) 227296341Sdelphij{ 228296341Sdelphij register BF_LONG tin0, tin1; 229296341Sdelphij register BF_LONG tout0, tout1, xor0, xor1; 230296341Sdelphij register long l = length; 231296341Sdelphij BF_LONG tin[2]; 23255714Skris 233296341Sdelphij if (encrypt) { 234296341Sdelphij n2l(ivec, tout0); 235296341Sdelphij n2l(ivec, tout1); 236296341Sdelphij ivec -= 8; 237296341Sdelphij for (l -= 8; l >= 0; l -= 8) { 238296341Sdelphij n2l(in, tin0); 239296341Sdelphij n2l(in, tin1); 240296341Sdelphij tin0 ^= tout0; 241296341Sdelphij tin1 ^= tout1; 242296341Sdelphij tin[0] = tin0; 243296341Sdelphij tin[1] = tin1; 244296341Sdelphij BF_encrypt(tin, schedule); 245296341Sdelphij tout0 = tin[0]; 246296341Sdelphij tout1 = tin[1]; 247296341Sdelphij l2n(tout0, out); 248296341Sdelphij l2n(tout1, out); 249296341Sdelphij } 250296341Sdelphij if (l != -8) { 251296341Sdelphij n2ln(in, tin0, tin1, l + 8); 252296341Sdelphij tin0 ^= tout0; 253296341Sdelphij tin1 ^= tout1; 254296341Sdelphij tin[0] = tin0; 255296341Sdelphij tin[1] = tin1; 256296341Sdelphij BF_encrypt(tin, schedule); 257296341Sdelphij tout0 = tin[0]; 258296341Sdelphij tout1 = tin[1]; 259296341Sdelphij l2n(tout0, out); 260296341Sdelphij l2n(tout1, out); 261296341Sdelphij } 262296341Sdelphij l2n(tout0, ivec); 263296341Sdelphij l2n(tout1, ivec); 264296341Sdelphij } else { 265296341Sdelphij n2l(ivec, xor0); 266296341Sdelphij n2l(ivec, xor1); 267296341Sdelphij ivec -= 8; 268296341Sdelphij for (l -= 8; l >= 0; l -= 8) { 269296341Sdelphij n2l(in, tin0); 270296341Sdelphij n2l(in, tin1); 271296341Sdelphij tin[0] = tin0; 272296341Sdelphij tin[1] = tin1; 273296341Sdelphij BF_decrypt(tin, schedule); 274296341Sdelphij tout0 = tin[0] ^ xor0; 275296341Sdelphij tout1 = tin[1] ^ xor1; 276296341Sdelphij l2n(tout0, out); 277296341Sdelphij l2n(tout1, out); 278296341Sdelphij xor0 = tin0; 279296341Sdelphij xor1 = tin1; 280296341Sdelphij } 281296341Sdelphij if (l != -8) { 282296341Sdelphij n2l(in, tin0); 283296341Sdelphij n2l(in, tin1); 284296341Sdelphij tin[0] = tin0; 285296341Sdelphij tin[1] = tin1; 286296341Sdelphij BF_decrypt(tin, schedule); 287296341Sdelphij tout0 = tin[0] ^ xor0; 288296341Sdelphij tout1 = tin[1] ^ xor1; 289296341Sdelphij l2nn(tout0, tout1, out, l + 8); 290296341Sdelphij xor0 = tin0; 291296341Sdelphij xor1 = tin1; 292296341Sdelphij } 293296341Sdelphij l2n(xor0, ivec); 294296341Sdelphij l2n(xor1, ivec); 295296341Sdelphij } 296296341Sdelphij tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; 297296341Sdelphij tin[0] = tin[1] = 0; 298296341Sdelphij} 29955714Skris 30055714Skris#endif 301