From ed70f529a295969595ba7fff34579e76d777c823 Mon Sep 17 00:00:00 2001 From: Nunu Date: Tue, 23 Jan 2024 13:13:21 +0100 Subject: [PATCH] moved logic to new library --- Makefile | 1 + app/messenger.c | 102 +++++++----------------------------------------- app/messenger.h | 2 +- helper/crypto.c | 42 ++++++++++++++++++++ helper/crypto.h | 18 +++++++++ 5 files changed, 76 insertions(+), 89 deletions(-) create mode 100644 helper/crypto.c create mode 100644 helper/crypto.h diff --git a/Makefile b/Makefile index 267ebb4..38d9c5f 100644 --- a/Makefile +++ b/Makefile @@ -166,6 +166,7 @@ ifeq ($(ENABLE_MESSENGER),1) endif ifeq ($(ENABLE_ENCRYPTION),1) OBJS += external/chacha/chacha.o + OBJS += helper/crypto.o endif ifeq ($(OS), Windows_NT) diff --git a/app/messenger.c b/app/messenger.c index f80c885..606814b 100644 --- a/app/messenger.c +++ b/app/messenger.c @@ -16,9 +16,8 @@ #include "app/messenger.h" #include "ui/ui.h" #ifdef ENABLE_ENCRYPTION - #include "external/chacha/chacha.h" + #include "helper/crypto.h" #endif -#include "debugging.h" #if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG) #include "driver/uart.h" @@ -65,22 +64,15 @@ uint8_t hasNewMessage = 0; uint8_t keyTickCounter = 0; -struct chacha_ctx ctx; - - unsigned char key[32] = { - 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, - 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f - }; - - unsigned char nonce[12] = { - 0x07, 0x00, 0x00, 0x00, - 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 - }; - - // unsigned char pt[114]; - - // unsigned char ct[114]; +unsigned char key[32] = { + 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, + 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f +}; +unsigned char nonce[12] = { + 0x07, 0x00, 0x00, 0x00, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 +}; // ----------------------------------------------------- @@ -560,7 +552,7 @@ void moveUP(char (*rxMessages)[MAX_RX_MSG_LENGTH + 2]) { memset(rxMessages[3], 0, sizeof(rxMessages[3])); } -void MSG_Send(const char txMessage[TX_MSG_LENGTH], bool bServiceMessage) { +void MSG_Send(char txMessage[TX_MSG_LENGTH], bool bServiceMessage) { if ( msgStatus != READY ) return; @@ -579,42 +571,10 @@ void MSG_Send(const char txMessage[TX_MSG_LENGTH], bool bServiceMessage) { msgFSKBuffer[1] = 'S'; char encryptedTxMessage[TX_MSG_LENGTH]; - // - unsigned char keystream[CHACHA_BLOCKLEN]; - // const unsigned char one[4] = { 1, 0, 0, 0 }; - - // init - memset(&ctx, 0, sizeof(ctx)); + memset(encryptedTxMessage, 0, sizeof(encryptedTxMessage)); - chacha_keysetup(&ctx, key, 256); - /* initialize keystream and generate poly1305 key */ - memset(keystream, 0, sizeof(keystream)); - chacha_ivsetup(&ctx, nonce, NULL); - chacha_encrypt_bytes(&ctx, keystream, keystream,sizeof(keystream)); - - /* crypt data */ - // // consider crypt-short version - // chacha_ivsetup(&ctx, nonce, one); - // chacha_encrypt_bytes(&ctx, (unsigned char *)pt, - // (unsigned char *)ct, 114); - - uint8_t i; - /* crypt data short*/ - // <--- use this as we have message short enough and this makes code much smaller - - // char String[40]; - for (i = 0; i < TX_MSG_LENGTH; i++) { - // sprintf(String, "i:%dc:%d\n", i, (u_int8_t)txMessage[i]); - // LogUart(String); - ((unsigned char *)encryptedTxMessage)[i] = - ((unsigned char *)txMessage)[i] ^ keystream[32 + i]; - // sprintf(String, "enc-> i:%dc:%d\n", i, (u_int8_t)encryptedTxMessage[i]); - // LogUart(String); - } - - - // chacha_ivsetup(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *counter) + CRYPTO_Crypt(txMessage, TX_MSG_LENGTH, encryptedTxMessage, nonce, key, 256); // next 20 for msg memcpy(msgFSKBuffer + 2, encryptedTxMessage, TX_MSG_LENGTH); @@ -737,45 +697,11 @@ void MSG_StorePacket(const uint16_t interrupt_bits) { } else { - unsigned char keystream[CHACHA_BLOCKLEN]; - char dencryptedTxMessage[TX_MSG_LENGTH]; - - // const unsigned char one[4] = { 1, 0, 0, 0 }; - - // init - memset(&ctx, 0, sizeof(ctx)); - chacha_keysetup(&ctx, key, 256); - - /* initialize keystream and generate poly1305 key */ - memset(keystream, 0, sizeof(keystream)); - chacha_ivsetup(&ctx, nonce, NULL); - chacha_encrypt_bytes(&ctx, keystream, keystream,sizeof(keystream)); - - /* crypt data */ - // // consider crypt-short version - // chacha_ivsetup(&ctx, nonce, one); - // chacha_encrypt_bytes(&ctx, (unsigned char *)pt, - // (unsigned char *)ct, 114); - - uint8_t i; - /* crypt data short*/ - // <--- use this as we have message short enough and this makes code much smaller - // char String[40]; - for (i = 0; i < TX_MSG_LENGTH; i++) { - // sprintf(String, "enc rec-> i:%dc:%d\n", i, (u_int8_t)msgFSKBuffer[i+2]); - // LogUart(String); - ((unsigned char *)dencryptedTxMessage)[i] = - ((unsigned char *)msgFSKBuffer)[i+2] ^ keystream[32 + i]; - // sprintf(String, "dec rec-> i:%dc:%d\n", i, (u_int8_t)dencryptedTxMessage[i]); - // LogUart(String); - } - - // chacha_ivsetup(struct chacha_ctx *x, const unsigned char *iv, const unsigned char *counter) - + char dencryptedTxMessage[TX_MSG_LENGTH]; + CRYPTO_Crypt(&msgFSKBuffer[2], TX_MSG_LENGTH, dencryptedTxMessage, nonce, key, 256); snprintf(rxMessage[3], TX_MSG_LENGTH + 2, "< %s", dencryptedTxMessage); - // sprintf(rxMessage[3], "< %s", dencryptedTxMessage); } #ifdef ENABLE_MESSENGER_UART diff --git a/app/messenger.h b/app/messenger.h index 47f55c7..034fe81 100644 --- a/app/messenger.h +++ b/app/messenger.h @@ -34,7 +34,7 @@ void MSG_EnableRX(const bool enable); void MSG_StorePacket(const uint16_t interrupt_bits); void MSG_Init(); void MSG_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld); -void MSG_Send(const char txMessage[TX_MSG_LENGTH], bool bServiceMessage); +void MSG_Send(char txMessage[TX_MSG_LENGTH], bool bServiceMessage); #endif diff --git a/helper/crypto.c b/helper/crypto.c new file mode 100644 index 0000000..5146bf7 --- /dev/null +++ b/helper/crypto.c @@ -0,0 +1,42 @@ +/* Copyright 2024 kamilsss655 + * https://github.com/kamilsss655 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "crypto.h" +#include "external/chacha/chacha.h" + +// Used for both encryption and decryption +void CRYPTO_Crypt(void *input, int input_len, void *output, void *nonce, const void *key, int key_len) +{ + struct chacha_ctx ctx; + unsigned char keystream[CHACHA_BLOCKLEN]; + char String[40]; + + memset(&ctx, 0, sizeof(ctx)); + chacha_keysetup(&ctx, key, key_len); + + // init keystream and generate key + memset(keystream, 0, sizeof(keystream)); + chacha_ivsetup(&ctx, nonce, NULL); + chacha_encrypt_bytes(&ctx, keystream, keystream,sizeof(keystream)); + + // crypt data, only works for input_len <= 32 + for (uint8_t i = 0; i < input_len; i++) { + ((unsigned char *)output)[i] = + ((unsigned char *)input)[i] ^ keystream[32 + i]; + } +} + + \ No newline at end of file diff --git a/helper/crypto.h b/helper/crypto.h new file mode 100644 index 0000000..748e0fe --- /dev/null +++ b/helper/crypto.h @@ -0,0 +1,18 @@ +/* Copyright 2024 kamilsss655 + * https://github.com/kamilsss655 + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Used for both encryption and decryption +void CRYPTO_Crypt(void *input, int input_len, void *output, void *nonce, const void *key, int key_len); \ No newline at end of file