3#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST)
10#if defined(USE_HMAC_SHA256_PSA)
16 psa_mac_abort(&this->
op_);
21 psa_mac_abort(&this->
op_);
24 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
25 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
26 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
27 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(PSA_ALG_SHA_256));
28 psa_import_key(&attributes, key,
len, &this->
key_id_);
30 this->
op_ = PSA_MAC_OPERATION_INIT;
31 psa_mac_sign_setup(&this->
op_, this->
key_id_, PSA_ALG_HMAC(PSA_ALG_SHA_256));
38 psa_mac_sign_finish(&this->
op_, this->
digest_,
sizeof(this->
digest_), &mac_length);
58#elif defined(USE_HMAC_SHA256_MBEDTLS)
63 mbedtls_md_init(&this->
ctx_);
64 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
65 mbedtls_md_setup(&this->
ctx_, md_info, 1);
66 mbedtls_md_hmac_starts(&this->
ctx_, key,
len);
100 memset(ipad, 0,
sizeof(ipad));
108 memcpy(ipad, key,
len);
110 memcpy(opad, ipad,
sizeof(opad));
132 this->
ohash_.
add(ibytes,
sizeof(ibytes));
void get_hex(char *output)
Retrieve the hash as hex characters. Output buffer must hold get_size() * 2 + 1 bytes.
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash.
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
void get_bytes(uint8_t *output)
Retrieve the hash as bytes.
bool equals_bytes(const uint8_t *expected)
Compare the digest against a provided byte-encoded digest (32 bytes).
mbedtls_md_context_t ctx_
mbedtls_svc_key_id_t key_id_
bool equals_hex(const char *expected)
Compare the digest against a provided hex-encoded digest (64 bytes).
void add(const uint8_t *data, size_t len)
Add bytes of data for the digest.
void get_hex(char *output)
Retrieve the HMAC-SHA256 digest as hex characters.
void get_bytes(uint8_t *output)
Retrieve the HMAC-SHA256 digest as bytes.
static constexpr size_t SHA256_DIGEST_SIZE
uint8_t digest_[SHA256_DIGEST_SIZE]
void calculate()
Compute the digest, based on the provided data.
void init(const uint8_t *key, size_t len)
Initialize a new HMAC-SHA256 digest computation.
SHA256 hash implementation.
void calculate() override
void add(const uint8_t *data, size_t len) override
constexpr size_t SHA256_DIGEST_SIZE
constexpr size_t HMAC_BLOCK_SIZE
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).