Files
mpc/pkg/approval/provider_mldsa.go
zeekay 82bd565201 mpc: deployment-mode flag + audit dispatcher for fund-private clusters
Adds the topology contract that lets a fund run mpcd as its own private
cluster — no Lux primary network registration, no shared M-Chain audit
ledger — using the same code path and the same operator surface.

Mode flag at cmd/mpcd/main.go:
  --network=primary|private              # registration target
  --audit-store=mchain|local-worm|...    # audit log destination
  --registrar-url, --registrar-auth      # primary-mode announcement
  --audit-worm-path, --audit-mchain-url  # backend selectors

pkg/audit:
  - Append-only Merkle hash chain. Tampering with any historical event
    invalidates every subsequent hash. Canonical bytes use fixed-order
    concatenation (json.Marshal map ordering is non-deterministic).
  - WORMDispatcher: NDJSON append, replay-on-start, fsync per Append.
  - MChainDispatcher: batched POST of (batchHash, fromSeq, toSeq) to a
    configurable anchor RPC.
  - compositeDispatcher: fan-out to multiple legs with per-leg sealing.
  - S3 Glacier Vault / Azure Immutable / GCS Object Lock dispatchers
    fail closed — wired into the flag surface but gated behind the
    cloud-audit build tag, no silent fallback.

pkg/identity/bootstrap.go: identity creation + (primary-mode only)
registrar announcement. Same on-disk format as the previous
loadOrGenerateIdentity (which is now removed); private mode is silent.

deployments/k8s-private: fund-only Kustomize overlay. Standalone
namespace, NetworkPolicy that blocks egress to anything outside the
fund's VPC, hardware-WORM PVC for the audit log, KMS-driven secret
template.

deployments/k8s-primary: env-var patch overlay on the existing lux-mpc
base. composite(local-worm,mchain) by default.

deployments/terraform/{aws,gcp,azure}: VPC + audit storage modules.
terraform validate passes; EKS/GKE/AKS + HSM modules are stubbed and
not yet plan-tested.

deployments/onprem/README.md: WORM storage + PKCS#11 HSM checklist.

Tests (14 total, all passing):
  pkg/audit:           6/6  (chain, WORM replay, MChain batching, composite,
                             cloud stubs fail loud, double-seal rejected)
  pkg/identity:        4/4  (private no-call, primary call, primary empty
                             URL no-op, primary registrar 503 surfaces)
  internal/privatemode: 4/4 (no registrar traffic, restart, tamper-detect,
                             cloud stubs fail loud)

Companion runbook in lux/papers/lux-private-mpc-deployment/
(LaTeX, 5 pages, tectonic-clean).
2025-12-28 16:20:40 -08:00

130 lines
3.7 KiB
Go

package approval
import (
"context"
"crypto"
"crypto/rand"
"errors"
"fmt"
"sync"
"time"
"github.com/luxfi/crypto/mldsa"
)
// MLDSAProvider produces ML-DSA-65 (FIPS 204, NIST level 3) post-quantum
// approval signatures. Use for long-lived attestations where you need
// resistance to future quantum attacks (e.g. board-level approvals
// archived for 10+ years).
//
// We hold private keys in process memory, mirroring luxfi/hsm's MLDSASigner.
// For production HSM-backed PQ approvals, use AWS KMS (which now supports
// ML-DSA) and prefer the "aws-kms" provider with an ML-DSA configured key —
// or wait for hardware ML-DSA support in Zymbit/YubiHSM.
//
// Configuration: none. Mode is fixed at ML-DSA-65.
type MLDSAProvider struct {
mu sync.RWMutex
identity map[string]mldsaEntry
}
type mldsaEntry struct {
PrivKey *mldsa.PrivateKey
PubKey *mldsa.PublicKey
PubBytes []byte
}
func NewMLDSA(config map[string]string) (ApprovalProvider, error) {
_ = config
return &MLDSAProvider{
identity: make(map[string]mldsaEntry),
}, nil
}
func (p *MLDSAProvider) Provider() string { return "mldsa" }
// Enroll either generates a new ML-DSA-65 keypair for approverID (if
// privBytes is nil) or restores a known key from raw bytes.
func (p *MLDSAProvider) Enroll(approverID string, privBytes []byte) error {
p.mu.Lock()
defer p.mu.Unlock()
var sk *mldsa.PrivateKey
if len(privBytes) == 0 {
generated, err := mldsa.GenerateKey(rand.Reader, mldsa.MLDSA65)
if err != nil {
return fmt.Errorf("approval/mldsa: keygen for %q: %w", approverID, err)
}
sk = generated
} else {
restored, err := mldsa.PrivateKeyFromBytes(mldsa.MLDSA65, privBytes)
if err != nil {
return fmt.Errorf("approval/mldsa: restore key for %q: %w", approverID, err)
}
sk = restored
}
p.identity[approverID] = mldsaEntry{
PrivKey: sk,
PubKey: sk.PublicKey,
PubBytes: sk.PublicKey.Bytes(),
}
return nil
}
func (p *MLDSAProvider) GetPublicIdentity(_ context.Context, approverID string) (PublicIdentity, error) {
p.mu.RLock()
defer p.mu.RUnlock()
entry, ok := p.identity[approverID]
if !ok {
return PublicIdentity{}, fmt.Errorf("approval/mldsa: approver %q not enrolled", approverID)
}
return PublicIdentity{
ApproverID: approverID,
Provider: p.Provider(),
PublicKey: entry.PubBytes,
Algorithm: AlgorithmMLDSA65,
}, nil
}
func (p *MLDSAProvider) ApproveIntent(_ context.Context, approverID string, intent CanonicalIntent) (ApprovalSignature, error) {
p.mu.RLock()
entry, ok := p.identity[approverID]
p.mu.RUnlock()
if !ok {
return ApprovalSignature{}, fmt.Errorf("approval/mldsa: approver %q not enrolled", approverID)
}
digest := intent.Digest()
sig, err := entry.PrivKey.Sign(rand.Reader, intent.Bytes(), crypto.Hash(0))
if err != nil {
return ApprovalSignature{}, fmt.Errorf("approval/mldsa: sign: %w", err)
}
return ApprovalSignature{
ApproverID: approverID,
Provider: p.Provider(),
IntentDigest: digest,
Signature: sig,
Timestamp: time.Now().UTC(),
Algorithm: AlgorithmMLDSA65,
}, nil
}
func (p *MLDSAProvider) VerifyApproval(_ context.Context, intent CanonicalIntent, sig ApprovalSignature) (bool, error) {
if sig.Provider != p.Provider() || sig.Algorithm != AlgorithmMLDSA65 {
return false, nil
}
digest := intent.Digest()
if digest != sig.IntentDigest {
return false, nil
}
p.mu.RLock()
entry, ok := p.identity[sig.ApproverID]
p.mu.RUnlock()
if !ok {
return false, nil
}
return entry.PubKey.VerifySignature(intent.Bytes(), sig.Signature), nil
}
// errMLDSAUnenrolled — kept for callers that want a clean sentinel for the
// "no such approver" case.
var errMLDSAUnenrolled = errors.New("approval/mldsa: approver not enrolled")