From 2ff9b4375852bc02b28841d9d26c7e7b66146af5 Mon Sep 17 00:00:00 2001 From: itojun Date: Wed, 27 Feb 2002 01:32:17 +0000 Subject: [PATCH] sync blowfish function prototype between i386 assembly and C. From: YAMAMOTO Takashi --- sys/crypto/blowfish/bf_enc.c | 119 +++++++++++++++++++-------------- sys/crypto/blowfish/bf_skey.c | 8 +-- sys/crypto/blowfish/blowfish.h | 5 +- sys/netinet6/esp_core.c | 10 +-- 4 files changed, 79 insertions(+), 63 deletions(-) diff --git a/sys/crypto/blowfish/bf_enc.c b/sys/crypto/blowfish/bf_enc.c index fb6c1eac8424..820159dd599a 100644 --- a/sys/crypto/blowfish/bf_enc.c +++ b/sys/crypto/blowfish/bf_enc.c @@ -57,7 +57,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: bf_enc.c,v 1.5 2001/11/13 01:40:08 lukem Exp $"); +__KERNEL_RCSID(0, "$NetBSD: bf_enc.c,v 1.6 2002/02/27 01:32:17 itojun Exp $"); #include #include @@ -75,10 +75,9 @@ to modify the code. /* 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 @@ BF_encrypt(data, key, encrypt) 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]; -#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]); -#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]; - } + 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]); +#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]; + data[1] = l & 0xffffffff; data[0] = r & 0xffffffff; } diff --git a/sys/crypto/blowfish/bf_skey.c b/sys/crypto/blowfish/bf_skey.c index aeca314fba2e..b80432874c14 100644 --- a/sys/crypto/blowfish/bf_skey.c +++ b/sys/crypto/blowfish/bf_skey.c @@ -1,4 +1,4 @@ -/* $NetBSD: bf_skey.c,v 1.3 2001/11/13 01:40:08 lukem Exp $ */ +/* $NetBSD: bf_skey.c,v 1.4 2002/02/27 01:32:17 itojun Exp $ */ /* $KAME: bf_skey.c,v 1.5 2000/11/06 13:58:08 itojun Exp $ */ /* crypto/bf/bf_skey.c */ @@ -60,7 +60,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: bf_skey.c,v 1.3 2001/11/13 01:40:08 lukem Exp $"); +__KERNEL_RCSID(0, "$NetBSD: bf_skey.c,v 1.4 2002/02/27 01:32:17 itojun Exp $"); #include #include @@ -113,14 +113,14 @@ BF_set_key(key, len, data) 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]; } diff --git a/sys/crypto/blowfish/blowfish.h b/sys/crypto/blowfish/blowfish.h index 26fe4729a30e..9cf0a6ca14c2 100644 --- a/sys/crypto/blowfish/blowfish.h +++ b/sys/crypto/blowfish/blowfish.h @@ -1,4 +1,4 @@ -/* $NetBSD: blowfish.h,v 1.4 2001/09/09 11:01:01 tls Exp $ */ +/* $NetBSD: blowfish.h,v 1.5 2002/02/27 01:32:17 itojun Exp $ */ /* $KAME: blowfish.h,v 1.10 2000/09/18 21:21:20 itojun Exp $ */ /* crypto/bf/blowfish.h */ @@ -81,7 +81,8 @@ typedef struct bf_key_st { } 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); diff --git a/sys/netinet6/esp_core.c b/sys/netinet6/esp_core.c index 77928027c060..52466c2ccf5d 100644 --- a/sys/netinet6/esp_core.c +++ b/sys/netinet6/esp_core.c @@ -1,4 +1,4 @@ -/* $NetBSD: esp_core.c,v 1.20 2001/12/21 08:54:52 itojun Exp $ */ +/* $NetBSD: esp_core.c,v 1.21 2002/02/27 01:32:17 itojun Exp $ */ /* $KAME: esp_core.c,v 1.53 2001/11/27 09:47:30 sakane Exp $ */ /* @@ -31,7 +31,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: esp_core.c,v 1.20 2001/12/21 08:54:52 itojun Exp $"); +__KERNEL_RCSID(0, "$NetBSD: esp_core.c,v 1.21 2002/02/27 01:32:17 itojun Exp $"); #include "opt_inet.h" @@ -464,13 +464,13 @@ esp_blowfish_blockdecrypt(algo, sav, s, d) 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 @@ esp_blowfish_blockencrypt(algo, sav, s, d) 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));