25 #include "utility/EndianUtil.h"
26 #include "utility/RotateUtil.h"
27 #include "utility/ProgMemUtil.h"
100 #define BLAKE2b_IV0 0x6a09e667f3bcc908ULL
101 #define BLAKE2b_IV1 0xbb67ae8584caa73bULL
102 #define BLAKE2b_IV2 0x3c6ef372fe94f82bULL
103 #define BLAKE2b_IV3 0xa54ff53a5f1d36f1ULL
104 #define BLAKE2b_IV4 0x510e527fade682d1ULL
105 #define BLAKE2b_IV5 0x9b05688c2b3e6c1fULL
106 #define BLAKE2b_IV6 0x1f83d9abfb41bd6bULL
107 #define BLAKE2b_IV7 0x5be0cd19137e2179ULL
111 state.h[0] = BLAKE2b_IV0 ^ 0x01010040;
112 state.h[1] = BLAKE2b_IV1;
113 state.h[2] = BLAKE2b_IV2;
114 state.h[3] = BLAKE2b_IV3;
115 state.h[4] = BLAKE2b_IV4;
116 state.h[5] = BLAKE2b_IV5;
117 state.h[6] = BLAKE2b_IV6;
118 state.h[7] = BLAKE2b_IV7;
121 state.lengthHigh = 0;
133 if (outputLength < 1)
135 else if (outputLength > 64)
137 state.h[0] = BLAKE2b_IV0 ^ 0x01010000 ^ outputLength;
138 state.h[1] = BLAKE2b_IV1;
139 state.h[2] = BLAKE2b_IV2;
140 state.h[3] = BLAKE2b_IV3;
141 state.h[4] = BLAKE2b_IV4;
142 state.h[5] = BLAKE2b_IV5;
143 state.h[6] = BLAKE2b_IV6;
144 state.h[7] = BLAKE2b_IV7;
147 state.lengthHigh = 0;
166 if (outputLength < 1)
168 else if (outputLength > 64)
170 state.h[0] = BLAKE2b_IV0 ^ 0x01010000 ^ (keyLen << 8) ^ outputLength;
171 state.h[1] = BLAKE2b_IV1;
172 state.h[2] = BLAKE2b_IV2;
173 state.h[3] = BLAKE2b_IV3;
174 state.h[4] = BLAKE2b_IV4;
175 state.h[5] = BLAKE2b_IV5;
176 state.h[6] = BLAKE2b_IV6;
177 state.h[7] = BLAKE2b_IV7;
180 memcpy(state.m, key, keyLen);
181 memset(((uint8_t *)state.m) + keyLen, 0, 128 - keyLen);
182 state.chunkSize = 128;
183 state.lengthLow = 128;
189 state.lengthHigh = 0;
195 const uint8_t *d = (
const uint8_t *)data;
197 if (state.chunkSize == 128) {
203 uint8_t size = 128 - state.chunkSize;
206 memcpy(((uint8_t *)state.m) + state.chunkSize, d, size);
207 state.chunkSize += size;
208 uint64_t temp = state.lengthLow;
209 state.lengthLow += size;
210 if (state.lengthLow < temp)
220 memset(((uint8_t *)state.m) + state.chunkSize, 0, 128 - state.chunkSize);
221 processChunk(0xFFFFFFFFFFFFFFFFULL);
224 for (uint8_t posn = 0; posn < 8; ++posn)
225 state.m[posn] = htole64(state.h[posn]);
230 memcpy(hash, state.m, len);
242 state.lengthLow += 128;
251 state.lengthLow += 128;
253 update(temp,
sizeof(temp));
259 static const uint8_t sigma[12][16] PROGMEM = {
260 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
261 {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
262 {11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4},
263 { 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8},
264 { 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13},
265 { 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9},
266 {12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11},
267 {13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10},
268 { 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5},
269 {10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13 , 0},
270 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
271 {14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3},
275 #define quarterRound(a, b, c, d, i) \
278 uint64_t _a = (a) + _b + state.m[pgm_read_byte(&(sigma[index][2 * (i)]))]; \
279 uint64_t _d = rightRotate32_64((d) ^ _a); \
280 uint64_t _c = (c) + _d; \
281 _b = rightRotate24_64(_b ^ _c); \
282 _a += _b + state.m[pgm_read_byte(&(sigma[index][2 * (i) + 1]))]; \
283 (d) = _d = rightRotate16_64(_d ^ _a); \
286 (b) = rightRotate63_64(_b ^ _c); \
290 void BLAKE2b::processChunk(uint64_t f0)
296 #if !defined(CRYPTO_LITTLE_ENDIAN)
297 for (index = 0; index < 16; ++index)
298 state.m[index] = le64toh(state.m[index]);
302 memcpy(v, state.h,
sizeof(state.h));
307 v[12] = BLAKE2b_IV4 ^ state.lengthLow;
308 v[13] = BLAKE2b_IV5 ^ state.lengthHigh;
309 v[14] = BLAKE2b_IV6 ^ f0;
313 for (index = 0; index < 12; ++index) {
315 quarterRound(v[0], v[4], v[8], v[12], 0);
316 quarterRound(v[1], v[5], v[9], v[13], 1);
317 quarterRound(v[2], v[6], v[10], v[14], 2);
318 quarterRound(v[3], v[7], v[11], v[15], 3);
321 quarterRound(v[0], v[5], v[10], v[15], 4);
322 quarterRound(v[1], v[6], v[11], v[12], 5);
323 quarterRound(v[2], v[7], v[8], v[13], 6);
324 quarterRound(v[3], v[4], v[9], v[14], 7);
328 for (index = 0; index < 8; ++index)
329 state.h[index] ^= (v[index] ^ v[index + 8]);
void finalize(void *hash, size_t len)
Finalizes the hashing process and returns the hash.
void reset()
Resets the hash ready for a new hashing process.
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
BLAKE2b()
Constructs a BLAKE2b hash object.
size_t blockSize() const
Size of the internal block used by the hash algorithm.
size_t hashSize() const
Size of the hash result from finalize().
virtual ~BLAKE2b()
Destroys this BLAKE2b hash object after clearing sensitive information.
void update(const void *data, size_t len)
Updates the hash with more data.
void resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
void formatHMACKey(void *block, const void *key, size_t len, uint8_t pad)
Formats a HMAC key into a block.
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.