Fabcoin Core  0.16.2
P2P Digital Currency
ttmac.cpp
Go to the documentation of this file.
1 // ttmac.cpp - written and placed in the public domain by Kevin Springle
2 
3 #include "pch.h"
4 #include "ttmac.h"
5 #include "misc.h"
6 
8 
9 void TTMAC_Base::UncheckedSetKey(const byte *userKey, unsigned int keylength, const NameValuePairs &)
10 {
11  AssertValidKeyLength(keylength);
12 
13  memcpy(m_key, userKey, KEYLENGTH);
14  CorrectEndianess(m_key, m_key, KEYLENGTH);
15 
16  Init();
17 }
18 
20 {
21  m_digest[0] = m_digest[5] = m_key[0];
22  m_digest[1] = m_digest[6] = m_key[1];
23  m_digest[2] = m_digest[7] = m_key[2];
24  m_digest[3] = m_digest[8] = m_key[3];
25  m_digest[4] = m_digest[9] = m_key[4];
26 }
27 
29 {
30  PadLastBlock(BlockSize() - 2*sizeof(HashWordType));
32 
35 
36  Transform(m_digest, m_data, true);
37 
38  word32 t2 = m_digest[2];
39  word32 t3 = m_digest[3];
40  if (size != DIGESTSIZE)
41  {
42  switch (size)
43  {
44  case 16:
45  m_digest[3] += m_digest[1] + m_digest[4];
46  // fall through
47  case 12:
48  m_digest[2] += m_digest[0] + t3;
49  // fall through
50  case 8:
51  m_digest[0] += m_digest[1] + t3;
52  m_digest[1] += m_digest[4] + t2;
53  break;
54 
55  case 4:
56  m_digest[0] +=
57  m_digest[1] +
58  m_digest[2] +
59  m_digest[3] +
60  m_digest[4];
61  break;
62 
63  case 0:
64  // Used by HashTransformation::Restart()
65  break;
66 
67  default:
68  throw InvalidArgument("TTMAC_Base: can't truncate a Two-Track-MAC 20 byte digest to " + IntToString(size) + " bytes");
69  break;
70  }
71  }
72 
74  memcpy(hash, m_digest, size);
75 
76  Restart(); // reinit for next use
77 }
78 
79 // RIPEMD-160 definitions used by Two-Track-MAC
80 
81 #define F(x, y, z) (x ^ y ^ z)
82 #define G(x, y, z) (z ^ (x & (y^z)))
83 #define H(x, y, z) (z ^ (x | ~y))
84 #define I(x, y, z) (y ^ (z & (x^y)))
85 #define J(x, y, z) (x ^ (y | ~z))
86 
87 #define k0 0
88 #define k1 0x5a827999UL
89 #define k2 0x6ed9eba1UL
90 #define k3 0x8f1bbcdcUL
91 #define k4 0xa953fd4eUL
92 #define k5 0x50a28be6UL
93 #define k6 0x5c4dd124UL
94 #define k7 0x6d703ef3UL
95 #define k8 0x7a6d76e9UL
96 #define k9 0
97 
98 void TTMAC_Base::Transform(word32 *digest, const word32 *X, bool last)
99 {
100 #define Subround(f, a, b, c, d, e, x, s, k) \
101  a += f(b, c, d) + x + k;\
102  a = rotlFixed((word32)a, s) + e;\
103  c = rotlFixed((word32)c, 10U)
104 
105  word32 a1, b1, c1, d1, e1, a2, b2, c2, d2, e2;
106  word32 *trackA, *trackB;
107 
108  if (!last)
109  {
110  trackA = digest;
111  trackB = digest+5;
112  }
113  else
114  {
115  trackB = digest;
116  trackA = digest+5;
117  }
118  a1 = trackA[0];
119  b1 = trackA[1];
120  c1 = trackA[2];
121  d1 = trackA[3];
122  e1 = trackA[4];
123  a2 = trackB[0];
124  b2 = trackB[1];
125  c2 = trackB[2];
126  d2 = trackB[3];
127  e2 = trackB[4];
128 
129  Subround(F, a1, b1, c1, d1, e1, X[ 0], 11, k0);
130  Subround(F, e1, a1, b1, c1, d1, X[ 1], 14, k0);
131  Subround(F, d1, e1, a1, b1, c1, X[ 2], 15, k0);
132  Subround(F, c1, d1, e1, a1, b1, X[ 3], 12, k0);
133  Subround(F, b1, c1, d1, e1, a1, X[ 4], 5, k0);
134  Subround(F, a1, b1, c1, d1, e1, X[ 5], 8, k0);
135  Subround(F, e1, a1, b1, c1, d1, X[ 6], 7, k0);
136  Subround(F, d1, e1, a1, b1, c1, X[ 7], 9, k0);
137  Subround(F, c1, d1, e1, a1, b1, X[ 8], 11, k0);
138  Subround(F, b1, c1, d1, e1, a1, X[ 9], 13, k0);
139  Subround(F, a1, b1, c1, d1, e1, X[10], 14, k0);
140  Subround(F, e1, a1, b1, c1, d1, X[11], 15, k0);
141  Subround(F, d1, e1, a1, b1, c1, X[12], 6, k0);
142  Subround(F, c1, d1, e1, a1, b1, X[13], 7, k0);
143  Subround(F, b1, c1, d1, e1, a1, X[14], 9, k0);
144  Subround(F, a1, b1, c1, d1, e1, X[15], 8, k0);
145 
146  Subround(G, e1, a1, b1, c1, d1, X[ 7], 7, k1);
147  Subround(G, d1, e1, a1, b1, c1, X[ 4], 6, k1);
148  Subround(G, c1, d1, e1, a1, b1, X[13], 8, k1);
149  Subround(G, b1, c1, d1, e1, a1, X[ 1], 13, k1);
150  Subround(G, a1, b1, c1, d1, e1, X[10], 11, k1);
151  Subround(G, e1, a1, b1, c1, d1, X[ 6], 9, k1);
152  Subround(G, d1, e1, a1, b1, c1, X[15], 7, k1);
153  Subround(G, c1, d1, e1, a1, b1, X[ 3], 15, k1);
154  Subround(G, b1, c1, d1, e1, a1, X[12], 7, k1);
155  Subround(G, a1, b1, c1, d1, e1, X[ 0], 12, k1);
156  Subround(G, e1, a1, b1, c1, d1, X[ 9], 15, k1);
157  Subround(G, d1, e1, a1, b1, c1, X[ 5], 9, k1);
158  Subround(G, c1, d1, e1, a1, b1, X[ 2], 11, k1);
159  Subround(G, b1, c1, d1, e1, a1, X[14], 7, k1);
160  Subround(G, a1, b1, c1, d1, e1, X[11], 13, k1);
161  Subround(G, e1, a1, b1, c1, d1, X[ 8], 12, k1);
162 
163  Subround(H, d1, e1, a1, b1, c1, X[ 3], 11, k2);
164  Subround(H, c1, d1, e1, a1, b1, X[10], 13, k2);
165  Subround(H, b1, c1, d1, e1, a1, X[14], 6, k2);
166  Subround(H, a1, b1, c1, d1, e1, X[ 4], 7, k2);
167  Subround(H, e1, a1, b1, c1, d1, X[ 9], 14, k2);
168  Subround(H, d1, e1, a1, b1, c1, X[15], 9, k2);
169  Subround(H, c1, d1, e1, a1, b1, X[ 8], 13, k2);
170  Subround(H, b1, c1, d1, e1, a1, X[ 1], 15, k2);
171  Subround(H, a1, b1, c1, d1, e1, X[ 2], 14, k2);
172  Subround(H, e1, a1, b1, c1, d1, X[ 7], 8, k2);
173  Subround(H, d1, e1, a1, b1, c1, X[ 0], 13, k2);
174  Subround(H, c1, d1, e1, a1, b1, X[ 6], 6, k2);
175  Subround(H, b1, c1, d1, e1, a1, X[13], 5, k2);
176  Subround(H, a1, b1, c1, d1, e1, X[11], 12, k2);
177  Subround(H, e1, a1, b1, c1, d1, X[ 5], 7, k2);
178  Subround(H, d1, e1, a1, b1, c1, X[12], 5, k2);
179 
180  Subround(I, c1, d1, e1, a1, b1, X[ 1], 11, k3);
181  Subround(I, b1, c1, d1, e1, a1, X[ 9], 12, k3);
182  Subround(I, a1, b1, c1, d1, e1, X[11], 14, k3);
183  Subround(I, e1, a1, b1, c1, d1, X[10], 15, k3);
184  Subround(I, d1, e1, a1, b1, c1, X[ 0], 14, k3);
185  Subround(I, c1, d1, e1, a1, b1, X[ 8], 15, k3);
186  Subround(I, b1, c1, d1, e1, a1, X[12], 9, k3);
187  Subround(I, a1, b1, c1, d1, e1, X[ 4], 8, k3);
188  Subround(I, e1, a1, b1, c1, d1, X[13], 9, k3);
189  Subround(I, d1, e1, a1, b1, c1, X[ 3], 14, k3);
190  Subround(I, c1, d1, e1, a1, b1, X[ 7], 5, k3);
191  Subround(I, b1, c1, d1, e1, a1, X[15], 6, k3);
192  Subround(I, a1, b1, c1, d1, e1, X[14], 8, k3);
193  Subround(I, e1, a1, b1, c1, d1, X[ 5], 6, k3);
194  Subround(I, d1, e1, a1, b1, c1, X[ 6], 5, k3);
195  Subround(I, c1, d1, e1, a1, b1, X[ 2], 12, k3);
196 
197  Subround(J, b1, c1, d1, e1, a1, X[ 4], 9, k4);
198  Subround(J, a1, b1, c1, d1, e1, X[ 0], 15, k4);
199  Subround(J, e1, a1, b1, c1, d1, X[ 5], 5, k4);
200  Subround(J, d1, e1, a1, b1, c1, X[ 9], 11, k4);
201  Subround(J, c1, d1, e1, a1, b1, X[ 7], 6, k4);
202  Subround(J, b1, c1, d1, e1, a1, X[12], 8, k4);
203  Subround(J, a1, b1, c1, d1, e1, X[ 2], 13, k4);
204  Subround(J, e1, a1, b1, c1, d1, X[10], 12, k4);
205  Subround(J, d1, e1, a1, b1, c1, X[14], 5, k4);
206  Subround(J, c1, d1, e1, a1, b1, X[ 1], 12, k4);
207  Subround(J, b1, c1, d1, e1, a1, X[ 3], 13, k4);
208  Subround(J, a1, b1, c1, d1, e1, X[ 8], 14, k4);
209  Subround(J, e1, a1, b1, c1, d1, X[11], 11, k4);
210  Subround(J, d1, e1, a1, b1, c1, X[ 6], 8, k4);
211  Subround(J, c1, d1, e1, a1, b1, X[15], 5, k4);
212  Subround(J, b1, c1, d1, e1, a1, X[13], 6, k4);
213 
214  Subround(J, a2, b2, c2, d2, e2, X[ 5], 8, k5);
215  Subround(J, e2, a2, b2, c2, d2, X[14], 9, k5);
216  Subround(J, d2, e2, a2, b2, c2, X[ 7], 9, k5);
217  Subround(J, c2, d2, e2, a2, b2, X[ 0], 11, k5);
218  Subround(J, b2, c2, d2, e2, a2, X[ 9], 13, k5);
219  Subround(J, a2, b2, c2, d2, e2, X[ 2], 15, k5);
220  Subround(J, e2, a2, b2, c2, d2, X[11], 15, k5);
221  Subround(J, d2, e2, a2, b2, c2, X[ 4], 5, k5);
222  Subround(J, c2, d2, e2, a2, b2, X[13], 7, k5);
223  Subround(J, b2, c2, d2, e2, a2, X[ 6], 7, k5);
224  Subround(J, a2, b2, c2, d2, e2, X[15], 8, k5);
225  Subround(J, e2, a2, b2, c2, d2, X[ 8], 11, k5);
226  Subround(J, d2, e2, a2, b2, c2, X[ 1], 14, k5);
227  Subround(J, c2, d2, e2, a2, b2, X[10], 14, k5);
228  Subround(J, b2, c2, d2, e2, a2, X[ 3], 12, k5);
229  Subround(J, a2, b2, c2, d2, e2, X[12], 6, k5);
230 
231  Subround(I, e2, a2, b2, c2, d2, X[ 6], 9, k6);
232  Subround(I, d2, e2, a2, b2, c2, X[11], 13, k6);
233  Subround(I, c2, d2, e2, a2, b2, X[ 3], 15, k6);
234  Subround(I, b2, c2, d2, e2, a2, X[ 7], 7, k6);
235  Subround(I, a2, b2, c2, d2, e2, X[ 0], 12, k6);
236  Subround(I, e2, a2, b2, c2, d2, X[13], 8, k6);
237  Subround(I, d2, e2, a2, b2, c2, X[ 5], 9, k6);
238  Subround(I, c2, d2, e2, a2, b2, X[10], 11, k6);
239  Subround(I, b2, c2, d2, e2, a2, X[14], 7, k6);
240  Subround(I, a2, b2, c2, d2, e2, X[15], 7, k6);
241  Subround(I, e2, a2, b2, c2, d2, X[ 8], 12, k6);
242  Subround(I, d2, e2, a2, b2, c2, X[12], 7, k6);
243  Subround(I, c2, d2, e2, a2, b2, X[ 4], 6, k6);
244  Subround(I, b2, c2, d2, e2, a2, X[ 9], 15, k6);
245  Subround(I, a2, b2, c2, d2, e2, X[ 1], 13, k6);
246  Subround(I, e2, a2, b2, c2, d2, X[ 2], 11, k6);
247 
248  Subround(H, d2, e2, a2, b2, c2, X[15], 9, k7);
249  Subround(H, c2, d2, e2, a2, b2, X[ 5], 7, k7);
250  Subround(H, b2, c2, d2, e2, a2, X[ 1], 15, k7);
251  Subround(H, a2, b2, c2, d2, e2, X[ 3], 11, k7);
252  Subround(H, e2, a2, b2, c2, d2, X[ 7], 8, k7);
253  Subround(H, d2, e2, a2, b2, c2, X[14], 6, k7);
254  Subround(H, c2, d2, e2, a2, b2, X[ 6], 6, k7);
255  Subround(H, b2, c2, d2, e2, a2, X[ 9], 14, k7);
256  Subround(H, a2, b2, c2, d2, e2, X[11], 12, k7);
257  Subround(H, e2, a2, b2, c2, d2, X[ 8], 13, k7);
258  Subround(H, d2, e2, a2, b2, c2, X[12], 5, k7);
259  Subround(H, c2, d2, e2, a2, b2, X[ 2], 14, k7);
260  Subround(H, b2, c2, d2, e2, a2, X[10], 13, k7);
261  Subround(H, a2, b2, c2, d2, e2, X[ 0], 13, k7);
262  Subround(H, e2, a2, b2, c2, d2, X[ 4], 7, k7);
263  Subround(H, d2, e2, a2, b2, c2, X[13], 5, k7);
264 
265  Subround(G, c2, d2, e2, a2, b2, X[ 8], 15, k8);
266  Subround(G, b2, c2, d2, e2, a2, X[ 6], 5, k8);
267  Subround(G, a2, b2, c2, d2, e2, X[ 4], 8, k8);
268  Subround(G, e2, a2, b2, c2, d2, X[ 1], 11, k8);
269  Subround(G, d2, e2, a2, b2, c2, X[ 3], 14, k8);
270  Subround(G, c2, d2, e2, a2, b2, X[11], 14, k8);
271  Subround(G, b2, c2, d2, e2, a2, X[15], 6, k8);
272  Subround(G, a2, b2, c2, d2, e2, X[ 0], 14, k8);
273  Subround(G, e2, a2, b2, c2, d2, X[ 5], 6, k8);
274  Subround(G, d2, e2, a2, b2, c2, X[12], 9, k8);
275  Subround(G, c2, d2, e2, a2, b2, X[ 2], 12, k8);
276  Subround(G, b2, c2, d2, e2, a2, X[13], 9, k8);
277  Subround(G, a2, b2, c2, d2, e2, X[ 9], 12, k8);
278  Subround(G, e2, a2, b2, c2, d2, X[ 7], 5, k8);
279  Subround(G, d2, e2, a2, b2, c2, X[10], 15, k8);
280  Subround(G, c2, d2, e2, a2, b2, X[14], 8, k8);
281 
282  Subround(F, b2, c2, d2, e2, a2, X[12], 8, k9);
283  Subround(F, a2, b2, c2, d2, e2, X[15], 5, k9);
284  Subround(F, e2, a2, b2, c2, d2, X[10], 12, k9);
285  Subround(F, d2, e2, a2, b2, c2, X[ 4], 9, k9);
286  Subround(F, c2, d2, e2, a2, b2, X[ 1], 12, k9);
287  Subround(F, b2, c2, d2, e2, a2, X[ 5], 5, k9);
288  Subround(F, a2, b2, c2, d2, e2, X[ 8], 14, k9);
289  Subround(F, e2, a2, b2, c2, d2, X[ 7], 6, k9);
290  Subround(F, d2, e2, a2, b2, c2, X[ 6], 8, k9);
291  Subround(F, c2, d2, e2, a2, b2, X[ 2], 13, k9);
292  Subround(F, b2, c2, d2, e2, a2, X[13], 6, k9);
293  Subround(F, a2, b2, c2, d2, e2, X[14], 5, k9);
294  Subround(F, e2, a2, b2, c2, d2, X[ 0], 15, k9);
295  Subround(F, d2, e2, a2, b2, c2, X[ 3], 13, k9);
296  Subround(F, c2, d2, e2, a2, b2, X[ 9], 11, k9);
297  Subround(F, b2, c2, d2, e2, a2, X[11], 11, k9);
298 
299  a1 -= trackA[0];
300  b1 -= trackA[1];
301  c1 -= trackA[2];
302  d1 -= trackA[3];
303  e1 -= trackA[4];
304  a2 -= trackB[0];
305  b2 -= trackB[1];
306  c2 -= trackB[2];
307  d2 -= trackB[3];
308  e2 -= trackB[4];
309 
310  if (!last)
311  {
312  trackA[0] = (b1 + e1) - d2;
313  trackA[1] = c1 - e2;
314  trackA[2] = d1 - a2;
315  trackA[3] = e1 - b2;
316  trackA[4] = a1 - c2;
317  trackB[0] = d1 - e2;
318  trackB[1] = (e1 + c1) - a2;
319  trackB[2] = a1 - b2;
320  trackB[3] = b1 - c2;
321  trackB[4] = c1 - d2;
322  }
323  else
324  {
325  trackB[0] = a2 - a1;
326  trackB[1] = b2 - b1;
327  trackB[2] = c2 - c1;
328  trackB[3] = d2 - d1;
329  trackB[4] = e2 - e1;
330  trackA[0] = 0;
331  trackA[1] = 0;
332  trackA[2] = 0;
333  trackA[3] = 0;
334  trackA[4] = 0;
335  }
336 }
337 
An invalid argument was detected.
Definition: cryptlib.h:184
void Init()
Definition: ttmac.cpp:19
#define k6
Definition: ttmac.cpp:93
#define k1
Definition: ttmac.cpp:88
#define H(x, y, z)
Definition: ttmac.cpp:83
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
TTMAC message authentication code information.
Definition: ttmac.h:17
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define k2
Definition: ttmac.cpp:89
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:524
#define k7
Definition: ttmac.cpp:94
#define G(x, y, z)
Definition: ttmac.cpp:82
#define k3
Definition: ttmac.cpp:90
#define k5
Definition: ttmac.cpp:92
#define a1
#define Subround(f, a, b, c, d, e, x, s, k)
#define I(x, y, z)
Definition: ttmac.cpp:84
FixedSizeSecBlock< word32, 10 > m_digest
Definition: ttmac.h:33
#define t3
#define k0
Definition: ttmac.cpp:87
void CorrectEndianess(HashWordType *out, const HashWordType *in, size_t byteCount)
Adjusts the byte ordering of the hash.
Definition: iterhash.h:129
void TruncatedFinal(byte *mac, size_t size)
Computes the hash of the current message.
Definition: ttmac.cpp:28
FixedSizeSecBlock< word32, T_BlockSize/sizeof(word32)> m_data
Definition: iterhash.h:136
#define J(x, y, z)
Definition: ttmac.cpp:85
#define t2
#define X(name)
Definition: net.cpp:642
void PadLastBlock(unsigned int lastBlockSize, byte padFirst=0x80)
#define F(x, y, z)
Definition: ttmac.cpp:81
uint8_t const size_t const size
Definition: sha3.h:20
void * memcpy(void *a, const void *b, size_t c)
unsigned int BlockSize() const
Provides the block size of the hash.
Definition: iterhash.h:116
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:539
#define NAMESPACE_END
Definition: config.h:201
#define k9
Definition: ttmac.cpp:96
FixedSizeSecBlock< word32, 5 > m_key
Definition: ttmac.h:34
static void Transform(word32 *digest, const word32 *X, bool last)
Definition: ttmac.cpp:98
#define a2
unsigned int word32
Definition: config.h:231
Classes for the TTMAC message authentication code.
#define k4
Definition: ttmac.cpp:91
#define k8
Definition: ttmac.cpp:95
Interface for retrieving values given their names.
Definition: cryptlib.h:279