ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
ble_aes_ccm.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <cstdint>
5
7
8// Self-contained AES-128-CCM authenticated decryption (RFC 3610).
9//
10// Encrypted BLE advertisements (BTHome, several Xiaomi/ATC variants) use
11// AES-128-CCM. The platform crypto that provides it is inconsistent across BLE
12// targets: ESP-IDF exposes PSA/mbedtls, but a LibreTiny SDK may keep its mbedtls
13// internal (e.g. the beken-72xx SDK ships mbedtls with CCM enabled but does not
14// put it on the application include path), so a sensor cannot rely on
15// <mbedtls/ccm.h> being available. This software implementation makes
16// encrypted-advertisement decryption work on every BLE platform without a
17// per-chip crypto dependency. Decryption volume is tiny (one short block per
18// matching advertisement), so software AES is not a meaningful cost.
19//
20// Verifies the CCM authentication tag and, on success, writes `ct_len` decrypted
21// bytes to `plaintext` and returns true. Returns false when authentication fails
22// (the caller must then discard `plaintext`). The CCM parameters follow the
23// caller (BTHome: 13-byte nonce, 4-byte tag, no associated data); `aad` may be
24// null when `aad_len` is 0.
28void aes128_encrypt_block(const uint8_t key[16], const uint8_t in[16], uint8_t out[16]);
29
30bool aes_ccm_auth_decrypt(const uint8_t key[16], const uint8_t *nonce, size_t nonce_len, const uint8_t *aad,
31 size_t aad_len, const uint8_t *ciphertext, size_t ct_len, uint8_t *plaintext,
32 const uint8_t *tag, size_t tag_len);
33
34} // namespace esphome::ble_device_base
bool aes_ccm_auth_decrypt(const uint8_t key[16], const uint8_t *nonce, size_t nonce_len, const uint8_t *aad, size_t aad_len, const uint8_t *ciphertext, size_t ct_len, uint8_t *plaintext, const uint8_t *tag, size_t tag_len)
void aes128_encrypt_block(const uint8_t key[16], const uint8_t in[16], uint8_t out[16])
AES-128 single-block encrypt (the same software cipher CCM uses).
const char * tag
Definition log.h:74