bf_enc.c revision 1.1
11.1Sthorpej/*	$NetBSD: bf_enc.c,v 1.1 2000/06/14 19:45:33 thorpej Exp $	*/
21.1Sthorpej/*	$KAME: bf_enc.c,v 1.3 2000/03/27 04:36:26 sumikawa Exp $	*/
31.1Sthorpej
41.1Sthorpej/* crypto/bf/bf_enc.c */
51.1Sthorpej/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
61.1Sthorpej * All rights reserved.
71.1Sthorpej *
81.1Sthorpej * This package is an SSL implementation written
91.1Sthorpej * by Eric Young (eay@mincom.oz.au).
101.1Sthorpej * The implementation was written so as to conform with Netscapes SSL.
111.1Sthorpej *
121.1Sthorpej * This library is free for commercial and non-commercial use as long as
131.1Sthorpej * the following conditions are aheared to.  The following conditions
141.1Sthorpej * apply to all code found in this distribution, be it the RC4, RSA,
151.1Sthorpej * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
161.1Sthorpej * included with this distribution is covered by the same copyright terms
171.1Sthorpej * except that the holder is Tim Hudson (tjh@mincom.oz.au).
181.1Sthorpej *
191.1Sthorpej * Copyright remains Eric Young's, and as such any Copyright notices in
201.1Sthorpej * the code are not to be removed.
211.1Sthorpej * If this package is used in a product, Eric Young should be given attribution
221.1Sthorpej * as the author of the parts of the library used.
231.1Sthorpej * This can be in the form of a textual message at program startup or
241.1Sthorpej * in documentation (online or textual) provided with the package.
251.1Sthorpej *
261.1Sthorpej * Redistribution and use in source and binary forms, with or without
271.1Sthorpej * modification, are permitted provided that the following conditions
281.1Sthorpej * are met:
291.1Sthorpej * 1. Redistributions of source code must retain the copyright
301.1Sthorpej *    notice, this list of conditions and the following disclaimer.
311.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
321.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
331.1Sthorpej *    documentation and/or other materials provided with the distribution.
341.1Sthorpej * 3. All advertising materials mentioning features or use of this software
351.1Sthorpej *    must display the following acknowledgement:
361.1Sthorpej *    "This product includes cryptographic software written by
371.1Sthorpej *     Eric Young (eay@mincom.oz.au)"
381.1Sthorpej *    The word 'cryptographic' can be left out if the rouines from the library
391.1Sthorpej *    being used are not cryptographic related :-).
401.1Sthorpej * 4. If you include any Windows specific code (or a derivative thereof) from
411.1Sthorpej *    the apps directory (application code) you must include an acknowledgement:
421.1Sthorpej *    "This product includes software written by Tim Hudson (tjh@mincom.oz.au)"
431.1Sthorpej *
441.1Sthorpej * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
451.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
461.1Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
471.1Sthorpej * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
481.1Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
491.1Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
501.1Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
511.1Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
521.1Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
531.1Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
541.1Sthorpej * SUCH DAMAGE.
551.1Sthorpej *
561.1Sthorpej * The licence and distribution terms for any publically available version or
571.1Sthorpej * derivative of this code cannot be changed.  i.e. this code cannot simply be
581.1Sthorpej * copied and put under another distribution licence
591.1Sthorpej * [including the GNU Public Licence.]
601.1Sthorpej */
611.1Sthorpej
621.1Sthorpej#include <crypto/blowfish/blowfish.h>
631.1Sthorpej#include <crypto/blowfish/bf_locl.h>
641.1Sthorpej
651.1Sthorpej/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
661.1Sthorpej * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
671.1Sthorpej * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
681.1Sthorpej */
691.1Sthorpej
701.1Sthorpej#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
711.1SthorpejIf you set BF_ROUNDS to some value other than 16 or 20, you will have
721.1Sthorpejto modify the code.
731.1Sthorpej#endif
741.1Sthorpej
751.1Sthorpejvoid BF_encrypt(data,key,encrypt)
761.1SthorpejBF_LONG *data;
771.1SthorpejBF_KEY *key;
781.1Sthorpejint encrypt;
791.1Sthorpej	{
801.1Sthorpej	register BF_LONG l,r,*p,*s;
811.1Sthorpej
821.1Sthorpej	p=key->P;
831.1Sthorpej	s= &(key->S[0]);
841.1Sthorpej	l=data[0];
851.1Sthorpej	r=data[1];
861.1Sthorpej
871.1Sthorpej	if (encrypt)
881.1Sthorpej		{
891.1Sthorpej		l^=p[0];
901.1Sthorpej		BF_ENC(r,l,s,p[ 1]);
911.1Sthorpej		BF_ENC(l,r,s,p[ 2]);
921.1Sthorpej		BF_ENC(r,l,s,p[ 3]);
931.1Sthorpej		BF_ENC(l,r,s,p[ 4]);
941.1Sthorpej		BF_ENC(r,l,s,p[ 5]);
951.1Sthorpej		BF_ENC(l,r,s,p[ 6]);
961.1Sthorpej		BF_ENC(r,l,s,p[ 7]);
971.1Sthorpej		BF_ENC(l,r,s,p[ 8]);
981.1Sthorpej		BF_ENC(r,l,s,p[ 9]);
991.1Sthorpej		BF_ENC(l,r,s,p[10]);
1001.1Sthorpej		BF_ENC(r,l,s,p[11]);
1011.1Sthorpej		BF_ENC(l,r,s,p[12]);
1021.1Sthorpej		BF_ENC(r,l,s,p[13]);
1031.1Sthorpej		BF_ENC(l,r,s,p[14]);
1041.1Sthorpej		BF_ENC(r,l,s,p[15]);
1051.1Sthorpej		BF_ENC(l,r,s,p[16]);
1061.1Sthorpej#if BF_ROUNDS == 20
1071.1Sthorpej		BF_ENC(r,l,s,p[17]);
1081.1Sthorpej		BF_ENC(l,r,s,p[18]);
1091.1Sthorpej		BF_ENC(r,l,s,p[19]);
1101.1Sthorpej		BF_ENC(l,r,s,p[20]);
1111.1Sthorpej#endif
1121.1Sthorpej		r^=p[BF_ROUNDS+1];
1131.1Sthorpej		}
1141.1Sthorpej	else
1151.1Sthorpej		{
1161.1Sthorpej		l^=p[BF_ROUNDS+1];
1171.1Sthorpej#if BF_ROUNDS == 20
1181.1Sthorpej		BF_ENC(r,l,s,p[20]);
1191.1Sthorpej		BF_ENC(l,r,s,p[19]);
1201.1Sthorpej		BF_ENC(r,l,s,p[18]);
1211.1Sthorpej		BF_ENC(l,r,s,p[17]);
1221.1Sthorpej#endif
1231.1Sthorpej		BF_ENC(r,l,s,p[16]);
1241.1Sthorpej		BF_ENC(l,r,s,p[15]);
1251.1Sthorpej		BF_ENC(r,l,s,p[14]);
1261.1Sthorpej		BF_ENC(l,r,s,p[13]);
1271.1Sthorpej		BF_ENC(r,l,s,p[12]);
1281.1Sthorpej		BF_ENC(l,r,s,p[11]);
1291.1Sthorpej		BF_ENC(r,l,s,p[10]);
1301.1Sthorpej		BF_ENC(l,r,s,p[ 9]);
1311.1Sthorpej		BF_ENC(r,l,s,p[ 8]);
1321.1Sthorpej		BF_ENC(l,r,s,p[ 7]);
1331.1Sthorpej		BF_ENC(r,l,s,p[ 6]);
1341.1Sthorpej		BF_ENC(l,r,s,p[ 5]);
1351.1Sthorpej		BF_ENC(r,l,s,p[ 4]);
1361.1Sthorpej		BF_ENC(l,r,s,p[ 3]);
1371.1Sthorpej		BF_ENC(r,l,s,p[ 2]);
1381.1Sthorpej		BF_ENC(l,r,s,p[ 1]);
1391.1Sthorpej		r^=p[0];
1401.1Sthorpej		}
1411.1Sthorpej	data[1]=l&0xffffffff;
1421.1Sthorpej	data[0]=r&0xffffffff;
1431.1Sthorpej	}
144