diff options
author | lloyd <[email protected]> | 2008-11-03 19:01:41 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-11-03 19:01:41 +0000 |
commit | 8efcfc120b82a5851ae1ac14da19893519d5717d (patch) | |
tree | 28b3c89ed31916027f4888a71872bb7814140ee8 /src | |
parent | f6bf1e59f39dd3bb628451a53292407a7e2e29f4 (diff) |
Inline more of Turing::generate
Diffstat (limited to 'src')
-rw-r--r-- | src/cipher/turing/tur_tab.cpp | 25 | ||||
-rw-r--r-- | src/cipher/turing/turing.cpp | 112 | ||||
-rw-r--r-- | src/cipher/turing/turing.h | 1 |
3 files changed, 68 insertions, 70 deletions
diff --git a/src/cipher/turing/tur_tab.cpp b/src/cipher/turing/tur_tab.cpp index 08793f59d..8162a999e 100644 --- a/src/cipher/turing/tur_tab.cpp +++ b/src/cipher/turing/tur_tab.cpp @@ -31,31 +31,6 @@ const byte Turing::SBOX[256] = { 0xEE, 0x17, 0x81, 0x42, 0x58, 0x0A, 0x4B, 0x63, 0xE5, 0xBE, 0x6E, 0xAD, 0xBF, 0x43, 0x94, 0x97 }; -const byte Turing::OFFSETS[272] = { - 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x0C, 0x0E, 0x0F, - 0x10, 0x00, 0x00, 0x00, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, - 0x0D, 0x00, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x0A, 0x0B, 0x0C, 0x0D, - 0x0E, 0x0F, 0x10, 0x00, 0x01, 0x05, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, - 0x0F, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x0A, 0x0C, 0x0D, - 0x0E, 0x00, 0x00, 0x00, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, - 0x0B, 0x0F, 0x00, 0x01, 0x02, 0x00, 0x00, 0x00, 0x08, 0x09, 0x0A, 0x0B, - 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x03, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00, - 0x0D, 0x0E, 0x0F, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x08, 0x0A, 0x0B, - 0x0C, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, - 0x09, 0x0D, 0x0F, 0x10, 0x00, 0x00, 0x00, 0x00, 0x06, 0x07, 0x08, 0x09, - 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x01, 0x03, 0x04, 0x05, 0x00, 0x00, 0x00, - 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x00, 0x01, 0x02, 0x06, 0x08, 0x09, - 0x0A, 0x00, 0x00, 0x00, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, - 0x07, 0x0B, 0x0D, 0x0E, 0x0F, 0x00, 0x00, 0x00, 0x04, 0x05, 0x06, 0x07, - 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x10, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, - 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x00, 0x04, 0x06, 0x07, - 0x08, 0x00, 0x00, 0x00, 0x0E, 0x0F, 0x10, 0x00, 0x01, 0x02, 0x03, 0x04, - 0x05, 0x09, 0x0B, 0x0C, 0x0D, 0x00, 0x00, 0x00, 0x02, 0x03, 0x04, 0x05, - 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0E, 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, - 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x02, 0x04, 0x05, - 0x06, 0x00, 0x00, 0x00, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x00, 0x01, 0x02, - 0x03, 0x07, 0x09, 0x0A, 0x0B, 0x00, 0x00, 0x00 }; - const u32bit Turing::Q_BOX[256] = { 0x1FAA1887, 0x4E5E435C, 0x9165C042, 0x250E6EF4, 0x5957EE20, 0xD484FED3, 0xA666C502, 0x7E54E8AE, 0xD12EE9D9, 0xFC1F38D4, 0x49829B5D, 0x1B5CDF3C, diff --git a/src/cipher/turing/turing.cpp b/src/cipher/turing/turing.cpp index 85a29ddfb..04b9d2439 100644 --- a/src/cipher/turing/turing.cpp +++ b/src/cipher/turing/turing.cpp @@ -25,11 +25,31 @@ inline void PHT(MemoryRegion<u32bit>& buf) buf[j] += sum; } +} + +/************************************************* +* Combine cipher stream with message * +*************************************************/ +void Turing::cipher(const byte in[], byte out[], u32bit length) + { + while(length >= buffer.size() - position) + { + xor_buf(out, in, buffer.begin() + position, buffer.size() - position); + length -= (buffer.size() - position); + in += (buffer.size() - position); + out += (buffer.size() - position); + generate(); + } + xor_buf(out, in, buffer.begin() + position, length); + position += length; + } + /************************************************* -* Turing's polynomial multiplication * +* Generate cipher stream * *************************************************/ -inline u32bit mul(u32bit X) +void Turing::generate() { + // Table for Turing's polynomial multiplication static const u32bit MULT_TAB[256] = { 0x00000000, 0xD02B4367, 0xED5686CE, 0x3D7DC5A9, 0x97AC41D1, 0x478702B6, 0x7AFAC71F, 0xAAD18478, 0x631582EF, 0xB33EC188, 0x8E430421, 0x5E684746, @@ -75,50 +95,42 @@ inline u32bit mul(u32bit X) 0x6131A7D0, 0xB11AE4B7, 0x78DEE220, 0xA8F5A147, 0x958864EE, 0x45A32789, 0xEF72A3F1, 0x3F59E096, 0x0224253F, 0xD20F6658 }; - return (X << 8) ^ MULT_TAB[(X >> 24) & 0xFF]; - } - -} + const byte OFFSETS[] = { + 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 14, 15, 16, 0, 0, 0, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 0, 2, 3, 4, 0, 0, 0, 10, 11, 12, 13, 14, 15, + 16, 0, 1, 5, 7, 8, 9, 0, 0, 0, 15, 16, 0, 1, 2, 3, 4, 5, 6, 10, + 12, 13, 14, 0, 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 0, 1, 2, + 0, 0, 0, 8, 9, 10, 11, 12, 13, 14, 15, 16, 3, 5, 6, 7, 0, 0, 0, + 13, 14, 15, 16, 0, 1, 2, 3, 4, 8, 10, 11, 12, 0, 0, 0, 1, 2, 3, + 4, 5, 6, 7, 8, 9, 13, 15, 16, 0, 0, 0, 0, 6, 7, 8, 9, 10, 11, + 12, 13, 14, 1, 3, 4, 5, 0, 0, 0, 11, 12, 13, 14, 15, 16, 0, 1, + 2, 6, 8, 9, 10, 0, 0, 0, 16, 0, 1, 2, 3, 4, 5, 6, 7, 11, 13, 14, + 15, 0, 0, 0, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 1, 2, 3, 0, 0, 0, + 9, 10, 11, 12, 13, 14, 15, 16, 0, 4, 6, 7, 8, 0, 0, 0, 14, 15, + 16, 0, 1, 2, 3, 4, 5, 9, 11, 12, 13, 0, 0, 0, 2, 3, 4, 5, 6, 7, + 8, 9, 10, 14, 16, 0, 1, 0, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 2, 4, 5, 6, 0, 0, 0, 12, 13, 14, 15, 16, 0, 1, 2, 3, 7, 9, + 10, 11, 0, 0, 0 }; -/************************************************* -* Combine cipher stream with message * -*************************************************/ -void Turing::cipher(const byte in[], byte out[], u32bit length) - { - while(length >= buffer.size() - position) - { - xor_buf(out, in, buffer.begin() + position, buffer.size() - position); - length -= (buffer.size() - position); - in += (buffer.size() - position); - out += (buffer.size() - position); - generate(); - } - xor_buf(out, in, buffer.begin() + position, length); - position += length; - } - -/************************************************* -* Generate cipher stream * -*************************************************/ -void Turing::generate() - { for(u32bit j = 0; j != 17; ++j) { - const u32bit idx_0 = OFFSETS[16*j]; - const u32bit idx_1 = OFFSETS[16*j+1]; - const u32bit idx_2 = OFFSETS[16*j+2]; - const u32bit idx_3 = OFFSETS[16*j+3]; - const u32bit idx_4 = OFFSETS[16*j+4]; - const u32bit idx_5 = OFFSETS[16*j+5]; - const u32bit idx_6 = OFFSETS[16*j+6]; - const u32bit idx_7 = OFFSETS[16*j+7]; - const u32bit idx_8 = OFFSETS[16*j+8]; - const u32bit idx_12 = OFFSETS[16*j+9]; - const u32bit idx_14 = OFFSETS[16*j+10]; - const u32bit idx_15 = OFFSETS[16*j+11]; - const u32bit idx_16 = OFFSETS[16*j+12]; - - R[idx_0] = mul(R[idx_0]) ^ R[idx_15] ^ R[idx_4]; + const byte idx_0 = OFFSETS[16*j]; + const byte idx_1 = OFFSETS[16*j+1]; + const byte idx_2 = OFFSETS[16*j+2]; + const byte idx_3 = OFFSETS[16*j+3]; + const byte idx_4 = OFFSETS[16*j+4]; + const byte idx_5 = OFFSETS[16*j+5]; + const byte idx_6 = OFFSETS[16*j+6]; + const byte idx_7 = OFFSETS[16*j+7]; + const byte idx_8 = OFFSETS[16*j+8]; + const byte idx_12 = OFFSETS[16*j+9]; + const byte idx_14 = OFFSETS[16*j+10]; + const byte idx_15 = OFFSETS[16*j+11]; + const byte idx_16 = OFFSETS[16*j+12]; + + // mul(X) == return (X << 8) ^ MULT_TAB[(X >> 24) & 0xFF]; + R[idx_0] = ((R[idx_0] << 8) ^ MULT_TAB[(R[idx_0] >> 24) & 0xFF]) ^ + R[idx_15] ^ R[idx_4]; u32bit A = R[idx_0]; u32bit B = R[idx_14]; @@ -143,13 +155,25 @@ void Turing::generate() E += A + B + C + D; A += E; B += E; C += E; D += E; + // mul(X) == return (X << 8) ^ MULT_TAB[(X >> 24) & 0xFF]; + R[idx_1] = ((R[idx_1] << 8) ^ MULT_TAB[(R[idx_1] >> 24) & 0xFF]) ^ + R[idx_16] ^ R[idx_5]; + R[idx_2] = ((R[idx_2] << 8) ^ MULT_TAB[(R[idx_2] >> 24) & 0xFF]) ^ + R[idx_0] ^ R[idx_6]; + R[idx_3] = ((R[idx_3] << 8) ^ MULT_TAB[(R[idx_3] >> 24) & 0xFF]) ^ + R[idx_1] ^ R[idx_7]; + +#if 0 R[idx_1] = mul(R[idx_1]) ^ R[idx_16] ^ R[idx_5]; R[idx_2] = mul(R[idx_2]) ^ R[idx_0] ^ R[idx_6]; R[idx_3] = mul(R[idx_3]) ^ R[idx_1] ^ R[idx_7]; +#endif E += R[idx_4]; - R[idx_4] = mul(R[idx_4]) ^ R[idx_2] ^ R[idx_8]; + //R[idx_4] = mul(R[idx_4]) ^ R[idx_2] ^ R[idx_8]; + R[idx_4] = ((R[idx_4] << 8) ^ MULT_TAB[(R[idx_4] >> 24) & 0xFF]) ^ + R[idx_2] ^ R[idx_8]; A += R[idx_1]; B += R[idx_16]; diff --git a/src/cipher/turing/turing.h b/src/cipher/turing/turing.h index 14b2e6fd6..a73d08d07 100644 --- a/src/cipher/turing/turing.h +++ b/src/cipher/turing/turing.h @@ -32,7 +32,6 @@ class BOTAN_DLL Turing : public StreamCipher static const u32bit Q_BOX[256]; static const byte SBOX[256]; - static const byte OFFSETS[272]; SecureBuffer<u32bit, 256> S0, S1, S2, S3; SecureBuffer<u32bit, 17> R; |