From 229806da0f99fa187115d7a30de85def60ef5999 Mon Sep 17 00:00:00 2001 From: Nunu Date: Wed, 31 Jan 2024 18:11:44 +0100 Subject: [PATCH] fix enc key initialization after boot --- app/app.c | 10 ++++++++++ app/messenger.c | 3 +++ helper/crypto.c | 2 ++ helper/crypto.h | 6 +++--- settings.c | 3 +-- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/app.c b/app/app.c index fc154a75e..2dfc01bf3 100644 --- a/app/app.c +++ b/app/app.c @@ -65,6 +65,9 @@ #ifdef ENABLE_MESSENGER #include "app/messenger.h" #endif +#ifdef ENABLE_ENCRYPTION + #include "helper/crypto.h" +#endif #ifdef ENABLE_MESSENGER_NOTIFICATION bool gPlayMSGRing = false; @@ -1309,6 +1312,13 @@ void APP_TimeSlice500ms(void) } else if (hasNewMessage == 2) { hasNewMessage = 1; } + } + #endif + + #ifdef ENABLE_ENCRYPTION + if(gRecalculateEncKey){ + CRYPTO_Generate256BitKey(gEeprom.ENC_KEY, gEncryptionKey, sizeof(gEeprom.ENC_KEY)); + gRecalculateEncKey = false; } #endif diff --git a/app/messenger.c b/app/messenger.c index 69bdbae27..b17416c13 100644 --- a/app/messenger.c +++ b/app/messenger.c @@ -682,6 +682,9 @@ void MSG_Init() { prevKey = 0; prevLetter = 0; cIndex = 0; + #ifdef ENABLE_ENCRYPTION + gRecalculateEncKey = true; + #endif } void MSG_SendAck() { diff --git a/helper/crypto.c b/helper/crypto.c index ddf905798..3eeea30fa 100644 --- a/helper/crypto.c +++ b/helper/crypto.c @@ -19,6 +19,8 @@ #include "driver/bk4819.h" #include "driver/systick.h" +bool gRecalculateEncKey; + u_int8_t gEncryptionKey[32]; // salt used for hashing encryption key from eeprom used for sending packets diff --git a/helper/crypto.h b/helper/crypto.h index 55f8d6c82..c7de28174 100644 --- a/helper/crypto.h +++ b/helper/crypto.h @@ -13,10 +13,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#include #include - -extern uint8_t gEncryptionKey[32]; +extern bool gRecalculateEncKey; +extern uint8_t gEncryptionKey[32]; static const uint8_t encryptionSalt[4][8]; static const uint8_t displaySalt[32]; diff --git a/settings.c b/settings.c index 01470c13c..6ffca7354 100644 --- a/settings.c +++ b/settings.c @@ -277,8 +277,7 @@ void SETTINGS_SaveEncryptionKey() { EEPROM_WriteBuffer(0x0F30, gEeprom.ENC_KEY, true); EEPROM_WriteBuffer(0x0F38, gEeprom.ENC_KEY + 8, true); - - CRYPTO_Generate256BitKey(gEeprom.ENC_KEY, gEncryptionKey, sizeof(gEeprom.ENC_KEY)); + gRecalculateEncKey = true; } #endif