diff options
Diffstat (limited to 'src/block/idea/idea.cpp')
-rw-r--r-- | src/block/idea/idea.cpp | 78 |
1 files changed, 26 insertions, 52 deletions
diff --git a/src/block/idea/idea.cpp b/src/block/idea/idea.cpp index fb5fe83f1..15ff7c0ec 100644 --- a/src/block/idea/idea.cpp +++ b/src/block/idea/idea.cpp @@ -55,13 +55,13 @@ u16bit mul_inv(u16bit x) return (1 - t0); } -} - -/* -* IDEA Encryption +/** +* IDEA is involutional, depending only on the key schedule */ -void IDEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const +void idea_op(const byte in[], byte out[], u32bit blocks, const u16bit K[52]) { + const u32bit BLOCK_SIZE = 8; + for(u32bit i = 0; i != blocks; ++i) { u16bit X1 = load_be<u16bit>(in, 0); @@ -71,16 +71,16 @@ void IDEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const for(u32bit j = 0; j != 8; ++j) { - X1 = mul(X1, EK[6*j+0]); - X2 += EK[6*j+1]; - X3 += EK[6*j+2]; - X4 = mul(X4, EK[6*j+3]); + X1 = mul(X1, K[6*j+0]); + X2 += K[6*j+1]; + X3 += K[6*j+2]; + X4 = mul(X4, K[6*j+3]); u16bit T0 = X3; - X3 = mul(X3 ^ X1, EK[6*j+4]); + X3 = mul(X3 ^ X1, K[6*j+4]); u16bit T1 = X2; - X2 = mul((X2 ^ X4) + X3, EK[6*j+5]); + X2 = mul((X2 ^ X4) + X3, K[6*j+5]); X3 += X2; X1 ^= X2; @@ -89,10 +89,10 @@ void IDEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const X3 ^= T1; } - X1 = mul(X1, EK[48]); - X2 += EK[50]; - X3 += EK[49]; - X4 = mul(X4, EK[51]); + X1 = mul(X1, K[48]); + X2 += K[50]; + X3 += K[49]; + X4 = mul(X4, K[51]); store_be(out, X1, X3, X2, X4); @@ -101,48 +101,22 @@ void IDEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const } } +} + +/* +* IDEA Encryption +*/ +void IDEA::encrypt_n(const byte in[], byte out[], u32bit blocks) const + { + idea_op(in, out, blocks, EK); + } + /* * IDEA Decryption */ void IDEA::decrypt_n(const byte in[], byte out[], u32bit blocks) const { - for(u32bit i = 0; i != blocks; ++i) - { - u16bit X1 = load_be<u16bit>(in, 0); - u16bit X2 = load_be<u16bit>(in, 1); - u16bit X3 = load_be<u16bit>(in, 2); - u16bit X4 = load_be<u16bit>(in, 3); - - for(u32bit j = 0; j != 8; ++j) - { - X1 = mul(X1, DK[6*j+0]); - X2 += DK[6*j+1]; - X3 += DK[6*j+2]; - X4 = mul(X4, DK[6*j+3]); - - u16bit T0 = X3; - X3 = mul(X3 ^ X1, DK[6*j+4]); - - u16bit T1 = X2; - X2 = mul((X2 ^ X4) + X3, DK[6*j+5]); - X3 += X2; - - X1 ^= X2; - X4 ^= X3; - X2 ^= T0; - X3 ^= T1; - } - - X1 = mul(X1, DK[48]); - X2 += DK[50]; - X3 += DK[49]; - X4 = mul(X4, DK[51]); - - store_be(out, X1, X3, X2, X4); - - in += BLOCK_SIZE; - out += BLOCK_SIZE; - } + idea_op(in, out, blocks, DK); } /* |