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/des.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/des.cpp')
-rw-r--r-- | src/des.cpp | 32 |
1 files changed, 8 insertions, 24 deletions
diff --git a/src/des.cpp b/src/des.cpp index 47bdb8f56..e8f173c5c 100644 --- a/src/des.cpp +++ b/src/des.cpp @@ -13,17 +13,13 @@ namespace Botan { *************************************************/ void DES::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), R = load_be<u32bit>(in, 1); IP(L, R); raw_encrypt(L, R); FP(L, R); - 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); } /************************************************* @@ -31,17 +27,13 @@ void DES::enc(const byte in[], byte out[]) const *************************************************/ void DES::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), R = load_be<u32bit>(in, 1); IP(L, R); raw_decrypt(L, R); FP(L, R); - 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); } /************************************************* @@ -193,8 +185,7 @@ void DES::key(const byte key[], u32bit) *************************************************/ void TripleDES::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), R = load_be<u32bit>(in, 1); DES::IP(L, R); des1.raw_encrypt(L, R); @@ -202,10 +193,7 @@ void TripleDES::enc(const byte in[], byte out[]) const des3.raw_encrypt(L, R); DES::FP(L, R); - 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); } /************************************************* @@ -213,8 +201,7 @@ void TripleDES::enc(const byte in[], byte out[]) const *************************************************/ void TripleDES::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), R = load_be<u32bit>(in, 1); DES::IP(L, R); des3.raw_decrypt(L, R); @@ -222,10 +209,7 @@ void TripleDES::dec(const byte in[], byte out[]) const des1.raw_decrypt(L, R); DES::FP(L, R); - 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); } /************************************************* |