File: skein_provider.hpp

package info (click to toggle)
salmon 1.10.2%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 35,580 kB
  • sloc: cpp: 199,285; ansic: 171,082; sh: 859; python: 792; makefile: 235
file content (392 lines) | stat: -rw-r--r-- 11,017 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
This code is written by kerukuro and released into public domain.
*/

#ifndef DIGESTPP_PROVIDERS_SKEIN_HPP
#define DIGESTPP_PROVIDERS_SKEIN_HPP

#include "../../detail/functions.hpp"
#include "../../detail/absorb_data.hpp"
#include "../../detail/validate_hash_size.hpp"
#include "constants/skein_constants.hpp"
#include <array>

namespace digestpp
{

namespace detail
{

template<size_t N>
struct skein_functions
{
};

template<>
struct skein_functions<4>
{
	template<size_t r> static inline void GTv4(uint64_t* G)
	{
		G[skein_constants<void>::I4[r][0]] += G[skein_constants<void>::I4[r][1]];
		G[skein_constants<void>::I4[r][1]] = rotate_left(G[skein_constants<void>::I4[r][1]], 
				skein_constants<void>::C4[r][0]) ^ G[skein_constants<void>::I4[r][0]];
		G[skein_constants<void>::I4[r][2]] += G[skein_constants<void>::I4[r][3]];
		G[skein_constants<void>::I4[r][3]] = rotate_left(G[skein_constants<void>::I4[r][3]], 
				skein_constants<void>::C4[r][1]) ^ G[skein_constants<void>::I4[r][2]];
	}

	template<size_t r>
	static inline void KStRv4(uint64_t* G, uint64_t* keys, uint64_t* tweaks)
	{
		G[0] += keys[(r + 0 + 1) % 5];
		G[1] += keys[(r + 1 + 1) % 5] + tweaks[(r + 1) % 3];
		G[2] += keys[(r + 2 + 1) % 5] + tweaks[(r + 2) % 3];
		G[3] += keys[(r + 3 + 1) % 5] + r + 1;
	}

	template<size_t r>
	static inline void G8(uint64_t* G, uint64_t* keys, uint64_t* tweaks)
	{
		GTv4<0>(G);
		GTv4<1>(G);
		GTv4<2>(G);
		GTv4<3>(G);
		KStRv4<r>(G, keys, tweaks);
		GTv4<4>(G);
		GTv4<5>(G);
		GTv4<6>(G);
		GTv4<7>(G);
		KStRv4<r + 1>(G, keys, tweaks);
	}
};

template<>
struct skein_functions<8>
{
	template<size_t r> static inline void GTv4(uint64_t* G)
	{
		G[skein_constants<void>::I8[r][0]] += G[skein_constants<void>::I8[r][1]];
		G[skein_constants<void>::I8[r][1]] = rotate_left(G[skein_constants<void>::I8[r][1]], 
				skein_constants<void>::C8[r][0]) ^ G[skein_constants<void>::I8[r][0]];
		G[skein_constants<void>::I8[r][2]] += G[skein_constants<void>::I8[r][3]];
		G[skein_constants<void>::I8[r][3]] = rotate_left(G[skein_constants<void>::I8[r][3]], 
				skein_constants<void>::C8[r][1]) ^ G[skein_constants<void>::I8[r][2]];
		G[skein_constants<void>::I8[r][4]] += G[skein_constants<void>::I8[r][5]];
		G[skein_constants<void>::I8[r][5]] = rotate_left(G[skein_constants<void>::I8[r][5]],
				skein_constants<void>::C8[r][2]) ^ G[skein_constants<void>::I8[r][4]];
		G[skein_constants<void>::I8[r][6]] += G[skein_constants<void>::I8[r][7]];
		G[skein_constants<void>::I8[r][7]] = rotate_left(G[skein_constants<void>::I8[r][7]],
				skein_constants<void>::C8[r][3]) ^ G[skein_constants<void>::I8[r][6]];
	}

	template<size_t r>
	static inline void KStRv4(uint64_t* G, uint64_t* keys, uint64_t* tweaks)
	{
		G[0] += keys[(r + 0 + 1) % 9];
		G[1] += keys[(r + 1 + 1) % 9];
		G[2] += keys[(r + 2 + 1) % 9];
		G[3] += keys[(r + 3 + 1) % 9];
		G[4] += keys[(r + 4 + 1) % 9];
		G[5] += keys[(r + 5 + 1) % 9] + tweaks[(r + 1) % 3];
		G[6] += keys[(r + 6 + 1) % 9] + tweaks[(r + 2) % 3];
		G[7] += keys[(r + 7 + 1) % 9] + r + 1;
	}

	template<size_t r>
	static inline void G8(uint64_t* G, uint64_t* keys, uint64_t* tweaks)
	{
		GTv4<0>(G);
		GTv4<1>(G);
		GTv4<2>(G);
		GTv4<3>(G);
		KStRv4<r>(G, keys, tweaks);
		GTv4<4>(G);
		GTv4<5>(G);
		GTv4<6>(G);
		GTv4<7>(G);
		KStRv4<r + 1>(G, keys, tweaks);
	}
};

template<>
struct skein_functions<16>
{

	template<size_t r> static inline void GTv4(uint64_t* G)
	{
		G[skein_constants<void>::I16[r][0]] += G[skein_constants<void>::I16[r][1]];
		G[skein_constants<void>::I16[r][1]] = rotate_left(G[skein_constants<void>::I16[r][1]], 
				skein_constants<void>::C16[r][0]) ^ G[skein_constants<void>::I16[r][0]];
		G[skein_constants<void>::I16[r][2]] += G[skein_constants<void>::I16[r][3]];
		G[skein_constants<void>::I16[r][3]] = rotate_left(G[skein_constants<void>::I16[r][3]], 
				skein_constants<void>::C16[r][1]) ^ G[skein_constants<void>::I16[r][2]];
		G[skein_constants<void>::I16[r][4]] += G[skein_constants<void>::I16[r][5]];
		G[skein_constants<void>::I16[r][5]] = rotate_left(G[skein_constants<void>::I16[r][5]],
				skein_constants<void>::C16[r][2]) ^ G[skein_constants<void>::I16[r][4]];
		G[skein_constants<void>::I16[r][6]] += G[skein_constants<void>::I16[r][7]];
		G[skein_constants<void>::I16[r][7]] = rotate_left(G[skein_constants<void>::I16[r][7]],
				skein_constants<void>::C16[r][3]) ^ G[skein_constants<void>::I16[r][6]];
		G[skein_constants<void>::I16[r][8]] += G[skein_constants<void>::I16[r][9]];
		G[skein_constants<void>::I16[r][9]] = rotate_left(G[skein_constants<void>::I16[r][9]],
				skein_constants<void>::C16[r][4]) ^ G[skein_constants<void>::I16[r][8]];
		G[skein_constants<void>::I16[r][10]] += G[skein_constants<void>::I16[r][11]];
		G[skein_constants<void>::I16[r][11]] = rotate_left(G[skein_constants<void>::I16[r][11]],
				skein_constants<void>::C16[r][5]) ^ G[skein_constants<void>::I16[r][10]];
		G[skein_constants<void>::I16[r][12]] += G[skein_constants<void>::I16[r][13]];
		G[skein_constants<void>::I16[r][13]] = rotate_left(G[skein_constants<void>::I16[r][13]],
				skein_constants<void>::C16[r][6]) ^ G[skein_constants<void>::I16[r][12]];
		G[skein_constants<void>::I16[r][14]] += G[skein_constants<void>::I16[r][15]];
		G[skein_constants<void>::I16[r][15]] = rotate_left(G[skein_constants<void>::I16[r][15]],
				skein_constants<void>::C16[r][7]) ^ G[skein_constants<void>::I16[r][14]];
	}

	template<size_t r>
	static inline void KStRv4(uint64_t* G, uint64_t* keys, uint64_t* tweaks)
	{
		G[0] += keys[(r + 0 + 1) % 17];
		G[1] += keys[(r + 1 + 1) % 17];
		G[2] += keys[(r + 2 + 1) % 17];
		G[3] += keys[(r + 3 + 1) % 17];
		G[4] += keys[(r + 4 + 1) % 17];
		G[5] += keys[(r + 5 + 1) % 17];
		G[6] += keys[(r + 6 + 1) % 17];
		G[7] += keys[(r + 7 + 1) % 17];
		G[8] += keys[(r + 8 + 1) % 17];
		G[9] += keys[(r + 9 + 1) % 17];
		G[10] += keys[(r + 10 + 1) % 17];
		G[11] += keys[(r + 11 + 1) % 17];
		G[12] += keys[(r + 12 + 1) % 17];
		G[13] += keys[(r + 13 + 1) % 17] + tweaks[(r + 1) % 3];
		G[14] += keys[(r + 14 + 1) % 17] + tweaks[(r + 2) % 3];
		G[15] += keys[(r + 15 + 1) % 17] + r + 1;
	}

	template<size_t r>
	static inline void G8(uint64_t* G, uint64_t* keys, uint64_t* tweaks)
	{
		GTv4<0>(G);
		GTv4<1>(G);
		GTv4<2>(G);
		GTv4<3>(G);
		KStRv4<r>(G, keys, tweaks);
		GTv4<4>(G);
		GTv4<5>(G);
		GTv4<6>(G);
		GTv4<7>(G);
		KStRv4<r + 1>(G, keys, tweaks);
	}
};

template<size_t N, bool XOF>
class skein_provider
{
public:
	static const bool is_xof = XOF;

	skein_provider(size_t hashsize = N)
		: hs(hashsize)
	{
		validate_hash_size(hashsize, SIZE_MAX);
	}

	~skein_provider()
	{
		clear();
	}

	inline void set_personalization(const std::string& personalization)
	{
		p = personalization;
	}

	inline void set_nonce(const std::string& nonce)
	{
		n = nonce;
	}

	inline void set_key(const std::string& key)
	{
		k = key;
	}

	inline void init()
	{
		squeezing = false;
		tweak[0] = 0ULL;
		pos = 0;
		total = 0;
		zero_memory(H);
		inject_parameter(k, 0ULL);
		tweak[1] = (1ULL << 62) | (4ULL << 56) | (1ULL << 63);
		zero_memory(m);
		m[0] = 0x53;
		m[1] = 0x48;
		m[2] = 0x41;
		m[3] = 0x33;
		m[4] = 0x01;
		uint64_t size64 = XOF ? static_cast<uint64_t>(-1) : hs;
		memcpy(&m[8], &size64, 8);
		transform(m.data(), 1, 32);
		pos = 0;
		total = 0;
		tweak[0] = 0ULL;
		tweak[1] = (1ULL << 62) | (48ULL << 56);
		inject_parameter(p, 8ULL);
		inject_parameter(n, 20ULL);

	}

	inline void update(const unsigned char* data, size_t len)
	{
		detail::absorb_bytes(data, len, N / 8, N / 8 + 1, m.data(), pos, total,
			[this](const unsigned char* data, size_t len) { transform(data, len, N / 8); });
	}

	inline void squeeze(unsigned char* hash, size_t hs)
	{
		size_t processed = 0;
		if (!squeezing)
		{
			squeezing = true;
			tweak[1] |= 1ull << 63; // last block
			if (pos < N / 8)
				memset(&m[pos], 0, N / 8 - pos);

			transform(m.data(), 1, pos);
			memset(&m[0], 0, N / 8);
			memcpy(&hbk[0], H.data(), N / 8);
			pos = 0;
			total = 0;
		}
		else if (pos < N / 8)
		{
			size_t to_copy = std::min(hs, N / 8 - pos);
			memcpy(hash, reinterpret_cast<unsigned char*>(H.data()) + pos, to_copy);
			processed += to_copy;
			pos += to_copy;
		}
		while (processed < hs)
		{
			pos = std::min(hs - processed, N / 8);
			tweak[0] = 0;
			tweak[1] = 255ULL << 56;
			memcpy(&m[0], &total, 8);
			memcpy(&H[0], hbk.data(), N / 8);
			transform(m.data(), 1, 8);
			memcpy(hash + processed, H.data(), pos);
			processed += pos;
			++total;
		}
	}

	inline void final(unsigned char* hash)
	{
		return squeeze(hash, hs / 8);
	}

	inline size_t hash_size() const 
	{ 
		return hs; 
	}

	inline void clear()
	{
		zero_memory(H);
		zero_memory(hbk);
		zero_memory(m);
		zero_memory(tweak);
		zero_memory(p);
		zero_memory(n);
		zero_memory(k);
		p.clear();
		n.clear();
		k.clear();
	}

private:
	inline void transform(const unsigned char* mp, uint64_t num_blks, size_t reallen)
	{
		uint64_t keys[N / 64 + 1];
		uint64_t tweaks[3];

		for (uint64_t b = 0; b < num_blks; b++)
		{
			uint64_t M[N / 64];
			uint64_t G[N / 64];
			for (uint64_t i = 0; i < N / 64; i++)
			{
				M[i] = (reinterpret_cast<const uint64_t*>(mp)[b * N / 64 + i]);
			}
			memcpy(keys, H.data(), sizeof(uint64_t) * N / 64);
			memcpy(tweaks, tweak.data(), sizeof(uint64_t) * 2);
			tweaks[0] += reallen;
			tweaks[2] = tweaks[0] ^ tweaks[1];
			keys[N / 64] = 0x1BD11BDAA9FC1A22ULL;
			for (int i = 0; i < N / 64; i++)
			{
				keys[N / 64] ^= keys[i];
				G[i] = M[i] + keys[i];
			}
			G[N / 64 - 3] += tweaks[0];
			G[N / 64 - 2] += tweaks[1];

			skein_functions<N / 64>::template G8<0>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<2>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<4>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<6>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<8>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<10>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<12>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<14>(G, keys, tweaks);
			skein_functions<N / 64>::template G8<16>(G, keys, tweaks);
			if (N == 1024)
				skein_functions<N / 64>::template G8<18>(G, keys, tweaks);

			tweaks[1] &= ~(64ULL << 56);
			tweak[0] = tweaks[0];
			tweak[1] = tweaks[1];

			for (int i = 0; i < N / 64; i++)
				H[i] = G[i] ^ M[i];
		}
	}

	inline void inject_parameter(const std::string& p, uint64_t code)
	{
		if (p.empty())
			return;

		tweak[1] = (1ULL << 62) | (code << 56);
		update(reinterpret_cast<const unsigned char*>(p.data()), p.length());
		tweak[1] |= 1ull << 63; // last block
		if (pos < N / 8)
			memset(&m[pos], 0, N / 8 - pos);
		transform(m.data(), 1, pos);
		squeezing = false;
		pos = 0;
		total = 0;
		tweak[0] = 0ULL;
		tweak[1] = (1ULL << 62) | (48ULL << 56);
	}

	std::array<uint64_t, N / 64> H, hbk;
	std::array<unsigned char, N / 8> m;
	size_t pos;
	uint64_t total;
	std::array<uint64_t, 2> tweak;
	size_t hs;
	bool squeezing;
	std::string p, n, k;
};


} // namespace detail

} // namespace digestpp

#endif