File: shake_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 (181 lines) | stat: -rw-r--r-- 3,729 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
/*
This code is written by kerukuro and released into public domain.
*/

#ifndef DIGESTPP_PROVIDERS_SHAKE_HPP
#define DIGESTPP_PROVIDERS_SHAKE_HPP

#include "../../detail/functions.hpp"
#include "../../detail/absorb_data.hpp"
#include "sha3_provider.hpp"
#include <array>

namespace digestpp
{

namespace detail
{

namespace shake_functions
{
	static inline size_t left_encode(size_t num, unsigned char* buf)
	{
		// first, calculate length
		unsigned char n = 1;
		size_t tmp = num;
		while (tmp >>= 8)
			++n;
		buf[0] = n;
		size_t result = n + 1;
		size_t i = 0;
		while (n)
			buf[n--] = static_cast<unsigned char>(num >> (8*i++));
		return result;
	}

	static inline size_t right_encode(size_t num, unsigned char* buf, bool k12)
	{
		// first, calculate length
		unsigned char n = k12 ? num ? 1 : 0 : 1;
		size_t tmp = num;
		while (tmp >>= 8)
			++n;
		buf[n] = n;
		size_t result = n + 1;
		size_t i = 0;
		while (n--)
			buf[n] = static_cast<unsigned char>(num >> (8*i++));
		return result;
	}

} // namespace shake_functions


template<size_t B, int R>
class shake_provider
{
public:
	static const bool is_xof = true;

	shake_provider()
	{
		static_assert(B == 128 || B == 256, "SHAKE only supports 128 and 256 bits");
	}

	~shake_provider()
	{
		clear();
	}

	inline void set_function_name(const std::string& function_name)
	{
		N = function_name;
	}

	inline void set_customization(const std::string& customization)
	{
		S = customization;
	}

	inline void init()
	{
		zero_memory(A);
		pos = 0;
		squeezing = false;
		suffix = 0;

		if (!N.empty() || !S.empty())
		{
			unsigned char buf[1024];
			size_t r = rate / 8;
			size_t len = shake_functions::left_encode(r, buf);
			size_t total = len;
			update(buf, len);
			len = shake_functions::left_encode(N.length() * 8, buf);
			total += len;
			update(buf, len);
			if (!N.empty())
				update(reinterpret_cast<const unsigned char*>(N.data()), N.length());
			len = shake_functions::left_encode(S.length() * 8, buf);
			update(buf, len);
			total += len;
			if (!S.empty())
				update(reinterpret_cast<const unsigned char*>(S.data()), S.length());
			total += S.length() + N.length();

			len = r - (total % r);
			memset(buf, 0, len);
			update(buf, len);
		}
	}

	inline void update(const unsigned char* data, size_t len)
	{
		detail::absorb_bytes(data, len, rate / 8, rate / 8, m.data(), pos, total, 
			[this](const unsigned char* data, size_t len) { sha3_functions::transform<R>(data, len, A.data(), rate); });
	}

	inline void set_suffix(unsigned char s)
	{
		suffix = s;
	}

	inline void squeeze(unsigned char* hash, size_t hs)
	{
		size_t r = rate / 8;
		size_t processed = 0;
		if (!squeezing)
		{
			m[pos++] = suffix ? suffix : N.empty() && S.empty() ? 0x1F : 0x04;
			if (r != pos)
				memset(&m[pos], 0, r - pos);
			m[r - 1] |= 0x80;
			sha3_functions::transform<R>(m.data(), 1, A.data(), rate);
			squeezing = true;
		}
		else if (pos < r)
		{
			size_t to_copy = std::min(hs, r - pos);
			memcpy(hash, reinterpret_cast<unsigned char*>(A.data()) + pos, to_copy);
			processed += to_copy;
			pos += to_copy;
		}
		while (processed < hs)
		{
			if (processed)
				sha3_functions::transform<R>(A.data());
			pos = std::min(hs - processed, r);
			memcpy(hash + processed, A.data(), pos);
			processed += pos;
		}
	}

	inline void clear()
	{
		zero_memory(A);
		zero_memory(m);
		zero_memory(N);
		zero_memory(S);
		N.clear();
		S.clear();
	}

private:
	std::array<uint64_t, 25> A;
	std::array<unsigned char, 168> m;
	std::string N;
	std::string S;
	const size_t rate = B == 128 ? 1344 : 1088;
	size_t pos;
	size_t total;
	bool squeezing;
	unsigned char suffix;
};

} // namespace detail

} // namespace digestpp

#endif // DIGESTPP_PROVIDERS_SHAKE_HPP