File: jh_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 (215 lines) | stat: -rw-r--r-- 4,746 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
/*
This code is written by kerukuro and released into public domain.
*/

#ifndef DIGESTPP_PROVIDERS_JH_HPP
#define DIGESTPP_PROVIDERS_JH_HPP

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

namespace digestpp
{

namespace detail
{

namespace jh_functions
{
	static inline void bitswap(uint64_t& x, uint64_t mask, int shift)
	{
		x = ((x & mask) << shift) | ((x & ~mask) >> shift);
	}

	static inline void bitswap(std::array<uint64_t, 16>& H, uint64_t mask, int shift)
	{
		bitswap(H[2], mask, shift);
		bitswap(H[3], mask, shift);
		bitswap(H[6], mask, shift);
		bitswap(H[7], mask, shift);
		bitswap(H[10], mask, shift);
		bitswap(H[11], mask, shift);
		bitswap(H[14], mask, shift);
		bitswap(H[15], mask, shift);
	}

	static inline void Sbitsli(uint64_t& x0a, uint64_t& x0b, uint64_t& x1a, uint64_t& x1b, uint64_t& x2a, uint64_t& x2b, 
			uint64_t& x3a, uint64_t& x3b, uint64_t ca, uint64_t cb)
	{
		x3a = ~x3a;
		x3b = ~x3b;
		x0a ^= (ca & ~x2a);
		x0b ^= (cb & ~x2b);
		uint64_t ta = ca ^ (x0a & x1a);
		uint64_t tb = cb ^ (x0b & x1b);
		x0a ^= (x2a & x3a);
		x0b ^= (x2b & x3b);
		x3a ^= (~x1a & x2a);
		x3b ^= (~x1b & x2b);
		x1a ^= (x0a & x2a);
		x1b ^= (x0b & x2b);
		x2a ^= (x0a & ~x3a);
		x2b ^= (x0b & ~x3b);
		x0a ^= (x1a | x3a);
		x0b ^= (x1b | x3b);
		x3a ^= (x1a & x2a);
		x3b ^= (x1b & x2b);
		x1a ^= (ta & x0a);
		x1b ^= (tb & x0b);
		x2a ^= ta;
		x2b ^= tb;
	}

	static inline void Lbitsli(std::array<uint64_t, 16>& H)
	{
		H[2] ^= H[4];
		H[3] ^= H[5];
		H[6] ^= H[8];
		H[7] ^= H[9];
		H[10] ^= H[12] ^ H[0];
		H[11] ^= H[13] ^ H[1];
		H[14] ^= H[0];
		H[15] ^= H[1];
		H[0] ^= H[6];
		H[1] ^= H[7];
		H[4] ^= H[10];
		H[5] ^= H[11];
		H[8] ^= H[14] ^ H[2];
		H[9] ^= H[15] ^ H[3];
		H[12] ^= H[2];
		H[13] ^= H[3];
	}

	static inline void round(std::array<uint64_t, 16>& H, int r, uint64_t mask, int shift)
	{
		jh_functions::Sbitsli(H[0], H[1], H[4], H[5], H[8], H[9], H[12], H[13], 
				jh_constants<void>::C[r * 4 + 0], jh_constants<void>::C[r * 4 + 1]);
		jh_functions::Sbitsli(H[2], H[3], H[6], H[7], H[10], H[11], H[14], H[15], 
				jh_constants<void>::C[r * 4 + 2], jh_constants<void>::C[r * 4 + 3]);
		jh_functions::Lbitsli(H);
		if (shift)
			jh_functions::bitswap(H, mask, shift);
	}
}

class jh_provider
{
public:
	static const bool is_xof = false;

	jh_provider(size_t hashsize = 512)
		: hs(hashsize)
	{
		validate_hash_size(hashsize, 512);
	}

	~jh_provider()
	{
		clear();
	}

	inline void init()
	{
		pos = 0;
		total = 0;

		zero_memory(H);
		H[0] = byteswap(static_cast<uint16_t>(hs));

		unsigned char msg[64];
		memset(msg, 0, sizeof(msg));
		transform(msg, 1);
	}

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

	inline void final(unsigned char* hash)
	{
		total += pos * 8;
		m[pos++] = 0x80;
		if (pos > 1)
		{
			if (pos != 64)
				memset(&m[pos], 0, 64 - pos);
			transform(&m[0], 1);
			pos = 0;
		}
		memset(&m[0] + pos, 0, 56 - pos);
		uint64_t mlen = byteswap(total);
		memcpy(&m[0] + (64 - 8), &mlen, 64 / 8);
		transform(&m[0], 1);

		memcpy(hash, reinterpret_cast<unsigned char*>(H.data()) + 128 - hash_size() / 8, hash_size() / 8);
	}

	inline void clear()
	{
		zero_memory(H);
		zero_memory(m);
	}

	inline size_t hash_size() const { return hs; }

private:
	inline void transform(const unsigned char* mp, size_t num_blks)
	{
		for (uint64_t blk = 0; blk < num_blks; blk++)
		{
			const uint64_t* M = (const uint64_t*)(((const unsigned char*)mp) + blk * 64);
			H[0] ^= M[0];
			H[1] ^= M[1];
			H[2] ^= M[2];
			H[3] ^= M[3];
			H[4] ^= M[4];
			H[5] ^= M[5];
			H[6] ^= M[6];
			H[7] ^= M[7];

			// partially unroll
			for (int r = 0; r < 42; r += 7)
			{
				jh_functions::round(H, r, 0x5555555555555555ULL, 1);
				jh_functions::round(H, r + 1, 0x3333333333333333ULL, 2);
				jh_functions::round(H, r + 2, 0x0f0f0f0f0f0f0f0fULL, 4);
				jh_functions::round(H, r + 3, 0x00ff00ff00ff00ffULL, 8);
				jh_functions::round(H, r + 4, 0x0000ffff0000ffffULL, 16);
				jh_functions::round(H, r + 5, 0x00000000ffffffffULL, 32);
				jh_functions::round(H, r + 6, 0, 0);

				std::swap(H[2], H[3]);
				std::swap(H[6], H[7]);
				std::swap(H[10], H[11]);
				std::swap(H[14], H[15]);
			}

			H[8] ^= M[0];
			H[9] ^= M[1];
			H[10] ^= M[2];
			H[11] ^= M[3];
			H[12] ^= M[4];
			H[13] ^= M[5];
			H[14] ^= M[6];
			H[15] ^= M[7];
		}
	}

	std::array<uint64_t, 16> H;
	std::array<unsigned char, 64> m;
	size_t hs;
	size_t pos;
	uint64_t total;
};

} // namespace detail

} // namespace digestpp

#endif