diff options
-rw-r--r-- | checks/check.cpp | 2 | ||||
-rw-r--r-- | include/idea.h | 1 | ||||
-rw-r--r-- | misc/config/cc/msvc | 2 | ||||
-rw-r--r-- | modules/mp_asm64/modinfo.txt | 2 | ||||
-rw-r--r-- | modules/mp_asm64/mp_asm.h | 9 | ||||
-rw-r--r-- | src/idea.cpp | 102 |
6 files changed, 59 insertions, 59 deletions
diff --git a/checks/check.cpp b/checks/check.cpp index f009372ee..f31c9d204 100644 --- a/checks/check.cpp +++ b/checks/check.cpp @@ -47,7 +47,7 @@ int main(int argc, char* argv[]) << " --validate: Check test vectors\n" << " --benchmark: Benchmark everything\n" << " --bench-type={block,mode,stream,hash,mac,rng,pk}:\n" - << " Benchmark only algorithms of a particular type\n" + << " Benchmark only algorithms of a particular type\n" << " --html: Produce HTML output for benchmarks\n" << " --seconds=n: Benchmark for n seconds\n" << " --init=<str>: Pass <str> to the library\n" diff --git a/include/idea.h b/include/idea.h index 6f6932c38..10b0f7049 100644 --- a/include/idea.h +++ b/include/idea.h @@ -24,7 +24,6 @@ class IDEA : public BlockCipher void enc(const byte[], byte[]) const; void dec(const byte[], byte[]) const; void key(const byte[], u32bit); - static u16bit mul_inv(u16bit); SecureBuffer<u16bit, 52> EK, DK; }; diff --git a/misc/config/cc/msvc b/misc/config/cc/msvc index 63ee0a944..5bcec8e23 100644 --- a/misc/config/cc/msvc +++ b/misc/config/cc/msvc @@ -10,7 +10,7 @@ add_lib_option "" lib_opt_flags "/O2 /Ob2" check_opt_flags "/O2" -debug_flags "" +debug_flags "/Zi" no_debug_flags "" lang_flags "/EHsc /GR /D_CONSOLE" warning_flags "" diff --git a/modules/mp_asm64/modinfo.txt b/modules/mp_asm64/modinfo.txt index 6c51ed5b8..e371c9ede 100644 --- a/modules/mp_asm64/modinfo.txt +++ b/modules/mp_asm64/modinfo.txt @@ -8,10 +8,8 @@ load_on asm_ok mp_asm.h </replace> -# Disable amd64 since (nominally) the mp_amd64 module has that covered <arch> alpha -# amd64 ia64 mips64 ppc64 diff --git a/modules/mp_asm64/mp_asm.h b/modules/mp_asm64/mp_asm.h index b3a60219d..6abab6166 100644 --- a/modules/mp_asm64/mp_asm.h +++ b/modules/mp_asm64/mp_asm.h @@ -12,14 +12,7 @@ #error The mp_asm64 module requires that BOTAN_MP_WORD_BITS == 64 #endif -#if defined(BOTAN_TARGET_ARCH_IS_AMD64) - -#define BOTAN_WORD_MUL(a,b,z1,z0) do { \ - asm("mulq %3" : "=d" (z0), "=a" (z1) : \ - "a" (a), "rm" (b) : "cc"); \ -} while(0); - -#elif defined(BOTAN_TARGET_ARCH_IS_ALPHA) +#if defined(BOTAN_TARGET_ARCH_IS_ALPHA) #define BOTAN_WORD_MUL(a,b,z1,z0) do { \ asm("umulh %1,%2,%0" : "=r" (z0) : "r" (a), "r" (b)); \ diff --git a/src/idea.cpp b/src/idea.cpp index 267a14504..e2338fa0d 100644 --- a/src/idea.cpp +++ b/src/idea.cpp @@ -13,17 +13,44 @@ namespace { /************************************************* * Multiplication modulo 65537 * *************************************************/ -inline void mul(u16bit& a, u16bit b) +inline u16bit mul(u16bit x, u16bit y) { - if(a && b) + if(x && y) { - u32bit temp = static_cast<u32bit>(a) * b; - a = static_cast<u16bit>(temp >> 16); - b = static_cast<u16bit>(temp & 0xFFFF); - a = static_cast<u16bit>(b - a + ((b < a) ? 1 : 0)); + u32bit T = static_cast<u32bit>(x) * y; + x = static_cast<u16bit>(T >> 16); + y = static_cast<u16bit>(T & 0xFFFF); + return static_cast<u16bit>(y - x + ((y < x) ? 1 : 0)); } else - a = static_cast<u16bit>(1 - a - b); + return static_cast<u16bit>(1 - x - y); + } + +/************************************************* +* Find multiplicative inverses modulo 65537 * +*************************************************/ +u16bit mul_inv(u16bit x) + { + if(x <= 1) + return x; + + u16bit t0 = static_cast<u16bit>(65537 / x), t1 = 1; + u16bit y = static_cast<u16bit>(65537 % x); + + while(y != 1) + { + u16bit q = x / y; + x %= y; + t1 += q * t0; + + if(x == 1) + return t1; + + q = y / x; + y %= x; + t0 += q * t1; + } + return (1 - t0); } } @@ -40,24 +67,28 @@ void IDEA::enc(const byte in[], byte out[]) const for(u32bit j = 0; j != 8; ++j) { - mul(X1, EK[6*j+0]); + X1 = mul(X1, EK[6*j+0]); X2 += EK[6*j+1]; X3 += EK[6*j+2]; - mul(X4, EK[6*j+3]); + X4 = mul(X4, EK[6*j+3]); + u16bit T0 = X3; - X3 ^= X1; - mul(X3, EK[6*j+4]); + X3 = mul(X3 ^ X1, EK[6*j+4]); + u16bit T1 = X2; - X2 = static_cast<u16bit>((X2 ^ X4) + X3); - mul(X2, EK[6*j+5]); + X2 = mul((X2 ^ X4) + X3, EK[6*j+5]); X3 += X2; + X1 ^= X2; X4 ^= X3; X2 ^= T0; X3 ^= T1; } - mul(X1, EK[48]); X2 += EK[50]; X3 += EK[49]; mul(X4, EK[51]); + X1 = mul(X1, EK[48]); + X2 += EK[50]; + X3 += EK[49]; + X4 = mul(X4, EK[51]); store_be(out, X1, X3, X2, X4); } @@ -74,54 +105,33 @@ void IDEA::dec(const byte in[], byte out[]) const for(u32bit j = 0; j != 8; ++j) { - mul(X1, DK[6*j+0]); + X1 = mul(X1, DK[6*j+0]); X2 += DK[6*j+1]; X3 += DK[6*j+2]; - mul(X4, DK[6*j+3]); + X4 = mul(X4, DK[6*j+3]); + u16bit T0 = X3; - X3 ^= X1; - mul(X3, DK[6*j+4]); + X3 = mul(X3 ^ X1, DK[6*j+4]); + u16bit T1 = X2; - X2 = static_cast<u16bit>((X2 ^ X4) + X3); - mul(X2, DK[6*j+5]); + X2 = mul((X2 ^ X4) + X3, DK[6*j+5]); X3 += X2; + X1 ^= X2; X4 ^= X3; X2 ^= T0; X3 ^= T1; } - mul(X1, DK[48]); X2 += DK[50]; X3 += DK[49]; mul(X4, DK[51]); + X1 = mul(X1, DK[48]); + X2 += DK[50]; + X3 += DK[49]; + X4 = mul(X4, DK[51]); store_be(out, X1, X3, X2, X4); } /************************************************* -* Find multiplicative inverses modulo 65537 * -*************************************************/ -u16bit IDEA::mul_inv(u16bit x) - { - if(x <= 1) - return x; - - u16bit t0 = static_cast<u16bit>(65537 / x), t1 = 1; - u16bit y = static_cast<u16bit>(65537 % x); - - while(y != 1) - { - u16bit q = static_cast<u16bit>(x / y); - x %= y; - t1 += static_cast<u16bit>(q * t0); - if(x == 1) - return t1; - q = static_cast<u16bit>(y / x); - y %= x; - t0 += static_cast<u16bit>(q * t1); - } - return static_cast<u16bit>(1 - t0); - } - -/************************************************* * IDEA Key Schedule * *************************************************/ void IDEA::key(const byte key[], u32bit) |