8NAMESPACE_BEGIN(CryptoPP)
10static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4);
12#define low16(x) ((x)&0xffff)
13#define high16(x) ((x)>>16)
15CRYPTOPP_COMPILE_ASSERT(
sizeof(IDEA::Word) >= 2);
18#define DirectMUL(a,b) \
20 word32 p=(word32)low16(a)*b; \
24 p = low16(p) - high16(p); \
25 a = (IDEA::Word)p - (IDEA::Word)high16(p); \
32volatile bool IDEA::Base::tablesBuilt =
false;
33word16 IDEA::Base::log[0x10000];
34word16 IDEA::Base::antilog[0x10000];
36void IDEA::Base::BuildLogTables()
47 for (i=0; i<0x10000; i++)
53 for (i=0; i<0x10000; i++)
54 log[antilog[i]] = (
word16)i;
58void IDEA::Base::LookupKeyLogs()
74inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b)
76 a = antilog[low16(log[low16(a)]+b)];
80void IDEA::Base::UncheckedSetKey(
const byte *userKey,
unsigned int length,
const NameValuePairs &)
82 AssertValidKeyLength(length);
90 if (!IsForwardTransformation())
98void IDEA::Base::EnKey (
const byte *userKey)
103 m_key[i] = ((IDEA::Word)userKey[2*i]<<8) | userKey[2*i+1];
105 for (; i<IDEA_KEYLEN; i++)
108 m_key[i] = low16((m_key[j+(i+1)%8] << 9) | (m_key[j+(i+2)%8] >> 7));
112static IDEA::Word MulInv(IDEA::Word x)
115 for (
unsigned i=0; i<15; i++)
117 DirectMUL(y,low16(y));
123static inline IDEA::Word AddInv(IDEA::Word x)
128void IDEA::Base::DeKey()
133 for (i=0; i<ROUNDS; i++)
135 tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);
136 tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1+(i>0)]);
137 tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2-(i>0)]);
138 tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);
139 tempkey[i*6+4] = m_key[(ROUNDS-1-i)*6+4];
140 tempkey[i*6+5] = m_key[(ROUNDS-1-i)*6+5];
143 tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);
144 tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1]);
145 tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2]);
146 tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);
151#ifdef IDEA_LARGECACHE
152#define MUL(a,b) LookupMUL(a,b)
154#define MUL(a,b) DirectMUL(a,b)
157void IDEA::Base::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock,
byte *outBlock)
const
161 const IDEA::Word *key = m_key;
162 IDEA::Word x0,x1,x2,x3,t0,t1;
163 Block::Get(inBlock)(x0)(x1)(x2)(x3);
165 for (
unsigned int i=0; i<ROUNDS; i++)
183 MUL(x0, key[ROUNDS*6+0]);
184 x2 += key[ROUNDS*6+1];
185 x1 += key[ROUNDS*6+2];
186 MUL(x3, key[ROUNDS*6+3]);
188 Block::Put(xorBlock, outBlock)(x0)(x2)(x1)(x3);
Fixed size stack-based SecBlock.
Interface for retrieving values given their names.
Access a block of memory.
unsigned int word32
32-bit unsigned datatype
unsigned short word16
16-bit unsigned datatype
Classes for the IDEA block cipher.
Utility functions for the Crypto++ library.
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
Classes and functions for secure memory allocations.
Access a block of memory.