bf_enc.c revision 116174
1165623Sglebius/* $KAME: bf_enc.c,v 1.7 2002/02/27 01:33:59 itojun Exp $ */ 2165623Sglebius 3165623Sglebius/* crypto/bf/bf_enc.c */ 4165623Sglebius 5165623Sglebius/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 6165623Sglebius * All rights reserved. 7165623Sglebius * 8165623Sglebius * This package is an SSL implementation written 9165623Sglebius * by Eric Young (eay@cryptsoft.com). 10165623Sglebius * The implementation was written so as to conform with Netscapes SSL. 11165623Sglebius * 12165623Sglebius * This library is free for commercial and non-commercial use as long as 13165623Sglebius * the following conditions are aheared to. The following conditions 14165623Sglebius * apply to all code found in this distribution, be it the RC4, RSA, 15165623Sglebius * lhash, DES, etc., code; not just the SSL code. The SSL documentation 16165623Sglebius * included with this distribution is covered by the same copyright terms 17165623Sglebius * except that the holder is Tim Hudson (tjh@cryptsoft.com). 18165623Sglebius * 19165623Sglebius * Copyright remains Eric Young's, and as such any Copyright notices in 20165623Sglebius * the code are not to be removed. 21165623Sglebius * If this package is used in a product, Eric Young should be given attribution 22165623Sglebius * as the author of the parts of the library used. 23165623Sglebius * This can be in the form of a textual message at program startup or 24165623Sglebius * in documentation (online or textual) provided with the package. 25165623Sglebius * 26165623Sglebius * Redistribution and use in source and binary forms, with or without 27165623Sglebius * modification, are permitted provided that the following conditions 28165623Sglebius * are met: 29165623Sglebius * 1. Redistributions of source code must retain the copyright 30165623Sglebius * notice, this list of conditions and the following disclaimer. 31165623Sglebius * 2. Redistributions in binary form must reproduce the above copyright 32165623Sglebius * notice, this list of conditions and the following disclaimer in the 33165623Sglebius * documentation and/or other materials provided with the distribution. 34165623Sglebius * 3. All advertising materials mentioning features or use of this software 35165623Sglebius * must display the following acknowledgement: 36165623Sglebius * "This product includes cryptographic software written by 37165623Sglebius * Eric Young (eay@cryptsoft.com)" 38165623Sglebius * The word 'cryptographic' can be left out if the rouines from the library 39165623Sglebius * being used are not cryptographic related :-). 40165623Sglebius * 4. If you include any Windows specific code (or a derivative thereof) from 41233648Seadler * the apps directory (application code) you must include an acknowledgement: 42165623Sglebius * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 43165623Sglebius * 44165623Sglebius * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 45165623Sglebius * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46165623Sglebius * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 47165623Sglebius * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 48165623Sglebius * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49165623Sglebius * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50165623Sglebius * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51165623Sglebius * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52165623Sglebius * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53165623Sglebius * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54165623Sglebius * SUCH DAMAGE. 55165623Sglebius * 56165623Sglebius * The licence and distribution terms for any publically available version or 57242997Sjoel * derivative of this code cannot be changed. i.e. this code cannot simply be 58165623Sglebius * copied and put under another distribution licence 59165623Sglebius * [including the GNU Public Licence.] 60165623Sglebius */ 61165623Sglebius 62165623Sglebius#include <sys/cdefs.h> 63165623Sglebius__FBSDID("$FreeBSD: head/sys/crypto/blowfish/bf_enc.c 116174 2003-06-10 21:44:29Z obrien $"); 64165623Sglebius 65165623Sglebius#include <sys/types.h> 66165623Sglebius#include <crypto/blowfish/blowfish.h> 67165623Sglebius#include <crypto/blowfish/bf_locl.h> 68165623Sglebius 69165623Sglebius/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper' 70165623Sglebius * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, 71165623Sglebius * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993) 72165623Sglebius */ 73165623Sglebius 74165623Sglebius#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20) 75165623SglebiusIf you set BF_ROUNDS to some value other than 16 or 20, you will have 76165623Sglebiusto modify the code. 77242997Sjoel#endif 78165623Sglebius 79165623Sglebius/* XXX "data" is host endian */ 80165623Sglebiusvoid 81165623SglebiusBF_encrypt(data, key) 82165623Sglebius BF_LONG *data; 83165623Sglebius BF_KEY *key; 84165623Sglebius{ 85165623Sglebius register BF_LONG l, r, *p, *s; 86165623Sglebius 87165623Sglebius p = key->P; 88165623Sglebius s= &key->S[0]; 89165623Sglebius l = data[0]; 90165623Sglebius r = data[1]; 91242997Sjoel 92165623Sglebius l^=p[0]; 93165623Sglebius BF_ENC(r, l, s, p[ 1]); 94165623Sglebius BF_ENC(l, r, s, p[ 2]); 95165623Sglebius BF_ENC(r, l, s, p[ 3]); 96165623Sglebius BF_ENC(l, r, s, p[ 4]); 97165623Sglebius BF_ENC(r, l, s, p[ 5]); 98165623Sglebius BF_ENC(l, r, s, p[ 6]); 99165623Sglebius BF_ENC(r, l, s, p[ 7]); 100165623Sglebius BF_ENC(l, r, s, p[ 8]); 101233648Seadler BF_ENC(r, l, s, p[ 9]); 102165623Sglebius BF_ENC(l, r, s, p[10]); 103242997Sjoel BF_ENC(r, l, s, p[11]); 104165623Sglebius BF_ENC(l, r, s, p[12]); 105165623Sglebius BF_ENC(r, l, s, p[13]); 106165623Sglebius BF_ENC(l, r, s, p[14]); 107165623Sglebius BF_ENC(r, l, s, p[15]); 108165623Sglebius BF_ENC(l, r, s, p[16]); 109165623Sglebius#if BF_ROUNDS == 20 110165623Sglebius BF_ENC(r, l, s, p[17]); 111165623Sglebius BF_ENC(l, r, s, p[18]); 112165623Sglebius BF_ENC(r, l, s, p[19]); 113165623Sglebius BF_ENC(l, r, s, p[20]); 114165623Sglebius#endif 115165623Sglebius r ^= p[BF_ROUNDS + 1]; 116165623Sglebius 117242997Sjoel data[1] = l & 0xffffffff; 118165623Sglebius data[0] = r & 0xffffffff; 119242997Sjoel} 120165623Sglebius 121165623Sglebius/* XXX "data" is host endian */ 122165623Sglebiusvoid 123165623SglebiusBF_decrypt(data, key) 124165623Sglebius BF_LONG *data; 125165623Sglebius BF_KEY *key; 126165623Sglebius{ 127165623Sglebius register BF_LONG l, r, *p, *s; 128165623Sglebius 129165623Sglebius p = key->P; 130165623Sglebius s= &key->S[0]; 131165623Sglebius l = data[0]; 132165623Sglebius r = data[1]; 133165623Sglebius 134165623Sglebius l ^= p[BF_ROUNDS + 1]; 135165623Sglebius#if BF_ROUNDS == 20 136165623Sglebius BF_ENC(r, l, s, p[20]); 137165623Sglebius BF_ENC(l, r, s, p[19]); 138165623Sglebius BF_ENC(r, l, s, p[18]); 139165623Sglebius BF_ENC(l, r, s, p[17]); 140165623Sglebius#endif 141165623Sglebius BF_ENC(r, l, s, p[16]); 142165623Sglebius BF_ENC(l, r, s, p[15]); 143165623Sglebius BF_ENC(r, l, s, p[14]); 144233648Seadler BF_ENC(l, r, s, p[13]); 145165623Sglebius BF_ENC(r, l, s, p[12]); 146 BF_ENC(l, r, s, p[11]); 147 BF_ENC(r, l, s, p[10]); 148 BF_ENC(l, r, s, p[ 9]); 149 BF_ENC(r, l, s, p[ 8]); 150 BF_ENC(l, r, s, p[ 7]); 151 BF_ENC(r, l, s, p[ 6]); 152 BF_ENC(l, r, s, p[ 5]); 153 BF_ENC(r, l, s, p[ 4]); 154 BF_ENC(l, r, s, p[ 3]); 155 BF_ENC(r, l, s, p[ 2]); 156 BF_ENC(l, r, s, p[ 1]); 157 r ^= p[0]; 158 159 data[1] = l & 0xffffffff; 160 data[0] = r & 0xffffffff; 161} 162