really still wip

This commit is contained in:
2025-12-28 12:23:11 +01:00
parent a48ef9d5e0
commit 31dda62474
33 changed files with 1721 additions and 589 deletions

View File

@@ -9,61 +9,64 @@
#define TAG "GroupMessage"
void sendGroupMessage (GroupTextMessage msg) {
msg.channelHash = persistent.aesKeys[msg.keyIndex][0];
msg.flags = 0;
msg.timestamp = RTC_GetCounter();
void sendGroupMessage (const GroupTextMessage *msg) {
// Prepare values locally instead of modifying msg
uint8_t channelHash = persistent.aesKeys[msg->keyIndex][0];
uint8_t flags = 0;
int32_t timestamp = RTC_GetCounter();
FrameStruct frame;
frame.header = ROUTE_TYPE_FLOOD | PAYLOAD_TYPE_GRP_TXT | PAYLOAD_VERSION_0;
frame.path.pathLen = 0;
size_t offset = 0;
memset (frame.payload, 0, sizeof (frame.payload));
frame.payload[offset++] = msg.channelHash;
frame.payload[offset++] = channelHash;
uint8_t cipherBuf[176];
size_t offset2 = 0;
memcpy (cipherBuf, (const void *)&(msg.timestamp), 4);
offset2 += 4;
cipherBuf[offset2++] = msg.flags;
size_t textSize = offset2 + strlen ((const char *)msg.text);
if (textSize > 175) {
textSize = 175;
// Build encryption buffer directly on stack (no extra large buffer)
uint8_t buf[180]; // enough for timestamp + flags + text
size_t buf_offset = 0;
memcpy (buf + buf_offset, &timestamp, sizeof (timestamp));
buf_offset += sizeof (timestamp);
buf[buf_offset++] = flags;
size_t textLen = strlen ((const char *)msg->text);
if (buf_offset + textLen > sizeof (buf)) {
textLen = sizeof (buf) - buf_offset;
}
memcpy (cipherBuf + offset2, msg.text, textSize);
offset2 += textSize;
memcpy (buf + buf_offset, msg->text, textLen);
buf_offset += textLen;
hexdump ("TxDumpDec", buf, buf_offset);
// Encrypt and MAC directly into frame payload after channelHash
size_t olen = 0;
hexdump ("TxDumpDec", cipherBuf, offset2);
encrypt_then_mac (&(persistent.aesKeys[msg.keyIndex][1]), 16, cipherBuf, offset2, &(frame.payload[offset]), &olen);
encrypt_then_mac (&persistent.aesKeys[msg->keyIndex][1], 16, buf, buf_offset, &frame.payload[offset], &olen);
frame.payloadLen = olen + 1;
frame.payloadLen = olen + 1; // +1 for channelHash
sendFrame (frame);
return;
LoRaTransmit (&frame);
}
void makeSendGroupMessage (char *txt, uint8_t keyIndex) {
GroupTextMessage msg;
strcpy((char *) msg.text, persistent.nodeName);
strcpy ((char *)msg.text, persistent.nodeName);
strcat ((char *)msg.text, ": ");
strcat ((char *)msg.text, txt);
msg.keyIndex = keyIndex;
sendGroupMessage (msg);
sendGroupMessage (&msg);
return;
}
GroupTextMessage decodeGroupMessage (FrameStruct frame) {
void decodeGroupMessage (const FrameStruct *frame) {
GroupTextMessage msg;
memset (&msg, 0, sizeof (msg));
if ((frame.header & PAYLOAD_TYPE_MASK) != PAYLOAD_TYPE_GRP_TXT) {
ESP_LOGW (TAG, "Not a group text");
return msg;
if ((frame->header & PAYLOAD_TYPE_MASK) != PAYLOAD_TYPE_GRP_TXT) {
MESH_LOGW (TAG, "Not a group text");
return;
}
unsigned char index = 0;
msg.channelHash = frame.payload[index++];
msg.channelHash = frame->payload[index++];
unsigned char tmp[184];
@@ -71,27 +74,27 @@ GroupTextMessage decodeGroupMessage (FrameStruct frame) {
for (unsigned char i = 0; i < AESKeyCount; i++) {
if (msg.channelHash != persistent.aesKeys[i][0]) {
ESP_LOGW (TAG, "Hash %d does not equal %d", persistent.aesKeys[i][0], msg.channelHash);
MESH_LOGW (TAG, "Hash %d does not equal %d", persistent.aesKeys[i][0], msg.channelHash);
continue;
}
ESP_LOGW (TAG, "Hash does equal %d", msg.channelHash);
MESH_LOGW (TAG, "Hash does equal %d", msg.channelHash);
if (mac_then_decrypt (persistent.aesKeys[i] + 1, 16, frame.payload + index, frame.payloadLen - index, tmp) != 0) {
ESP_LOGW (TAG, "HMAC failed on grouphash key %d not matching %d", persistent.aesKeys[i][0], msg.channelHash);
if (mac_then_decrypt (persistent.aesKeys[i] + 1, 16, frame->payload + index, frame->payloadLen - index, tmp) != 0) {
MESH_LOGW (TAG, "HMAC failed on grouphash key %d not matching %d", persistent.aesKeys[i][0], msg.channelHash);
continue;
}
hexdump ("RxDumpDec", tmp, frame.payloadLen - index);
hexdump ("RxDumpDec", tmp, frame->payloadLen - index);
decrypted = 1;
break;
}
if (!decrypted) {
return msg;
return;
}
unsigned char plaintextLen = frame.payloadLen - index;
unsigned char plaintextLen = frame->payloadLen - index;
index = 0;
memcpy (&msg.timestamp, tmp + index, 4);
@@ -99,10 +102,11 @@ GroupTextMessage decodeGroupMessage (FrameStruct frame) {
msg.flags = tmp[index++];
memcpy (msg.text, tmp + index, plaintextLen - index);
return msg;
printGroupMessage (&msg);
}
void printGroupMessage (GroupTextMessage msg) {
printf ("Message with channel hash %d, flags %d: %s\n", msg.channelHash,
msg.flags, msg.text);
void printGroupMessage (const GroupTextMessage *msg) {
printf ("Message with channel hash %d, flags %d: %s\n", msg->channelHash,
msg->flags, msg->text);
}