moved logic to new library

This commit is contained in:
Nunu 2024-01-23 13:13:21 +01:00
parent 40a6b408de
commit ed70f529a2
5 changed files with 76 additions and 89 deletions

View File

@ -166,6 +166,7 @@ ifeq ($(ENABLE_MESSENGER),1)
endif endif
ifeq ($(ENABLE_ENCRYPTION),1) ifeq ($(ENABLE_ENCRYPTION),1)
OBJS += external/chacha/chacha.o OBJS += external/chacha/chacha.o
OBJS += helper/crypto.o
endif endif
ifeq ($(OS), Windows_NT) ifeq ($(OS), Windows_NT)

View File

@ -16,9 +16,8 @@
#include "app/messenger.h" #include "app/messenger.h"
#include "ui/ui.h" #include "ui/ui.h"
#ifdef ENABLE_ENCRYPTION #ifdef ENABLE_ENCRYPTION
#include "external/chacha/chacha.h" #include "helper/crypto.h"
#endif #endif
#include "debugging.h"
#if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG) #if defined(ENABLE_UART) && defined(ENABLE_UART_DEBUG)
#include "driver/uart.h" #include "driver/uart.h"
@ -65,22 +64,15 @@ uint8_t hasNewMessage = 0;
uint8_t keyTickCounter = 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,
unsigned char key[32] = { 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
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 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])); 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; if ( msgStatus != READY ) return;
@ -579,42 +571,10 @@ void MSG_Send(const char txMessage[TX_MSG_LENGTH], bool bServiceMessage) {
msgFSKBuffer[1] = 'S'; msgFSKBuffer[1] = 'S';
char encryptedTxMessage[TX_MSG_LENGTH]; 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)); memset(encryptedTxMessage, 0, sizeof(encryptedTxMessage));
chacha_keysetup(&ctx, key, 256);
/* initialize keystream and generate poly1305 key */ CRYPTO_Crypt(txMessage, TX_MSG_LENGTH, encryptedTxMessage, nonce, key, 256);
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)
// next 20 for msg // next 20 for msg
memcpy(msgFSKBuffer + 2, encryptedTxMessage, TX_MSG_LENGTH); memcpy(msgFSKBuffer + 2, encryptedTxMessage, TX_MSG_LENGTH);
@ -737,45 +697,11 @@ void MSG_StorePacket(const uint16_t interrupt_bits) {
} }
else else
{ {
unsigned char keystream[CHACHA_BLOCKLEN]; char dencryptedTxMessage[TX_MSG_LENGTH];
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)
CRYPTO_Crypt(&msgFSKBuffer[2], TX_MSG_LENGTH, dencryptedTxMessage, nonce, key, 256);
snprintf(rxMessage[3], TX_MSG_LENGTH + 2, "< %s", dencryptedTxMessage); snprintf(rxMessage[3], TX_MSG_LENGTH + 2, "< %s", dencryptedTxMessage);
// sprintf(rxMessage[3], "< %s", dencryptedTxMessage);
} }
#ifdef ENABLE_MESSENGER_UART #ifdef ENABLE_MESSENGER_UART

View File

@ -34,7 +34,7 @@ void MSG_EnableRX(const bool enable);
void MSG_StorePacket(const uint16_t interrupt_bits); void MSG_StorePacket(const uint16_t interrupt_bits);
void MSG_Init(); void MSG_Init();
void MSG_ProcessKeys(KEY_Code_t Key, bool bKeyPressed, bool bKeyHeld); 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 #endif

42
helper/crypto.c Normal file
View File

@ -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];
}
}

18
helper/crypto.h Normal file
View File

@ -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);