aboutsummaryrefslogtreecommitdiffstats
path: root/src/idea.cpp
diff options
context:
space:
mode:
authorlloyd <[email protected]>2007-11-11 19:48:29 +0000
committerlloyd <[email protected]>2007-11-11 19:48:29 +0000
commit7f3a3d666d9f11f4243c6443e75e0930dd3e9d9b (patch)
tree4b636f95a8459c11f18c0772f7456b88be1b6ae2 /src/idea.cpp
parentbfab991c9ab5450d78b5c2424d9c7b213995c642 (diff)
Make IDEA::mul_inv just a function in an anonymous namespace, since it really
had no reason/need to be a class method.
Diffstat (limited to 'src/idea.cpp')
-rw-r--r--src/idea.cpp74
1 files changed, 36 insertions, 38 deletions
diff --git a/src/idea.cpp b/src/idea.cpp
index f37d51d0e..e2338fa0d 100644
--- a/src/idea.cpp
+++ b/src/idea.cpp
@@ -13,17 +13,44 @@ namespace {
/*************************************************
* Multiplication modulo 65537 *
*************************************************/
-inline u16bit 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);
- return 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
- return 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);
}
}
@@ -46,8 +73,7 @@ void IDEA::enc(const byte in[], byte out[]) const
X4 = mul(X4, EK[6*j+3]);
u16bit T0 = X3;
- X3 ^= X1;
- X3 = mul(X3, EK[6*j+4]);
+ X3 = mul(X3 ^ X1, EK[6*j+4]);
u16bit T1 = X2;
X2 = mul((X2 ^ X4) + X3, EK[6*j+5]);
@@ -85,8 +111,7 @@ void IDEA::dec(const byte in[], byte out[]) const
X4 = mul(X4, DK[6*j+3]);
u16bit T0 = X3;
- X3 ^= X1;
- X3 = mul(X3, DK[6*j+4]);
+ X3 = mul(X3 ^ X1, DK[6*j+4]);
u16bit T1 = X2;
X2 = mul((X2 ^ X4) + X3, DK[6*j+5]);
@@ -107,33 +132,6 @@ void IDEA::dec(const byte in[], byte out[]) const
}
/*************************************************
-* 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 = x / y;
- x %= y;
- t1 += q * t0;
-
- if(x == 1)
- return t1;
-
- q = y / x;
- y %= x;
- t0 += q * t1;
- }
- return (1 - t0);
- }
-
-/*************************************************
* IDEA Key Schedule *
*************************************************/
void IDEA::key(const byte key[], u32bit)