temp sync broken
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
|
||||
void sendGroupMessage (const GroupTextMessage *msg) {
|
||||
// Prepare values locally instead of modifying msg
|
||||
uint8_t channelHash = persistent.aesKeys[msg->keyIndex][0];
|
||||
Channel *channel = &(persistent.channels[msg->keyIndex]);
|
||||
uint8_t flags = 0;
|
||||
int32_t timestamp = RTC_GetCounter();
|
||||
|
||||
@@ -19,7 +19,7 @@ void sendGroupMessage (const GroupTextMessage *msg) {
|
||||
frame.header = ROUTE_TYPE_FLOOD | PAYLOAD_TYPE_GRP_TXT | PAYLOAD_VERSION_0;
|
||||
frame.path.pathLen = 0;
|
||||
size_t offset = 0;
|
||||
frame.payload[offset++] = channelHash;
|
||||
frame.payload[offset++] = channel->hash;
|
||||
|
||||
// Build encryption buffer directly on stack (no extra large buffer)
|
||||
uint8_t buf[180]; // enough for timestamp + flags + text
|
||||
@@ -41,10 +41,13 @@ void sendGroupMessage (const GroupTextMessage *msg) {
|
||||
|
||||
// Encrypt and MAC directly into frame payload after channelHash
|
||||
size_t olen = 0;
|
||||
encrypt_then_mac (&persistent.aesKeys[msg->keyIndex][1], 16, buf, buf_offset, &frame.payload[offset], &olen);
|
||||
|
||||
encrypt_then_mac (channel->key, 16, buf, buf_offset, &frame.payload[offset], &olen);
|
||||
|
||||
frame.payloadLen = olen + 1; // +1 for channelHash
|
||||
|
||||
channel->timestamp = timestamp;
|
||||
|
||||
LoRaTransmit (&frame);
|
||||
}
|
||||
|
||||
@@ -70,43 +73,35 @@ void decodeGroupMessage (const FrameStruct *frame) {
|
||||
unsigned char tmp[184];
|
||||
|
||||
|
||||
unsigned char decrypted = 0;
|
||||
for (unsigned char i = 0; i < AESKeyCount; i++) {
|
||||
Channel *channel = NULL;
|
||||
|
||||
if (msg.channelHash != persistent.aesKeys[i][0]) {
|
||||
MESH_LOGW (TAG, "Hash %d does not equal %d", persistent.aesKeys[i][0], msg.channelHash);
|
||||
uint8_t i = 0;
|
||||
do {
|
||||
channel = getChannel (msg.channelHash, i);
|
||||
if (channel == NULL) {
|
||||
MESH_LOGW (TAG, "Channel hash %d not found", msg.channelHash);
|
||||
return;
|
||||
}
|
||||
if (mac_then_decrypt (channel->key, 16, frame->payload + index, frame->payloadLen - index, tmp) != 0) {
|
||||
MESH_LOGW (TAG, "HMAC failed on grouphash key %d", msg.channelHash);
|
||||
i++;
|
||||
continue;
|
||||
} else {
|
||||
unsigned char plaintextLen = frame->payloadLen - index;
|
||||
index = 0;
|
||||
|
||||
memcpy (&msg.timestamp, tmp + index, 4);
|
||||
index += 4;
|
||||
msg.flags = tmp[index++];
|
||||
|
||||
memcpy (msg.text, tmp + index, plaintextLen - index);
|
||||
|
||||
channel->timestamp = RTC_GetCounter();
|
||||
|
||||
printf ("Message from channel %s: %s\n", channel->name, msg.text);
|
||||
break;
|
||||
}
|
||||
|
||||
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) {
|
||||
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);
|
||||
decrypted = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if (!decrypted) {
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char plaintextLen = frame->payloadLen - index;
|
||||
index = 0;
|
||||
|
||||
memcpy (&msg.timestamp, tmp + index, 4);
|
||||
index += 4;
|
||||
msg.flags = tmp[index++];
|
||||
|
||||
memcpy (msg.text, tmp + index, plaintextLen - index);
|
||||
|
||||
printGroupMessage (&msg);
|
||||
}
|
||||
|
||||
void printGroupMessage (const GroupTextMessage *msg) {
|
||||
printf ("Message with channel hash %d, flags %d: %s\n", msg->channelHash,
|
||||
msg->flags, msg->text);
|
||||
} while (channel != NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user