feat(pqc): plumb pqcMode to identity path, hybrid HELLO ct, spec review fixes

- Node stores _pqcMode; Identity::generate(bool,int) derives pq from daemon mode
- OneService reads local.conf settings.pqcMode before new Node (opt-in, absent=classic)
- Peer/IncomingPacket: hybrid KEM ct in HELLO/OK, capability-bit fallback classic
- tools/pqc-lab.sh two-node E2E lab + tools/zt-dissector.lua capture dissector
- docs/pqc-test-scenario.md test plan
- SPEC.md review pass: V12 evidence -> live daemon check, V14/V15 cite realign,
  T24/T25 tasks (pqconly preset gap documented)
This commit is contained in:
dedysutanto committed 2026-10-03 07:01:52 +07:00
1 parent 1ead221c6a
commit fa08792c78
13 files changed
+1058 -144

No files matched your search

+95 -35
View File
@@ -10,35 +10,35 @@
* of this software will be governed by version 2.0 of the Apache License.
*/
/****/
#include "IncomingPacket.hpp"
#include "../include/ZeroTierOne.h"
#include "../version.h"
#include "Bond.hpp"
#include "Capability.hpp"
#include "CertificateOfMembership.hpp"
#include "Constants.hpp"
#include "Metrics.hpp"
#include "NetworkController.hpp"
#include "Node.hpp"
#include "PQHybrid.hpp"
#include "PacketMultiplexer.hpp"
#include "Path.hpp"
#include "Peer.hpp"
#include "Revocation.hpp"
#include "RuntimeEnvironment.hpp"
#include "SHA512.hpp"
#include "Salsa20.hpp"
#include "SelfAwareness.hpp"
#include "Switch.hpp"
#include "Tag.hpp"
#include "Topology.hpp"
#include "Trace.hpp"
#include "World.hpp"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "../version.h"
#include "../include/ZeroTierOne.h"
#include "Constants.hpp"
#include "RuntimeEnvironment.hpp"
#include "IncomingPacket.hpp"
#include "Topology.hpp"
#include "Switch.hpp"
#include "Peer.hpp"
#include "NetworkController.hpp"
#include "SelfAwareness.hpp"
#include "Salsa20.hpp"
#include "SHA512.hpp"
#include "World.hpp"
#include "Node.hpp"
#include "CertificateOfMembership.hpp"
#include "Capability.hpp"
#include "Tag.hpp"
#include "Revocation.hpp"
#include "Trace.hpp"
#include "Path.hpp"
#include "Bond.hpp"
#include "Metrics.hpp"
#include "PacketMultiplexer.hpp"
#include <string.h>
namespace ZeroTier {
@@ -69,9 +69,16 @@ bool IncomingPacket::tryDecode(const RuntimeEnvironment *RR,void *tPtr,int32_t f
if (peer) {
if (!_authenticated) {
if (!dearmor(peer->key(), peer->aesKeys())) {
RR->t->incomingPacketMessageAuthenticationFailure(tPtr,_path,packetId(),sourceAddress,hops(),"invalid MAC");
peer->recordIncomingInvalidPacket(_path);
return true;
// HELLO and OK(HELLO) are always armored with the classical
// key, so retry with it once the session key has gone hybrid.
if (((verb() == Packet::VERB_HELLO) || (verb() == Packet::VERB_OK)) && (dearmor(peer->keyClassical(), peer->aesKeysClassical()))) {
// classical fallback accepted
}
else {
RR->t->incomingPacketMessageAuthenticationFailure(tPtr, _path, packetId(), sourceAddress, hops(), "invalid MAC");
peer->recordIncomingInvalidPacket(_path);
return true;
}
}
}
@@ -435,7 +442,7 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool
} else {
// Identity is the same as the one we already have -- check packet integrity
if (!dearmor(peer->key(), peer->aesKeysIfSupported())) {
if (! dearmor(peer->keyClassical(), peer->aesKeysClassical())) {
RR->t->incomingPacketMessageAuthenticationFailure(tPtr,_path,pid,fromAddress,hops(),"invalid MAC");
return true;
}
@@ -458,9 +465,9 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool
return true;
}
// Check packet integrity and MAC (this is faster than locallyValidate() so do it first to filter out total crap)
SharedPtr<Peer> newPeer(new Peer(RR,RR->identity,id));
if (!dearmor(newPeer->key(), newPeer->aesKeysIfSupported())) {
newPeer->setRemoteVersion(protoVersion, vMajor, vMinor, vRevision);
if (! dearmor(newPeer->keyClassical(), newPeer->aesKeysClassical())) {
RR->t->incomingPacketMessageAuthenticationFailure(tPtr,_path,pid,fromAddress,hops(),"invalid MAC");
return true;
}
@@ -476,6 +483,10 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool
// Continue at // VALID
}
// Learn the peer's PQC capability from the HELLO revision bit now: the
// hybrid ciphertext below can only be interpreted once we know it.
peer->setRemoteVersion(protoVersion, vMajor, vMinor, vRevision);
// VALID -- if we made it here, packet passed identity and authenticity checks!
// Get external surface address if present (was not in old versions)
@@ -499,8 +510,9 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool
std::vector< std::pair<uint64_t,uint64_t> > moonIdsAndTimestamps;
if (ptr < size()) {
// Remainder of packet, if present, is encrypted
cryptField(peer->key(),ptr,size() - ptr);
// Remainder of packet, if present, is encrypted (always with the
// classical key: it carries the hybrid ciphertext itself).
cryptField(peer->keyClassical(), ptr, size() - ptr);
// Get moon IDs and timestamps if present
if ((ptr + 2) <= size()) {
@@ -513,6 +525,21 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool
ptr += 16;
}
}
// Hybrid KEM ciphertext, if the sender included one. Sender order is
// [moon count(2B)][moons][flag(1B)][ciphertext(1088B)] (see
// Peer::sendHELLO), so the flag follows the moon list. Only the
// lowest-address side ever writes it, so require that direction
// (sender == lowest) rather than trusting a coincidental byte value.
if ((peer->hybridEligible()) && (peer->identity().address() < RR->identity.address()) && ((ptr + 1 + PQHybrid::MLKEM768_CT_LEN) <= size()) && ((unsigned char)(*this)[ptr] == 1)) {
++ptr;
uint8_t hkey[ZT_SYMMETRIC_KEY_SIZE];
if (RR->identity.agreeHybridDecaps(id, field(ptr, PQHybrid::MLKEM768_CT_LEN), hkey)) {
peer->setHybridSessionKey(hkey);
}
Utils::burn(hkey, sizeof(hkey));
ptr += PQHybrid::MLKEM768_CT_LEN;
}
}
// Send OK(HELLO) with an echo of the packet's timestamp and some of the same
@@ -578,7 +605,20 @@ bool IncomingPacket::_doHELLO(const RuntimeEnvironment *RR,void *tPtr,const bool
}
outp.setAt<uint16_t>(worldUpdateSizeAt,(uint16_t)(outp.size() - (worldUpdateSizeAt + 2)));
outp.armor(peer->key(),true,peer->aesKeysIfSupported());
// Echo our ciphertext in OK(HELLO) so the initiator can derive the same
// hybrid key; only the lowest address ever encapsulates. Generate it now
// if this HELLO is the first time we learned the peer is hybrid-capable,
// so the handshake completes in this round trip instead of the next HELLO.
if (peer->hybridEligible()) {
peer->ensurePendingHybridCt();
}
unsigned char ctBuf[PQHybrid::MLKEM768_CT_LEN];
if (peer->pendingHybridCt(ctBuf)) {
outp.append((uint8_t)1);
outp.append(ctBuf, sizeof(ctBuf));
}
outp.armor(peer->keyClassical(), true, peer->aesKeysClassical());
peer->recordOutgoingPacket(_path,outp.packetId(),outp.payloadLength(),outp.verb(),ZT_QOS_NO_FLOW,now);
Metrics::pkt_ok_out++;
_path->send(RR,tPtr,outp.data(),outp.size(),now);
@@ -642,6 +682,26 @@ bool IncomingPacket::_doOK(const RuntimeEnvironment *RR,void *tPtr,const SharedP
peer->setRemoteVersion(vProto,vMajor,vMinor,vRevision);
// Hybrid ciphertext echoed by the lowest-address side; decrypting
// it completes the hybrid key agreement on our side.
unsigned int cto = ZT_PROTO_VERB_HELLO__OK__IDX_REVISION + 2;
InetAddress _esa;
if (cto < size()) {
cto += _esa.deserialize(*this, cto);
}
if ((cto + 2) <= size()) {
cto += 2 + at<uint16_t>(cto);
}
// Echo comes from the lowest-address side only; require that
// direction rather than trusting a coincidental flag byte.
if ((peer->hybridEligible()) && (peer->identity().address() < RR->identity.address()) && (cto < size()) && ((unsigned char)(*this)[cto] == 1) && ((cto + 1 + PQHybrid::MLKEM768_CT_LEN) <= size())) {
uint8_t hkey[ZT_SYMMETRIC_KEY_SIZE];
if (RR->identity.agreeHybridDecaps(peer->identity(), field(cto + 1, PQHybrid::MLKEM768_CT_LEN), hkey)) {
peer->setHybridSessionKey(hkey);
}
Utils::burn(hkey, sizeof(hkey));
}
if ((externalSurfaceAddress)&&(hops() == 0)) {
RR->sa->iam(tPtr,peer->address(),_path->localSocket(),_path->address(),externalSurfaceAddress,RR->topology->isUpstream(peer->identity()),RR->node->now());
}