[Crypto] Cppcheck fixes

This commit is contained in:
jgromes 2025-01-18 17:51:30 +01:00
parent cc9ce359b6
commit aab3e05386
2 changed files with 11 additions and 9 deletions

View file

@ -11,7 +11,7 @@ void RadioLibAES128::init(uint8_t* key) {
this->keyExpansion(this->roundKey, key); this->keyExpansion(this->roundKey, key);
} }
size_t RadioLibAES128::encryptECB(uint8_t* in, size_t len, uint8_t* out) { size_t RadioLibAES128::encryptECB(const uint8_t* in, size_t len, uint8_t* out) {
size_t num_blocks = len / RADIOLIB_AES128_BLOCK_SIZE; size_t num_blocks = len / RADIOLIB_AES128_BLOCK_SIZE;
if(len % RADIOLIB_AES128_BLOCK_SIZE) { if(len % RADIOLIB_AES128_BLOCK_SIZE) {
num_blocks++; num_blocks++;
@ -27,7 +27,7 @@ size_t RadioLibAES128::encryptECB(uint8_t* in, size_t len, uint8_t* out) {
return(num_blocks*RADIOLIB_AES128_BLOCK_SIZE); return(num_blocks*RADIOLIB_AES128_BLOCK_SIZE);
} }
size_t RadioLibAES128::decryptECB(uint8_t* in, size_t len, uint8_t* out) { size_t RadioLibAES128::decryptECB(const uint8_t* in, size_t len, uint8_t* out) {
size_t num_blocks = len / RADIOLIB_AES128_BLOCK_SIZE; size_t num_blocks = len / RADIOLIB_AES128_BLOCK_SIZE;
if(len % RADIOLIB_AES128_BLOCK_SIZE) { if(len % RADIOLIB_AES128_BLOCK_SIZE) {
num_blocks++; num_blocks++;
@ -43,7 +43,7 @@ size_t RadioLibAES128::decryptECB(uint8_t* in, size_t len, uint8_t* out) {
return(num_blocks*RADIOLIB_AES128_BLOCK_SIZE); return(num_blocks*RADIOLIB_AES128_BLOCK_SIZE);
} }
void RadioLibAES128::generateCMAC(uint8_t* in, size_t len, uint8_t* cmac) { void RadioLibAES128::generateCMAC(const uint8_t* in, size_t len, uint8_t* cmac) {
uint8_t key1[RADIOLIB_AES128_BLOCK_SIZE]; uint8_t key1[RADIOLIB_AES128_BLOCK_SIZE];
uint8_t key2[RADIOLIB_AES128_BLOCK_SIZE]; uint8_t key2[RADIOLIB_AES128_BLOCK_SIZE];
this->generateSubkeys(key1, key2); this->generateSubkeys(key1, key2);
@ -155,7 +155,8 @@ void RadioLibAES128::decipher(state_t* state, uint8_t* roundKey) {
void RadioLibAES128::subWord(uint8_t* word) { void RadioLibAES128::subWord(uint8_t* word) {
for(size_t i = 0; i < 4; i++) { for(size_t i = 0; i < 4; i++) {
word[i] = RADIOLIB_NONVOLATILE_READ_BYTE(&aesSbox[word[i]]); uint8_t* ptr = const_cast<uint8_t*>(&aesSbox[word[i]]);
word[i] = RADIOLIB_NONVOLATILE_READ_BYTE(ptr);
} }
} }
@ -198,7 +199,7 @@ void RadioLibAES128::generateSubkeys(uint8_t* key1, uint8_t* key2) {
0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00
}; };
uint8_t const_Rb[] = { const uint8_t const_Rb[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@ -221,7 +222,8 @@ void RadioLibAES128::generateSubkeys(uint8_t* key1, uint8_t* key2) {
void RadioLibAES128::subBytes(state_t* state, const uint8_t* box) { void RadioLibAES128::subBytes(state_t* state, const uint8_t* box) {
for(size_t row = 0; row < 4; row++) { for(size_t row = 0; row < 4; row++) {
for(size_t col = 0; col < 4; col++) { for(size_t col = 0; col < 4; col++) {
(*state)[col][row] = RADIOLIB_NONVOLATILE_READ_BYTE(&box[(*state)[col][row]]); uint8_t* ptr = const_cast<uint8_t*>(&box[(*state)[col][row]]);
(*state)[col][row] = RADIOLIB_NONVOLATILE_READ_BYTE(ptr);
} }
} }
} }

View file

@ -115,7 +115,7 @@ class RadioLibAES128 {
to ensure the buffer is sufficiently large to save the data! to ensure the buffer is sufficiently large to save the data!
\returns The number of bytes saved into the output buffer. \returns The number of bytes saved into the output buffer.
*/ */
size_t encryptECB(uint8_t* in, size_t len, uint8_t* out); size_t encryptECB(const uint8_t* in, size_t len, uint8_t* out);
/*! /*!
\brief Perform ECB-type AES decryption. \brief Perform ECB-type AES decryption.
@ -125,7 +125,7 @@ class RadioLibAES128 {
to ensure the buffer is sufficiently large to save the data! to ensure the buffer is sufficiently large to save the data!
\returns The number of bytes saved into the output buffer. \returns The number of bytes saved into the output buffer.
*/ */
size_t decryptECB(uint8_t* in, size_t len, uint8_t* out); size_t decryptECB(const uint8_t* in, size_t len, uint8_t* out);
/*! /*!
\brief Calculate message authentication code according to RFC4493. \brief Calculate message authentication code according to RFC4493.
@ -133,7 +133,7 @@ class RadioLibAES128 {
\param len Length of the input data. \param len Length of the input data.
\param cmac Buffer to save the output MAC into. The buffer must be at least 16 bytes long! \param cmac Buffer to save the output MAC into. The buffer must be at least 16 bytes long!
*/ */
void generateCMAC(uint8_t* in, size_t len, uint8_t* cmac); void generateCMAC(const uint8_t* in, size_t len, uint8_t* cmac);
/*! /*!
\brief Verify the received CMAC. This just calculates the CMAC again and compares the results. \brief Verify the received CMAC. This just calculates the CMAC again and compares the results.