Fabcoin Core  0.16.2
P2P Digital Currency
cast.cpp
Go to the documentation of this file.
1 // cast.cpp - written and placed in the public domain by Wei Dai and Leonard Janke
2 // based on Steve Reid's public domain cast.c
3 
4 #include "pch.h"
5 #include "cast.h"
6 #include "misc.h"
7 
9 
10 /* Macros to access 8-bit bytes out of a 32-bit word */
11 #define U8a(x) GETBYTE(x,3)
12 #define U8b(x) GETBYTE(x,2)
13 #define U8c(x) GETBYTE(x,1)
14 #define U8d(x) GETBYTE(x,0)
15 
16 /* CAST uses three different round functions */
17 #define f1(l, r, km, kr) \
18  t = rotlVariable(km + r, kr); \
19  l ^= ((S[0][U8a(t)] ^ S[1][U8b(t)]) - \
20  S[2][U8c(t)]) + S[3][U8d(t)];
21 #define f2(l, r, km, kr) \
22  t = rotlVariable(km ^ r, kr); \
23  l ^= ((S[0][U8a(t)] - S[1][U8b(t)]) + \
24  S[2][U8c(t)]) ^ S[3][U8d(t)];
25 #define f3(l, r, km, kr) \
26  t = rotlVariable(km - r, kr); \
27  l ^= ((S[0][U8a(t)] + S[1][U8b(t)]) ^ \
28  S[2][U8c(t)]) - S[3][U8d(t)];
29 
30 #define F1(l, r, i, j) f1(l, r, K[i], K[i+j])
31 #define F2(l, r, i, j) f2(l, r, K[i], K[i+j])
32 #define F3(l, r, i, j) f3(l, r, K[i], K[i+j])
33 
35 
36 void CAST128::Enc::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
37 {
38  word32 t, l, r;
39 
40  /* Get inblock into l,r */
41  Block::Get(inBlock)(l)(r);
42  /* Do the work */
43  F1(l, r, 0, 16);
44  F2(r, l, 1, 16);
45  F3(l, r, 2, 16);
46  F1(r, l, 3, 16);
47  F2(l, r, 4, 16);
48  F3(r, l, 5, 16);
49  F1(l, r, 6, 16);
50  F2(r, l, 7, 16);
51  F3(l, r, 8, 16);
52  F1(r, l, 9, 16);
53  F2(l, r, 10, 16);
54  F3(r, l, 11, 16);
55  /* Only do full 16 rounds if key length > 80 bits */
56  if (!reduced) {
57  F1(l, r, 12, 16);
58  F2(r, l, 13, 16);
59  F3(l, r, 14, 16);
60  F1(r, l, 15, 16);
61  }
62  /* Put l,r into outblock */
63  Block::Put(xorBlock, outBlock)(r)(l);
64 }
65 
66 void CAST128::Dec::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
67 {
68  word32 t, l, r;
69 
70  /* Get inblock into l,r */
71  Block::Get(inBlock)(r)(l);
72  /* Only do full 16 rounds if key length > 80 bits */
73  if (!reduced) {
74  F1(r, l, 15, 16);
75  F3(l, r, 14, 16);
76  F2(r, l, 13, 16);
77  F1(l, r, 12, 16);
78  }
79  F3(r, l, 11, 16);
80  F2(l, r, 10, 16);
81  F1(r, l, 9, 16);
82  F3(l, r, 8, 16);
83  F2(r, l, 7, 16);
84  F1(l, r, 6, 16);
85  F3(r, l, 5, 16);
86  F2(l, r, 4, 16);
87  F1(r, l, 3, 16);
88  F3(l, r, 2, 16);
89  F2(r, l, 1, 16);
90  F1(l, r, 0, 16);
91  /* Put l,r into outblock */
92  Block::Put(xorBlock, outBlock)(l)(r);
93  /* Wipe clean */
94  t = l = r = 0;
95 }
96 
97 void CAST128::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
98 {
99  AssertValidKeyLength(keylength);
100 
101  reduced = (keylength <= 10);
102 
103  word32 X[4], Z[4];
104  GetUserKey(BIG_ENDIAN_ORDER, X, 4, userKey, keylength);
105 
106 #define x(i) GETBYTE(X[i/4], 3-i%4)
107 #define z(i) GETBYTE(Z[i/4], 3-i%4)
108 
109  unsigned int i;
110  for (i=0; i<=16; i+=16)
111  {
112  // this part is copied directly from RFC 2144 (with some search and replace) by Wei Dai
113  Z[0] = X[0] ^ S[4][x(0xD)] ^ S[5][x(0xF)] ^ S[6][x(0xC)] ^ S[7][x(0xE)] ^ S[6][x(0x8)];
114  Z[1] = X[2] ^ S[4][z(0x0)] ^ S[5][z(0x2)] ^ S[6][z(0x1)] ^ S[7][z(0x3)] ^ S[7][x(0xA)];
115  Z[2] = X[3] ^ S[4][z(0x7)] ^ S[5][z(0x6)] ^ S[6][z(0x5)] ^ S[7][z(0x4)] ^ S[4][x(0x9)];
116  Z[3] = X[1] ^ S[4][z(0xA)] ^ S[5][z(0x9)] ^ S[6][z(0xB)] ^ S[7][z(0x8)] ^ S[5][x(0xB)];
117  K[i+0] = S[4][z(0x8)] ^ S[5][z(0x9)] ^ S[6][z(0x7)] ^ S[7][z(0x6)] ^ S[4][z(0x2)];
118  K[i+1] = S[4][z(0xA)] ^ S[5][z(0xB)] ^ S[6][z(0x5)] ^ S[7][z(0x4)] ^ S[5][z(0x6)];
119  K[i+2] = S[4][z(0xC)] ^ S[5][z(0xD)] ^ S[6][z(0x3)] ^ S[7][z(0x2)] ^ S[6][z(0x9)];
120  K[i+3] = S[4][z(0xE)] ^ S[5][z(0xF)] ^ S[6][z(0x1)] ^ S[7][z(0x0)] ^ S[7][z(0xC)];
121  X[0] = Z[2] ^ S[4][z(0x5)] ^ S[5][z(0x7)] ^ S[6][z(0x4)] ^ S[7][z(0x6)] ^ S[6][z(0x0)];
122  X[1] = Z[0] ^ S[4][x(0x0)] ^ S[5][x(0x2)] ^ S[6][x(0x1)] ^ S[7][x(0x3)] ^ S[7][z(0x2)];
123  X[2] = Z[1] ^ S[4][x(0x7)] ^ S[5][x(0x6)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[4][z(0x1)];
124  X[3] = Z[3] ^ S[4][x(0xA)] ^ S[5][x(0x9)] ^ S[6][x(0xB)] ^ S[7][x(0x8)] ^ S[5][z(0x3)];
125  K[i+4] = S[4][x(0x3)] ^ S[5][x(0x2)] ^ S[6][x(0xC)] ^ S[7][x(0xD)] ^ S[4][x(0x8)];
126  K[i+5] = S[4][x(0x1)] ^ S[5][x(0x0)] ^ S[6][x(0xE)] ^ S[7][x(0xF)] ^ S[5][x(0xD)];
127  K[i+6] = S[4][x(0x7)] ^ S[5][x(0x6)] ^ S[6][x(0x8)] ^ S[7][x(0x9)] ^ S[6][x(0x3)];
128  K[i+7] = S[4][x(0x5)] ^ S[5][x(0x4)] ^ S[6][x(0xA)] ^ S[7][x(0xB)] ^ S[7][x(0x7)];
129  Z[0] = X[0] ^ S[4][x(0xD)] ^ S[5][x(0xF)] ^ S[6][x(0xC)] ^ S[7][x(0xE)] ^ S[6][x(0x8)];
130  Z[1] = X[2] ^ S[4][z(0x0)] ^ S[5][z(0x2)] ^ S[6][z(0x1)] ^ S[7][z(0x3)] ^ S[7][x(0xA)];
131  Z[2] = X[3] ^ S[4][z(0x7)] ^ S[5][z(0x6)] ^ S[6][z(0x5)] ^ S[7][z(0x4)] ^ S[4][x(0x9)];
132  Z[3] = X[1] ^ S[4][z(0xA)] ^ S[5][z(0x9)] ^ S[6][z(0xB)] ^ S[7][z(0x8)] ^ S[5][x(0xB)];
133  K[i+8] = S[4][z(0x3)] ^ S[5][z(0x2)] ^ S[6][z(0xC)] ^ S[7][z(0xD)] ^ S[4][z(0x9)];
134  K[i+9] = S[4][z(0x1)] ^ S[5][z(0x0)] ^ S[6][z(0xE)] ^ S[7][z(0xF)] ^ S[5][z(0xC)];
135  K[i+10] = S[4][z(0x7)] ^ S[5][z(0x6)] ^ S[6][z(0x8)] ^ S[7][z(0x9)] ^ S[6][z(0x2)];
136  K[i+11] = S[4][z(0x5)] ^ S[5][z(0x4)] ^ S[6][z(0xA)] ^ S[7][z(0xB)] ^ S[7][z(0x6)];
137  X[0] = Z[2] ^ S[4][z(0x5)] ^ S[5][z(0x7)] ^ S[6][z(0x4)] ^ S[7][z(0x6)] ^ S[6][z(0x0)];
138  X[1] = Z[0] ^ S[4][x(0x0)] ^ S[5][x(0x2)] ^ S[6][x(0x1)] ^ S[7][x(0x3)] ^ S[7][z(0x2)];
139  X[2] = Z[1] ^ S[4][x(0x7)] ^ S[5][x(0x6)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[4][z(0x1)];
140  X[3] = Z[3] ^ S[4][x(0xA)] ^ S[5][x(0x9)] ^ S[6][x(0xB)] ^ S[7][x(0x8)] ^ S[5][z(0x3)];
141  K[i+12] = S[4][x(0x8)] ^ S[5][x(0x9)] ^ S[6][x(0x7)] ^ S[7][x(0x6)] ^ S[4][x(0x3)];
142  K[i+13] = S[4][x(0xA)] ^ S[5][x(0xB)] ^ S[6][x(0x5)] ^ S[7][x(0x4)] ^ S[5][x(0x7)];
143  K[i+14] = S[4][x(0xC)] ^ S[5][x(0xD)] ^ S[6][x(0x3)] ^ S[7][x(0x2)] ^ S[6][x(0x8)];
144  K[i+15] = S[4][x(0xE)] ^ S[5][x(0xF)] ^ S[6][x(0x1)] ^ S[7][x(0x0)] ^ S[7][x(0xD)];
145  }
146 
147  for (i=16; i<32; i++)
148  K[i] &= 0x1f;
149 }
150 
151 // The following CAST-256 implementation was contributed by Leonard Janke
152 
153 const word32 CAST256::Base::t_m[8][24]={
154 { 0x5a827999, 0xd151d6a1, 0x482133a9, 0xbef090b1, 0x35bfedb9, 0xac8f4ac1,
155  0x235ea7c9, 0x9a2e04d1, 0x10fd61d9, 0x87ccbee1, 0xfe9c1be9, 0x756b78f1,
156  0xec3ad5f9, 0x630a3301, 0xd9d99009, 0x50a8ed11, 0xc7784a19, 0x3e47a721,
157  0xb5170429, 0x2be66131, 0xa2b5be39, 0x19851b41, 0x90547849, 0x0723d551},
158 { 0xc95c653a, 0x402bc242, 0xb6fb1f4a, 0x2dca7c52, 0xa499d95a, 0x1b693662,
159  0x9238936a, 0x0907f072, 0x7fd74d7a, 0xf6a6aa82, 0x6d76078a, 0xe4456492,
160  0x5b14c19a, 0xd1e41ea2, 0x48b37baa, 0xbf82d8b2, 0x365235ba, 0xad2192c2,
161  0x23f0efca, 0x9ac04cd2, 0x118fa9da, 0x885f06e2, 0xff2e63ea, 0x75fdc0f2},
162 { 0x383650db, 0xaf05ade3, 0x25d50aeb, 0x9ca467f3, 0x1373c4fb, 0x8a432203,
163  0x01127f0b, 0x77e1dc13, 0xeeb1391b, 0x65809623, 0xdc4ff32b, 0x531f5033,
164  0xc9eead3b, 0x40be0a43, 0xb78d674b, 0x2e5cc453, 0xa52c215b, 0x1bfb7e63,
165  0x92cadb6b, 0x099a3873, 0x8069957b, 0xf738f283, 0x6e084f8b, 0xe4d7ac93},
166 { 0xa7103c7c, 0x1ddf9984, 0x94aef68c, 0x0b7e5394, 0x824db09c, 0xf91d0da4,
167  0x6fec6aac, 0xe6bbc7b4, 0x5d8b24bc, 0xd45a81c4, 0x4b29decc, 0xc1f93bd4,
168  0x38c898dc, 0xaf97f5e4, 0x266752ec, 0x9d36aff4, 0x14060cfc, 0x8ad56a04,
169  0x01a4c70c, 0x78742414, 0xef43811c, 0x6612de24, 0xdce23b2c, 0x53b19834},
170 { 0x15ea281d, 0x8cb98525, 0x0388e22d, 0x7a583f35, 0xf1279c3d, 0x67f6f945,
171  0xdec6564d, 0x5595b355, 0xcc65105d, 0x43346d65, 0xba03ca6d, 0x30d32775,
172  0xa7a2847d, 0x1e71e185, 0x95413e8d, 0x0c109b95, 0x82dff89d, 0xf9af55a5,
173  0x707eb2ad, 0xe74e0fb5, 0x5e1d6cbd, 0xd4ecc9c5, 0x4bbc26cd, 0xc28b83d5},
174 { 0x84c413be, 0xfb9370c6, 0x7262cdce, 0xe9322ad6, 0x600187de, 0xd6d0e4e6,
175  0x4da041ee, 0xc46f9ef6, 0x3b3efbfe, 0xb20e5906, 0x28ddb60e, 0x9fad1316,
176  0x167c701e, 0x8d4bcd26, 0x041b2a2e, 0x7aea8736, 0xf1b9e43e, 0x68894146,
177  0xdf589e4e, 0x5627fb56, 0xccf7585e, 0x43c6b566, 0xba96126e, 0x31656f76},
178 { 0xf39dff5f, 0x6a6d5c67, 0xe13cb96f, 0x580c1677, 0xcedb737f, 0x45aad087,
179  0xbc7a2d8f, 0x33498a97, 0xaa18e79f, 0x20e844a7, 0x97b7a1af, 0x0e86feb7,
180  0x85565bbf, 0xfc25b8c7, 0x72f515cf, 0xe9c472d7, 0x6093cfdf, 0xd7632ce7,
181  0x4e3289ef, 0xc501e6f7, 0x3bd143ff, 0xb2a0a107, 0x296ffe0f, 0xa03f5b17},
182 { 0x6277eb00, 0xd9474808, 0x5016a510, 0xc6e60218, 0x3db55f20, 0xb484bc28,
183  0x2b541930, 0xa2237638, 0x18f2d340, 0x8fc23048, 0x06918d50, 0x7d60ea58,
184  0xf4304760, 0x6affa468, 0xe1cf0170, 0x589e5e78, 0xcf6dbb80, 0x463d1888,
185  0xbd0c7590, 0x33dbd298, 0xaaab2fa0, 0x217a8ca8, 0x9849e9b0, 0x0f1946b8}
186 };
187 
188 const unsigned int CAST256::Base::t_r[8][24]={
189  {19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11, 19, 27, 3, 11},
190  {4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28, 4, 12, 20, 28},
191  {21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13, 21, 29, 5, 13},
192  {6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30, 6, 14, 22, 30},
193  {23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15, 23, 31, 7, 15},
194  {8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0, 8, 16, 24, 0},
195  {25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17, 25, 1, 9, 17},
196  {10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2, 10, 18, 26, 2}
197 };
198 
199 #define Q(i) \
200  F1(block[2],block[3],8*i+4,-4); \
201  F2(block[1],block[2],8*i+5,-4); \
202  F3(block[0],block[1],8*i+6,-4); \
203  F1(block[3],block[0],8*i+7,-4);
204 
205 #define QBar(i) \
206  F1(block[3],block[0],8*i+7,-4); \
207  F3(block[0],block[1],8*i+6,-4); \
208  F2(block[1],block[2],8*i+5,-4); \
209  F1(block[2],block[3],8*i+4,-4);
210 
211 /* CAST256's encrypt/decrypt functions are identical except for the order that
212 the keys are used */
213 
214 void CAST256::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
215 {
216  word32 t, block[4];
217  Block::Get(inBlock)(block[0])(block[1])(block[2])(block[3]);
218 
219  // Perform 6 forward quad rounds
220  Q(0);
221  Q(1);
222  Q(2);
223  Q(3);
224  Q(4);
225  Q(5);
226 
227  // Perform 6 reverse quad rounds
228  QBar(6);
229  QBar(7);
230  QBar(8);
231  QBar(9);
232  QBar(10);
233  QBar(11);
234 
235  Block::Put(xorBlock, outBlock)(block[0])(block[1])(block[2])(block[3]);
236 }
237 
238 /* Set up a CAST-256 key */
239 
240 void CAST256::Base::Omega(int i, word32 kappa[8])
241 {
242  word32 t;
243 
244  f1(kappa[6],kappa[7],t_m[0][i],t_r[0][i]);
245  f2(kappa[5],kappa[6],t_m[1][i],t_r[1][i]);
246  f3(kappa[4],kappa[5],t_m[2][i],t_r[2][i]);
247  f1(kappa[3],kappa[4],t_m[3][i],t_r[3][i]);
248  f2(kappa[2],kappa[3],t_m[4][i],t_r[4][i]);
249  f3(kappa[1],kappa[2],t_m[5][i],t_r[5][i]);
250  f1(kappa[0],kappa[1],t_m[6][i],t_r[6][i]);
251  f2(kappa[7],kappa[0],t_m[7][i],t_r[7][i]);
252 }
253 
254 void CAST256::Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
255 {
256  AssertValidKeyLength(keylength);
257 
258  word32 kappa[8];
259  GetUserKey(BIG_ENDIAN_ORDER, kappa, 8, userKey, keylength);
260 
261  for(int i=0; i<12; ++i)
262  {
263  Omega(2*i,kappa);
264  Omega(2*i+1,kappa);
265 
266  K[8*i]=kappa[0] & 31;
267  K[8*i+1]=kappa[2] & 31;
268  K[8*i+2]=kappa[4] & 31;
269  K[8*i+3]=kappa[6] & 31;
270  K[8*i+4]=kappa[7];
271  K[8*i+5]=kappa[5];
272  K[8*i+6]=kappa[3];
273  K[8*i+7]=kappa[1];
274  }
275 
277  {
278  for(int j=0; j<6; ++j)
279  {
280  for(int i=0; i<4; ++i)
281  {
282  int i1=8*j+i;
283  int i2=8*(11-j)+i;
284 
285  CRYPTOPP_ASSERT(i1<i2);
286 
287  std::swap(K[i1],K[i2]);
288  std::swap(K[i1+4],K[i2+4]);
289  }
290  }
291  }
292 
293  SecureWipeBuffer(kappa, 8);
294 }
295 
void SecureWipeBuffer(T *buf, size_t n)
Sets each element of an array to 0.
Definition: misc.h:1085
#define f1(l, r, km, kr)
Definition: cast.cpp:17
void GetUserKey(ByteOrder order, T *out, size_t outlen, const byte *in, size_t inlen)
Definition: misc.h:1879
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
PutBlock< T, B, PA > Put
Definition: misc.h:2237
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
Definition: Interface.h:284
FixedSizeSecBlock< word32, 32 > K
Definition: cast.h:43
static void Omega(int i, word32 kappa[8])
Definition: cast.cpp:240
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define Q(i)
Definition: cast.cpp:199
static GetBlock< T, B, GA > Get(const void *block)
Definition: misc.h:2236
static const word32 t_m[8][24]
Definition: cast.h:88
#define f2(l, r, km, kr)
Definition: cast.cpp:21
#define F3(l, r, i, j)
Definition: cast.cpp:32
bool reduced
Definition: cast.h:42
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: cast.cpp:36
#define F2(l, r, i, j)
Definition: cast.cpp:31
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: cast.cpp:214
#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
Classes for the CAST-128 and CAST-256 block ciphers.
void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const
Encrypt or decrypt a block.
Definition: cast.cpp:66
void AssertValidKeyLength(size_t length) const
Validates the key length.
Definition: cryptlib.h:725
#define f3(l, r, km, kr)
Definition: cast.cpp:25
#define QBar(i)
Definition: cast.cpp:205
#define F1(l, r, i, j)
Definition: cast.cpp:30
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: cast.cpp:97
byte order is big-endian
Definition: cryptlib.h:128
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
#define X(name)
Definition: net.cpp:642
void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params)
Sets the key for this object without performing parameter validation.
Definition: cast.cpp:254
#define NAMESPACE_END
Definition: config.h:201
static const word32 S[8][256]
Definition: cast.h:19
#define z(i)
unsigned int word32
Definition: config.h:231
Interface for retrieving values given their names.
Definition: cryptlib.h:279
static const unsigned int t_r[8][24]
Definition: cast.h:89