bf_enc.c revision 55009
155009Sshin/* crypto/bf/bf_enc.c */
255009Sshin/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
355009Sshin * All rights reserved.
455009Sshin *
555009Sshin * This package is an SSL implementation written
655009Sshin * by Eric Young (eay@mincom.oz.au).
755009Sshin * The implementation was written so as to conform with Netscapes SSL.
855009Sshin *
955009Sshin * This library is free for commercial and non-commercial use as long as
1055009Sshin * the following conditions are aheared to.  The following conditions
1155009Sshin * apply to all code found in this distribution, be it the RC4, RSA,
1255009Sshin * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355009Sshin * included with this distribution is covered by the same copyright terms
1455009Sshin * except that the holder is Tim Hudson (tjh@mincom.oz.au).
1555009Sshin *
1655009Sshin * Copyright remains Eric Young's, and as such any Copyright notices in
1755009Sshin * the code are not to be removed.
1855009Sshin * If this package is used in a product, Eric Young should be given attribution
1955009Sshin * as the author of the parts of the library used.
2055009Sshin * This can be in the form of a textual message at program startup or
2155009Sshin * in documentation (online or textual) provided with the package.
2255009Sshin *
2355009Sshin * Redistribution and use in source and binary forms, with or without
2455009Sshin * modification, are permitted provided that the following conditions
2555009Sshin * are met:
2655009Sshin * 1. Redistributions of source code must retain the copyright
2755009Sshin *    notice, this list of conditions and the following disclaimer.
2855009Sshin * 2. Redistributions in binary form must reproduce the above copyright
2955009Sshin *    notice, this list of conditions and the following disclaimer in the
3055009Sshin *    documentation and/or other materials provided with the distribution.
3155009Sshin * 3. All advertising materials mentioning features or use of this software
3255009Sshin *    must display the following acknowledgement:
3355009Sshin *    "This product includes cryptographic software written by
3455009Sshin *     Eric Young (eay@mincom.oz.au)"
3555009Sshin *    The word 'cryptographic' can be left out if the rouines from the library
3655009Sshin *    being used are not cryptographic related :-).
3755009Sshin * 4. If you include any Windows specific code (or a derivative thereof) from
3855009Sshin *    the apps directory (application code) you must include an acknowledgement:
3955009Sshin *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
4055009Sshin *
4155009Sshin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255009Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355009Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455009Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555009Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655009Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755009Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855009Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955009Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055009Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155009Sshin * SUCH DAMAGE.
5255009Sshin *
5355009Sshin * The licence and distribution terms for any publically available version or
5455009Sshin * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555009Sshin * copied and put under another distribution licence
5655009Sshin * [including the GNU Public Licence.]
5755009Sshin *
5855009Sshin * $FreeBSD: head/sys/crypto/blowfish/bf_enc.c 55009 1999-12-22 19:13:38Z shin $
5955009Sshin */
6055009Sshin
6155009Sshin#include <crypto/blowfish/blowfish.h>
6255009Sshin#include <crypto/blowfish/bf_locl.h>
6355009Sshin
6455009Sshin/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
6555009Sshin * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
6655009Sshin * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
6755009Sshin */
6855009Sshin
6955009Sshin#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
7055009SshinIf you set BF_ROUNDS to some value other than 16 or 20, you will have
7155009Sshinto modify the code.
7255009Sshin#endif
7355009Sshin
7455009Sshinvoid BF_encrypt(data,key,encrypt)
7555009SshinBF_LONG *data;
7655009SshinBF_KEY *key;
7755009Sshinint encrypt;
7855009Sshin	{
7955009Sshin	register BF_LONG l,r,*p,*s;
8055009Sshin
8155009Sshin	p=key->P;
8255009Sshin	s= &(key->S[0]);
8355009Sshin	l=data[0];
8455009Sshin	r=data[1];
8555009Sshin
8655009Sshin	if (encrypt)
8755009Sshin		{
8855009Sshin		l^=p[0];
8955009Sshin		BF_ENC(r,l,s,p[ 1]);
9055009Sshin		BF_ENC(l,r,s,p[ 2]);
9155009Sshin		BF_ENC(r,l,s,p[ 3]);
9255009Sshin		BF_ENC(l,r,s,p[ 4]);
9355009Sshin		BF_ENC(r,l,s,p[ 5]);
9455009Sshin		BF_ENC(l,r,s,p[ 6]);
9555009Sshin		BF_ENC(r,l,s,p[ 7]);
9655009Sshin		BF_ENC(l,r,s,p[ 8]);
9755009Sshin		BF_ENC(r,l,s,p[ 9]);
9855009Sshin		BF_ENC(l,r,s,p[10]);
9955009Sshin		BF_ENC(r,l,s,p[11]);
10055009Sshin		BF_ENC(l,r,s,p[12]);
10155009Sshin		BF_ENC(r,l,s,p[13]);
10255009Sshin		BF_ENC(l,r,s,p[14]);
10355009Sshin		BF_ENC(r,l,s,p[15]);
10455009Sshin		BF_ENC(l,r,s,p[16]);
10555009Sshin#if BF_ROUNDS == 20
10655009Sshin		BF_ENC(r,l,s,p[17]);
10755009Sshin		BF_ENC(l,r,s,p[18]);
10855009Sshin		BF_ENC(r,l,s,p[19]);
10955009Sshin		BF_ENC(l,r,s,p[20]);
11055009Sshin#endif
11155009Sshin		r^=p[BF_ROUNDS+1];
11255009Sshin		}
11355009Sshin	else
11455009Sshin		{
11555009Sshin		l^=p[BF_ROUNDS+1];
11655009Sshin#if BF_ROUNDS == 20
11755009Sshin		BF_ENC(r,l,s,p[20]);
11855009Sshin		BF_ENC(l,r,s,p[19]);
11955009Sshin		BF_ENC(r,l,s,p[18]);
12055009Sshin		BF_ENC(l,r,s,p[17]);
12155009Sshin#endif
12255009Sshin		BF_ENC(r,l,s,p[16]);
12355009Sshin		BF_ENC(l,r,s,p[15]);
12455009Sshin		BF_ENC(r,l,s,p[14]);
12555009Sshin		BF_ENC(l,r,s,p[13]);
12655009Sshin		BF_ENC(r,l,s,p[12]);
12755009Sshin		BF_ENC(l,r,s,p[11]);
12855009Sshin		BF_ENC(r,l,s,p[10]);
12955009Sshin		BF_ENC(l,r,s,p[ 9]);
13055009Sshin		BF_ENC(r,l,s,p[ 8]);
13155009Sshin		BF_ENC(l,r,s,p[ 7]);
13255009Sshin		BF_ENC(r,l,s,p[ 6]);
13355009Sshin		BF_ENC(l,r,s,p[ 5]);
13455009Sshin		BF_ENC(r,l,s,p[ 4]);
13555009Sshin		BF_ENC(l,r,s,p[ 3]);
13655009Sshin		BF_ENC(r,l,s,p[ 2]);
13755009Sshin		BF_ENC(l,r,s,p[ 1]);
13855009Sshin		r^=p[0];
13955009Sshin		}
14055009Sshin	data[1]=l&0xffffffff;
14155009Sshin	data[0]=r&0xffffffff;
14255009Sshin	}
143