C++ conversions (#111)

* Remove version from artifact name
* Target C++ 20 and higher
* Use cpp string
* Better crash implementation
* String utils in cpp style
* Replace parameter methods with start() method
* MutexType to Mutex::Type
* Kernel c to cpp style
* Cleanup event flag
* More cpp conversions
* Test fixes
* Updated ideas docs
This commit is contained in:
Ken Van Hoeylandt
2024-12-07 12:24:28 +01:00
committed by GitHub
parent d52fe52d96
commit 42e843b463
66 changed files with 272 additions and 258 deletions
+10 -13
View File
@@ -4,6 +4,7 @@
#include "Log.h"
#include "mbedtls/aes.h"
#include <cstring>
#include <cstdint>
#ifdef ESP_PLATFORM
#include "esp_cpu.h"
@@ -95,7 +96,7 @@ static void get_nvs_key(uint8_t key[32]) {
* @param[out] out output buffer for result of XOR
* @param[in] length data length (all buffers must be at least this size)
*/
static void xor_key(const uint8_t* in_left, const uint8_t* in_right, uint8_t* out, size_t length) {
static void xorKey(const uint8_t* in_left, const uint8_t* in_right, uint8_t* out, size_t length) {
for (int i = 0; i < length; ++i) {
out[i] = in_left[i] ^ in_right[i];
}
@@ -105,7 +106,7 @@ static void xor_key(const uint8_t* in_left, const uint8_t* in_right, uint8_t* ou
* Combines a stored key and a hardware key into a single reliable key value.
* @param[out] key the key output
*/
static void get_key(uint8_t key[32]) {
static void getKey(uint8_t key[32]) {
#if !defined(CONFIG_SECURE_BOOT) || !defined(CONFIG_SECURE_FLASH_ENC_ENABLED)
TT_LOG_W(TAG, "Using tt_secure_* code with secure boot and/or flash encryption disabled.");
TT_LOG_W(TAG, "An attacker with physical access to your ESP32 can decrypt your secure data.");
@@ -117,14 +118,14 @@ static void get_key(uint8_t key[32]) {
#ifdef ESP_PLATFORM
get_hardware_key(hardware_key);
get_nvs_key(nvs_key);
xor_key(hardware_key, nvs_key, key, 32);
xorKey(hardware_key, nvs_key, key, 32);
#else
TT_LOG_W(TAG, "Using unsafe key for debugging purposes.");
memset(key, 0, 32);
#endif
}
void get_iv_from_data(const void* data, size_t data_length, uint8_t iv[16]) {
void getIv(const void* data, size_t data_length, uint8_t iv[16]) {
memset((void*)iv, 0, 16);
uint8_t* data_bytes = (uint8_t*)data;
for (int i = 0; i < data_length; ++i) {
@@ -133,11 +134,7 @@ void get_iv_from_data(const void* data, size_t data_length, uint8_t iv[16]) {
}
}
void get_iv_from_string(const char* input, uint8_t iv[16]) {
get_iv_from_data((const void*)input, strlen(input), iv);
}
static int aes256_crypt_cbc(
static int aes256CryptCbc(
const uint8_t key[32],
int mode,
size_t length,
@@ -166,25 +163,25 @@ static int aes256_crypt_cbc(
int encrypt(const uint8_t iv[16], uint8_t* in_data, uint8_t* out_data, size_t length) {
tt_check(length % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256");
uint8_t key[32];
get_key(key);
getKey(key);
// TODO: Is this still needed after switching to regular AES functions?
uint8_t iv_copy[16];
memcpy(iv_copy, iv, sizeof(iv_copy));
return aes256_crypt_cbc(key, MBEDTLS_AES_ENCRYPT, length, iv_copy, in_data, out_data);
return aes256CryptCbc(key, MBEDTLS_AES_ENCRYPT, length, iv_copy, in_data, out_data);
}
int decrypt(const uint8_t iv[16], uint8_t* in_data, uint8_t* out_data, size_t length) {
tt_check(length % 16 == 0, "Length is not a multiple of 16 bytes (for AES 256");
uint8_t key[32];
get_key(key);
getKey(key);
// TODO: Is this still needed after switching to regular AES functions?
uint8_t iv_copy[16];
memcpy(iv_copy, iv, sizeof(iv_copy));
return aes256_crypt_cbc(key, MBEDTLS_AES_DECRYPT, length, iv_copy, in_data, out_data);
return aes256CryptCbc(key, MBEDTLS_AES_DECRYPT, length, iv_copy, in_data, out_data);
}
} // namespace
+2 -8
View File
@@ -20,6 +20,7 @@
#include <cstdio>
#include <cstdint>
#include <string>
namespace tt::crypt {
@@ -29,14 +30,7 @@ namespace tt::crypt {
* @param data_length input data length
* @param iv output IV
*/
void get_iv_from_data(const void* data, size_t data_length, uint8_t iv[16]);
/**
* @brief Fills the IV with zeros and then creates an IV based on the input data.
* @param input input text
* @param iv output IV
*/
void get_iv_from_string(const char* input, uint8_t iv[16]);
void getIv(const void* data, size_t data_length, uint8_t iv[16]);
/**
* @brief Encrypt data.