Fabcoin Core  0.16.2
P2P Digital Currency
idea.cpp
Go to the documentation of this file.
1 // idea.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "idea.h"
5 #include "misc.h"
6 #include "secblock.h"
7 
9 
10 static const int IDEA_KEYLEN=(6*IDEA::ROUNDS+4); // key schedule length in # of word16s
11 
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)
14 
16 
17 // should use an inline function but macros are still faster in MSVC 4.0
18 #define DirectMUL(a,b) \
19 { \
20  word32 p=(word32)low16(a)*b; \
21  \
22  if (p) \
23  { \
24  p = low16(p) - high16(p); \
25  a = (IDEA::Word)p - (IDEA::Word)high16(p); \
26  } \
27  else \
28  a = 1-a-b; \
29 }
30 
31 #ifdef IDEA_LARGECACHE
32 volatile bool IDEA::Base::tablesBuilt = false;
33 word16 IDEA::Base::log[0x10000];
34 word16 IDEA::Base::antilog[0x10000];
35 
36 void IDEA::Base::BuildLogTables()
37 {
38  if (tablesBuilt)
39  return;
40  else
41  {
42  tablesBuilt = true;
43 
44  IDEA::Word x=1;
45  word32 i;
46 
47  for (i=0; i<0x10000; i++)
48  {
49  antilog[i] = (word16)x;
50  DirectMUL(x, 3);
51  }
52 
53  for (i=0; i<0x10000; i++)
54  log[antilog[i]] = (word16)i;
55  }
56 }
57 
58 void IDEA::Base::LookupKeyLogs()
59 {
60  IDEA::Word* Z=key;
61  int r=ROUNDS;
62  do
63  {
64  Z[0] = log[Z[0]];
65  Z[3] = log[Z[3]];
66  Z[4] = log[Z[4]];
67  Z[5] = log[Z[5]];
68  Z+=6;
69  } while (--r);
70  Z[0] = log[Z[0]];
71  Z[3] = log[Z[3]];
72 }
73 
74 inline void IDEA::Base::LookupMUL(IDEA::Word &a, IDEA::Word b)
75 {
76  a = antilog[low16(log[low16(a)]+b)];
77 }
78 #endif // IDEA_LARGECACHE
79 
80 void IDEA::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)
81 {
82  AssertValidKeyLength(length);
83 
84 #ifdef IDEA_LARGECACHE
85  BuildLogTables();
86 #endif
87 
88  EnKey(userKey);
89 
91  DeKey();
92 
93 #ifdef IDEA_LARGECACHE
94  LookupKeyLogs();
95 #endif
96 }
97 
98 void IDEA::Base::EnKey (const byte *userKey)
99 {
100  unsigned int i;
101 
102  for (i=0; i<8; i++)
103  m_key[i] = ((IDEA::Word)userKey[2*i]<<8) | userKey[2*i+1];
104 
105  for (; i<IDEA_KEYLEN; i++)
106  {
107  unsigned int j = RoundDownToMultipleOf(i,8U)-8;
108  m_key[i] = low16((m_key[j+(i+1)%8] << 9) | (m_key[j+(i+2)%8] >> 7));
109  }
110 }
111 
112 static IDEA::Word MulInv(IDEA::Word x)
113 {
114  IDEA::Word y=x;
115  for (unsigned i=0; i<15; i++)
116  {
117  DirectMUL(y,low16(y));
118  DirectMUL(y,x);
119  }
120  return low16(y);
121 }
122 
123 static inline IDEA::Word AddInv(IDEA::Word x)
124 {
125  return low16(0-x);
126 }
127 
129 {
131  size_t i;
132 
133  for (i=0; i<ROUNDS; i++)
134  {
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];
141  }
142 
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]);
147 
148  m_key = tempkey;
149 }
150 
151 #ifdef IDEA_LARGECACHE
152 #define MUL(a,b) LookupMUL(a,b)
153 #else
154 #define MUL(a,b) DirectMUL(a,b)
155 #endif
156 
157 void IDEA::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
158 {
160 
161  const IDEA::Word *key = m_key;
162  IDEA::Word x0,x1,x2,x3,t0,t1;
163  Block::Get(inBlock)(x0)(x1)(x2)(x3);
164 
165  for (unsigned int i=0; i<ROUNDS; i++)
166  {
167  MUL(x0, key[i*6+0]);
168  x1 += key[i*6+1];
169  x2 += key[i*6+2];
170  MUL(x3, key[i*6+3]);
171  t0 = x0^x2;
172  MUL(t0, key[i*6+4]);
173  t1 = t0 + (x1^x3);
174  MUL(t1, key[i*6+5]);
175  t0 += t1;
176  x0 ^= t1;
177  x3 ^= t0;
178  t0 ^= x1;
179  x1 = x2^t1;
180  x2 = t0;
181  }
182 
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]);
187 
188  Block::Put(xorBlock, outBlock)(x0)(x2)(x1)(x3);
189 }
190 
IDEA block cipher.
Definition: idea.h:26
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: idea.cpp:157
PutBlock< T, B, PA > Put
Definition: misc.h:2237
#define MUL(a, b)
Definition: idea.cpp:154
unsigned short word16
Definition: config.h:230
void EnKey(const byte *)
Definition: idea.cpp:98
void DeKey()
Definition: idea.cpp:128
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
CRYPTOPP_COMPILE_ASSERT(sizeof(IDEA::Word) >=2)
#define DirectMUL(a, b)
Definition: idea.cpp:18
Access a block of memory.
Definition: misc.h:2233
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: idea.cpp:80
word Word
Definition: idea.h:30
FixedSizeSecBlock< Word, 6 *ROUNDS+4 > m_key
Definition: idea.h:47
Classes and functions for secure memory allocations.
#define a(i)
#define x(i)
virtual bool IsForwardTransformation() const =0
Determines if the cipher is being operated in its forward direction.
BlockGetAndPut< word32, BigEndian > Block
Definition: cast.cpp:34
#define t1
void AssertValidKeyLength(size_t length) const
Validates the key length.
Definition: cryptlib.h:725
Fixed size stack-based SecBlock.
Definition: secblock.h:753
#define b(i, j)
#define low16(x)
Definition: idea.cpp:12
#define NAMESPACE_END
Definition: config.h:201
unsigned int word32
Definition: config.h:231
T1 RoundDownToMultipleOf(const T1 &n, const T2 &m)
Rounds a value down to a multiple of a second value.
Definition: misc.h:889
Classes for the IDEA block cipher.
Interface for retrieving values given their names.
Definition: cryptlib.h:279