10 static const
int IDEA_KEYLEN=(6*
IDEA::ROUNDS+4);
12 #define low16(x) ((x)&0xffff) // compiler should be able to optimize this away if word is 16 bits 13 #define high16(x) ((x)>>16) 18 #define DirectMUL(a,b) \ 20 assert(b <= 0xffff); \ 22 word32 p=(word32)low16(a)*b; \ 26 p = low16(p) - high16(p); \ 27 a = (IDEA::Word)p - (IDEA::Word)high16(p); \ 33 #ifdef IDEA_LARGECACHE 34 volatile bool IDEA::Base::tablesBuilt =
false;
35 word16 IDEA::Base::log[0x10000];
36 word16 IDEA::Base::antilog[0x10000];
38 void IDEA::Base::BuildLogTables()
49 for (i=0; i<0x10000; i++)
51 antilog[i] = (word16)x;
55 for (i=0; i<0x10000; i++)
56 log[antilog[i]] = (word16)i;
60 void IDEA::Base::LookupKeyLogs()
76 inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b)
78 a = antilog[low16(log[low16(a)]+b)];
80 #endif // IDEA_LARGECACHE 82 void IDEA::Base::UncheckedSetKey(
const byte *userKey,
unsigned int length,
const NameValuePairs &)
84 AssertValidKeyLength(length);
86 #ifdef IDEA_LARGECACHE 92 if (!IsForwardTransformation())
95 #ifdef IDEA_LARGECACHE 100 void IDEA::Base::EnKey (
const byte *userKey)
105 m_key[i] = ((IDEA::Word)userKey[2*i]<<8) | userKey[2*i+1];
107 for (; i<IDEA_KEYLEN; i++)
110 m_key[i] = low16((m_key[j+(i+1)%8] << 9) | (m_key[j+(i+2)%8] >> 7));
114 static IDEA::Word MulInv(IDEA::Word x)
117 for (
unsigned i=0; i<15; i++)
119 DirectMUL(y,low16(y));
125 static inline IDEA::Word AddInv(IDEA::Word x)
130 void IDEA::Base::DeKey()
135 for (i=0; i<ROUNDS; i++)
137 tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);
138 tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1+(i>0)]);
139 tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2-(i>0)]);
140 tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);
141 tempkey[i*6+4] = m_key[(ROUNDS-1-i)*6+4];
142 tempkey[i*6+5] = m_key[(ROUNDS-1-i)*6+5];
145 tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);
146 tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1]);
147 tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2]);
148 tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);
153 #ifdef IDEA_LARGECACHE 154 #define MUL(a,b) LookupMUL(a,b) 156 #define MUL(a,b) DirectMUL(a,b) 159 void IDEA::Base::ProcessAndXorBlock(
const byte *inBlock,
const byte *xorBlock, byte *outBlock)
const 163 const IDEA::Word *key = m_key;
164 IDEA::Word x0,x1,x2,x3,t0,t1;
165 Block::Get(inBlock)(x0)(x1)(x2)(x3);
167 for (
unsigned int i=0; i<ROUNDS; i++)
185 MUL(x0, key[ROUNDS*6+0]);
186 x2 += key[ROUNDS*6+1];
187 x1 += key[ROUNDS*6+2];
188 MUL(x3, key[ROUNDS*6+3]);
190 Block::Put(xorBlock, outBlock)(x0)(x2)(x1)(x3);
Utility functions for the Crypto++ library.
Classes and functions for secure memory allocations.
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Fixed size stack-based SecBlock.
Crypto++ library namespace.
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
Classes for the IDEA block cipher.
Interface for retrieving values given their names.