replace delete in CryptoEngine.{cpp,h} with std::unique_ptr (#9649)

Is part of the unique_ptr modernization effort.
This commit is contained in:
Jorropo
2026-02-14 22:44:36 +01:00
committed by GitHub
co-authored by GitHub
parent 167bcf2863
commit b0bd3df226
2 changed files with 7 additions and 8 deletions
+5 -6
View File
@@ -1,6 +1,7 @@
#include "CryptoEngine.h" #include "CryptoEngine.h"
// #include "NodeDB.h" // #include "NodeDB.h"
#include "architecture.h" #include "architecture.h"
#include <memory>
#if !(MESHTASTIC_EXCLUDE_PKI) #if !(MESHTASTIC_EXCLUDE_PKI)
#include "NodeDB.h" #include "NodeDB.h"
@@ -169,10 +170,9 @@ void CryptoEngine::hash(uint8_t *bytes, size_t numBytes)
void CryptoEngine::aesSetKey(const uint8_t *key_bytes, size_t key_len) void CryptoEngine::aesSetKey(const uint8_t *key_bytes, size_t key_len)
{ {
delete aes;
aes = nullptr; aes = nullptr;
if (key_len != 0) { if (key_len != 0) {
aes = new AESSmall256(); aes = std::unique_ptr<AESSmall256>(new AESSmall256());
aes->setKey(key_bytes, key_len); aes->setKey(key_bytes, key_len);
} }
} }
@@ -231,12 +231,11 @@ void CryptoEngine::decrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes
// Generic implementation of AES-CTR encryption. // Generic implementation of AES-CTR encryption.
void CryptoEngine::encryptAESCtr(CryptoKey _key, uint8_t *_nonce, size_t numBytes, uint8_t *bytes) void CryptoEngine::encryptAESCtr(CryptoKey _key, uint8_t *_nonce, size_t numBytes, uint8_t *bytes)
{ {
delete ctr; std::unique_ptr<CTRCommon> ctr;
ctr = nullptr;
if (_key.length == 16) if (_key.length == 16)
ctr = new CTR<AES128>(); ctr = std::unique_ptr<CTRCommon>(new CTR<AES128>());
else else
ctr = new CTR<AES256>(); ctr = std::unique_ptr<CTRCommon>(new CTR<AES256>());
ctr->setKey(_key.bytes, _key.length); ctr->setKey(_key.bytes, _key.length);
static uint8_t scratch[MAX_BLOCKSIZE]; static uint8_t scratch[MAX_BLOCKSIZE];
memcpy(scratch, bytes, numBytes); memcpy(scratch, bytes, numBytes);
+2 -2
View File
@@ -5,6 +5,7 @@
#include "configuration.h" #include "configuration.h"
#include "mesh-pb-constants.h" #include "mesh-pb-constants.h"
#include <Arduino.h> #include <Arduino.h>
#include <memory>
extern concurrency::Lock *cryptLock; extern concurrency::Lock *cryptLock;
@@ -48,7 +49,7 @@ class CryptoEngine
virtual void aesSetKey(const uint8_t *key, size_t key_len); virtual void aesSetKey(const uint8_t *key, size_t key_len);
virtual void aesEncrypt(uint8_t *in, uint8_t *out); virtual void aesEncrypt(uint8_t *in, uint8_t *out);
AESSmall256 *aes = NULL; std::unique_ptr<AESSmall256> aes = nullptr;
#endif #endif
@@ -77,7 +78,6 @@ class CryptoEngine
/** Our per packet nonce */ /** Our per packet nonce */
uint8_t nonce[16] = {0}; uint8_t nonce[16] = {0};
CryptoKey key = {}; CryptoKey key = {};
CTRCommon *ctr = NULL;
#if !(MESHTASTIC_EXCLUDE_PKI) #if !(MESHTASTIC_EXCLUDE_PKI)
uint8_t shared_key[32] = {0}; uint8_t shared_key[32] = {0};
uint8_t private_key[32] = {0}; uint8_t private_key[32] = {0};