Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hmac: add stateless HMAC-SHA256 wrapper #706

Merged
merged 1 commit into from
Feb 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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