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. 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/blowfish.h> 6055714Skris#include "bf_locl.h" 6155714Skris 62280304Sjkim/* 63280304Sjkim * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From 64280304Sjkim * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE 65280304Sjkim * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 6655714Skris */ 6755714Skris 6855714Skris#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 69280304Sjkim# 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) 74280304Sjkim{ 7555714Skris#ifndef BF_PTR2 76280304Sjkim register BF_LONG l, r; 77280304Sjkim register const BF_LONG *p, *s; 7855714Skris 79280304Sjkim p = key->P; 80280304Sjkim s = &(key->S[0]); 81280304Sjkim l = data[0]; 82280304Sjkim r = data[1]; 8355714Skris 84280304Sjkim l ^= p[0]; 85280304Sjkim BF_ENC(r, l, s, p[1]); 86280304Sjkim BF_ENC(l, r, s, p[2]); 87280304Sjkim BF_ENC(r, l, s, p[3]); 88280304Sjkim BF_ENC(l, r, s, p[4]); 89280304Sjkim BF_ENC(r, l, s, p[5]); 90280304Sjkim BF_ENC(l, r, s, p[6]); 91280304Sjkim BF_ENC(r, l, s, p[7]); 92280304Sjkim BF_ENC(l, r, s, p[8]); 93280304Sjkim BF_ENC(r, l, s, p[9]); 94280304Sjkim BF_ENC(l, r, s, p[10]); 95280304Sjkim BF_ENC(r, l, s, p[11]); 96280304Sjkim BF_ENC(l, r, s, p[12]); 97280304Sjkim BF_ENC(r, l, s, p[13]); 98280304Sjkim BF_ENC(l, r, s, p[14]); 99280304Sjkim BF_ENC(r, l, s, p[15]); 100280304Sjkim BF_ENC(l, r, s, p[16]); 101280304Sjkim# if BF_ROUNDS == 20 102280304Sjkim BF_ENC(r, l, s, p[17]); 103280304Sjkim BF_ENC(l, r, s, p[18]); 104280304Sjkim BF_ENC(r, l, s, p[19]); 105280304Sjkim BF_ENC(l, r, s, p[20]); 106280304Sjkim# endif 107280304Sjkim r ^= p[BF_ROUNDS + 1]; 10855714Skris 109280304Sjkim data[1] = l & 0xffffffffL; 110280304Sjkim data[0] = r & 0xffffffffL; 11155714Skris#else 112280304Sjkim register BF_LONG l, r, t, *k; 11355714Skris 114280304Sjkim l = data[0]; 115280304Sjkim r = data[1]; 116280304Sjkim k = (BF_LONG *)key; 11755714Skris 118280304Sjkim l ^= k[0]; 119280304Sjkim BF_ENC(r, l, k, 1); 120280304Sjkim BF_ENC(l, r, k, 2); 121280304Sjkim BF_ENC(r, l, k, 3); 122280304Sjkim BF_ENC(l, r, k, 4); 123280304Sjkim BF_ENC(r, l, k, 5); 124280304Sjkim BF_ENC(l, r, k, 6); 125280304Sjkim BF_ENC(r, l, k, 7); 126280304Sjkim BF_ENC(l, r, k, 8); 127280304Sjkim BF_ENC(r, l, k, 9); 128280304Sjkim BF_ENC(l, r, k, 10); 129280304Sjkim BF_ENC(r, l, k, 11); 130280304Sjkim BF_ENC(l, r, k, 12); 131280304Sjkim BF_ENC(r, l, k, 13); 132280304Sjkim BF_ENC(l, r, k, 14); 133280304Sjkim BF_ENC(r, l, k, 15); 134280304Sjkim BF_ENC(l, r, k, 16); 135280304Sjkim# if BF_ROUNDS == 20 136280304Sjkim BF_ENC(r, l, k, 17); 137280304Sjkim BF_ENC(l, r, k, 18); 138280304Sjkim BF_ENC(r, l, k, 19); 139280304Sjkim BF_ENC(l, r, k, 20); 140280304Sjkim# endif 141280304Sjkim r ^= k[BF_ROUNDS + 1]; 14255714Skris 143280304Sjkim data[1] = l & 0xffffffffL; 144280304Sjkim data[0] = r & 0xffffffffL; 14555714Skris#endif 146280304Sjkim} 14755714Skris 14855714Skris#ifndef BF_DEFAULT_OPTIONS 14955714Skris 15059191Skrisvoid BF_decrypt(BF_LONG *data, const BF_KEY *key) 151280304Sjkim{ 152280304Sjkim# ifndef BF_PTR2 153280304Sjkim register BF_LONG l, r; 154280304Sjkim register const BF_LONG *p, *s; 15555714Skris 156280304Sjkim p = key->P; 157280304Sjkim s = &(key->S[0]); 158280304Sjkim l = data[0]; 159280304Sjkim r = data[1]; 16055714Skris 161280304Sjkim l ^= p[BF_ROUNDS + 1]; 162280304Sjkim# if BF_ROUNDS == 20 163280304Sjkim BF_ENC(r, l, s, p[20]); 164280304Sjkim BF_ENC(l, r, s, p[19]); 165280304Sjkim BF_ENC(r, l, s, p[18]); 166280304Sjkim BF_ENC(l, r, s, p[17]); 167280304Sjkim# endif 168280304Sjkim BF_ENC(r, l, s, p[16]); 169280304Sjkim BF_ENC(l, r, s, p[15]); 170280304Sjkim BF_ENC(r, l, s, p[14]); 171280304Sjkim BF_ENC(l, r, s, p[13]); 172280304Sjkim BF_ENC(r, l, s, p[12]); 173280304Sjkim BF_ENC(l, r, s, p[11]); 174280304Sjkim BF_ENC(r, l, s, p[10]); 175280304Sjkim BF_ENC(l, r, s, p[9]); 176280304Sjkim BF_ENC(r, l, s, p[8]); 177280304Sjkim BF_ENC(l, r, s, p[7]); 178280304Sjkim BF_ENC(r, l, s, p[6]); 179280304Sjkim BF_ENC(l, r, s, p[5]); 180280304Sjkim BF_ENC(r, l, s, p[4]); 181280304Sjkim BF_ENC(l, r, s, p[3]); 182280304Sjkim BF_ENC(r, l, s, p[2]); 183280304Sjkim BF_ENC(l, r, s, p[1]); 184280304Sjkim r ^= p[0]; 18555714Skris 186280304Sjkim data[1] = l & 0xffffffffL; 187280304Sjkim data[0] = r & 0xffffffffL; 188280304Sjkim# else 189280304Sjkim register BF_LONG l, r, t, *k; 19055714Skris 191280304Sjkim l = data[0]; 192280304Sjkim r = data[1]; 193280304Sjkim k = (BF_LONG *)key; 19455714Skris 195280304Sjkim l ^= k[BF_ROUNDS + 1]; 196280304Sjkim# if BF_ROUNDS == 20 197280304Sjkim BF_ENC(r, l, k, 20); 198280304Sjkim BF_ENC(l, r, k, 19); 199280304Sjkim BF_ENC(r, l, k, 18); 200280304Sjkim BF_ENC(l, r, k, 17); 201280304Sjkim# endif 202280304Sjkim BF_ENC(r, l, k, 16); 203280304Sjkim BF_ENC(l, r, k, 15); 204280304Sjkim BF_ENC(r, l, k, 14); 205280304Sjkim BF_ENC(l, r, k, 13); 206280304Sjkim BF_ENC(r, l, k, 12); 207280304Sjkim BF_ENC(l, r, k, 11); 208280304Sjkim BF_ENC(r, l, k, 10); 209280304Sjkim BF_ENC(l, r, k, 9); 210280304Sjkim BF_ENC(r, l, k, 8); 211280304Sjkim BF_ENC(l, r, k, 7); 212280304Sjkim BF_ENC(r, l, k, 6); 213280304Sjkim BF_ENC(l, r, k, 5); 214280304Sjkim BF_ENC(r, l, k, 4); 215280304Sjkim BF_ENC(l, r, k, 3); 216280304Sjkim BF_ENC(r, l, k, 2); 217280304Sjkim BF_ENC(l, r, k, 1); 218280304Sjkim r ^= k[0]; 21955714Skris 220280304Sjkim data[1] = l & 0xffffffffL; 221280304Sjkim data[0] = r & 0xffffffffL; 222280304Sjkim# endif 223280304Sjkim} 22455714Skris 22559191Skrisvoid BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length, 226280304Sjkim const BF_KEY *schedule, unsigned char *ivec, int encrypt) 227280304Sjkim{ 228280304Sjkim register BF_LONG tin0, tin1; 229280304Sjkim register BF_LONG tout0, tout1, xor0, xor1; 230280304Sjkim register long l = length; 231280304Sjkim BF_LONG tin[2]; 23255714Skris 233280304Sjkim if (encrypt) { 234280304Sjkim n2l(ivec, tout0); 235280304Sjkim n2l(ivec, tout1); 236280304Sjkim ivec -= 8; 237280304Sjkim for (l -= 8; l >= 0; l -= 8) { 238280304Sjkim n2l(in, tin0); 239280304Sjkim n2l(in, tin1); 240280304Sjkim tin0 ^= tout0; 241280304Sjkim tin1 ^= tout1; 242280304Sjkim tin[0] = tin0; 243280304Sjkim tin[1] = tin1; 244280304Sjkim BF_encrypt(tin, schedule); 245280304Sjkim tout0 = tin[0]; 246280304Sjkim tout1 = tin[1]; 247280304Sjkim l2n(tout0, out); 248280304Sjkim l2n(tout1, out); 249280304Sjkim } 250280304Sjkim if (l != -8) { 251280304Sjkim n2ln(in, tin0, tin1, l + 8); 252280304Sjkim tin0 ^= tout0; 253280304Sjkim tin1 ^= tout1; 254280304Sjkim tin[0] = tin0; 255280304Sjkim tin[1] = tin1; 256280304Sjkim BF_encrypt(tin, schedule); 257280304Sjkim tout0 = tin[0]; 258280304Sjkim tout1 = tin[1]; 259280304Sjkim l2n(tout0, out); 260280304Sjkim l2n(tout1, out); 261280304Sjkim } 262280304Sjkim l2n(tout0, ivec); 263280304Sjkim l2n(tout1, ivec); 264280304Sjkim } else { 265280304Sjkim n2l(ivec, xor0); 266280304Sjkim n2l(ivec, xor1); 267280304Sjkim ivec -= 8; 268280304Sjkim for (l -= 8; l >= 0; l -= 8) { 269280304Sjkim n2l(in, tin0); 270280304Sjkim n2l(in, tin1); 271280304Sjkim tin[0] = tin0; 272280304Sjkim tin[1] = tin1; 273280304Sjkim BF_decrypt(tin, schedule); 274280304Sjkim tout0 = tin[0] ^ xor0; 275280304Sjkim tout1 = tin[1] ^ xor1; 276280304Sjkim l2n(tout0, out); 277280304Sjkim l2n(tout1, out); 278280304Sjkim xor0 = tin0; 279280304Sjkim xor1 = tin1; 280280304Sjkim } 281280304Sjkim if (l != -8) { 282280304Sjkim n2l(in, tin0); 283280304Sjkim n2l(in, tin1); 284280304Sjkim tin[0] = tin0; 285280304Sjkim tin[1] = tin1; 286280304Sjkim BF_decrypt(tin, schedule); 287280304Sjkim tout0 = tin[0] ^ xor0; 288280304Sjkim tout1 = tin[1] ^ xor1; 289280304Sjkim l2nn(tout0, tout1, out, l + 8); 290280304Sjkim xor0 = tin0; 291280304Sjkim xor1 = tin1; 292280304Sjkim } 293280304Sjkim l2n(xor0, ivec); 294280304Sjkim l2n(xor1, ivec); 295280304Sjkim } 296280304Sjkim tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; 297280304Sjkim tin[0] = tin[1] = 0; 298280304Sjkim} 29955714Skris 30055714Skris#endif 301