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
2634bool 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
4971bool 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
5895bool transportAvailable (void )
@@ -68,7 +105,15 @@ bool transportSanityCheck(void)
68105
69106uint8_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
0 commit comments