@@ -256,8 +256,8 @@ void SensorMesh::sendAlert(const ClientInfo* c, Trigger* t) {
256256 mesh::Utils::sha256 ((uint8_t *)&t->expected_acks [t->attempt ], 4 , data, 5 + text_len, self_id.pub_key , PUB_KEY_SIZE );
257257 t->attempt ++;
258258
259- bool use_v2 = c->supports_chacha ;
260- auto pkt = createDatagram (PAYLOAD_TYPE_TXT_MSG , c->id , c->shared_secret , data, 5 + text_len, use_v2 );
259+ bool use_chacha = c->supports_chacha ;
260+ auto pkt = createDatagram (PAYLOAD_TYPE_TXT_MSG , c->id , c->shared_secret , data, 5 + text_len, use_chacha );
261261 if (pkt) {
262262 if (c->out_path_len >= 0 ) { // we have an out_path, so send DIRECT
263263 sendDirect (pkt, c->out_path , c->out_path_len );
@@ -448,7 +448,7 @@ void SensorMesh::handleCommand(uint32_t sender_timestamp, char* command, char* r
448448 }
449449}
450450
451- void SensorMesh::onAnonDataRecv (mesh::Packet* packet, const uint8_t * secret, const mesh::Identity& sender, uint8_t * data, size_t len) {
451+ void SensorMesh::onAnonDataRecv (mesh::Packet* packet, const uint8_t * secret, const mesh::Identity& sender, uint8_t * data, size_t len, bool decrypted_with_chacha ) {
452452 if (packet->getPayloadType () == PAYLOAD_TYPE_ANON_REQ ) { // received an initial request by a possible admin client (unknown at this stage)
453453 uint32_t timestamp;
454454 memcpy (×tamp, data, 4 );
@@ -465,14 +465,17 @@ void SensorMesh::onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, con
465465
466466 if (reply_len == 0 ) return ; // invalid request
467467
468- bool use_v2 = (packet->getPayloadVer () == PAYLOAD_VER_2 );
468+ // Mark peer as ChaCha-capable if we decoded their packet with ChaCha
469+ if (decrypted_with_chacha) setPeerSupportsCHACHA (&sender, -1 );
470+
471+ bool use_chacha = peerSupportsCHACHA (sender);
469472 if (packet->isRouteFlood ()) {
470473 // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
471474 mesh::Packet* path = createPathReturn (sender, secret, packet->path , packet->path_len ,
472- PAYLOAD_TYPE_RESPONSE , reply_data, reply_len, use_v2 );
475+ PAYLOAD_TYPE_RESPONSE , reply_data, reply_len, use_chacha );
473476 if (path) sendFlood (path, SERVER_RESPONSE_DELAY );
474477 } else {
475- mesh::Packet* reply = createDatagram (PAYLOAD_TYPE_RESPONSE , sender, secret, reply_data, reply_len, use_v2 );
478+ mesh::Packet* reply = createDatagram (PAYLOAD_TYPE_RESPONSE , sender, secret, reply_data, reply_len, use_chacha );
476479 if (reply) sendFlood (reply, SERVER_RESPONSE_DELAY );
477480 }
478481 }
@@ -504,10 +507,24 @@ bool SensorMesh::peerSupportsCHACHA(const mesh::Identity& dest) {
504507 if (client) {
505508 return client->supports_chacha ;
506509 }
507- // Unknown peer - default to v1 (safe fallback)
510+ // Unknown peer - default to AES (safe fallback)
508511 return false ;
509512}
510513
514+ void SensorMesh::setPeerSupportsCHACHA (const mesh::Identity* id, int peer_idx) {
515+ if (peer_idx >= 0 ) {
516+ int i = matching_peer_indexes[peer_idx];
517+ if (i >= 0 && i < acl.getNumClients ()) {
518+ acl.getClientByIdx (i)->supports_chacha = true ;
519+ }
520+ } else if (id != nullptr ) {
521+ ClientInfo* client = acl.getClient (id->pub_key , PUB_KEY_SIZE );
522+ if (client) {
523+ client->supports_chacha = true ;
524+ }
525+ }
526+ }
527+
511528void SensorMesh::onAdvertRecv (mesh::Packet *packet, const mesh::Identity &id, uint32_t timestamp,
512529 const uint8_t *app_data, size_t app_data_len) {
513530 mesh::Mesh::onAdvertRecv (packet, id, timestamp, app_data, app_data_len); // chain to super impl
@@ -559,14 +576,14 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
559576 from->last_timestamp = timestamp;
560577 from->last_activity = getRTCClock ()->getCurrentTime ();
561578
562- bool use_v2 = (packet-> getPayloadVer () == PAYLOAD_VER_2 ) ;
579+ bool use_chacha = from-> supports_chacha ;
563580 if (packet->isRouteFlood ()) {
564581 // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the response
565582 mesh::Packet* path = createPathReturn (from->id , secret, packet->path , packet->path_len ,
566- PAYLOAD_TYPE_RESPONSE , reply_data, reply_len, use_v2 );
583+ PAYLOAD_TYPE_RESPONSE , reply_data, reply_len, use_chacha );
567584 if (path) sendFlood (path, SERVER_RESPONSE_DELAY );
568585 } else {
569- mesh::Packet* reply = createDatagram (PAYLOAD_TYPE_RESPONSE , from->id , secret, reply_data, reply_len, use_v2 );
586+ mesh::Packet* reply = createDatagram (PAYLOAD_TYPE_RESPONSE , from->id , secret, reply_data, reply_len, use_chacha );
570587 if (reply) {
571588 if (from->out_path_len >= 0 ) { // we have an out_path, so send DIRECT
572589 sendDirect (reply, from->out_path , from->out_path_len , SERVER_RESPONSE_DELAY );
@@ -592,9 +609,9 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
592609
593610 if (packet->isRouteFlood ()) {
594611 // let this sender know path TO here, so they can use sendDirect(), and ALSO encode the ACK
595- bool use_v2 = (packet-> getPayloadVer () == PAYLOAD_VER_2 ) ;
612+ bool use_chacha = from-> supports_chacha ;
596613 mesh::Packet* path = createPathReturn (from->id , secret, packet->path , packet->path_len ,
597- PAYLOAD_TYPE_ACK , (uint8_t *) &ack_hash, 4 , use_v2 );
614+ PAYLOAD_TYPE_ACK , (uint8_t *) &ack_hash, 4 , use_chacha );
598615 if (path) sendFlood (path, TXT_ACK_DELAY );
599616 } else {
600617 sendAckTo (*from, ack_hash);
@@ -622,8 +639,8 @@ void SensorMesh::onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_i
622639 memcpy (temp, ×tamp, 4 ); // mostly an extra blob to help make packet_hash unique
623640 temp[4 ] = (TXT_TYPE_CLI_DATA << 2 );
624641
625- bool use_v2 = (packet-> getPayloadVer () == PAYLOAD_VER_2 ) ;
626- auto reply = createDatagram (PAYLOAD_TYPE_TXT_MSG , from->id , secret, temp, 5 + text_len, use_v2 );
642+ bool use_chacha = from-> supports_chacha ;
643+ auto reply = createDatagram (PAYLOAD_TYPE_TXT_MSG , from->id , secret, temp, 5 + text_len, use_chacha );
627644 if (reply) {
628645 if (from->out_path_len < 0 ) {
629646 sendFlood (reply, CLI_REPLY_DELAY_MILLIS );
0 commit comments