Skip to content

Commit 415cead

Browse files
committed
WIP: HMAC
1 parent 0aa8319 commit 415cead

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

tests/harness/HMAC/verify.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <openssl/crypto.h>
2+
#include <openssl/err.h>
3+
#include <openssl/evp.h>
4+
#include <sched.h>
5+
#include <stdint.h>
6+
#include <stdlib.h>
7+
8+
int CC_HMAC_verify_sha256(const uint8_t *mac, const size_t mac_size,
9+
const uint8_t *key, const size_t key_size,
10+
const uint8_t *msg, const size_t msg_size) {
11+
EVP_MD_CTX *mdctx = NULL;
12+
const EVP_MD *md = NULL;
13+
EVP_PKEY *pkey = NULL;
14+
uint8_t buf[EVP_MAX_MD_SIZE] = {0};
15+
16+
if (NULL == (mdctx = EVP_MD_CTX_new()))
17+
goto error_ctx_new;
18+
if (NULL == (md = EVP_get_digestbyname("SHA-384")))
19+
goto error_by_name;
20+
21+
if (NULL ==
22+
(pkey = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, key, key_size)))
23+
goto error;
24+
if (1 != EVP_DigestVerifyInit(mdctx, NULL, md, NULL, pkey))
25+
goto error;
26+
if (1 != EVP_DigestVerifyUpdate(mdctx, msg, msg_size))
27+
goto error;
28+
if (1 != EVP_DigestVerifyFinal(mdctx, buf, EVP_MAX_MD_SIZE))
29+
goto error;
30+
31+
EVP_MD_CTX_free(mdctx);
32+
EVP_PKEY_free(pkey);
33+
return (CRYPTO_memcmp(mac, buf, mac_size) == 0);
34+
35+
error:
36+
EVP_PKEY_free(pkey);
37+
error_by_name:
38+
EVP_MD_CTX_free(mdctx);
39+
error_ctx_new:
40+
return -2;
41+
}

tests/harness/HMAC/verify.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@
66
def CC_HMAC_verify_sha256(key: bytes, msg: bytes, mac: bytes) -> bytes:
77
digest = hmac.digest(key, msg, "sha256")
88
return hmac.compare_digest(digest, mac)
9+
10+
11+
def CC_HMAC_verify_sha256_truncated(key: bytes, msg: bytes, mac: bytes) -> bytes:
12+
digest = hmac.digest(key, msg, "sha256")
13+
return hmac.compare_digest(digest[: len(mac)], mac)

0 commit comments

Comments
 (0)