11.11Sandvar/*	$NetBSD: bf_enc.c,v 1.11 2024/02/05 21:46:06 andvar Exp $	*/
21.9Sthorpej
31.1Sthorpej/* crypto/bf/bf_enc.c */
41.4Stls/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
51.1Sthorpej * All rights reserved.
61.1Sthorpej *
71.1Sthorpej * This package is an SSL implementation written
81.4Stls * by Eric Young (eay@cryptsoft.com).
91.1Sthorpej * The implementation was written so as to conform with Netscapes SSL.
101.1Sthorpej *
111.1Sthorpej * This library is free for commercial and non-commercial use as long as
121.1Sthorpej * the following conditions are aheared to.  The following conditions
131.1Sthorpej * apply to all code found in this distribution, be it the RC4, RSA,
141.1Sthorpej * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
151.1Sthorpej * included with this distribution is covered by the same copyright terms
161.4Stls * except that the holder is Tim Hudson (tjh@cryptsoft.com).
171.1Sthorpej *
181.1Sthorpej * Copyright remains Eric Young's, and as such any Copyright notices in
191.1Sthorpej * the code are not to be removed.
201.1Sthorpej * If this package is used in a product, Eric Young should be given attribution
211.1Sthorpej * as the author of the parts of the library used.
221.1Sthorpej * This can be in the form of a textual message at program startup or
231.1Sthorpej * in documentation (online or textual) provided with the package.
241.1Sthorpej *
251.1Sthorpej * Redistribution and use in source and binary forms, with or without
261.1Sthorpej * modification, are permitted provided that the following conditions
271.1Sthorpej * are met:
281.1Sthorpej * 1. Redistributions of source code must retain the copyright
291.1Sthorpej *    notice, this list of conditions and the following disclaimer.
301.1Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
311.1Sthorpej *    notice, this list of conditions and the following disclaimer in the
321.1Sthorpej *    documentation and/or other materials provided with the distribution.
331.1Sthorpej * 3. All advertising materials mentioning features or use of this software
341.1Sthorpej *    must display the following acknowledgement:
351.1Sthorpej *    "This product includes cryptographic software written by
361.4Stls *     Eric Young (eay@cryptsoft.com)"
371.1Sthorpej *    The word 'cryptographic' can be left out if the rouines from the library
381.1Sthorpej *    being used are not cryptographic related :-).
391.1Sthorpej * 4. If you include any Windows specific code (or a derivative thereof) from
401.1Sthorpej *    the apps directory (application code) you must include an acknowledgement:
411.4Stls *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
421.1Sthorpej *
431.1Sthorpej * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
441.1Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
451.1Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
461.1Sthorpej * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
471.1Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
481.1Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
491.1Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
501.1Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
511.1Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
521.1Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
531.1Sthorpej * SUCH DAMAGE.
541.1Sthorpej *
551.1Sthorpej * The licence and distribution terms for any publically available version or
561.1Sthorpej * derivative of this code cannot be changed.  i.e. this code cannot simply be
571.1Sthorpej * copied and put under another distribution licence
581.1Sthorpej * [including the GNU Public Licence.]
591.1Sthorpej */
601.1Sthorpej
611.5Slukem#include <sys/cdefs.h>
621.11Sandvar__KERNEL_RCSID(0, "$NetBSD: bf_enc.c,v 1.11 2024/02/05 21:46:06 andvar Exp $");
631.5Slukem
641.2Sitojun#include <sys/types.h>
651.1Sthorpej#include <crypto/blowfish/blowfish.h>
661.1Sthorpej#include <crypto/blowfish/bf_locl.h>
671.1Sthorpej
681.1Sthorpej/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
691.11Sandvar * (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
701.1Sthorpej * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
711.1Sthorpej */
721.1Sthorpej
731.1Sthorpej#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
741.1SthorpejIf you set BF_ROUNDS to some value other than 16 or 20, you will have
751.1Sthorpejto modify the code.
761.1Sthorpej#endif
771.1Sthorpej
781.3Sitojun/* XXX "data" is host endian */
791.3Sitojunvoid
801.7SthorpejBF_encrypt(BF_LONG *data, const BF_KEY *key)
811.3Sitojun{
821.8Stron	BF_LONG l, r;
831.8Stron	const BF_LONG *p, *s;
841.3Sitojun
851.3Sitojun	p = key->P;
861.3Sitojun	s= &key->S[0];
871.3Sitojun	l = data[0];
881.3Sitojun	r = data[1];
891.1Sthorpej
901.6Sitojun	l^=p[0];
911.6Sitojun	BF_ENC(r, l, s, p[ 1]);
921.6Sitojun	BF_ENC(l, r, s, p[ 2]);
931.6Sitojun	BF_ENC(r, l, s, p[ 3]);
941.6Sitojun	BF_ENC(l, r, s, p[ 4]);
951.6Sitojun	BF_ENC(r, l, s, p[ 5]);
961.6Sitojun	BF_ENC(l, r, s, p[ 6]);
971.6Sitojun	BF_ENC(r, l, s, p[ 7]);
981.6Sitojun	BF_ENC(l, r, s, p[ 8]);
991.6Sitojun	BF_ENC(r, l, s, p[ 9]);
1001.6Sitojun	BF_ENC(l, r, s, p[10]);
1011.6Sitojun	BF_ENC(r, l, s, p[11]);
1021.6Sitojun	BF_ENC(l, r, s, p[12]);
1031.6Sitojun	BF_ENC(r, l, s, p[13]);
1041.6Sitojun	BF_ENC(l, r, s, p[14]);
1051.6Sitojun	BF_ENC(r, l, s, p[15]);
1061.6Sitojun	BF_ENC(l, r, s, p[16]);
1071.1Sthorpej#if BF_ROUNDS == 20
1081.6Sitojun	BF_ENC(r, l, s, p[17]);
1091.6Sitojun	BF_ENC(l, r, s, p[18]);
1101.6Sitojun	BF_ENC(r, l, s, p[19]);
1111.6Sitojun	BF_ENC(l, r, s, p[20]);
1121.1Sthorpej#endif
1131.6Sitojun	r ^= p[BF_ROUNDS + 1];
1141.6Sitojun
1151.6Sitojun	data[1] = l & 0xffffffff;
1161.6Sitojun	data[0] = r & 0xffffffff;
1171.6Sitojun}
1181.6Sitojun
1191.6Sitojun/* XXX "data" is host endian */
1201.6Sitojunvoid
1211.7SthorpejBF_decrypt(BF_LONG *data, const BF_KEY *key)
1221.6Sitojun{
1231.8Stron	BF_LONG l, r;
1241.8Stron	const BF_LONG *p, *s;
1251.6Sitojun
1261.6Sitojun	p = key->P;
1271.6Sitojun	s= &key->S[0];
1281.6Sitojun	l = data[0];
1291.6Sitojun	r = data[1];
1301.6Sitojun
1311.6Sitojun	l ^= p[BF_ROUNDS + 1];
1321.1Sthorpej#if BF_ROUNDS == 20
1331.6Sitojun	BF_ENC(r, l, s, p[20]);
1341.6Sitojun	BF_ENC(l, r, s, p[19]);
1351.6Sitojun	BF_ENC(r, l, s, p[18]);
1361.6Sitojun	BF_ENC(l, r, s, p[17]);
1371.1Sthorpej#endif
1381.6Sitojun	BF_ENC(r, l, s, p[16]);
1391.6Sitojun	BF_ENC(l, r, s, p[15]);
1401.6Sitojun	BF_ENC(r, l, s, p[14]);
1411.6Sitojun	BF_ENC(l, r, s, p[13]);
1421.6Sitojun	BF_ENC(r, l, s, p[12]);
1431.6Sitojun	BF_ENC(l, r, s, p[11]);
1441.6Sitojun	BF_ENC(r, l, s, p[10]);
1451.6Sitojun	BF_ENC(l, r, s, p[ 9]);
1461.6Sitojun	BF_ENC(r, l, s, p[ 8]);
1471.6Sitojun	BF_ENC(l, r, s, p[ 7]);
1481.6Sitojun	BF_ENC(r, l, s, p[ 6]);
1491.6Sitojun	BF_ENC(l, r, s, p[ 5]);
1501.6Sitojun	BF_ENC(r, l, s, p[ 4]);
1511.6Sitojun	BF_ENC(l, r, s, p[ 3]);
1521.6Sitojun	BF_ENC(r, l, s, p[ 2]);
1531.6Sitojun	BF_ENC(l, r, s, p[ 1]);
1541.6Sitojun	r ^= p[0];
1551.6Sitojun
1561.3Sitojun	data[1] = l & 0xffffffff;
1571.3Sitojun	data[0] = r & 0xffffffff;
1581.3Sitojun}
159