mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-04-20 22:13:47 +00:00
* refactor: on-demand getSharedSecret()
This commit is contained in:
parent
46f6146df7
commit
676c317f78
3 changed files with 19 additions and 26 deletions
|
|
@ -146,8 +146,7 @@ int BaseChatMesh::searchPeersByHash(const uint8_t* hash) {
|
|||
void BaseChatMesh::getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) {
|
||||
int i = matching_peer_indexes[peer_idx];
|
||||
if (i >= 0 && i < num_contacts) {
|
||||
ensureSharedSecretIsValid(contacts[i]);
|
||||
memcpy(dest_secret, contacts[i].shared_secret, PUB_KEY_SIZE);
|
||||
memcpy(dest_secret, contacts[i].getSharedSecret(self_id), PUB_KEY_SIZE);
|
||||
} else {
|
||||
MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i);
|
||||
}
|
||||
|
|
@ -292,8 +291,7 @@ void BaseChatMesh::onAckRecv(mesh::Packet* packet, uint32_t ack_crc) {
|
|||
void BaseChatMesh::handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len) {
|
||||
// NOTE: simplest impl is just to re-send a reciprocal return path to sender (DIRECTLY)
|
||||
// override this method in various firmwares, if there's a better strategy
|
||||
ensureSharedSecretIsValid(contact);
|
||||
mesh::Packet* rpath = createPathReturn(contact.id, contact.shared_secret, path, path_len, 0, NULL, 0);
|
||||
mesh::Packet* rpath = createPathReturn(contact.id, contact.getSharedSecret(self_id), path, path_len, 0, NULL, 0);
|
||||
if (rpath) sendDirect(rpath, contact.out_path, contact.out_path_len, 3000); // 3 second delay
|
||||
}
|
||||
|
||||
|
|
@ -342,8 +340,7 @@ mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint3
|
|||
temp[len++] = attempt; // hide attempt number at tail end of payload
|
||||
}
|
||||
|
||||
ensureSharedSecretIsValid(recipient);
|
||||
return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, len);
|
||||
return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.getSharedSecret(self_id), temp, len);
|
||||
}
|
||||
|
||||
int BaseChatMesh::sendMessage(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char* text, uint32_t& expected_ack, uint32_t& est_timeout) {
|
||||
|
|
@ -374,8 +371,7 @@ int BaseChatMesh::sendCommandData(const ContactInfo& recipient, uint32_t timest
|
|||
temp[4] = (attempt & 3) | (TXT_TYPE_CLI_DATA << 2);
|
||||
memcpy(&temp[5], text, text_len + 1);
|
||||
|
||||
ensureSharedSecretIsValid(recipient);
|
||||
auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, 5 + text_len);
|
||||
auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.getSharedSecret(self_id), temp, 5 + text_len);
|
||||
if (pkt == NULL) return MSG_SEND_FAILED;
|
||||
|
||||
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
|
||||
|
|
@ -464,8 +460,7 @@ int BaseChatMesh::sendLogin(const ContactInfo& recipient, const char* password,
|
|||
tlen = 4 + len;
|
||||
}
|
||||
|
||||
ensureSharedSecretIsValid(recipient);
|
||||
pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.shared_secret, temp, tlen);
|
||||
pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.getSharedSecret(self_id), temp, tlen);
|
||||
}
|
||||
if (pkt) {
|
||||
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
|
||||
|
|
@ -492,8 +487,7 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, const uint8_t* req_
|
|||
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
|
||||
memcpy(&temp[4], req_data, data_len);
|
||||
|
||||
ensureSharedSecretIsValid(recipient);
|
||||
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, 4 + data_len);
|
||||
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.getSharedSecret(self_id), temp, 4 + data_len);
|
||||
}
|
||||
if (pkt) {
|
||||
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
|
||||
|
|
@ -520,8 +514,7 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, uint8_t req_type, u
|
|||
memset(&temp[5], 0, 4); // reserved (possibly for 'since' param)
|
||||
getRNG()->random(&temp[9], 4); // random blob to help make packet-hash unique
|
||||
|
||||
ensureSharedSecretIsValid(recipient);
|
||||
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, sizeof(temp));
|
||||
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.getSharedSecret(self_id), temp, sizeof(temp));
|
||||
}
|
||||
if (pkt) {
|
||||
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
|
||||
|
|
@ -644,8 +637,7 @@ void BaseChatMesh::checkConnections() {
|
|||
// calc expected ACK reply
|
||||
mesh::Utils::sha256((uint8_t *)&connections[i].expected_ack, 4, data, 9, self_id.pub_key, PUB_KEY_SIZE);
|
||||
|
||||
ensureSharedSecretIsValid(*contact);
|
||||
auto pkt = createDatagram(PAYLOAD_TYPE_REQ, contact->id, contact->shared_secret, data, 9);
|
||||
auto pkt = createDatagram(PAYLOAD_TYPE_REQ, contact->id, contact->getSharedSecret(self_id), data, 9);
|
||||
if (pkt) {
|
||||
sendDirect(pkt, contact->out_path, contact->out_path_len);
|
||||
}
|
||||
|
|
@ -715,14 +707,6 @@ bool BaseChatMesh::addContact(const ContactInfo& contact) {
|
|||
return false;
|
||||
}
|
||||
|
||||
void BaseChatMesh::ensureSharedSecretIsValid(const ContactInfo& contact) {
|
||||
if (contact.shared_secret_valid) {
|
||||
return; // already calculated
|
||||
}
|
||||
self_id.calcSharedSecret(contact.shared_secret, contact.id);
|
||||
contact.shared_secret_valid = true;
|
||||
}
|
||||
|
||||
bool BaseChatMesh::removeContact(ContactInfo& contact) {
|
||||
int idx = 0;
|
||||
while (idx < num_contacts && !contacts[idx].id.matches(contact.id)) {
|
||||
|
|
|
|||
|
|
@ -73,7 +73,6 @@ class BaseChatMesh : public mesh::Mesh {
|
|||
|
||||
mesh::Packet* composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack);
|
||||
void sendAckTo(const ContactInfo& dest, uint32_t ack_hash);
|
||||
void ensureSharedSecretIsValid(const ContactInfo& contact);
|
||||
|
||||
protected:
|
||||
BaseChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::PacketManager& mgr, mesh::MeshTables& tables)
|
||||
|
|
|
|||
|
|
@ -12,8 +12,18 @@ struct ContactInfo {
|
|||
mutable bool shared_secret_valid; // flag to indicate if shared_secret has been calculated
|
||||
uint8_t out_path[MAX_PATH_SIZE];
|
||||
uint32_t last_advert_timestamp; // by THEIR clock
|
||||
mutable uint8_t shared_secret[PUB_KEY_SIZE];
|
||||
uint32_t lastmod; // by OUR clock
|
||||
int32_t gps_lat, gps_lon; // 6 dec places
|
||||
uint32_t sync_since;
|
||||
|
||||
const uint8_t* getSharedSecret(const mesh::LocalIdentity& self_id) const {
|
||||
if (!shared_secret_valid) {
|
||||
self_id.calcSharedSecret(shared_secret, id.pub_key);
|
||||
shared_secret_valid = true;
|
||||
}
|
||||
return shared_secret;
|
||||
}
|
||||
|
||||
private:
|
||||
mutable uint8_t shared_secret[PUB_KEY_SIZE];
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue