Subject: ESP using blowfish doesn't work (on i386)
To: None <port-i386@netbsd.org, tech-net@netbsd.org>
From: YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
List: tech-net
Date: 02/26/2002 15:45:06
----Next_Part(Tue_Feb_26_15:45:06_2002_252)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
hi.
I tried to use ESP with blowfish as its encrypt algorithm,
but it doesn't work because i386 asm version of BF_encrypt
is prototype-mismatched with C version.
applying attached patches (or fixing i386 asm version instead) solve
the problem. are this patches right and can i commit?
thanks.
---
YAMAMOTO Takashi<yamt@mwd.biglobe.ne.jp>
----Next_Part(Tue_Feb_26_15:45:06_2002_252)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="bf3.diff"
Index: bf_enc.c
===================================================================
RCS file: /cvs/cvsroot/syssrc/sys/crypto/blowfish/bf_enc.c,v
retrieving revision 1.5
diff -u -r1.5 bf_enc.c
--- bf_enc.c 2001/11/13 01:40:08 1.5
+++ bf_enc.c 2002/02/26 03:41:43
@@ -75,10 +75,9 @@
/* XXX "data" is host endian */
void
-BF_encrypt(data, key, encrypt)
+BF_encrypt(data, key)
BF_LONG *data;
BF_KEY *key;
- int encrypt;
{
register BF_LONG l, r, *p, *s;
@@ -87,57 +86,73 @@
l = data[0];
r = data[1];
- if (encrypt) {
- l^=p[0];
- BF_ENC(r, l, s, p[ 1]);
- BF_ENC(l, r, s, p[ 2]);
- BF_ENC(r, l, s, p[ 3]);
- BF_ENC(l, r, s, p[ 4]);
- BF_ENC(r, l, s, p[ 5]);
- BF_ENC(l, r, s, p[ 6]);
- BF_ENC(r, l, s, p[ 7]);
- BF_ENC(l, r, s, p[ 8]);
- BF_ENC(r, l, s, p[ 9]);
- BF_ENC(l, r, s, p[10]);
- BF_ENC(r, l, s, p[11]);
- BF_ENC(l, r, s, p[12]);
- BF_ENC(r, l, s, p[13]);
- BF_ENC(l, r, s, p[14]);
- BF_ENC(r, l, s, p[15]);
- BF_ENC(l, r, s, p[16]);
+ l^=p[0];
+ BF_ENC(r, l, s, p[ 1]);
+ BF_ENC(l, r, s, p[ 2]);
+ BF_ENC(r, l, s, p[ 3]);
+ BF_ENC(l, r, s, p[ 4]);
+ BF_ENC(r, l, s, p[ 5]);
+ BF_ENC(l, r, s, p[ 6]);
+ BF_ENC(r, l, s, p[ 7]);
+ BF_ENC(l, r, s, p[ 8]);
+ BF_ENC(r, l, s, p[ 9]);
+ BF_ENC(l, r, s, p[10]);
+ BF_ENC(r, l, s, p[11]);
+ BF_ENC(l, r, s, p[12]);
+ BF_ENC(r, l, s, p[13]);
+ BF_ENC(l, r, s, p[14]);
+ BF_ENC(r, l, s, p[15]);
+ BF_ENC(l, r, s, p[16]);
#if BF_ROUNDS == 20
- BF_ENC(r, l, s, p[17]);
- BF_ENC(l, r, s, p[18]);
- BF_ENC(r, l, s, p[19]);
- BF_ENC(l, r, s, p[20]);
+ BF_ENC(r, l, s, p[17]);
+ BF_ENC(l, r, s, p[18]);
+ BF_ENC(r, l, s, p[19]);
+ BF_ENC(l, r, s, p[20]);
#endif
- r ^= p[BF_ROUNDS + 1];
- } else {
- l ^= p[BF_ROUNDS + 1];
+ r ^= p[BF_ROUNDS + 1];
+
+ data[1] = l & 0xffffffff;
+ data[0] = r & 0xffffffff;
+}
+
+/* XXX "data" is host endian */
+void
+BF_decrypt(data, key)
+ BF_LONG *data;
+ BF_KEY *key;
+{
+ register BF_LONG l, r, *p, *s;
+
+ p = key->P;
+ s= &key->S[0];
+ l = data[0];
+ r = data[1];
+
+ l ^= p[BF_ROUNDS + 1];
#if BF_ROUNDS == 20
- BF_ENC(r, l, s, p[20]);
- BF_ENC(l, r, s, p[19]);
- BF_ENC(r, l, s, p[18]);
- BF_ENC(l, r, s, p[17]);
+ BF_ENC(r, l, s, p[20]);
+ BF_ENC(l, r, s, p[19]);
+ BF_ENC(r, l, s, p[18]);
+ BF_ENC(l, r, s, p[17]);
#endif
- BF_ENC(r, l, s, p[16]);
- BF_ENC(l, r, s, p[15]);
- BF_ENC(r, l, s, p[14]);
- BF_ENC(l, r, s, p[13]);
- BF_ENC(r, l, s, p[12]);
- BF_ENC(l, r, s, p[11]);
- BF_ENC(r, l, s, p[10]);
- BF_ENC(l, r, s, p[ 9]);
- BF_ENC(r, l, s, p[ 8]);
- BF_ENC(l, r, s, p[ 7]);
- BF_ENC(r, l, s, p[ 6]);
- BF_ENC(l, r, s, p[ 5]);
- BF_ENC(r, l, s, p[ 4]);
- BF_ENC(l, r, s, p[ 3]);
- BF_ENC(r, l, s, p[ 2]);
- BF_ENC(l, r, s, p[ 1]);
- r ^= p[0];
- }
+ BF_ENC(r, l, s, p[16]);
+ BF_ENC(l, r, s, p[15]);
+ BF_ENC(r, l, s, p[14]);
+ BF_ENC(l, r, s, p[13]);
+ BF_ENC(r, l, s, p[12]);
+ BF_ENC(l, r, s, p[11]);
+ BF_ENC(r, l, s, p[10]);
+ BF_ENC(l, r, s, p[ 9]);
+ BF_ENC(r, l, s, p[ 8]);
+ BF_ENC(l, r, s, p[ 7]);
+ BF_ENC(r, l, s, p[ 6]);
+ BF_ENC(l, r, s, p[ 5]);
+ BF_ENC(r, l, s, p[ 4]);
+ BF_ENC(l, r, s, p[ 3]);
+ BF_ENC(r, l, s, p[ 2]);
+ BF_ENC(l, r, s, p[ 1]);
+ r ^= p[0];
+
data[1] = l & 0xffffffff;
data[0] = r & 0xffffffff;
}
----Next_Part(Tue_Feb_26_15:45:06_2002_252)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="bf2.diff"
Index: bf_skey.c
===================================================================
RCS file: /cvs/cvsroot/syssrc/sys/crypto/blowfish/bf_skey.c,v
retrieving revision 1.3
diff -u -r1.3 bf_skey.c
--- bf_skey.c 2001/11/13 01:40:08 1.3
+++ bf_skey.c 2002/02/26 03:29:26
@@ -113,14 +113,14 @@
in[0] = 0L;
in[1] = 0L;
for (i = 0; i < BF_ROUNDS + 2; i += 2) {
- BF_encrypt(in, key, BF_ENCRYPT);
+ BF_encrypt(in, key);
p[i ] = in[0];
p[i+1] = in[1];
}
p = key->S;
for (i = 0; i < 4 * 256; i += 2) {
- BF_encrypt(in, key, BF_ENCRYPT);
+ BF_encrypt(in, key);
p[i ] = in[0];
p[i+1] = in[1];
}
Index: blowfish.h
===================================================================
RCS file: /cvs/cvsroot/syssrc/sys/crypto/blowfish/blowfish.h,v
retrieving revision 1.4
diff -u -r1.4 blowfish.h
--- blowfish.h 2001/09/09 11:01:01 1.4
+++ blowfish.h 2002/02/26 03:27:09
@@ -81,7 +81,8 @@
} BF_KEY;
void BF_set_key __P((BF_KEY *, int, unsigned char *));
-void BF_encrypt __P((BF_LONG *, BF_KEY *, int));
+void BF_encrypt __P((BF_LONG *, BF_KEY *));
+void BF_decrypt __P((BF_LONG *, BF_KEY *));
void BF_cbc_encrypt(const unsigned char *, unsigned char *, long,
const BF_KEY *, unsigned char *, int);
----Next_Part(Tue_Feb_26_15:45:06_2002_252)--
Content-Type: Text/Plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="bf1.diff"
Index: esp_core.c
===================================================================
RCS file: /cvs/cvsroot/syssrc/sys/netinet6/esp_core.c,v
retrieving revision 1.20
diff -u -r1.20 esp_core.c
--- esp_core.c 2001/12/21 08:54:52 1.20
+++ esp_core.c 2002/02/26 03:26:51
@@ -464,13 +464,13 @@
u_int8_t *s;
u_int8_t *d;
{
- /* HOLY COW! BF_encrypt() takes values in host byteorder */
+ /* HOLY COW! BF_decrypt() takes values in host byteorder */
BF_LONG t[2];
bcopy(s, t, sizeof(t));
t[0] = ntohl(t[0]);
t[1] = ntohl(t[1]);
- BF_encrypt(t, (BF_KEY *)sav->sched, BF_DECRYPT);
+ BF_decrypt(t, (BF_KEY *)sav->sched);
t[0] = htonl(t[0]);
t[1] = htonl(t[1]);
bcopy(t, d, sizeof(t));
@@ -490,7 +490,7 @@
bcopy(s, t, sizeof(t));
t[0] = ntohl(t[0]);
t[1] = ntohl(t[1]);
- BF_encrypt(t, (BF_KEY *)sav->sched, BF_ENCRYPT);
+ BF_encrypt(t, (BF_KEY *)sav->sched);
t[0] = htonl(t[0]);
t[1] = htonl(t[1]);
bcopy(t, d, sizeof(t));
----Next_Part(Tue_Feb_26_15:45:06_2002_252)----