1*82bba4e9Sandvar /* $NetBSD: bf_enc.c,v 1.11 2024/02/05 21:46:06 andvar Exp $ */
2d5d0a860Sthorpej
3599c8087Sthorpej /* crypto/bf/bf_enc.c */
43d4146e2Stls /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5599c8087Sthorpej * All rights reserved.
6599c8087Sthorpej *
7599c8087Sthorpej * This package is an SSL implementation written
83d4146e2Stls * by Eric Young (eay@cryptsoft.com).
9599c8087Sthorpej * The implementation was written so as to conform with Netscapes SSL.
10599c8087Sthorpej *
11599c8087Sthorpej * This library is free for commercial and non-commercial use as long as
12599c8087Sthorpej * the following conditions are aheared to. The following conditions
13599c8087Sthorpej * apply to all code found in this distribution, be it the RC4, RSA,
14599c8087Sthorpej * lhash, DES, etc., code; not just the SSL code. The SSL documentation
15599c8087Sthorpej * included with this distribution is covered by the same copyright terms
163d4146e2Stls * except that the holder is Tim Hudson (tjh@cryptsoft.com).
17599c8087Sthorpej *
18599c8087Sthorpej * Copyright remains Eric Young's, and as such any Copyright notices in
19599c8087Sthorpej * the code are not to be removed.
20599c8087Sthorpej * If this package is used in a product, Eric Young should be given attribution
21599c8087Sthorpej * as the author of the parts of the library used.
22599c8087Sthorpej * This can be in the form of a textual message at program startup or
23599c8087Sthorpej * in documentation (online or textual) provided with the package.
24599c8087Sthorpej *
25599c8087Sthorpej * Redistribution and use in source and binary forms, with or without
26599c8087Sthorpej * modification, are permitted provided that the following conditions
27599c8087Sthorpej * are met:
28599c8087Sthorpej * 1. Redistributions of source code must retain the copyright
29599c8087Sthorpej * notice, this list of conditions and the following disclaimer.
30599c8087Sthorpej * 2. Redistributions in binary form must reproduce the above copyright
31599c8087Sthorpej * notice, this list of conditions and the following disclaimer in the
32599c8087Sthorpej * documentation and/or other materials provided with the distribution.
33599c8087Sthorpej * 3. All advertising materials mentioning features or use of this software
34599c8087Sthorpej * must display the following acknowledgement:
35599c8087Sthorpej * "This product includes cryptographic software written by
363d4146e2Stls * Eric Young (eay@cryptsoft.com)"
37599c8087Sthorpej * The word 'cryptographic' can be left out if the rouines from the library
38599c8087Sthorpej * being used are not cryptographic related :-).
39599c8087Sthorpej * 4. If you include any Windows specific code (or a derivative thereof) from
40599c8087Sthorpej * the apps directory (application code) you must include an acknowledgement:
413d4146e2Stls * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
42599c8087Sthorpej *
43599c8087Sthorpej * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
44599c8087Sthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45599c8087Sthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46599c8087Sthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47599c8087Sthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48599c8087Sthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49599c8087Sthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50599c8087Sthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51599c8087Sthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52599c8087Sthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53599c8087Sthorpej * SUCH DAMAGE.
54599c8087Sthorpej *
55599c8087Sthorpej * The licence and distribution terms for any publically available version or
56599c8087Sthorpej * derivative of this code cannot be changed. i.e. this code cannot simply be
57599c8087Sthorpej * copied and put under another distribution licence
58599c8087Sthorpej * [including the GNU Public Licence.]
59599c8087Sthorpej */
60599c8087Sthorpej
614f2ad952Slukem #include <sys/cdefs.h>
62*82bba4e9Sandvar __KERNEL_RCSID(0, "$NetBSD: bf_enc.c,v 1.11 2024/02/05 21:46:06 andvar Exp $");
634f2ad952Slukem
640f164915Sitojun #include <sys/types.h>
65599c8087Sthorpej #include <crypto/blowfish/blowfish.h>
66599c8087Sthorpej #include <crypto/blowfish/bf_locl.h>
67599c8087Sthorpej
68599c8087Sthorpej /* Blowfish as implemented from 'Blowfish: Springer-Verlag paper'
69*82bba4e9Sandvar * (From LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION,
70599c8087Sthorpej * CAMBRIDGE SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
71599c8087Sthorpej */
72599c8087Sthorpej
73599c8087Sthorpej #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
74599c8087Sthorpej If you set BF_ROUNDS to some value other than 16 or 20, you will have
75599c8087Sthorpej to modify the code.
76599c8087Sthorpej #endif
77599c8087Sthorpej
787236276cSitojun /* XXX "data" is host endian */
797236276cSitojun void
80e77423d9Sthorpej BF_encrypt(BF_LONG *data, const BF_KEY *key)
81599c8087Sthorpej {
82710886efStron BF_LONG l, r;
83710886efStron const BF_LONG *p, *s;
84599c8087Sthorpej
85599c8087Sthorpej p = key->P;
867236276cSitojun s= &key->S[0];
87599c8087Sthorpej l = data[0];
88599c8087Sthorpej r = data[1];
89599c8087Sthorpej
90599c8087Sthorpej l^=p[0];
91599c8087Sthorpej BF_ENC(r, l, s, p[ 1]);
92599c8087Sthorpej BF_ENC(l, r, s, p[ 2]);
93599c8087Sthorpej BF_ENC(r, l, s, p[ 3]);
94599c8087Sthorpej BF_ENC(l, r, s, p[ 4]);
95599c8087Sthorpej BF_ENC(r, l, s, p[ 5]);
96599c8087Sthorpej BF_ENC(l, r, s, p[ 6]);
97599c8087Sthorpej BF_ENC(r, l, s, p[ 7]);
98599c8087Sthorpej BF_ENC(l, r, s, p[ 8]);
99599c8087Sthorpej BF_ENC(r, l, s, p[ 9]);
100599c8087Sthorpej BF_ENC(l, r, s, p[10]);
101599c8087Sthorpej BF_ENC(r, l, s, p[11]);
102599c8087Sthorpej BF_ENC(l, r, s, p[12]);
103599c8087Sthorpej BF_ENC(r, l, s, p[13]);
104599c8087Sthorpej BF_ENC(l, r, s, p[14]);
105599c8087Sthorpej BF_ENC(r, l, s, p[15]);
106599c8087Sthorpej BF_ENC(l, r, s, p[16]);
107599c8087Sthorpej #if BF_ROUNDS == 20
108599c8087Sthorpej BF_ENC(r, l, s, p[17]);
109599c8087Sthorpej BF_ENC(l, r, s, p[18]);
110599c8087Sthorpej BF_ENC(r, l, s, p[19]);
111599c8087Sthorpej BF_ENC(l, r, s, p[20]);
112599c8087Sthorpej #endif
113599c8087Sthorpej r ^= p[BF_ROUNDS + 1];
1142ff9b437Sitojun
1152ff9b437Sitojun data[1] = l & 0xffffffff;
1162ff9b437Sitojun data[0] = r & 0xffffffff;
1172ff9b437Sitojun }
1182ff9b437Sitojun
1192ff9b437Sitojun /* XXX "data" is host endian */
1202ff9b437Sitojun void
BF_decrypt(BF_LONG * data,const BF_KEY * key)121e77423d9Sthorpej BF_decrypt(BF_LONG *data, const BF_KEY *key)
1222ff9b437Sitojun {
123710886efStron BF_LONG l, r;
124710886efStron const BF_LONG *p, *s;
1252ff9b437Sitojun
1262ff9b437Sitojun p = key->P;
1272ff9b437Sitojun s= &key->S[0];
1282ff9b437Sitojun l = data[0];
1292ff9b437Sitojun r = data[1];
1302ff9b437Sitojun
131599c8087Sthorpej l ^= p[BF_ROUNDS + 1];
132599c8087Sthorpej #if BF_ROUNDS == 20
133599c8087Sthorpej BF_ENC(r, l, s, p[20]);
134599c8087Sthorpej BF_ENC(l, r, s, p[19]);
135599c8087Sthorpej BF_ENC(r, l, s, p[18]);
136599c8087Sthorpej BF_ENC(l, r, s, p[17]);
137599c8087Sthorpej #endif
138599c8087Sthorpej BF_ENC(r, l, s, p[16]);
139599c8087Sthorpej BF_ENC(l, r, s, p[15]);
140599c8087Sthorpej BF_ENC(r, l, s, p[14]);
141599c8087Sthorpej BF_ENC(l, r, s, p[13]);
142599c8087Sthorpej BF_ENC(r, l, s, p[12]);
143599c8087Sthorpej BF_ENC(l, r, s, p[11]);
144599c8087Sthorpej BF_ENC(r, l, s, p[10]);
145599c8087Sthorpej BF_ENC(l, r, s, p[ 9]);
146599c8087Sthorpej BF_ENC(r, l, s, p[ 8]);
147599c8087Sthorpej BF_ENC(l, r, s, p[ 7]);
148599c8087Sthorpej BF_ENC(r, l, s, p[ 6]);
149599c8087Sthorpej BF_ENC(l, r, s, p[ 5]);
150599c8087Sthorpej BF_ENC(r, l, s, p[ 4]);
151599c8087Sthorpej BF_ENC(l, r, s, p[ 3]);
152599c8087Sthorpej BF_ENC(r, l, s, p[ 2]);
153599c8087Sthorpej BF_ENC(l, r, s, p[ 1]);
154599c8087Sthorpej r ^= p[0];
1552ff9b437Sitojun
156599c8087Sthorpej data[1] = l & 0xffffffff;
157599c8087Sthorpej data[0] = r & 0xffffffff;
158599c8087Sthorpej }
159