Skip to content

Commit bbaa939

Browse files
fallbergtekka007
authored andcommitted
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.
1 parent 59b15f5 commit bbaa939

File tree

6 files changed

+79
-8
lines changed

6 files changed

+79
-8
lines changed

MyConfig.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,18 @@
817817
*/
818818
//#define MY_DEBUG_VERBOSE_RFM95
819819

820+
/**
821+
* @def MY_RFM95_ENABLE_ENCRYPTION
822+
* @brief Define this to enable software based %AES encryption.
823+
*
824+
* All nodes and gateway must have this enabled, and all must be personalized with the same %AES
825+
* key.
826+
* @see @ref personalization
827+
*
828+
* @warning This driver always sets the initialization vector to 0 so encryption is weak.
829+
*/
830+
//#define MY_RFM95_ENABLE_ENCRYPTION
831+
820832
/**
821833
* @def MY_RFM95_FREQUENCY
822834
* @brief The frequency to use.
@@ -1799,7 +1811,7 @@
17991811
/**
18001812
* @def MY_SIGNING_FEATURE
18011813
* @ingroup internals
1802-
* @brief Helper flag to indicate that some signing feature is enabled
1814+
* @brief Helper flag to indicate that some signing feature is enabled, set automatically
18031815
*/
18041816
#if defined(MY_SIGNING_ATSHA204) || defined(MY_SIGNING_SOFT)
18051817
#define MY_SIGNING_FEATURE
@@ -1812,7 +1824,7 @@
18121824
* @brief These options control encryption related configurations.
18131825
*
18141826
* Note that encryption is toggled on a per-radio basis.
1815-
* @see MY_RF24_ENABLE_ENCRYPTION, MY_RFM69_ENABLE_ENCRYPTION, MY_NRF5_ESB_ENABLE_ENCRYPTION
1827+
* @see MY_RF24_ENABLE_ENCRYPTION, MY_RFM69_ENABLE_ENCRYPTION, MY_NRF5_ESB_ENABLE_ENCRYPTION, MY_RFM95_ENABLE_ENCRYPTION
18161828
* @{
18171829
*/
18181830

@@ -1845,14 +1857,18 @@
18451857
#ifndef MY_NRF5_ESB_ENABLE_ENCRYPTION
18461858
#define MY_NRF5_ESB_ENABLE_ENCRYPTION
18471859
#endif
1860+
#ifndef MY_RFM95_ENABLE_ENCRYPTION
1861+
#define MY_RFM95_ENABLE_ENCRYPTION
1862+
#endif
18481863
#endif
18491864

18501865
/**
18511866
* @def MY_ENCRYPTION_FEATURE
18521867
* @ingroup internals
1853-
* @brief Helper flag to indicate that some encryption feature is enabled
1868+
* @brief Helper flag to indicate that some encryption feature is enabled, set automatically
1869+
* @see MY_RF24_ENABLE_ENCRYPTION, MY_RFM69_ENABLE_ENCRYPTION, MY_NRF5_ESB_ENABLE_ENCRYPTION, MY_RFM95_ENABLE_ENCRYPTION
18541870
*/
1855-
#if defined(MY_RF24_ENABLE_ENCRYPTION) || defined(MY_RFM69_ENABLE_ENCRYPTION) || defined(MY_NRF5_ESB_ENABLE_ENCRYPTION)
1871+
#if defined(MY_RF24_ENABLE_ENCRYPTION) || defined(MY_RFM69_ENABLE_ENCRYPTION) || defined(MY_NRF5_ESB_ENABLE_ENCRYPTION) || defined(MY_RFM95_ENABLE_ENCRYPTION)
18561872
#define MY_ENCRYPTION_FEATURE
18571873
#endif
18581874
/** @}*/ // End of EncryptionSettingGrpPub group
@@ -2303,6 +2319,7 @@
23032319
// RFM95
23042320
#define MY_RADIO_RFM95
23052321
#define MY_DEBUG_VERBOSE_RFM95
2322+
#define MY_RFM95_ENABLE_ENCRYPTION
23062323
#define MY_RFM95_ATC_MODE_DISABLED
23072324
#define MY_RFM95_RST_PIN
23082325
#define MY_RFM95_MODEM_CONFIGRUATION

configure

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ MySensors options:
8585
Enables RFM69 encryption.
8686
All nodes and gateway must have this enabled, and all must be
8787
personalized with the same AES key.
88+
--my-rfm95-encryption-enabled
89+
Enables RFM95 encryption.
90+
All nodes and gateway must have this enabled, and all must be
91+
personalized with the same AES key.
8892
--my-rs485-serial-port=<PORT>
8993
RS485 serial port. You must provide a port.
9094
--my-rs485-baudrate=<BAUD> RS485 baudrate. [9600]
@@ -450,6 +454,10 @@ for opt do
450454
encryption=true
451455
CPPFLAGS="-DMY_RFM69_ENABLE_ENCRYPTION $CPPFLAGS"
452456
;;
457+
--my-rfm95-encryption-enabled*)
458+
encryption=true
459+
CPPFLAGS="-DMY_RFM95_ENABLE_ENCRYPTION $CPPFLAGS"
460+
;;
453461
--my-rs485-serial-port=*)
454462
CPPFLAGS="-DMY_RS485_HWSERIAL=\\\"${optarg}\\\" $CPPFLAGS"
455463
;;

core/MyCapabilities.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,14 @@
218218
* @def MY_CAP_ENCR
219219
* @brief Indicate the encryption setting.
220220
*
221-
* @see MY_RF24_ENABLE_ENCRYPTION, MY_RFM69_ENABLE_ENCRYPTION, MY_NRF5_ESB_ENABLE_ENCRYPTION
221+
* @see MY_ENCRYPTION_FEATURE
222222
*
223223
* | Setting | Indicator
224224
* |------------|----------
225225
* | Enabled | X
226226
* | Disabled | -
227227
*/
228-
#if defined(MY_RF24_ENABLE_ENCRYPTION) || defined(MY_RFM69_ENABLE_ENCRYPTION) || defined (MY_NRF5_ESB_ENABLE_ENCRYPTION)
228+
#if defined(MY_ENCRYPTION_FEATURE)
229229
#define MY_CAP_ENCR "X"
230230
#else
231231
#define MY_CAP_ENCR "-"

hal/transport/RFM95/MyTransportRFM95.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,35 @@
1818
*/
1919

2020
#include "drivers/RFM95/RFM95.h"
21+
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
22+
#include "drivers/AES/AES.h"
23+
#endif
24+
25+
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
26+
AES RFM95_aes;
27+
uint8_t RFM95_dataenc[32] = {0};
28+
#endif
2129

2230
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
2331
#include "drivers/AES/AES.cpp"
2432
#endif
2533

2634
bool transportInit(void)
2735
{
36+
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
37+
uint8_t RFM95_psk[16];
38+
#ifdef MY_SIGNING_SIMPLE_PASSWD
39+
memset(RFM95_psk, 0, 16);
40+
memcpy(RFM95_psk, MY_SIGNING_SIMPLE_PASSWD, strnlen(MY_SIGNING_SIMPLE_PASSWD, 16));
41+
#else
42+
hwReadConfigBlock((void*)RFM95_psk, (void*)EEPROM_RF_ENCRYPTION_AES_KEY_ADDRESS, 16);
43+
#endif
44+
//set up AES-key
45+
RFM95_aes.set_key(RFM95_psk, 16);
46+
// Make sure it is purged from memory when set
47+
memset(RFM95_psk, 0, 16);
48+
#endif
49+
2850
const bool result = RFM95_initialise(MY_RFM95_FREQUENCY);
2951
#if defined(MY_RFM95_TCXO)
3052
RFM95_enableTCXO();
@@ -48,11 +70,26 @@ uint8_t transportGetAddress(void)
4870

4971
bool transportSend(const uint8_t to, const void *data, const uint8_t len, const bool noACK)
5072
{
73+
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
74+
// copy input data because it is read-only
75+
(void)memcpy(RFM95_dataenc,data,len);
76+
// has to be adjusted, WIP!
77+
RFM95_aes.set_IV(0);
78+
const uint8_t finalLength = len > 16 ? 32 : 16;
79+
//encrypt data
80+
RFM95_aes.cbc_encrypt(RFM95_dataenc, RFM95_dataenc, finalLength /16);
81+
if (noACK) {
82+
(void)RFM95_sendWithRetry(to, RFM95_dataenc, finalLength, 0, 0);
83+
return true;
84+
}
85+
return RFM95_sendWithRetry(to, RFM95_dataenc, finalLength);
86+
#else
5187
if (noACK) {
5288
(void)RFM95_sendWithRetry(to, data, len, 0, 0);
5389
return true;
5490
}
5591
return RFM95_sendWithRetry(to, data, len);
92+
#endif
5693
}
5794

5895
bool transportAvailable(void)
@@ -68,7 +105,15 @@ bool transportSanityCheck(void)
68105

69106
uint8_t transportReceive(void *data)
70107
{
71-
const uint8_t len = RFM95_receive((uint8_t *)data, MAX_MESSAGE_LENGTH);
108+
uint8_t len = RFM95_receive((uint8_t*)data, MAX_MESSAGE_LENGTH);
109+
#if defined(MY_RFM95_ENABLE_ENCRYPTION)
110+
// has to be adjusted, WIP!
111+
RFM95_aes.set_IV(0);
112+
// decrypt data
113+
if (RFM95_aes.cbc_decrypt((uint8_t*)(data), (uint8_t*)(data), len > 16 ? 2 : 1) != AES_SUCCESS) {
114+
len = 0;
115+
}
116+
#endif
72117
return len;
73118
}
74119

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ MY_RFM95_TCXO LITERAL1
192192
MY_RFM95_TX_POWER LITERAL1
193193
MY_RFM95_TX_POWER_DBM LITERAL1
194194
MY_RFM95_TX_TIMEOUT_MS LITERAL1
195+
MY_RFM95_ENABLE_ENCRYPTION LITERAL1
195196

196197
# RFM69
197198
MY_DEBUG_VERBOSE_RFM69 LITERAL1

tests/Arduino/sketches/hard_signing_whitelisting_full_debug_rfm95_rsa/hard_signing_whitelisting_full_debug_rfm95_rsa.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
#ifndef MY_SIGNING_ATSHA204_PIN
3434
#define MY_SIGNING_ATSHA204_PIN 17
3535
#endif
36-
#define MY_RFM69_ENABLE_ENCRYPTION
36+
#define MY_RFM95_ENABLE_ENCRYPTION
3737

3838
#include <MySensors.h>

0 commit comments

Comments
 (0)