Skip to content

Commit 676c317

Browse files
author
Scott Powell
committed
* refactor: on-demand getSharedSecret()
1 parent 46f6146 commit 676c317

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

src/helpers/BaseChatMesh.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ int BaseChatMesh::searchPeersByHash(const uint8_t* hash) {
146146
void BaseChatMesh::getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) {
147147
int i = matching_peer_indexes[peer_idx];
148148
if (i >= 0 && i < num_contacts) {
149-
ensureSharedSecretIsValid(contacts[i]);
150-
memcpy(dest_secret, contacts[i].shared_secret, PUB_KEY_SIZE);
149+
memcpy(dest_secret, contacts[i].getSharedSecret(self_id), PUB_KEY_SIZE);
151150
} else {
152151
MESH_DEBUG_PRINTLN("getPeerSharedSecret: Invalid peer idx: %d", i);
153152
}
@@ -292,8 +291,7 @@ void BaseChatMesh::onAckRecv(mesh::Packet* packet, uint32_t ack_crc) {
292291
void BaseChatMesh::handleReturnPathRetry(const ContactInfo& contact, const uint8_t* path, uint8_t path_len) {
293292
// NOTE: simplest impl is just to re-send a reciprocal return path to sender (DIRECTLY)
294293
// override this method in various firmwares, if there's a better strategy
295-
ensureSharedSecretIsValid(contact);
296-
mesh::Packet* rpath = createPathReturn(contact.id, contact.shared_secret, path, path_len, 0, NULL, 0);
294+
mesh::Packet* rpath = createPathReturn(contact.id, contact.getSharedSecret(self_id), path, path_len, 0, NULL, 0);
297295
if (rpath) sendDirect(rpath, contact.out_path, contact.out_path_len, 3000); // 3 second delay
298296
}
299297

@@ -342,8 +340,7 @@ mesh::Packet* BaseChatMesh::composeMsgPacket(const ContactInfo& recipient, uint3
342340
temp[len++] = attempt; // hide attempt number at tail end of payload
343341
}
344342

345-
ensureSharedSecretIsValid(recipient);
346-
return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, len);
343+
return createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.getSharedSecret(self_id), temp, len);
347344
}
348345

349346
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
374371
temp[4] = (attempt & 3) | (TXT_TYPE_CLI_DATA << 2);
375372
memcpy(&temp[5], text, text_len + 1);
376373

377-
ensureSharedSecretIsValid(recipient);
378-
auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.shared_secret, temp, 5 + text_len);
374+
auto pkt = createDatagram(PAYLOAD_TYPE_TXT_MSG, recipient.id, recipient.getSharedSecret(self_id), temp, 5 + text_len);
379375
if (pkt == NULL) return MSG_SEND_FAILED;
380376

381377
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
@@ -464,8 +460,7 @@ int BaseChatMesh::sendLogin(const ContactInfo& recipient, const char* password,
464460
tlen = 4 + len;
465461
}
466462

467-
ensureSharedSecretIsValid(recipient);
468-
pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.shared_secret, temp, tlen);
463+
pkt = createAnonDatagram(PAYLOAD_TYPE_ANON_REQ, self_id, recipient.id, recipient.getSharedSecret(self_id), temp, tlen);
469464
}
470465
if (pkt) {
471466
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
@@ -492,8 +487,7 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, const uint8_t* req_
492487
memcpy(temp, &tag, 4); // mostly an extra blob to help make packet_hash unique
493488
memcpy(&temp[4], req_data, data_len);
494489

495-
ensureSharedSecretIsValid(recipient);
496-
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, 4 + data_len);
490+
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.getSharedSecret(self_id), temp, 4 + data_len);
497491
}
498492
if (pkt) {
499493
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
@@ -520,8 +514,7 @@ int BaseChatMesh::sendRequest(const ContactInfo& recipient, uint8_t req_type, u
520514
memset(&temp[5], 0, 4); // reserved (possibly for 'since' param)
521515
getRNG()->random(&temp[9], 4); // random blob to help make packet-hash unique
522516

523-
ensureSharedSecretIsValid(recipient);
524-
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.shared_secret, temp, sizeof(temp));
517+
pkt = createDatagram(PAYLOAD_TYPE_REQ, recipient.id, recipient.getSharedSecret(self_id), temp, sizeof(temp));
525518
}
526519
if (pkt) {
527520
uint32_t t = _radio->getEstAirtimeFor(pkt->getRawLength());
@@ -644,8 +637,7 @@ void BaseChatMesh::checkConnections() {
644637
// calc expected ACK reply
645638
mesh::Utils::sha256((uint8_t *)&connections[i].expected_ack, 4, data, 9, self_id.pub_key, PUB_KEY_SIZE);
646639

647-
ensureSharedSecretIsValid(*contact);
648-
auto pkt = createDatagram(PAYLOAD_TYPE_REQ, contact->id, contact->shared_secret, data, 9);
640+
auto pkt = createDatagram(PAYLOAD_TYPE_REQ, contact->id, contact->getSharedSecret(self_id), data, 9);
649641
if (pkt) {
650642
sendDirect(pkt, contact->out_path, contact->out_path_len);
651643
}
@@ -715,14 +707,6 @@ bool BaseChatMesh::addContact(const ContactInfo& contact) {
715707
return false;
716708
}
717709

718-
void BaseChatMesh::ensureSharedSecretIsValid(const ContactInfo& contact) {
719-
if (contact.shared_secret_valid) {
720-
return; // already calculated
721-
}
722-
self_id.calcSharedSecret(contact.shared_secret, contact.id);
723-
contact.shared_secret_valid = true;
724-
}
725-
726710
bool BaseChatMesh::removeContact(ContactInfo& contact) {
727711
int idx = 0;
728712
while (idx < num_contacts && !contacts[idx].id.matches(contact.id)) {

src/helpers/BaseChatMesh.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ class BaseChatMesh : public mesh::Mesh {
7373

7474
mesh::Packet* composeMsgPacket(const ContactInfo& recipient, uint32_t timestamp, uint8_t attempt, const char *text, uint32_t& expected_ack);
7575
void sendAckTo(const ContactInfo& dest, uint32_t ack_hash);
76-
void ensureSharedSecretIsValid(const ContactInfo& contact);
7776

7877
protected:
7978
BaseChatMesh(mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::PacketManager& mgr, mesh::MeshTables& tables)

src/helpers/ContactInfo.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@ struct ContactInfo {
1212
mutable bool shared_secret_valid; // flag to indicate if shared_secret has been calculated
1313
uint8_t out_path[MAX_PATH_SIZE];
1414
uint32_t last_advert_timestamp; // by THEIR clock
15-
mutable uint8_t shared_secret[PUB_KEY_SIZE];
1615
uint32_t lastmod; // by OUR clock
1716
int32_t gps_lat, gps_lon; // 6 dec places
1817
uint32_t sync_since;
18+
19+
const uint8_t* getSharedSecret(const mesh::LocalIdentity& self_id) const {
20+
if (!shared_secret_valid) {
21+
self_id.calcSharedSecret(shared_secret, id.pub_key);
22+
shared_secret_valid = true;
23+
}
24+
return shared_secret;
25+
}
26+
27+
private:
28+
mutable uint8_t shared_secret[PUB_KEY_SIZE];
1929
};

0 commit comments

Comments
 (0)