25 #include "utility/EndianUtil.h" 
   26 #include "utility/RotateUtil.h" 
   27 #include "utility/ProgMemUtil.h" 
  110 #define BLAKE2s_IV0 0x6A09E667 
  111 #define BLAKE2s_IV1 0xBB67AE85 
  112 #define BLAKE2s_IV2 0x3C6EF372 
  113 #define BLAKE2s_IV3 0xA54FF53A 
  114 #define BLAKE2s_IV4 0x510E527F 
  115 #define BLAKE2s_IV5 0x9B05688C 
  116 #define BLAKE2s_IV6 0x1F83D9AB 
  117 #define BLAKE2s_IV7 0x5BE0CD19 
  121     state.h[0] = BLAKE2s_IV0 ^ 0x01010020; 
 
  122     state.h[1] = BLAKE2s_IV1;
 
  123     state.h[2] = BLAKE2s_IV2;
 
  124     state.h[3] = BLAKE2s_IV3;
 
  125     state.h[4] = BLAKE2s_IV4;
 
  126     state.h[5] = BLAKE2s_IV5;
 
  127     state.h[6] = BLAKE2s_IV6;
 
  128     state.h[7] = BLAKE2s_IV7;
 
  142     if (outputLength < 1)
 
  144     else if (outputLength > 32)
 
  146     state.h[0] = BLAKE2s_IV0 ^ 0x01010000 ^ outputLength;
 
  147     state.h[1] = BLAKE2s_IV1;
 
  148     state.h[2] = BLAKE2s_IV2;
 
  149     state.h[3] = BLAKE2s_IV3;
 
  150     state.h[4] = BLAKE2s_IV4;
 
  151     state.h[5] = BLAKE2s_IV5;
 
  152     state.h[6] = BLAKE2s_IV6;
 
  153     state.h[7] = BLAKE2s_IV7;
 
  174     if (outputLength < 1)
 
  176     else if (outputLength > 32)
 
  178     state.h[0] = BLAKE2s_IV0 ^ 0x01010000 ^ (keyLen << 8) ^ outputLength;
 
  179     state.h[1] = BLAKE2s_IV1;
 
  180     state.h[2] = BLAKE2s_IV2;
 
  181     state.h[3] = BLAKE2s_IV3;
 
  182     state.h[4] = BLAKE2s_IV4;
 
  183     state.h[5] = BLAKE2s_IV5;
 
  184     state.h[6] = BLAKE2s_IV6;
 
  185     state.h[7] = BLAKE2s_IV7;
 
  188         memcpy(state.m, key, keyLen);
 
  189         memset(((uint8_t *)state.m) + keyLen, 0, 64 - keyLen);
 
  190         state.chunkSize = 64;
 
  202     const uint8_t *d = (
const uint8_t *)data;
 
  204         if (state.chunkSize == 64) {
 
  210         uint8_t size = 64 - state.chunkSize;
 
  213         memcpy(((uint8_t *)state.m) + state.chunkSize, d, size);
 
  214         state.chunkSize += size;
 
  215         state.length += size;
 
  224     memset(((uint8_t *)state.m) + state.chunkSize, 0, 64 - state.chunkSize);
 
  225     processChunk(0xFFFFFFFF);
 
  228     for (uint8_t posn = 0; posn < 8; ++posn)
 
  229         state.m[posn] = htole32(state.h[posn]);
 
  234     memcpy(hash, state.m, len);
 
  247     state.chunkSize = 64;
 
  256     state.chunkSize = 64;
 
  257     update(temp, 
sizeof(temp));
 
  263 static const uint8_t sigma[10][16] PROGMEM = {
 
  264     { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15},
 
  265     {14, 10,  4,  8,  9, 15, 13,  6,  1, 12,  0,  2, 11,  7,  5,  3},
 
  266     {11,  8, 12,  0,  5,  2, 15, 13, 10, 14,  3,  6,  7,  1,  9,  4},
 
  267     { 7,  9,  3,  1, 13, 12, 11, 14,  2,  6,  5, 10,  4,  0, 15,  8},
 
  268     { 9,  0,  5,  7,  2,  4, 10, 15, 14,  1, 11, 12,  6,  8,  3, 13},
 
  269     { 2, 12,  6, 10,  0, 11,  8,  3,  4, 13,  7,  5, 15, 14,  1,  9},
 
  270     {12,  5,  1, 15, 14, 13,  4, 10,  0,  7,  6,  3,  9,  2,  8, 11},
 
  271     {13, 11,  7, 14, 12,  1,  3,  9,  5,  0, 15,  4,  8,  6,  2, 10},
 
  272     { 6, 15, 14,  9, 11,  3,  0,  8, 12,  2, 13,  7,  1,  4, 10,  5},
 
  273     {10,  2,  8,  4,  7,  6,  1,  5, 15, 11,  9, 14,  3, 12, 13 , 0}
 
  277 #define quarterRound(a, b, c, d, i)    \ 
  280         uint32_t _a = (a) + _b + state.m[pgm_read_byte(&(sigma[index][2 * (i)]))]; \ 
  281         uint32_t _d = rightRotate16((d) ^ _a); \ 
  282         uint32_t _c = (c) + _d; \ 
  283         _b = rightRotate12(_b ^ _c); \ 
  284         _a += _b + state.m[pgm_read_byte(&(sigma[index][2 * (i) + 1]))]; \ 
  285         (d) = _d = rightRotate8(_d ^ _a); \ 
  288         (b) = rightRotate7(_b ^ _c); \ 
  292 void BLAKE2s::processChunk(uint32_t f0)
 
  298 #if !defined(CRYPTO_LITTLE_ENDIAN) 
  299     for (index = 0; index < 16; ++index)
 
  300         state.m[index] = le32toh(state.m[index]);
 
  304     memcpy(v, state.h, 
sizeof(state.h));
 
  309     v[12] = BLAKE2s_IV4 ^ (uint32_t)(state.length);
 
  310     v[13] = BLAKE2s_IV5 ^ (uint32_t)(state.length >> 32);
 
  311     v[14] = BLAKE2s_IV6 ^ f0;
 
  315     for (index = 0; index < 10; ++index) {
 
  317         quarterRound(v[0], v[4], v[8],  v[12], 0);
 
  318         quarterRound(v[1], v[5], v[9],  v[13], 1);
 
  319         quarterRound(v[2], v[6], v[10], v[14], 2);
 
  320         quarterRound(v[3], v[7], v[11], v[15], 3);
 
  323         quarterRound(v[0], v[5], v[10], v[15], 4);
 
  324         quarterRound(v[1], v[6], v[11], v[12], 5);
 
  325         quarterRound(v[2], v[7], v[8],  v[13], 6);
 
  326         quarterRound(v[3], v[4], v[9],  v[14], 7);
 
  330     for (index = 0; index < 8; ++index)
 
  331         state.h[index] ^= (v[index] ^ v[index + 8]);
 
void clear()
Clears the hash state, removing all sensitive data, and then resets the hash ready for a new hashing ...
size_t blockSize() const
Size of the internal block used by the hash algorithm.
void finalizeHMAC(const void *key, size_t keyLen, void *hash, size_t hashLen)
Finalizes the HMAC hashing process and returns the hash.
virtual ~BLAKE2s()
Destroys this BLAKE2s hash object after clearing sensitive information.
BLAKE2s()
Constructs a BLAKE2s hash object.
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 resetHMAC(const void *key, size_t keyLen)
Resets the hash ready for a new HMAC hashing process.
void update(const void *data, size_t len)
Updates the hash with more data.
size_t hashSize() const
Size of the hash result from finalize().
void formatHMACKey(void *block, const void *key, size_t len, uint8_t pad)
Formats a HMAC key into a block.