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/rc5.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/rc5.cpp')
-rw-r--r-- | src/rc5.cpp | 20 |
1 files changed, 8 insertions, 12 deletions
diff --git a/src/rc5.cpp b/src/rc5.cpp index 261529ea8..3e87dc8ab 100644 --- a/src/rc5.cpp +++ b/src/rc5.cpp @@ -15,8 +15,8 @@ namespace Botan { *************************************************/ void RC5::enc(const byte in[], byte out[]) const { - u32bit A = make_u32bit(in[3], in[2], in[1], in[0]), - B = make_u32bit(in[7], in[6], in[5], in[4]); + u32bit A = load_le<u32bit>(in, 0), B = load_le<u32bit>(in, 1); + A += S[0]; B += S[1]; for(u32bit j = 0; j != ROUNDS; j += 4) { @@ -29,10 +29,8 @@ void RC5::enc(const byte in[], byte out[]) const A = rotate_left(A ^ B, B % 32) + S[2*j+8]; B = rotate_left(B ^ A, A % 32) + S[2*j+9]; } - out[0] = get_byte(3, A); out[1] = get_byte(2, A); - out[2] = get_byte(1, A); out[3] = get_byte(0, A); - out[4] = get_byte(3, B); out[5] = get_byte(2, B); - out[6] = get_byte(1, B); out[7] = get_byte(0, B); + + store_le(out, A, B); } /************************************************* @@ -40,8 +38,8 @@ void RC5::enc(const byte in[], byte out[]) const *************************************************/ void RC5::dec(const byte in[], byte out[]) const { - u32bit A = make_u32bit(in[3], in[2], in[1], in[0]), - B = make_u32bit(in[7], in[6], in[5], in[4]); + u32bit A = load_le<u32bit>(in, 0), B = load_le<u32bit>(in, 1); + for(u32bit j = ROUNDS; j != 0; j -= 4) { B = rotate_right(B - S[2*j+1], A % 32) ^ A; @@ -54,10 +52,8 @@ void RC5::dec(const byte in[], byte out[]) const A = rotate_right(A - S[2*j-6], B % 32) ^ B; } B -= S[1]; A -= S[0]; - out[0] = get_byte(3, A); out[1] = get_byte(2, A); - out[2] = get_byte(1, A); out[3] = get_byte(0, A); - out[4] = get_byte(3, B); out[5] = get_byte(2, B); - out[6] = get_byte(1, B); out[7] = get_byte(0, B); + + store_le(out, A, B); } /************************************************* |