mirror of
https://github.com/meshtastic/firmware.git
synced 2025-08-22 13:08:06 +00:00
Fix (some ?) memory alignment issues on the crypto part - resulting in crashes or strange behavior (#4867)
* Replace multiple potentially non aligned pointer dereference (#4855) First step to fix some Crypto crashes or strange behaviors * Makes the two Crypto byte buffers aligned (#4855) Fix #4855, and probably multiple Crypto problems depending on hardware --------- Co-authored-by: Ben Meadors <benmmeadors@gmail.com> Co-authored-by: GUVWAF <78759985+GUVWAF@users.noreply.github.com>
This commit is contained in:
parent
8f84a96b69
commit
4794cdb120
@ -70,8 +70,8 @@ bool CryptoEngine::encryptCurve25519(uint32_t toNode, uint32_t fromNode, uint64_
|
|||||||
long extraNonceTmp = random();
|
long extraNonceTmp = random();
|
||||||
auth = bytesOut + numBytes;
|
auth = bytesOut + numBytes;
|
||||||
extraNonce = (uint32_t *)(auth + 8);
|
extraNonce = (uint32_t *)(auth + 8);
|
||||||
*extraNonce = extraNonceTmp;
|
memcpy(extraNonce, &extraNonceTmp, 4); // do not use dereference on potential non aligned pointers : *extraNonce = extraNonceTmp;
|
||||||
LOG_INFO("Random nonce value: %d\n", *extraNonce);
|
LOG_INFO("Random nonce value: %d\n", extraNonceTmp);
|
||||||
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(toNode);
|
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(toNode);
|
||||||
if (node->num < 1 || node->user.public_key.size == 0) {
|
if (node->num < 1 || node->user.public_key.size == 0) {
|
||||||
LOG_DEBUG("Node %d or their public_key not found\n", toNode);
|
LOG_DEBUG("Node %d or their public_key not found\n", toNode);
|
||||||
@ -80,14 +80,14 @@ bool CryptoEngine::encryptCurve25519(uint32_t toNode, uint32_t fromNode, uint64_
|
|||||||
if (!crypto->setDHKey(toNode)) {
|
if (!crypto->setDHKey(toNode)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
initNonce(fromNode, packetNum, *extraNonce);
|
initNonce(fromNode, packetNum, extraNonceTmp);
|
||||||
|
|
||||||
// Calculate the shared secret with the destination node and encrypt
|
// Calculate the shared secret with the destination node and encrypt
|
||||||
printBytes("Attempting encrypt using nonce: ", nonce, 13);
|
printBytes("Attempting encrypt using nonce: ", nonce, 13);
|
||||||
printBytes("Attempting encrypt using shared_key starting with: ", shared_key, 8);
|
printBytes("Attempting encrypt using shared_key starting with: ", shared_key, 8);
|
||||||
aes_ccm_ae(shared_key, 32, nonce, 8, bytes, numBytes, nullptr, 0, bytesOut,
|
aes_ccm_ae(shared_key, 32, nonce, 8, bytes, numBytes, nullptr, 0, bytesOut,
|
||||||
auth); // this can write up to 15 bytes longer than numbytes past bytesOut
|
auth); // this can write up to 15 bytes longer than numbytes past bytesOut
|
||||||
*extraNonce = extraNonceTmp;
|
memcpy(extraNonce, &extraNonceTmp, 4); // do not use dereference on potential non aligned pointers : *extraNonce = extraNonceTmp;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,12 +100,12 @@ bool CryptoEngine::encryptCurve25519(uint32_t toNode, uint32_t fromNode, uint64_
|
|||||||
bool CryptoEngine::decryptCurve25519(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes, uint8_t *bytesOut)
|
bool CryptoEngine::decryptCurve25519(uint32_t fromNode, uint64_t packetNum, size_t numBytes, uint8_t *bytes, uint8_t *bytesOut)
|
||||||
{
|
{
|
||||||
uint8_t *auth; // set to last 8 bytes of text?
|
uint8_t *auth; // set to last 8 bytes of text?
|
||||||
uint32_t *extraNonce;
|
uint32_t extraNonce; // pointer was not really used
|
||||||
auth = bytes + numBytes - 12;
|
auth = bytes + numBytes - 12;
|
||||||
extraNonce = (uint32_t *)(auth + 8);
|
memcpy(&extraNonce, auth +8, 4); // do not use dereference on potential non aligned pointers : (uint32_t *)(auth + 8);
|
||||||
LOG_INFO("Random nonce value: %d\n", *extraNonce);
|
LOG_INFO("Random nonce value: %d\n", extraNonce);
|
||||||
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(fromNode);
|
meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(fromNode);
|
||||||
|
|
||||||
if (node == nullptr || node->num < 1 || node->user.public_key.size == 0) {
|
if (node == nullptr || node->num < 1 || node->user.public_key.size == 0) {
|
||||||
LOG_DEBUG("Node or its public key not found in database\n");
|
LOG_DEBUG("Node or its public key not found in database\n");
|
||||||
return false;
|
return false;
|
||||||
@ -115,7 +115,7 @@ bool CryptoEngine::decryptCurve25519(uint32_t fromNode, uint64_t packetNum, size
|
|||||||
if (!crypto->setDHKey(fromNode)) {
|
if (!crypto->setDHKey(fromNode)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
initNonce(fromNode, packetNum, *extraNonce);
|
initNonce(fromNode, packetNum, extraNonce);
|
||||||
printBytes("Attempting decrypt using nonce: ", nonce, 13);
|
printBytes("Attempting decrypt using nonce: ", nonce, 13);
|
||||||
printBytes("Attempting decrypt using shared_key starting with: ", shared_key, 8);
|
printBytes("Attempting decrypt using shared_key starting with: ", shared_key, 8);
|
||||||
return aes_ccm_ad(shared_key, 32, nonce, 8, bytes, numBytes - 12, nullptr, 0, auth, bytesOut);
|
return aes_ccm_ad(shared_key, 32, nonce, 8, bytes, numBytes - 12, nullptr, 0, auth, bytesOut);
|
||||||
|
@ -36,8 +36,8 @@ static MemoryDynamic<meshtastic_MeshPacket> staticPool;
|
|||||||
|
|
||||||
Allocator<meshtastic_MeshPacket> &packetPool = staticPool;
|
Allocator<meshtastic_MeshPacket> &packetPool = staticPool;
|
||||||
|
|
||||||
static uint8_t bytes[MAX_LORA_PAYLOAD_LEN + 1];
|
static uint8_t bytes[MAX_LORA_PAYLOAD_LEN + 1] __attribute__ ((__aligned__));
|
||||||
static uint8_t ScratchEncrypted[MAX_LORA_PAYLOAD_LEN + 1];
|
static uint8_t ScratchEncrypted[MAX_LORA_PAYLOAD_LEN + 1] __attribute__ ((__aligned__));
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor
|
||||||
|
Loading…
Reference in New Issue
Block a user