1
0
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:
Rhys Weatherley
2015-03-24 19:41:24 +10:00
parent e0803c01fc
commit fd38b7e127
25 changed files with 845 additions and 13 deletions

View File

@@ -29,16 +29,19 @@ This example runs tests on the SHA1 implementation to verify correct behaviour.
#include <string.h>
#define HASH_SIZE 20
#define BLOCK_SIZE 64
struct TestHashVector
{
const char *name;
const char *key;
const char *data;
uint8_t hash[HASH_SIZE];
};
static TestHashVector const testVectorSHA1_1 = {
"SHA-1 #1",
0,
"abc",
{0xA9, 0x99, 0x3E, 0x36, 0x47, 0x06, 0x81, 0x6A,
0xBA, 0x3E, 0x25, 0x71, 0x78, 0x50, 0xC2, 0x6C,
@@ -46,11 +49,28 @@ static TestHashVector const testVectorSHA1_1 = {
};
static TestHashVector const testVectorSHA1_2 = {
"SHA-1 #2",
0,
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
{0x84, 0x98, 0x3E, 0x44, 0x1C, 0x3B, 0xD2, 0x6E,
0xBA, 0xAE, 0x4A, 0xA1, 0xF9, 0x51, 0x29, 0xE5,
0xE5, 0x46, 0x70, 0xF1}
};
static TestHashVector const testVectorHMAC_SHA1_1 = {
"HMAC-SHA-1 #1",
"",
"",
{0xfb, 0xdb, 0x1d, 0x1b, 0x18, 0xaa, 0x6c, 0x08,
0x32, 0x4b, 0x7d, 0x64, 0xb7, 0x1f, 0xb7, 0x63,
0x70, 0x69, 0x0e, 0x1d}
};
static TestHashVector const testVectorHMAC_SHA1_2 = {
"HMAC-SHA-1 #2",
"key",
"The quick brown fox jumps over the lazy dog",
{0xde, 0x7c, 0x9b, 0x85, 0xb8, 0xb7, 0x8a, 0xa6,
0xbc, 0x8a, 0x7a, 0x36, 0xf7, 0x0a, 0x90, 0x70,
0x1c, 0x9d, 0xb4, 0xd9}
};
SHA1 sha1;
@@ -100,6 +120,86 @@ void testHash(Hash *hash, const struct TestHashVector *test)
Serial.println("Failed");
}
// 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-SHA-1 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 testHMAC(Hash *hash, const struct TestHashVector *test)
{
uint8_t result[HASH_SIZE];
Serial.print(test->name);
Serial.print(" ... ");
hash->resetHMAC(test->key, strlen(test->key));
hash->update(test->data, strlen(test->data));
hash->finalizeHMAC(test->key, strlen(test->key), result, sizeof(result));
if (!memcmp(result, test->hash, HASH_SIZE))
Serial.println("Passed");
else
Serial.println("Failed");
}
void perfHash(Hash *hash)
{
unsigned long start;
@@ -133,6 +233,14 @@ void setup()
Serial.println("Test Vectors:");
testHash(&sha1, &testVectorSHA1_1);
testHash(&sha1, &testVectorSHA1_2);
testHMAC(&sha1, &testVectorHMAC_SHA1_1);
testHMAC(&sha1, &testVectorHMAC_SHA1_2);
testHMAC(&sha1, (size_t)0);
testHMAC(&sha1, 1);
testHMAC(&sha1, HASH_SIZE);
testHMAC(&sha1, BLOCK_SIZE);
testHMAC(&sha1, BLOCK_SIZE + 1);
testHMAC(&sha1, sizeof(buffer));
Serial.println();