bf_enc.c revision 91671
162587Sitojun/*	$FreeBSD: head/sys/crypto/blowfish/bf_enc.c 91671 2002-03-05 09:19:02Z ume $	*/
291671Sume/*	$KAME: bf_enc.c,v 1.7 2002/02/27 01:33:59 itojun Exp $	*/
362587Sitojun
455009Sshin/* crypto/bf/bf_enc.c */
591671Sume/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
655009Sshin * All rights reserved.
755009Sshin *
855009Sshin * This package is an SSL implementation written
991671Sume * by Eric Young (eay@cryptsoft.com).
1055009Sshin * The implementation was written so as to conform with Netscapes SSL.
1155009Sshin *
1255009Sshin * This library is free for commercial and non-commercial use as long as
1355009Sshin * the following conditions are aheared to.  The following conditions
1455009Sshin * apply to all code found in this distribution, be it the RC4, RSA,
1555009Sshin * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1655009Sshin * included with this distribution is covered by the same copyright terms
1791671Sume * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1855009Sshin *
1955009Sshin * Copyright remains Eric Young's, and as such any Copyright notices in
2055009Sshin * the code are not to be removed.
2155009Sshin * If this package is used in a product, Eric Young should be given attribution
2255009Sshin * as the author of the parts of the library used.
2355009Sshin * This can be in the form of a textual message at program startup or
2455009Sshin * in documentation (online or textual) provided with the package.
2555009Sshin *
2655009Sshin * Redistribution and use in source and binary forms, with or without
2755009Sshin * modification, are permitted provided that the following conditions
2855009Sshin * are met:
2955009Sshin * 1. Redistributions of source code must retain the copyright
3055009Sshin *    notice, this list of conditions and the following disclaimer.
3155009Sshin * 2. Redistributions in binary form must reproduce the above copyright
3255009Sshin *    notice, this list of conditions and the following disclaimer in the
3355009Sshin *    documentation and/or other materials provided with the distribution.
3455009Sshin * 3. All advertising materials mentioning features or use of this software
3555009Sshin *    must display the following acknowledgement:
3655009Sshin *    "This product includes cryptographic software written by
3791671Sume *     Eric Young (eay@cryptsoft.com)"
3855009Sshin *    The word 'cryptographic' can be left out if the rouines from the library
3955009Sshin *    being used are not cryptographic related :-).
4055009Sshin * 4. If you include any Windows specific code (or a derivative thereof) from
4155009Sshin *    the apps directory (application code) you must include an acknowledgement:
4291671Sume *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4355009Sshin *
4455009Sshin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4555009Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4655009Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4755009Sshin * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4855009Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4955009Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5055009Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5155009Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5255009Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5355009Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5455009Sshin * SUCH DAMAGE.
5555009Sshin *
5655009Sshin * The licence and distribution terms for any publically available version or
5755009Sshin * derivative of this code cannot be changed.  i.e. this code cannot simply be
5855009Sshin * copied and put under another distribution licence
5955009Sshin * [including the GNU Public Licence.]
6055009Sshin */
6155009Sshin
6278064Sume#include <sys/types.h>
6355009Sshin#include <crypto/blowfish/blowfish.h>
6455009Sshin#include <crypto/blowfish/bf_locl.h>
6555009Sshin
6655009Sshin/* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
6755009Sshin * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
6855009Sshin * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
6955009Sshin */
7055009Sshin
7155009Sshin#if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
7255009SshinIf you set BF_ROUNDS to some value other than 16 or 20, you will have
7355009Sshinto modify the code.
7455009Sshin#endif
7555009Sshin
7678064Sume/* XXX "data" is host endian */
7778064Sumevoid
7891671SumeBF_encrypt(data, key)
7978064Sume	BF_LONG *data;
8078064Sume	BF_KEY *key;
8178064Sume{
8278064Sume	register BF_LONG l, r, *p, *s;
8355009Sshin
8478064Sume	p = key->P;
8578064Sume	s= &key->S[0];
8678064Sume	l = data[0];
8778064Sume	r = data[1];
8855009Sshin
8991671Sume	l^=p[0];
9091671Sume	BF_ENC(r, l, s, p[ 1]);
9191671Sume	BF_ENC(l, r, s, p[ 2]);
9291671Sume	BF_ENC(r, l, s, p[ 3]);
9391671Sume	BF_ENC(l, r, s, p[ 4]);
9491671Sume	BF_ENC(r, l, s, p[ 5]);
9591671Sume	BF_ENC(l, r, s, p[ 6]);
9691671Sume	BF_ENC(r, l, s, p[ 7]);
9791671Sume	BF_ENC(l, r, s, p[ 8]);
9891671Sume	BF_ENC(r, l, s, p[ 9]);
9991671Sume	BF_ENC(l, r, s, p[10]);
10091671Sume	BF_ENC(r, l, s, p[11]);
10191671Sume	BF_ENC(l, r, s, p[12]);
10291671Sume	BF_ENC(r, l, s, p[13]);
10391671Sume	BF_ENC(l, r, s, p[14]);
10491671Sume	BF_ENC(r, l, s, p[15]);
10591671Sume	BF_ENC(l, r, s, p[16]);
10655009Sshin#if BF_ROUNDS == 20
10791671Sume	BF_ENC(r, l, s, p[17]);
10891671Sume	BF_ENC(l, r, s, p[18]);
10991671Sume	BF_ENC(r, l, s, p[19]);
11091671Sume	BF_ENC(l, r, s, p[20]);
11155009Sshin#endif
11291671Sume	r ^= p[BF_ROUNDS + 1];
11391671Sume
11491671Sume	data[1] = l & 0xffffffff;
11591671Sume	data[0] = r & 0xffffffff;
11691671Sume}
11791671Sume
11891671Sume/* XXX "data" is host endian */
11991671Sumevoid
12091671SumeBF_decrypt(data, key)
12191671Sume	BF_LONG *data;
12291671Sume	BF_KEY *key;
12391671Sume{
12491671Sume	register BF_LONG l, r, *p, *s;
12591671Sume
12691671Sume	p = key->P;
12791671Sume	s= &key->S[0];
12891671Sume	l = data[0];
12991671Sume	r = data[1];
13091671Sume
13191671Sume	l ^= p[BF_ROUNDS + 1];
13255009Sshin#if BF_ROUNDS == 20
13391671Sume	BF_ENC(r, l, s, p[20]);
13491671Sume	BF_ENC(l, r, s, p[19]);
13591671Sume	BF_ENC(r, l, s, p[18]);
13691671Sume	BF_ENC(l, r, s, p[17]);
13755009Sshin#endif
13891671Sume	BF_ENC(r, l, s, p[16]);
13991671Sume	BF_ENC(l, r, s, p[15]);
14091671Sume	BF_ENC(r, l, s, p[14]);
14191671Sume	BF_ENC(l, r, s, p[13]);
14291671Sume	BF_ENC(r, l, s, p[12]);
14391671Sume	BF_ENC(l, r, s, p[11]);
14491671Sume	BF_ENC(r, l, s, p[10]);
14591671Sume	BF_ENC(l, r, s, p[ 9]);
14691671Sume	BF_ENC(r, l, s, p[ 8]);
14791671Sume	BF_ENC(l, r, s, p[ 7]);
14891671Sume	BF_ENC(r, l, s, p[ 6]);
14991671Sume	BF_ENC(l, r, s, p[ 5]);
15091671Sume	BF_ENC(r, l, s, p[ 4]);
15191671Sume	BF_ENC(l, r, s, p[ 3]);
15291671Sume	BF_ENC(r, l, s, p[ 2]);
15391671Sume	BF_ENC(l, r, s, p[ 1]);
15491671Sume	r ^= p[0];
15591671Sume
15678064Sume	data[1] = l & 0xffffffff;
15778064Sume	data[0] = r & 0xffffffff;
15878064Sume}
159