Add C example, and remove SIZEOFARRAY macro

This commit is contained in:
h5p9sl 2021-11-02 20:57:34 -06:00
parent 4189b1de64
commit bbdfaac381
2 changed files with 46 additions and 8 deletions

39
examples/simple_example.c Normal file
View File

@ -0,0 +1,39 @@
#include "../hmac_sha256.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}

View File

@ -9,7 +9,6 @@
#include <stdlib.h>
#include <string.h>
#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);