Crypto++ 8.9 Free C++ class library of cryptographic schemes

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