diff options
author | lloyd <[email protected]> | 2008-09-17 23:12:53 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-09-17 23:12:53 +0000 |
commit | 119cba52f4e96e18fbad846d1fc8dfe682f38974 (patch) | |
tree | 46127441faf677c51eca9de4ef0889351aac55b1 /src/blowfish.cpp | |
parent | dffdb97af1b2ef5d68c647385190bccdd965b28c (diff) |
Add an optimization suggested by Yves Jerschow to combine the four
Blowfish Sboxes into one 1024 word array and index into them at
offsets. On my x86-64 machine there is no real difference between the
two, but on register constrained processor like x86 it may make a large
difference, since the x86 has a much easier time indexing off a single
address held in a register rather than 4 distinct ones.
Diffstat (limited to 'src/blowfish.cpp')
-rw-r--r-- | src/blowfish.cpp | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/src/blowfish.cpp b/src/blowfish.cpp index a18fb7662..ffca9241f 100644 --- a/src/blowfish.cpp +++ b/src/blowfish.cpp @@ -13,6 +13,11 @@ namespace Botan { *************************************************/ void Blowfish::enc(const byte in[], byte out[]) const { + const u32bit* S1 = S + 0; + const u32bit* S2 = S + 256; + const u32bit* S3 = S + 512; + const u32bit* S4 = S + 768; + u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); @@ -37,6 +42,11 @@ void Blowfish::enc(const byte in[], byte out[]) const *************************************************/ void Blowfish::dec(const byte in[], byte out[]) const { + const u32bit* S1 = S + 0; + const u32bit* S2 = S + 256; + const u32bit* S3 = S + 512; + const u32bit* S4 = S + 768; + u32bit L = load_be<u32bit>(in, 0); u32bit R = load_be<u32bit>(in, 1); @@ -69,10 +79,7 @@ void Blowfish::key(const byte key[], u32bit length) u32bit L = 0, R = 0; generate_sbox(P, 18, L, R); - generate_sbox(S1, 256, L, R); - generate_sbox(S2, 256, L, R); - generate_sbox(S3, 256, L, R); - generate_sbox(S4, 256, L, R); + generate_sbox(S, 1024, L, R); } /************************************************* @@ -81,6 +88,11 @@ void Blowfish::key(const byte key[], u32bit length) void Blowfish::generate_sbox(u32bit Box[], u32bit size, u32bit& L, u32bit& R) const { + const u32bit* S1 = S + 0; + const u32bit* S2 = S + 256; + const u32bit* S3 = S + 512; + const u32bit* S4 = S + 768; + for(u32bit j = 0; j != size; j += 2) { for(u32bit k = 0; k != 16; k += 2) @@ -104,11 +116,8 @@ void Blowfish::generate_sbox(u32bit Box[], u32bit size, *************************************************/ void Blowfish::clear() throw() { - P.copy(PBOX, 18); - S1.copy(SBOX + 0, 256); - S2.copy(SBOX + 256, 256); - S3.copy(SBOX + 512, 256); - S4.copy(SBOX + 768, 256); + P.copy(P_INIT, 18); + S.copy(S_INIT, 1024); } } |