Add software AES encryption to RFM95 transport HAL (#1054)
Also clean up capabilities flags a bit and fix test-sketch for rfm95 to use proper encryption flag.
This commit is contained in:
@@ -18,6 +18,14 @@
|
||||
*/
|
||||
|
||||
#include "drivers/RFM95/RFM95.h"
|
||||
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
|
||||
#include "drivers/AES/AES.h"
|
||||
#endif
|
||||
|
||||
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
|
||||
AES RFM95_aes;
|
||||
uint8_t RFM95_dataenc[32] = {0};
|
||||
#endif
|
||||
|
||||
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
|
||||
#include "drivers/AES/AES.cpp"
|
||||
@@ -25,6 +33,20 @@
|
||||
|
||||
bool transportInit(void)
|
||||
{
|
||||
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
|
||||
uint8_t RFM95_psk[16];
|
||||
#ifdef MY_SIGNING_SIMPLE_PASSWD
|
||||
memset(RFM95_psk, 0, 16);
|
||||
memcpy(RFM95_psk, MY_SIGNING_SIMPLE_PASSWD, strnlen(MY_SIGNING_SIMPLE_PASSWD, 16));
|
||||
#else
|
||||
hwReadConfigBlock((void*)RFM95_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
|
||||
#endif
|
||||
//set up AES-key
|
||||
RFM95_aes.set_key(RFM95_psk, 16);
|
||||
// Make sure it is purged from memory when set
|
||||
memset(RFM95_psk, 0, 16);
|
||||
#endif
|
||||
|
||||
const bool result = RFM95_initialise(MY_RFM95_FREQUENCY);
|
||||
#if defined(MY_RFM95_TCXO)
|
||||
RFM95_enableTCXO();
|
||||
@@ -48,11 +70,26 @@ uint8_t transportGetAddress(void)
|
||||
|
||||
bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
|
||||
{
|
||||
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
|
||||
// copy input data because it is read-only
|
||||
(void)memcpy(RFM95_dataenc,data,len);
|
||||
// has to be adjusted, WIP!
|
||||
RFM95_aes.set_IV(0);
|
||||
const uint8_t finalLength = len > 16 ? 32 : 16;
|
||||
//encrypt data
|
||||
RFM95_aes.cbc_encrypt(RFM95_dataenc, RFM95_dataenc, finalLength /16);
|
||||
if (noACK) {
|
||||
(void)RFM95_sendWithRetry(to, RFM95_dataenc, finalLength, 0, 0);
|
||||
return true;
|
||||
}
|
||||
return RFM95_sendWithRetry(to, RFM95_dataenc, finalLength);
|
||||
#else
|
||||
if (noACK) {
|
||||
(void)RFM95_sendWithRetry(to, data, len, 0, 0);
|
||||
return true;
|
||||
}
|
||||
return RFM95_sendWithRetry(to, data, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool transportAvailable(void)
|
||||
@@ -68,7 +105,15 @@ bool transportSanityCheck(void)
|
||||
|
||||
uint8_t transportReceive(void *data)
|
||||
{
|
||||
const uint8_t len = RFM95_receive((uint8_t *)data, MAX_MESSAGE_LENGTH);
|
||||
uint8_t len = RFM95_receive((uint8_t*)data, MAX_MESSAGE_LENGTH);
|
||||
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
|
||||
// has to be adjusted, WIP!
|
||||
RFM95_aes.set_IV(0);
|
||||
// decrypt data
|
||||
if (RFM95_aes.cbc_decrypt((uint8_t*)(data), (uint8_t*)(data), len > 16 ? 2 : 1) != AES_SUCCESS) {
|
||||
len = 0;
|
||||
}
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user