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 word32 p=(word32)low16(a)*b; \ 24 p = low16(p) - high16(p); \ 25 a = (IDEA::Word)p - (IDEA::Word)high16(p); \ 31 #ifdef IDEA_LARGECACHE 32 volatile bool IDEA::Base::tablesBuilt =
false;
33 word16 IDEA::Base::log[0x10000];
34 word16 IDEA::Base::antilog[0x10000];
36 void IDEA::Base::BuildLogTables()
47 for (i=0; i<0x10000; i++)
49 antilog[i] = (word16)x;
53 for (i=0; i<0x10000; i++)
54 log[antilog[i]] = (word16)i;
58 void IDEA::Base::LookupKeyLogs()
74 inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b)
76 a = antilog[low16(log[low16(a)]+b)];
78 #endif // IDEA_LARGECACHE 80 void IDEA::Base::UncheckedSetKey(
const byte *userKey,
unsigned int length,
const NameValuePairs &)
82 AssertValidKeyLength(length);
84 #ifdef IDEA_LARGECACHE 90 if (!IsForwardTransformation())
93 #ifdef IDEA_LARGECACHE 98 void 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));
112 static IDEA::Word MulInv(IDEA::Word x)
115 for (
unsigned i=0; i<15; i++)
117 DirectMUL(y,low16(y));
123 static inline IDEA::Word AddInv(IDEA::Word x)
128 void 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) 157 void 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);
Utility functions for the Crypto++ library.
Access a block of memory.
Classes and functions for secure memory allocations.
#define CRYPTOPP_COMPILE_ASSERT(expr)
Compile time assertion.
Fixed size stack-based SecBlock.
static const int ROUNDS
The number of rounds for the algorithm provided as a constant.
Access a block of memory.
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.