ESPHome 2026.1.4
Loading...
Searching...
No Matches
hmac_sha256.cpp
Go to the documentation of this file.
1#include <cstring>
2#include "hmac_sha256.h"
3#if defined(USE_ESP32) || defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_LIBRETINY) || defined(USE_HOST)
5
7
8constexpr size_t SHA256_DIGEST_SIZE = 32;
9
10#if defined(USE_ESP32) || defined(USE_LIBRETINY)
11
12HmacSHA256::~HmacSHA256() { mbedtls_md_free(&this->ctx_); }
13
14void HmacSHA256::init(const uint8_t *key, size_t len) {
15 mbedtls_md_init(&this->ctx_);
16 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
17 mbedtls_md_setup(&this->ctx_, md_info, 1); // 1 = HMAC mode
18 mbedtls_md_hmac_starts(&this->ctx_, key, len);
19}
20
21void HmacSHA256::add(const uint8_t *data, size_t len) { mbedtls_md_hmac_update(&this->ctx_, data, len); }
22
23void HmacSHA256::calculate() { mbedtls_md_hmac_finish(&this->ctx_, this->digest_); }
24
25void HmacSHA256::get_bytes(uint8_t *output) { memcpy(output, this->digest_, SHA256_DIGEST_SIZE); }
26
27void HmacSHA256::get_hex(char *output) {
28 format_hex_to(output, SHA256_DIGEST_SIZE * 2 + 1, this->digest_, SHA256_DIGEST_SIZE);
29}
30
31bool HmacSHA256::equals_bytes(const uint8_t *expected) {
32 return memcmp(this->digest_, expected, SHA256_DIGEST_SIZE) == 0;
33}
34
35bool HmacSHA256::equals_hex(const char *expected) {
36 char hex_output[SHA256_DIGEST_SIZE * 2 + 1];
37 this->get_hex(hex_output);
38 hex_output[SHA256_DIGEST_SIZE * 2] = '\0';
39 return strncmp(hex_output, expected, SHA256_DIGEST_SIZE * 2) == 0;
40}
41
42#else
43
44HmacSHA256::~HmacSHA256() = default;
45
46// HMAC block size for SHA256 (RFC 2104)
47constexpr size_t HMAC_BLOCK_SIZE = 64;
48
49void HmacSHA256::init(const uint8_t *key, size_t len) {
50 uint8_t ipad[HMAC_BLOCK_SIZE], opad[HMAC_BLOCK_SIZE];
51
52 memset(ipad, 0, sizeof(ipad));
53 if (len > HMAC_BLOCK_SIZE) {
54 sha256::SHA256 keysha256;
55 keysha256.init();
56 keysha256.add(key, len);
57 keysha256.calculate();
58 keysha256.get_bytes(ipad);
59 } else {
60 memcpy(ipad, key, len);
61 }
62 memcpy(opad, ipad, sizeof(opad));
63
64 for (size_t i = 0; i < HMAC_BLOCK_SIZE; i++) {
65 ipad[i] ^= 0x36;
66 opad[i] ^= 0x5c;
67 }
68
69 this->ihash_.init();
70 this->ihash_.add(ipad, sizeof(ipad));
71
72 this->ohash_.init();
73 this->ohash_.add(opad, sizeof(opad));
74}
75
76void HmacSHA256::add(const uint8_t *data, size_t len) { this->ihash_.add(data, len); }
77
79 uint8_t ibytes[32];
80
81 this->ihash_.calculate();
82 this->ihash_.get_bytes(ibytes);
83
84 this->ohash_.add(ibytes, sizeof(ibytes));
85 this->ohash_.calculate();
86}
87
88void HmacSHA256::get_bytes(uint8_t *output) { this->ohash_.get_bytes(output); }
89
90void HmacSHA256::get_hex(char *output) { this->ohash_.get_hex(output); }
91
92bool HmacSHA256::equals_bytes(const uint8_t *expected) { return this->ohash_.equals_bytes(expected); }
93
94bool HmacSHA256::equals_hex(const char *expected) { return this->ohash_.equals_hex(expected); }
95
96#endif // USE_ESP32 || USE_LIBRETINY
97
98} // namespace esphome::hmac_sha256
99#endif
void get_hex(char *output)
Retrieve the hash as hex characters. Output buffer must hold get_size() * 2 + 1 bytes.
Definition hash_base.h:29
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash.
Definition hash_base.h:35
bool equals_bytes(const uint8_t *expected)
Compare the hash against a provided byte-encoded hash.
Definition hash_base.h:32
void get_bytes(uint8_t *output)
Retrieve the hash as bytes.
Definition hash_base.h:26
bool equals_bytes(const uint8_t *expected)
Compare the digest against a provided byte-encoded digest (32 bytes).
mbedtls_md_context_t ctx_
Definition hmac_sha256.h:50
uint8_t digest_[SHA256_DIGEST_SIZE]
Definition hmac_sha256.h:51
static constexpr size_t SHA256_DIGEST_SIZE
Definition hmac_sha256.h:49
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.
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.
Definition sha256.h:38
void calculate() override
Definition sha256.cpp:56
void add(const uint8_t *data, size_t len) override
Definition sha256.cpp:54
void init() override
Definition sha256.cpp:49
constexpr size_t SHA256_DIGEST_SIZE
constexpr size_t HMAC_BLOCK_SIZE
std::string size_t len
Definition helpers.h:595
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).
Definition helpers.cpp:322