diff options
author | lloyd <[email protected]> | 2007-05-31 03:25:19 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2007-05-31 03:25:19 +0000 |
commit | 55608e7dd1aa593944f967f2549564e4f42b654e (patch) | |
tree | ec2ec03a762a6dac82eb608487d5394370135624 /src/blowfish.cpp | |
parent | 22ecdc45a0efa4c444d0b7010b7cd743aeb68c57 (diff) |
Write functions to handle loading and saving words a block at a time, taking into
account endian differences.
The current code does not take advantage of the knowledge of which endianness
we are running on; an optimization suggested by Yves Jerschow is to use (unsafe)
casts to speed up the load/store operations. This turns out to provide large
performance increases (30% or more) in some cases.
Even without the unsafe casts, this version seems to average a few percent
faster, probably because the longer loading loops have been partially or
fully unrolled.
This also makes the code implementing low-level algorithms like ciphers and
hashes a bit more succint.
Diffstat (limited to 'src/blowfish.cpp')
-rw-r--r-- | src/blowfish.cpp | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/src/blowfish.cpp b/src/blowfish.cpp index 384a12461..8fd43ee7e 100644 --- a/src/blowfish.cpp +++ b/src/blowfish.cpp @@ -13,8 +13,8 @@ namespace Botan { *************************************************/ void Blowfish::enc(const byte in[], byte out[]) const { - u32bit L = make_u32bit(in[0], in[1], in[2], in[3]), - R = make_u32bit(in[4], in[5], in[6], in[7]); + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); for(u32bit j = 0; j != 16; j += 2) { @@ -29,10 +29,7 @@ void Blowfish::enc(const byte in[], byte out[]) const L ^= P[16]; R ^= P[17]; - out[0] = get_byte(0, R); out[1] = get_byte(1, R); - out[2] = get_byte(2, R); out[3] = get_byte(3, R); - out[4] = get_byte(0, L); out[5] = get_byte(1, L); - out[6] = get_byte(2, L); out[7] = get_byte(3, L); + store_be(out, R, L); } /************************************************* @@ -40,8 +37,8 @@ void Blowfish::enc(const byte in[], byte out[]) const *************************************************/ void Blowfish::dec(const byte in[], byte out[]) const { - u32bit L = make_u32bit(in[0], in[1], in[2], in[3]), - R = make_u32bit(in[4], in[5], in[6], in[7]); + u32bit L = load_be<u32bit>(in, 0); + u32bit R = load_be<u32bit>(in, 1); for(u32bit j = 17; j != 1; j -= 2) { @@ -56,10 +53,7 @@ void Blowfish::dec(const byte in[], byte out[]) const L ^= P[1]; R ^= P[0]; - out[0] = get_byte(0, R); out[1] = get_byte(1, R); - out[2] = get_byte(2, R); out[3] = get_byte(3, R); - out[4] = get_byte(0, L); out[5] = get_byte(1, L); - out[6] = get_byte(2, L); out[7] = get_byte(3, L); + store_be(out, R, L); } /************************************************* @@ -68,9 +62,11 @@ void Blowfish::dec(const byte in[], byte out[]) const void Blowfish::key(const byte key[], u32bit length) { clear(); + for(u32bit j = 0, k = 0; j != 18; ++j, k += 4) P[j] ^= make_u32bit(key[(k ) % length], key[(k+1) % length], - key[(k+2) % length], key[(k+3) % length]); + key[(k+2) % length], key[(k+3) % length]); + u32bit L = 0, R = 0; generate_sbox(P, 18, L, R); generate_sbox(S1, 256, L, R); |