xref: /dflybsd-src/sys/crypto/blowfish/bf_enc.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /*	$FreeBSD: src/sys/crypto/blowfish/bf_enc.c,v 1.6 2003/06/10 21:38:38 obrien Exp $	*/
286d7f5d3SJohn Marino /*	$KAME: bf_enc.c,v 1.7 2002/02/27 01:33:59 itojun Exp $	*/
386d7f5d3SJohn Marino 
486d7f5d3SJohn Marino /* crypto/bf/bf_enc.c */
586d7f5d3SJohn Marino 
686d7f5d3SJohn Marino /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
786d7f5d3SJohn Marino  * All rights reserved.
886d7f5d3SJohn Marino  *
986d7f5d3SJohn Marino  * This package is an SSL implementation written
1086d7f5d3SJohn Marino  * by Eric Young (eay@cryptsoft.com).
1186d7f5d3SJohn Marino  * The implementation was written so as to conform with Netscapes SSL.
1286d7f5d3SJohn Marino  *
1386d7f5d3SJohn Marino  * This library is free for commercial and non-commercial use as long as
1486d7f5d3SJohn Marino  * the following conditions are aheared to.  The following conditions
1586d7f5d3SJohn Marino  * apply to all code found in this distribution, be it the RC4, RSA,
1686d7f5d3SJohn Marino  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1786d7f5d3SJohn Marino  * included with this distribution is covered by the same copyright terms
1886d7f5d3SJohn Marino  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1986d7f5d3SJohn Marino  *
2086d7f5d3SJohn Marino  * Copyright remains Eric Young's, and as such any Copyright notices in
2186d7f5d3SJohn Marino  * the code are not to be removed.
2286d7f5d3SJohn Marino  * If this package is used in a product, Eric Young should be given attribution
2386d7f5d3SJohn Marino  * as the author of the parts of the library used.
2486d7f5d3SJohn Marino  * This can be in the form of a textual message at program startup or
2586d7f5d3SJohn Marino  * in documentation (online or textual) provided with the package.
2686d7f5d3SJohn Marino  *
2786d7f5d3SJohn Marino  * Redistribution and use in source and binary forms, with or without
2886d7f5d3SJohn Marino  * modification, are permitted provided that the following conditions
2986d7f5d3SJohn Marino  * are met:
3086d7f5d3SJohn Marino  * 1. Redistributions of source code must retain the copyright
3186d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer.
3286d7f5d3SJohn Marino  * 2. Redistributions in binary form must reproduce the above copyright
3386d7f5d3SJohn Marino  *    notice, this list of conditions and the following disclaimer in the
3486d7f5d3SJohn Marino  *    documentation and/or other materials provided with the distribution.
3586d7f5d3SJohn Marino  * 3. All advertising materials mentioning features or use of this software
3686d7f5d3SJohn Marino  *    must display the following acknowledgement:
3786d7f5d3SJohn Marino  *    "This product includes cryptographic software written by
3886d7f5d3SJohn Marino  *     Eric Young (eay@cryptsoft.com)"
3986d7f5d3SJohn Marino  *    The word 'cryptographic' can be left out if the rouines from the library
4086d7f5d3SJohn Marino  *    being used are not cryptographic related :-).
4186d7f5d3SJohn Marino  * 4. If you include any Windows specific code (or a derivative thereof) from
4286d7f5d3SJohn Marino  *    the apps directory (application code) you must include an acknowledgement:
4386d7f5d3SJohn Marino  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4486d7f5d3SJohn Marino  *
4586d7f5d3SJohn Marino  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4686d7f5d3SJohn Marino  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4786d7f5d3SJohn Marino  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4886d7f5d3SJohn Marino  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4986d7f5d3SJohn Marino  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5086d7f5d3SJohn Marino  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5186d7f5d3SJohn Marino  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5286d7f5d3SJohn Marino  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
5386d7f5d3SJohn Marino  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5486d7f5d3SJohn Marino  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5586d7f5d3SJohn Marino  * SUCH DAMAGE.
5686d7f5d3SJohn Marino  *
5786d7f5d3SJohn Marino  * The licence and distribution terms for any publically available version or
5886d7f5d3SJohn Marino  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5986d7f5d3SJohn Marino  * copied and put under another distribution licence
6086d7f5d3SJohn Marino  * [including the GNU Public Licence.]
6186d7f5d3SJohn Marino  */
6286d7f5d3SJohn Marino 
6386d7f5d3SJohn Marino #include <sys/types.h>
6486d7f5d3SJohn Marino #include <crypto/blowfish/blowfish.h>
6586d7f5d3SJohn Marino #include <crypto/blowfish/bf_locl.h>
6686d7f5d3SJohn Marino 
6786d7f5d3SJohn Marino /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
6886d7f5d3SJohn Marino  * (From LECTURE NOTES IN COIMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
6986d7f5d3SJohn Marino  * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
7086d7f5d3SJohn Marino  */
7186d7f5d3SJohn Marino 
7286d7f5d3SJohn Marino #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
7386d7f5d3SJohn Marino If you set BF_ROUNDS to some value other than 16 or 20, you will have
7486d7f5d3SJohn Marino to modify the code.
7586d7f5d3SJohn Marino #endif
7686d7f5d3SJohn Marino 
7786d7f5d3SJohn Marino /* XXX "data" is host endian */
7886d7f5d3SJohn Marino void
7986d7f5d3SJohn Marino BF_encrypt(BF_LONG *data, BF_KEY *key)
8086d7f5d3SJohn Marino {
8186d7f5d3SJohn Marino 	BF_LONG l, r, *p, *s;
8286d7f5d3SJohn Marino 
8386d7f5d3SJohn Marino 	p = key->P;
8486d7f5d3SJohn Marino 	s= &key->S[0];
8586d7f5d3SJohn Marino 	l = data[0];
8686d7f5d3SJohn Marino 	r = data[1];
8786d7f5d3SJohn Marino 
8886d7f5d3SJohn Marino 	l^=p[0];
8986d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 1]);
9086d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 2]);
9186d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 3]);
9286d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 4]);
9386d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 5]);
9486d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 6]);
9586d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 7]);
9686d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 8]);
9786d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 9]);
9886d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[10]);
9986d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[11]);
10086d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[12]);
10186d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[13]);
10286d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[14]);
10386d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[15]);
10486d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[16]);
10586d7f5d3SJohn Marino #if BF_ROUNDS == 20
10686d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[17]);
10786d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[18]);
10886d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[19]);
10986d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[20]);
11086d7f5d3SJohn Marino #endif
11186d7f5d3SJohn Marino 	r ^= p[BF_ROUNDS + 1];
11286d7f5d3SJohn Marino 
11386d7f5d3SJohn Marino 	data[1] = l & 0xffffffff;
11486d7f5d3SJohn Marino 	data[0] = r & 0xffffffff;
11586d7f5d3SJohn Marino }
11686d7f5d3SJohn Marino 
11786d7f5d3SJohn Marino /* XXX "data" is host endian */
11886d7f5d3SJohn Marino void
BF_decrypt(BF_LONG * data,BF_KEY * key)11986d7f5d3SJohn Marino BF_decrypt(BF_LONG *data, BF_KEY *key)
12086d7f5d3SJohn Marino {
12186d7f5d3SJohn Marino 	BF_LONG l, r, *p, *s;
12286d7f5d3SJohn Marino 
12386d7f5d3SJohn Marino 	p = key->P;
12486d7f5d3SJohn Marino 	s= &key->S[0];
12586d7f5d3SJohn Marino 	l = data[0];
12686d7f5d3SJohn Marino 	r = data[1];
12786d7f5d3SJohn Marino 
12886d7f5d3SJohn Marino 	l ^= p[BF_ROUNDS + 1];
12986d7f5d3SJohn Marino #if BF_ROUNDS == 20
13086d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[20]);
13186d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[19]);
13286d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[18]);
13386d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[17]);
13486d7f5d3SJohn Marino #endif
13586d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[16]);
13686d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[15]);
13786d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[14]);
13886d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[13]);
13986d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[12]);
14086d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[11]);
14186d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[10]);
14286d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 9]);
14386d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 8]);
14486d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 7]);
14586d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 6]);
14686d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 5]);
14786d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 4]);
14886d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 3]);
14986d7f5d3SJohn Marino 	BF_ENC(r, l, s, p[ 2]);
15086d7f5d3SJohn Marino 	BF_ENC(l, r, s, p[ 1]);
15186d7f5d3SJohn Marino 	r ^= p[0];
15286d7f5d3SJohn Marino 
15386d7f5d3SJohn Marino 	data[1] = l & 0xffffffff;
15486d7f5d3SJohn Marino 	data[0] = r & 0xffffffff;
15586d7f5d3SJohn Marino }
156