forked from jancarlsson/snarkfront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecureHashStd.hpp
218 lines (182 loc) · 6.23 KB
/
SecureHashStd.hpp
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
#ifndef _SNARKFRONT_SECURE_HASH_STD_HPP_
#define _SNARKFRONT_SECURE_HASH_STD_HPP_
#include <cassert>
#include <cstdint>
#include <ostream>
#include <string>
#include "DataBuffer.hpp"
namespace snarkfront {
////////////////////////////////////////////////////////////////////////////////
// FIPS PUB 180-4, NIST March 2012
//
// Algorithm Message Size Block Size Word Size Message Digest
// (bits) (bits) (bits) Size (bits)
// SHA-1 < 2^64 512 32 160
// SHA-224 < 2^64 512 32 224
// SHA-256 < 2^64 512 32 256
// SHA-384 < 2^128 1024 64 384
// SHA-512 < 2^128 1024 64 512
// SHA-512/224 < 2^128 1024 64 224
// SHA-512/256 < 2^128 1024 64 256
//
enum class SHA_BlockSize {
BLOCK_512, // for: SHA-1, SHA-224, SHA-256
BLOCK_1024 // for: SHA-384, SHA-512, SHA-512/224, SHA-512/256
};
template <typename CRTP, SHA_BlockSize BLK, typename MSG>
class SHA_Base
{
public:
virtual ~SHA_Base() = default;
static void padMessage(DataBuffer<ClearText>& buf) {
const std::size_t msgLengthBits = buf->sizeBits();
buf.push8(0x80); // append bit "1" to end of the message
// keep padding zero bits to the length block at the end
const size_t stopPadBits = blockSizeBits() - 2 * wordSizeBits();
while (stopPadBits != buf->sizeBits() % blockSizeBits()) {
buf.push8(0x00);
}
// append length of message
// (message size is limited to < 2^64 bits in this implementation)
if (SHA_BlockSize::BLOCK_1024 == BLK) {
buf.push64(0);
}
buf.push64(msgLengthBits);
}
static void padMessage(DataBufferStream& buf) {
padMessage(*buf);
}
static bool padNeeded(const DataBuffer<ClearText>& buf) {
// non-empty message must be even number of message input blocks
return buf->empty() || 0 != buf->sizeBits() % blockSizeBits();
}
static bool padNeeded(DataBufferStream& buf) {
return padNeeded(*buf);
}
// append (possibly lazy) word to message
template <typename T>
void msgInput(const T& a) {
m_message.emplace_back(a);
}
// append array of words to message
template <typename T, std::size_t N>
void msgInput(const std::array<T, N>& a) {
for (const auto& b : a)
msgInput(b);
}
void clearMessage() {
m_message.clear();
}
void computeHash() {
#ifdef USE_ASSERT
assert(inputOK());
#endif
auto* ptr = static_cast<CRTP*>(this);
ptr->initHashValue();
std::size_t msgIndex = 0;
while (msgIndex < m_message.size()) {
ptr->prepMsgSchedule(msgIndex);
ptr->initWorkingVars();
ptr->workingLoop();
ptr->updateHash();
}
ptr->afterHash();
}
protected:
SHA_Base() = default;
// note: reference not const so assignment can unbox laziness
MSG& msgWord(std::size_t& index) {
return m_message[index++];
}
private:
static std::size_t blockSizeBits() {
switch (BLK) {
case (SHA_BlockSize::BLOCK_512) : return 512;
case (SHA_BlockSize::BLOCK_1024) : return 1024;
}
}
static std::size_t wordSizeBits() {
switch (BLK) {
case (SHA_BlockSize::BLOCK_512) : return 32;
case (SHA_BlockSize::BLOCK_1024) : return 64;
}
}
bool inputOK() const {
// non-empty message must be even number of message input blocks
const std::size_t msgSizeBits = m_message.size() * wordSizeBits();
return !m_message.empty() && 0 == msgSizeBits % blockSizeBits();
}
std::vector<MSG> m_message;
};
////////////////////////////////////////////////////////////////////////////////
// SHA common functions
//
template <typename T, typename U, typename BITWISE>
class SHA_Functions : public BITWISE
{
public:
static U Ch(const T& x, const T& y, const T& z) {
return
BITWISE::XOR(
BITWISE::_AND(x, y),
BITWISE::_AND(BITWISE::_CMPLMNT(x), z));
}
static U Parity(const T& x, const T& y, const T& z) {
return
BITWISE::XOR(
BITWISE::_XOR(x, y),
z);
}
static U Maj(const T& x, const T& y, const T& z) {
return
BITWISE::XOR(
BITWISE::_XOR(
BITWISE::_AND(x, y),
BITWISE::_AND(x, z)),
BITWISE::_AND(y, z));
}
static U f(const T& x, const T& y, const T& z, const std::size_t round) {
if (round < 20) {
return Ch(x, y, z);
} else if (round < 40) {
return Parity(x, y, z);
} else if (round < 60) {
return Maj(x, y, z);
} else {
return Parity(x, y, z);
}
}
static U SIGMA_256_0(const T& x) { return SIGMA(x, 2, 13, 22); }
static U SIGMA_256_1(const T& x) { return SIGMA(x, 6, 11, 25); }
static U sigma_256_0(const T& x) { return sigma(x, 7, 18, 3); }
static U sigma_256_1(const T& x) { return sigma(x, 17, 19, 10); }
static U SIGMA_512_0(const T& x) { return SIGMA(x, 28, 34, 39); }
static U SIGMA_512_1(const T& x) { return SIGMA(x, 14, 18, 41); }
static U sigma_512_0(const T& x) { return sigma(x, 1, 8, 7); }
static U sigma_512_1(const T& x) { return sigma(x, 19, 61, 6); }
private:
static U SIGMA(const T& x,
const unsigned int a,
const unsigned int b,
const unsigned int c) {
return
BITWISE::XOR(
BITWISE::_XOR(
BITWISE::_ROTR(x, a),
BITWISE::_ROTR(x, b)),
BITWISE::_ROTR(x, c));
}
static U sigma(const T& x,
const unsigned int a,
const unsigned int b,
const unsigned int c) {
return
BITWISE::XOR(
BITWISE::_XOR(
BITWISE::_ROTR(x, a),
BITWISE::_ROTR(x, b)),
BITWISE::_SHR(x, c));
}
};
} // namespace snarkfront
#endif