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/tea.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/tea.cpp')
-rw-r--r-- | src/tea.cpp | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/src/tea.cpp b/src/tea.cpp index 9b04aba11..aa04b1df8 100644 --- a/src/tea.cpp +++ b/src/tea.cpp @@ -13,8 +13,8 @@ namespace Botan { *************************************************/ void TEA::enc(const byte in[], byte out[]) const { - u32bit left = make_u32bit(in[0], in[1], in[2], in[3]), - right = make_u32bit(in[4], in[5], in[6], in[7]); + u32bit left = load_be<u32bit>(in, 0), right = load_be<u32bit>(in, 1); + u32bit sum = 0; for(u32bit j = 0; j != 32; ++j) { @@ -22,10 +22,8 @@ void TEA::enc(const byte in[], byte out[]) const left += ((right << 4) + K[0]) ^ (right + sum) ^ ((right >> 5) + K[1]); right += ((left << 4) + K[2]) ^ (left + sum) ^ ((left >> 5) + K[3]); } - out[0] = get_byte(0, left); out[1] = get_byte(1, left); - out[2] = get_byte(2, left); out[3] = get_byte(3, left); - out[4] = get_byte(0, right); out[5] = get_byte(1, right); - out[6] = get_byte(2, right); out[7] = get_byte(3, right); + + store_be(out, left, right); } /************************************************* @@ -33,8 +31,8 @@ void TEA::enc(const byte in[], byte out[]) const *************************************************/ void TEA::dec(const byte in[], byte out[]) const { - u32bit left = make_u32bit(in[0], in[1], in[2], in[3]), - right = make_u32bit(in[4], in[5], in[6], in[7]); + u32bit left = load_be<u32bit>(in, 0), right = load_be<u32bit>(in, 1); + u32bit sum = 0xC6EF3720; for(u32bit j = 0; j != 32; ++j) { @@ -42,10 +40,8 @@ void TEA::dec(const byte in[], byte out[]) const left -= ((right << 4) + K[0]) ^ (right + sum) ^ ((right >> 5) + K[1]); sum -= 0x9E3779B9; } - out[0] = get_byte(0, left); out[1] = get_byte(1, left); - out[2] = get_byte(2, left); out[3] = get_byte(3, left); - out[4] = get_byte(0, right); out[5] = get_byte(1, right); - out[6] = get_byte(2, right); out[7] = get_byte(3, right); + + store_be(out, left, right); } /************************************************* @@ -54,7 +50,7 @@ void TEA::dec(const byte in[], byte out[]) const void TEA::key(const byte key[], u32bit) { for(u32bit j = 0; j != 4; ++j) - K[j] = make_u32bit(key[4*j], key[4*j+1], key[4*j+2], key[4*j+3]); + K[j] = load_be<u32bit>(key, j); } } |