cert: Name the post-quantum algorithms of a version 3 certificate with a PQSuite, like its curve, and pick the hybrid subtype by the suite's KEM

This commit is contained in:
JackDoan committed 2026-10-14 14:40:52 -05:00
1 parent 84e28c62ea
commit 656164b259
29 files changed
+1124 -365

No files matched your search

+10 -13
View File
@@ -15,7 +15,7 @@ type Credential struct {
Bytes []byte // pre-marshaled certificate bytes
privateKey []byte // static private key (public half lives in Cert)
publicKey []byte // static public key, the classical half for a v3 Cert
kemKey noise.KEMKey // static ML-KEM-1024 keypair, only for a v3 Cert
kemKey noise.KEMKey // static KEM keypair of the Cert's suite, only for a v3 Cert
cipherSuite noise.CipherSuite // pre-built cipher suite (DH + cipher + hash)
}
@@ -24,7 +24,8 @@ type Credential struct {
// appropriate DH function, cipher, and hash.
//
// For a version 3 certificate privateKey is the composite private key,
// classical || ML-KEM-1024 seed, matching the composite Cert.PublicKey().
// classical || the seed of its suite's KEM, matching the composite
// Cert.PublicKey().
func NewCredential(
c cert.Certificate,
hsBytes []byte,
@@ -38,14 +39,10 @@ func NewCredential(
publicKey: c.PublicKey(),
cipherSuite: cipherSuite,
}
if v2 := cert.V2Form(c); v2 != nil {
classical := v2.PublicKey()
hc.publicKey = classical
hc.kemKey = noise.KEMKey{
Private: privateKey[len(privateKey)-cert.MLKEM1024PrivateKeySize:],
Public: c.PublicKey()[len(classical):],
}
hc.privateKey = privateKey[:len(privateKey)-cert.MLKEM1024PrivateKeySize]
if suite := cert.PQSuiteOf(c); suite != cert.PQSuiteUnknown {
// The pki checked the private key against the certificate, so both split.
hc.publicKey, hc.kemKey.Public, _ = suite.SplitPublicKey(false, c.PublicKey())
hc.privateKey, hc.kemKey.Private, _ = suite.SplitPrivateKey(false, privateKey)
}
return hc
}
@@ -53,7 +50,7 @@ func NewCredential(
// buildHandshakeState creates a noise.HandshakeState from this credential.
// pskPlacement selects the noise psk modifier; 0 means none. A psk for
// placements 2 and up is supplied later with SetPresharedKey. A hybrid
// pattern also gets ML-KEM-1024 and the static ML-KEM key.
// pattern also gets its KEM and the static KEM key.
func (hc *Credential) buildHandshakeState(initiator bool, info subtypeInfo) (*noise.HandshakeState, error) {
cfg := noise.Config{
CipherSuite: hc.cipherSuite,
@@ -64,8 +61,8 @@ func (hc *Credential) buildHandshakeState(initiator bool, info subtypeInfo) (*no
PresharedKey: []byte{},
PresharedKeyPlacement: info.pskPlacement,
}
if info.hybrid {
cfg.KEMFunc = KEMMLKEM1024
if info.kem != nil {
cfg.KEMFunc = info.kem
cfg.StaticKEMKeypair = hc.kemKey
}
return noise.NewHandshakeState(cfg)
+1
View File
@@ -20,4 +20,5 @@ var (
ErrMultiMessageUnsupported = errors.New("multi-message handshake patterns are not yet supported by the manager")
ErrSubtypeMismatch = errors.New("packet subtype does not match handshake machine subtype")
ErrCertVersionMismatch = errors.New("handshake subtype does not allow this certificate version")
ErrPQSuiteMismatch = errors.New("handshake subtype does not use the KEM of this certificate's post-quantum suite")
)
+1 -1
View File
@@ -524,7 +524,7 @@ func TestTableAnswered(t *testing.T) {
r := rand.New(rand.NewPCG(3, 3))
tb := NewTable[testKey, string](testConfig())
now := time.Unix(1000, 0)
const msgLen = 8006 // a hybrid msg1: K=7 data symbols, 3 repair
const msgLen = 8009 // a hybrid msg1: K=7 data symbols, 3 repair
sendAll := func(key testKey, pkts [][]byte) []*Result {
var out []*Result
+25 -2
View File
@@ -94,7 +94,8 @@ func NewMachine(
// Version 3 certificates go with hybrid patterns and nothing else, the
// classical and phase 1 subtypes use the v2 form instead.
if info.hybrid != (version == cert.Version3) {
hybrid := info.kem != nil
if hybrid != (version == cert.Version3) {
return nil, fmt.Errorf("%w: %v", ErrCertVersionMismatch, version)
}
@@ -102,6 +103,11 @@ func NewMachine(
if cred == nil {
return nil, fmt.Errorf("%w: %v", ErrNoCredential, version)
}
if hybrid {
if err := checkHybridSuite(subtype, cred.Cert); err != nil {
return nil, err
}
}
hs, err := cred.buildHandshakeState(initiator, info)
if err != nil {
@@ -112,7 +118,7 @@ func NewMachine(
hs: hs,
subtype: subtype,
msgs: info.msgs,
hybrid: info.hybrid,
hybrid: hybrid,
getCred: getCred,
allocIndex: allocIndex,
verifier: verifier,
@@ -390,6 +396,16 @@ func (m *Machine) processPayload(msg []byte, flags msgFlags) error {
return nil
}
// checkHybridSuite checks that a version 3 certificate's suite uses the KEM of
// the hybrid subtype.
func checkHybridSuite(subtype header.MessageSubType, c cert.Certificate) error {
suite := cert.PQSuiteOf(c)
if st, ok := HybridSubtype(suite); !ok || st != subtype {
return fmt.Errorf("%w: %v", ErrPQSuiteMismatch, suite)
}
return nil
}
func (m *Machine) validateCert(payload Payload) error {
// A v3 certificate only ever comes with a hybrid pattern, and a hybrid
// pattern only ever with one. The composite static key would not recombine
@@ -420,6 +436,13 @@ func (m *Machine) validateCert(payload Payload) error {
return ErrPublicKeyMismatch
}
if m.hybrid {
if err := checkHybridSuite(m.subtype, rc); err != nil {
m.failed = true
return err
}
}
// Version negotiation, if the peer sent a different version and we have it, switch
if rc.Version() != m.myVersion {
if m.getCred(rc.Version()) != nil {
+26 -4
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"github.com/DefinedNet/noise"
"github.com/slackhq/nebula/cert"
"github.com/slackhq/nebula/header"
)
@@ -22,9 +23,10 @@ type subtypeInfo struct {
pskPlacement int // noise psk modifier position, 0 means no psk token
msgs []msgFlags
// hybrid patterns carry composite static keys and need version 3
// certificates on both sides; every other pattern refuses them.
hybrid bool
// kem is the KEM of a hybrid pattern, nil for any other. Hybrid patterns
// carry composite static keys and need version 3 certificates on both
// sides, in suites using this KEM; every other pattern refuses them.
kem noise.KEMFunc
}
// subtypeInfos defines the noise pattern and message content layout for each
@@ -68,7 +70,7 @@ var subtypeInfos = map[header.MessageSubType]subtypeInfo{
// first packet under the new keys does.
header.HandshakeIXHybridMLKEM1024: {
pattern: noise.HandshakeHybridIX,
hybrid: true,
kem: KEMMLKEM1024,
msgs: []msgFlags{
{expectsPayload: true, expectsCert: true},
{expectsPayload: true, expectsCert: true},
@@ -90,6 +92,26 @@ var subtypeInfos = map[header.MessageSubType]subtypeInfo{
//},
}
// hybridSubtypes maps the suite of a version 3 certificate to the hybrid
// subtype for its KEM. Peers need the same KEM, not the same suite, so suites
// sharing a KEM share a subtype; the suite itself travels in the certificate.
var hybridSubtypes = map[cert.PQSuite]header.MessageSubType{
cert.PQSuite_MLKEM1024_MLDSA87: header.HandshakeIXHybridMLKEM1024,
}
// HybridSubtype returns the hybrid handshake subtype for the KEM of a version 3
// certificate's suite.
func HybridSubtype(suite cert.PQSuite) (header.MessageSubType, bool) {
st, ok := hybridSubtypes[suite]
return st, ok
}
// IsHybrid reports whether subtype is a hybrid handshake, one that needs version
// 3 certificates.
func IsHybrid(subtype header.MessageSubType) bool {
return subtypeInfos[subtype].kem != nil
}
func subtypeInfoFor(subtype header.MessageSubType) (subtypeInfo, error) {
if info, ok := subtypeInfos[subtype]; ok {
return info, nil