Skip to content

Commit

Permalink
hmac: add stateless HMAC-SHA256 wrapper (#706)
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredh authored Feb 26, 2023
1 parent d385d14 commit 7c7eb1b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions include/re_hmac.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ void hmac_sha1(const uint8_t *k, /* secret key */
uint8_t* out, /* output buffer, at least "t" bytes */
size_t t);

void hmac_sha256(const uint8_t *key,
size_t key_len,
const uint8_t *data,
size_t data_len,
uint8_t *out,
size_t out_len);


enum hmac_hash {
HMAC_HASH_SHA1,
Expand Down
30 changes: 30 additions & 0 deletions src/hmac/hmac_sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,35 @@ void hmac_sha1(const uint8_t *k, /* secret key */
#error missing HMAC-SHA1 backend


#endif
}


void hmac_sha256(const uint8_t *key, size_t key_len,
const uint8_t *data, size_t data_len,
uint8_t *out, size_t out_len)
{
#ifdef USE_OPENSSL

(void)out_len;

if (!HMAC(EVP_sha256(), key, (int)key_len, data, data_len, out, NULL))
ERR_clear_error();

#elif defined (__APPLE__)
(void)out_len;

CCHmac(kCCHmacAlgSHA256, key, key_len, data, data_len, out);
#else
(void)key;
(void)key_len;
(void)data;
(void)data_len;
(void)out;
(void)out_len;

#error missing HMAC-SHA256 backend


#endif
}
10 changes: 10 additions & 0 deletions test/hmac.c
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,16 @@ int test_hmac_sha256(void)
TEST_MEMCMP(digest, sizeof(digest), md, sizeof(md));

hmac = mem_deref(hmac);

/* Test Stateless API */

uint8_t md2[SHA256_DIGEST_LENGTH];

hmac_sha256(key, key_len,
data, data_len,
md2, sizeof(md2));

TEST_MEMCMP(digest, sizeof(digest), md2, sizeof(md2));
}

out:
Expand Down

0 comments on commit 7c7eb1b

Please sign in to comment.