diff options
author | lloyd <[email protected]> | 2007-03-01 00:53:20 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2007-03-01 00:53:20 +0000 |
commit | afcd75d5f60be62e7a9dfc8e1a1cda3fc3d0762e (patch) | |
tree | eaaa21bb757bff25e63387576438333b64f46cef | |
parent | fe17e1b8f9f73fcc7664b1f880677f45c8d6f1ea (diff) |
Inline the round functions of RC2. This is about 15% faster on my machine,
and actually reduced the total line count.
-rw-r--r-- | include/rc2.h | 5 | ||||
-rw-r--r-- | src/rc2.cpp | 100 |
2 files changed, 49 insertions, 56 deletions
diff --git a/include/rc2.h b/include/rc2.h index c29ba0d66..e83b44eb2 100644 --- a/include/rc2.h +++ b/include/rc2.h @@ -26,10 +26,7 @@ class RC2 : public BlockCipher void enc(const byte[], byte[]) const; void dec(const byte[], byte[]) const; void key(const byte[], u32bit); - void mash(u16bit&, u16bit&, u16bit&, u16bit&) const; - void rmash(u16bit&, u16bit&, u16bit&, u16bit&) const; - void mix(u16bit&, u16bit&, u16bit&, u16bit&, u32bit) const; - void rmix(u16bit&, u16bit&, u16bit&, u16bit&, u32bit) const; + SecureBuffer<u16bit, 64> K; }; diff --git a/src/rc2.cpp b/src/rc2.cpp index fb81b2d1f..0e35950e7 100644 --- a/src/rc2.cpp +++ b/src/rc2.cpp @@ -15,12 +15,30 @@ void RC2::enc(const byte in[], byte out[]) const { u16bit R0 = make_u16bit(in[1], in[0]), R1 = make_u16bit(in[3], in[2]), R2 = make_u16bit(in[5], in[4]), R3 = make_u16bit(in[7], in[6]); - mix(R0, R1, R2, R3, 0); mix(R0, R1, R2, R3, 1); mix(R0, R1, R2, R3, 2); - mix(R0, R1, R2, R3, 3); mix(R0, R1, R2, R3, 4); mash(R0, R1, R2, R3); - mix(R0, R1, R2, R3, 5); mix(R0, R1, R2, R3, 6); mix(R0, R1, R2, R3, 7); - mix(R0, R1, R2, R3, 8); mix(R0, R1, R2, R3, 9); mix(R0, R1, R2, R3,10); - mash(R0, R1, R2, R3); mix(R0, R1, R2, R3,11); mix(R0, R1, R2, R3,12); - mix(R0, R1, R2, R3,13); mix(R0, R1, R2, R3,14); mix(R0, R1, R2, R3,15); + + for(u32bit j = 0; j != 16; j++) + { + R0 += (R1 & ~R3) + (R2 & R3) + K[4*j]; + R0 = rotate_left(R0, 1); + + R1 += (R2 & ~R0) + (R3 & R0) + K[4*j + 1]; + R1 = rotate_left(R1, 2); + + R2 += (R3 & ~R1) + (R0 & R1) + K[4*j + 2]; + R2 = rotate_left(R2, 3); + + R3 += (R0 & ~R2) + (R1 & R2) + K[4*j + 3]; + R3 = rotate_left(R3, 5); + + if(j == 4 || j == 10) + { + R0 += K[R3 % 64]; + R1 += K[R0 % 64]; + R2 += K[R1 % 64]; + R3 += K[R2 % 64]; + } + } + out[0] = get_byte(1, R0); out[1] = get_byte(0, R0); out[2] = get_byte(1, R1); out[3] = get_byte(0, R1); out[4] = get_byte(1, R2); out[5] = get_byte(0, R2); @@ -34,56 +52,34 @@ void RC2::dec(const byte in[], byte out[]) const { u16bit R0 = make_u16bit(in[1], in[0]), R1 = make_u16bit(in[3], in[2]), R2 = make_u16bit(in[5], in[4]), R3 = make_u16bit(in[7], in[6]); - rmix(R0, R1, R2, R3,15); rmix(R0, R1, R2, R3,14); rmix(R0, R1, R2, R3,13); - rmix(R0, R1, R2, R3,12); rmix(R0, R1, R2, R3,11); rmash(R0, R1, R2, R3); - rmix(R0, R1, R2, R3,10); rmix(R0, R1, R2, R3, 9); rmix(R0, R1, R2, R3, 8); - rmix(R0, R1, R2, R3, 7); rmix(R0, R1, R2, R3, 6); rmix(R0, R1, R2, R3, 5); - rmash(R0, R1, R2, R3); rmix(R0, R1, R2, R3, 4); rmix(R0, R1, R2, R3, 3); - rmix(R0, R1, R2, R3, 2); rmix(R0, R1, R2, R3, 1); rmix(R0, R1, R2, R3, 0); - out[0] = get_byte(1, R0); out[1] = get_byte(0, R0); - out[2] = get_byte(1, R1); out[3] = get_byte(0, R1); - out[4] = get_byte(1, R2); out[5] = get_byte(0, R2); - out[6] = get_byte(1, R3); out[7] = get_byte(0, R3); - } -/************************************************* -* RC2 Mix Function * -*************************************************/ -void RC2::mix(u16bit& R0, u16bit& R1, u16bit& R2, - u16bit& R3, u32bit round) const - { - R0 += (R1 & ~R3) + (R2 & R3) + K[4*round ]; R0 = rotate_left(R0, 1); - R1 += (R2 & ~R0) + (R3 & R0) + K[4*round + 1]; R1 = rotate_left(R1, 2); - R2 += (R3 & ~R1) + (R0 & R1) + K[4*round + 2]; R2 = rotate_left(R2, 3); - R3 += (R0 & ~R2) + (R1 & R2) + K[4*round + 3]; R3 = rotate_left(R3, 5); - } + for(u32bit j = 0; j != 16; j++) + { + R3 = rotate_right(R3, 5); + R3 -= (R0 & ~R2) + (R1 & R2) + K[63 - (4*j + 0)]; -/************************************************* -* RC2 R-Mix Function * -*************************************************/ -void RC2::rmix(u16bit& R0, u16bit& R1, u16bit& R2, - u16bit& R3, u32bit round) const - { - R3 = rotate_right(R3, 5); R3 -= (R0 & ~R2) + (R1 & R2) + K[4*round + 3]; - R2 = rotate_right(R2, 3); R2 -= (R3 & ~R1) + (R0 & R1) + K[4*round + 2]; - R1 = rotate_right(R1, 2); R1 -= (R2 & ~R0) + (R3 & R0) + K[4*round + 1]; - R0 = rotate_right(R0, 1); R0 -= (R1 & ~R3) + (R2 & R3) + K[4*round + 0]; - } + R2 = rotate_right(R2, 3); + R2 -= (R3 & ~R1) + (R0 & R1) + K[63 - (4*j + 1)]; -/************************************************* -* RC2 Mash Function * -*************************************************/ -void RC2::mash(u16bit& R0, u16bit& R1, u16bit& R2, u16bit& R3) const - { - R0 += K[R3 % 64]; R1 += K[R0 % 64]; R2 += K[R1 % 64]; R3 += K[R2 % 64]; - } + R1 = rotate_right(R1, 2); + R1 -= (R2 & ~R0) + (R3 & R0) + K[63 - (4*j + 2)]; -/************************************************* -* RC2 R-Mash Function * -*************************************************/ -void RC2::rmash(u16bit& R0, u16bit& R1, u16bit& R2, u16bit& R3) const - { - R3 -= K[R2 % 64]; R2 -= K[R1 % 64]; R1 -= K[R0 % 64]; R0 -= K[R3 % 64]; + R0 = rotate_right(R0, 1); + R0 -= (R1 & ~R3) + (R2 & R3) + K[63 - (4*j + 3)]; + + if(j == 4 || j == 10) + { + R3 -= K[R2 % 64]; + R2 -= K[R1 % 64]; + R1 -= K[R0 % 64]; + R0 -= K[R3 % 64]; + } + } + + out[0] = get_byte(1, R0); out[1] = get_byte(0, R0); + out[2] = get_byte(1, R1); out[3] = get_byte(0, R1); + out[4] = get_byte(1, R2); out[5] = get_byte(0, R2); + out[6] = get_byte(1, R3); out[7] = get_byte(0, R3); } /************************************************* |