initial-quantum

This commit is contained in:
ariska committed 2026-10-15 11:29:40 +07:00
1 parent 813d3aa06e
commit ae1ce9673a
6924 files changed
+1908770 -17453

No files matched your search

+211 -32
View File
@@ -11,18 +11,20 @@
*/
/****/
#include "Peer.hpp"
#include "../version.h"
#include "Constants.hpp"
#include "Peer.hpp"
#include "Switch.hpp"
#include "Network.hpp"
#include "SelfAwareness.hpp"
#include "Packet.hpp"
#include "Trace.hpp"
#include "InetAddress.hpp"
#include "RingBuffer.hpp"
#include "Utils.hpp"
#include "Metrics.hpp"
#include "Network.hpp"
#include "PQHybrid.hpp"
#include "Packet.hpp"
#include "RingBuffer.hpp"
#include "SelfAwareness.hpp"
#include "Switch.hpp"
#include "Trace.hpp"
#include "Utils.hpp"
namespace ZeroTier {
@@ -70,6 +72,101 @@ Peer::Peer(const RuntimeEnvironment *renv,const Identity &myIdentity,const Ident
KBKDFHMACSHA384(_key,ZT_KBKDF_LABEL_AES_GMAC_SIV_K1,0,0,ktmp);
_aesKeys[1].init(ktmp);
Utils::burn(ktmp,ZT_SYMMETRIC_KEY_SIZE);
// Keep the classical key for HELLO/OK(HELLO) armor for the life of the
// peer; the session key may later be replaced by the hybrid one.
memcpy(_keyClassical, _key, sizeof(_keyClassical));
memcpy(_aesKeysClassical, _aesKeys, sizeof(_aesKeysClassical));
_hybridCapable = false;
_pqKeysValid = false;
_pqKeysSent = false;
_pqKeysAcked = false;
_pqKeysStale = false;
_lastPQKeysSentAt = 0;
_pendingHybridCtValid = 0;
_pendingHybridKeyValid = 0;
memset(_pendingHybridKey, 0, sizeof(_pendingHybridKey));
_hybridSessionKeyInstalled = false;
}
void Peer::_initAesKeys(const uint8_t* key, AES aesKeys[2])
{
uint8_t ktmp[ZT_SYMMETRIC_KEY_SIZE];
KBKDFHMACSHA384(key, ZT_KBKDF_LABEL_AES_GMAC_SIV_K0, 0, 0, ktmp);
aesKeys[0].init(ktmp);
KBKDFHMACSHA384(key, ZT_KBKDF_LABEL_AES_GMAC_SIV_K1, 0, 0, ktmp);
aesKeys[1].init(ktmp);
Utils::burn(ktmp, ZT_SYMMETRIC_KEY_SIZE);
}
void Peer::setHybridSessionKey(const void* key)
{
memcpy(_key, key, ZT_SYMMETRIC_KEY_SIZE);
_initAesKeys(_key, _aesKeys);
_hybridSessionKeyInstalled = true;
}
void Peer::setPendingHybridCt(const void* ct)
{
memcpy(_pendingHybridCt, ct, PQHybrid::MLKEM768_CT_LEN);
_pendingHybridCtValid = 1;
}
void Peer::setPendingHybridKey(const void* key)
{
memcpy(_pendingHybridKey, key, ZT_SYMMETRIC_KEY_SIZE);
_pendingHybridKeyValid = 1;
}
void Peer::commitPendingHybridKey()
{
if (_pendingHybridKeyValid) {
setHybridSessionKey(_pendingHybridKey);
_pendingHybridKeyValid = 0;
}
}
void Peer::dropPendingHybridKey()
{
Utils::burn(_pendingHybridKey, sizeof(_pendingHybridKey));
_pendingHybridKeyValid = 0;
}
bool Peer::setPQPublicKeys(const uint8_t* mlkemPk, const uint8_t* mldsaPk)
{
// True when this is material we had not seen before, so the caller knows
// to answer with its own keys.
const bool firstView = (!_pqKeysValid);
memcpy(_pqMlkemPk, mlkemPk, PQHybrid::MLKEM768_PK_LEN);
memcpy(_pqMldsaPk, mldsaPk, PQHybrid::MLDSA65_PK_LEN);
_pqKeysValid = true;
// Keep the peer's Identity itself complete, not just these crypto caches:
// anything that verifies a type 2 COM reads the controller's identity back
// through Topology::getIdentity(), which hands out peer->identity(). HELLO
// only carries the classic projection, so without this the controller stays
// type 0 here and its signatures never verify.
_id.attachPQPublicKeys(mlkemPk, mldsaPk);
return firstView;
}
void Peer::ensurePendingHybridCt()
{
if ((_pendingHybridCtValid) || (! hybridEligible()) || (RR->identity.address() >= _id.address())) {
return;
}
uint8_t ct[PQHybrid::MLKEM768_CT_LEN];
uint8_t hkey[ZT_SYMMETRIC_KEY_SIZE];
if (RR->identity.agreeHybridEncaps(_id, _pqMlkemPk, ct, hkey)) {
setPendingHybridCt(ct);
// Staged, not installed: the far side derives this same key only once
// the ciphertext reaches it. Installing now would armor outgoing data
// frames with a key the peer cannot yet reproduce, so those frames
// fail its MAC. commitPendingHybridKey() runs once the peer's confirm
// tag proves it derived the same value.
setPendingHybridKey(hkey);
}
Utils::burn(hkey, sizeof(hkey));
}
void Peer::received(
@@ -419,45 +516,126 @@ void Peer::sendHELLO(void *tPtr,const int64_t localSocket,const InetAddress &atA
{
Packet outp(_id.address(),RR->identity.address(),Packet::VERB_HELLO);
outp.append((unsigned char)ZT_PROTO_VERSION);
outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
outp.append((uint16_t)ZEROTIER_ONE_VERSION_REVISION);
outp.append(now);
RR->identity.serialize(outp,false);
atAddress.serialize(outp);
try {
outp.append((unsigned char)ZT_PROTO_VERSION);
outp.append((unsigned char)ZEROTIER_ONE_VERSION_MAJOR);
outp.append((unsigned char)ZEROTIER_ONE_VERSION_MINOR);
outp.append((uint16_t)(ZEROTIER_ONE_VERSION_REVISION | ((RR->node->pqcMode() != ZT_PQC_MODE_CLASSIC) ? ZT_PROTO_VERB_HELLO_REVISION_CAPABILITY_BIT : 0)));
outp.append(now);
// Classic projection only: a vanilla ZeroTier peer must be able to parse
// this HELLO. PQ public keys go out in VERB_PQ_KEYS instead.
RR->identity.serializeClassic(outp);
atAddress.serialize(outp);
outp.append((uint64_t)RR->topology->planetWorldId());
outp.append((uint64_t)RR->topology->planetWorldTimestamp());
outp.append((uint64_t)RR->topology->planetWorldId());
outp.append((uint64_t)RR->topology->planetWorldTimestamp());
const unsigned int startCryptedPortionAt = outp.size();
const unsigned int startCryptedPortionAt = outp.size();
std::vector<World> moons(RR->topology->moons());
std::vector<uint64_t> moonsWanted(RR->topology->moonsWanted());
outp.append((uint16_t)(moons.size() + moonsWanted.size()));
for(std::vector<World>::const_iterator m(moons.begin());m!=moons.end();++m) {
outp.append((uint8_t)m->type());
outp.append((uint64_t)m->id());
outp.append((uint64_t)m->timestamp());
std::vector<World> moons(RR->topology->moons());
std::vector<uint64_t> moonsWanted(RR->topology->moonsWanted());
outp.append((uint16_t)(moons.size() + moonsWanted.size()));
for (std::vector<World>::const_iterator m(moons.begin()); m != moons.end(); ++m) {
outp.append((uint8_t)m->type());
outp.append((uint64_t)m->id());
outp.append((uint64_t)m->timestamp());
}
for (std::vector<uint64_t>::const_iterator m(moonsWanted.begin()); m != moonsWanted.end(); ++m) {
outp.append((uint8_t)World::TYPE_MOON);
outp.append(*m);
outp.append((uint64_t)0);
}
// The hybrid ciphertext is not carried here -- it would push HELLO past
// the MTU. It rides in VERB_PQ_KEYS once the peer is known capable.
outp.cryptField(_keyClassical, startCryptedPortionAt, outp.size() - startCryptedPortionAt);
}
for(std::vector<uint64_t>::const_iterator m(moonsWanted.begin());m!=moonsWanted.end();++m) {
outp.append((uint8_t)World::TYPE_MOON);
outp.append(*m);
outp.append((uint64_t)0);
catch (int) {
// G4 bound: Buffer<ZT_PROTO_MAX_PACKET_LENGTH> overflow would kill the
// daemon via catch(int) in OneServiceImpl::run(); degrade to skipped HELLO.
char addrBuf[11];
fprintf(stderr, "WARNING: HELLO to %s exceeds ZT_PROTO_MAX_PACKET_LENGTH; not sent" ZT_EOL_S, _id.address().toString(addrBuf));
return;
}
outp.cryptField(_key,startCryptedPortionAt,outp.size() - startCryptedPortionAt);
Metrics::pkt_hello_out++;
if (atAddress) {
outp.armor(_key,false,nullptr); // false == don't encrypt full payload, but add MAC
outp.armor(_keyClassical, false, nullptr); // HELLO is always classical-armored
RR->node->expectReplyTo(outp.packetId());
RR->node->putPacket(tPtr,RR->node->lowBandwidthModeEnabled() ? localSocket : -1,atAddress,outp.data(),outp.size());
} else {
RR->node->expectReplyTo(outp.packetId());
RR->sw->send(tPtr,outp,false); // false == don't encrypt full payload, but add MAC
}
// A capable peer learns our PQ public keys out of band (VERB_PQ_KEYS), since
// they do not fit in HELLO. Driven from the peer timer rather than from
// sendHELLO: HELLO always carries a concrete address, so an address-based
// gate there never fires.
}
void Peer::sendPQKeys(void *tPtr,int64_t now)
{
Packet outp(_id.address(),RR->identity.address(),Packet::VERB_PQ_KEYS);
try {
// The lowest address encapsulates; the other side derives the same key
// from its own secret when the ciphertext arrives. Neither side needs an
// ephemeral key: ML-KEM decapsulation consumes only sk and ct.
bool haveCt = false;
if (RR->identity.address() < _id.address()) {
ensurePendingHybridCt();
haveCt = _pendingHybridCtValid;
}
// Bit 0: we are the encapsulating side and carry a ciphertext.
// Bit 1: we already hold the peer's keys, so this doubles as the
// acknowledgement that ends its retry; without it both sides keep
// re-offering forever. Both bits ride in one byte because the receiver
// parses the keys from a fixed offset after it.
outp.append((uint8_t)((haveCt ? 0x01 : 0x00) | (_pqKeysValid ? 0x02 : 0x00)));
uint8_t mlkemPk[PQHybrid::MLKEM768_PK_LEN];
uint8_t mldsaPk[PQHybrid::MLDSA65_PK_LEN];
RR->identity.pqKemKey(mlkemPk);
RR->identity.pqSigKey(mldsaPk);
outp.append(mlkemPk, sizeof(mlkemPk));
outp.append(mldsaPk, sizeof(mldsaPk));
if (haveCt) {
outp.append(_pendingHybridCt, PQHybrid::MLKEM768_CT_LEN);
}
// Key confirmation, appended last so its position is unambiguous and a
// shorter packet from an older build still parses. This is what makes a
// mismatched derivation detectable: ML-KEM's implicit rejection gives
// both sides a well-formed but different key, and every later data frame
// fails its MAC with no other signal that renegotiation is needed.
uint8_t confirm[ZT_PQ_KEYS_CONFIRM_LEN];
pqConfirmTag(confirm);
outp.append(confirm, sizeof(confirm));
// Exceeds the MTU by design; Switch::send fragments it like a
// NETWORK_CONFIG. Armored with the classical key so it is readable
// before either side has switched to the hybrid session key.
// FLAGS is AAD-covered, so the FRAGMENTED bit must be set here,
// before armor() computes the MAC -- Switch::_sendViaSpecificPath
// cannot set it on an already-armored packet without invalidating it.
outp.setFragmented(true);
outp.armor(_keyClassical, true, _aesKeysClassical);
}
catch (int e) {
// Buffer overflow would kill the daemon via catch(int) in
// OneServiceImpl::run(); degrade to a skipped exchange.
return;
}
RR->node->expectReplyTo(outp.packetId());
RR->sw->send(tPtr, outp, true);
setPQKeysSent(RR->node->now());
}
void Peer::attemptToContactAt(void *tPtr,const int64_t localSocket,const InetAddress &atAddress,int64_t now,bool sendFullHello)
@@ -599,6 +777,7 @@ unsigned int Peer::doPingAndKeepalive(void *tPtr,int64_t now)
#ifndef ZT_NO_PEER_METRICS
_peer_latency.Observe(latency(now));
#endif
return sent;
}