From bbdfaac3813a1883f886182f3c96774ab1c841fb Mon Sep 17 00:00:00 2001 From: h5p9sl <21267024+h5p9sl@users.noreply.github.com> Date: Tue, 2 Nov 2021 20:57:34 -0600 Subject: [PATCH] Add C example, and remove SIZEOFARRAY macro --- examples/simple_example.c | 39 +++++++++++++++++++++++++++++++++++++++ hmac_sha256.c | 15 +++++++-------- 2 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 examples/simple_example.c diff --git a/examples/simple_example.c b/examples/simple_example.c new file mode 100644 index 0000000..d456417 --- /dev/null +++ b/examples/simple_example.c @@ -0,0 +1,39 @@ +#include "../hmac_sha256.h" + +#include +#include +#include +#include +#include + +#define SHA256_HASH_SIZE 32 + +int main() { + const char* str_data = "Hello World!"; + const char* str_key = "super-secret-key"; + uint8_t out[SHA256_HASH_SIZE]; + char out_str[SHA256_HASH_SIZE * 2 + 1]; + unsigned i; + + // Call hmac-sha256 function + hmac_sha256(str_key, strlen(str_key), str_data, strlen(str_data), &out, + sizeof(out)); + + // Convert `out` to string with printf + memset(&out_str, 0, sizeof(out_str)); + for (i = 0; i < sizeof(out); i++) { + snprintf(&out_str[i*2], 3, "%02x", out[i]); + } + + // Print out the result + printf("Message: %s\n", str_data); + printf("Key: %s\n", str_key); + printf("HMAC: %s\n", out_str); + + // This assertion fails if something went wrong + assert(strncmp( + out_str, + "4b393abced1c497f8048860ba1ede46a23f1ff5209b18e9c428bddfbb690aad8", + SHA256_HASH_SIZE * 2) == 0); + return 0; +} diff --git a/hmac_sha256.c b/hmac_sha256.c index e4d83e4..5f7c0f8 100644 --- a/hmac_sha256.c +++ b/hmac_sha256.c @@ -9,7 +9,6 @@ #include #include -#define SIZEOFARRAY(x) (sizeof(x) / sizeof(x[0])) #define SHA256_BLOCK_SIZE 64 /* LOCAL FUNCTIONS */ @@ -21,6 +20,7 @@ static void* H(const void* x, const size_t ylen, void* out, const size_t outlen); + // Wrapper for sha256 static void* sha256(const void* data, const size_t datalen, @@ -42,14 +42,14 @@ size_t hmac_sha256(const void* key, size_t sz; int i; - memset(k, 0, SIZEOFARRAY(k)); + memset(k, 0, sizeof(k)); memset(k_ipad, 0x36, SHA256_BLOCK_SIZE); memset(k_opad, 0x5c, SHA256_BLOCK_SIZE); if (keylen > SHA256_BLOCK_SIZE) { // If the key is larger than the hash algorithm's // block size, we must digest it first. - sha256(key, keylen, k, SIZEOFARRAY(k)); + sha256(key, keylen, k, sizeof(k)); } else { memcpy(k, key, keylen); } @@ -61,9 +61,8 @@ size_t hmac_sha256(const void* key, // Perform HMAC algorithm: ( https://tools.ietf.org/html/rfc2104 ) // `H(K XOR opad, H(K XOR ipad, data))` - H(k_ipad, SIZEOFARRAY(k_ipad), data, datalen, ihash, SIZEOFARRAY(ihash)); - H(k_opad, SIZEOFARRAY(k_opad), ihash, SIZEOFARRAY(ihash), ohash, - SIZEOFARRAY(ohash)); + H(k_ipad, sizeof(k_ipad), data, datalen, ihash, sizeof(ihash)); + H(k_opad, sizeof(k_opad), ihash, sizeof(ihash), ohash, sizeof(ohash)); sz = (outlen > SHA256_HASH_SIZE) ? SHA256_HASH_SIZE : outlen; memcpy(out, ohash, sz); @@ -76,9 +75,9 @@ static void* H(const void* x, const size_t ylen, void* out, const size_t outlen) { - const size_t buflen = xlen + ylen; - uint8_t* buf = (uint8_t*)malloc(buflen); void* result; + size_t buflen = (xlen + ylen); + uint8_t* buf = (uint8_t*)malloc(buflen); memcpy(buf, x, xlen); memcpy(buf + xlen, y, ylen);