mirror of
https://github.com/taigrr/arduinolibs
synced 2025-01-18 04:33:12 -08:00
Add HMAC support to all of the hash algorithms
This commit is contained in:
@@ -29,6 +29,7 @@ This example runs tests on the BLAKE2s implementation to verify correct behaviou
|
||||
#include <string.h>
|
||||
|
||||
#define HASH_SIZE 32
|
||||
#define BLOCK_SIZE 64
|
||||
|
||||
struct TestHashVector
|
||||
{
|
||||
@@ -144,6 +145,69 @@ void perfHash(Hash *hash)
|
||||
Serial.println(" bytes per second");
|
||||
}
|
||||
|
||||
// Very simple method for hashing a HMAC inner or outer key.
|
||||
void hashKey(Hash *hash, const uint8_t *key, size_t keyLen, uint8_t pad)
|
||||
{
|
||||
size_t posn;
|
||||
uint8_t buf;
|
||||
uint8_t result[HASH_SIZE];
|
||||
if (keyLen <= BLOCK_SIZE) {
|
||||
hash->reset();
|
||||
for (posn = 0; posn < BLOCK_SIZE; ++posn) {
|
||||
if (posn < keyLen)
|
||||
buf = key[posn] ^ pad;
|
||||
else
|
||||
buf = pad;
|
||||
hash->update(&buf, 1);
|
||||
}
|
||||
} else {
|
||||
hash->reset();
|
||||
hash->update(key, keyLen);
|
||||
hash->finalize(result, HASH_SIZE);
|
||||
hash->reset();
|
||||
for (posn = 0; posn < BLOCK_SIZE; ++posn) {
|
||||
if (posn < HASH_SIZE)
|
||||
buf = result[posn] ^ pad;
|
||||
else
|
||||
buf = pad;
|
||||
hash->update(&buf, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testHMAC(Hash *hash, size_t keyLen)
|
||||
{
|
||||
uint8_t result[HASH_SIZE];
|
||||
|
||||
Serial.print("HMAC-BLAKE2s keysize=");
|
||||
Serial.print(keyLen);
|
||||
Serial.print(" ... ");
|
||||
|
||||
// Construct the expected result with a simple HMAC implementation.
|
||||
memset(buffer, (uint8_t)keyLen, keyLen);
|
||||
hashKey(hash, buffer, keyLen, 0x36);
|
||||
memset(buffer, 0xBA, sizeof(buffer));
|
||||
hash->update(buffer, sizeof(buffer));
|
||||
hash->finalize(result, HASH_SIZE);
|
||||
memset(buffer, (uint8_t)keyLen, keyLen);
|
||||
hashKey(hash, buffer, keyLen, 0x5C);
|
||||
hash->update(result, HASH_SIZE);
|
||||
hash->finalize(result, HASH_SIZE);
|
||||
|
||||
// Now use the library to compute the HMAC.
|
||||
hash->resetHMAC(buffer, keyLen);
|
||||
memset(buffer, 0xBA, sizeof(buffer));
|
||||
hash->update(buffer, sizeof(buffer));
|
||||
memset(buffer, (uint8_t)keyLen, keyLen);
|
||||
hash->finalizeHMAC(buffer, keyLen, buffer, HASH_SIZE);
|
||||
|
||||
// Check the result.
|
||||
if (!memcmp(result, buffer, HASH_SIZE))
|
||||
Serial.println("Passed");
|
||||
else
|
||||
Serial.println("Failed");
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
@@ -155,6 +219,12 @@ void setup()
|
||||
testHash(&blake2s, &testVectorBLAKE2s_2);
|
||||
testHash(&blake2s, &testVectorBLAKE2s_3);
|
||||
testHash(&blake2s, &testVectorBLAKE2s_4);
|
||||
testHMAC(&blake2s, (size_t)0);
|
||||
testHMAC(&blake2s, 1);
|
||||
testHMAC(&blake2s, HASH_SIZE);
|
||||
testHMAC(&blake2s, BLOCK_SIZE);
|
||||
testHMAC(&blake2s, BLOCK_SIZE + 1);
|
||||
testHMAC(&blake2s, sizeof(buffer));
|
||||
|
||||
Serial.println();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user