oficina / home Public

handoff/seal: Sealer.Derive, a domain-separated key per label (smart-door step 5)

Purpose keys are HKDF(namespace+":"+purpose); Derive's info starts with
oficina/seal/derive/v1 and a NUL, namespaces are a closed set with no NUL,
and the label is NUL-separated, so no label can yield a sealing key. Home
derives its remembered-sign-in cookie key under "home/lastsignin/v1".

Reviewed by Claude Fable as part of feat/smart-door-id (9f2bf3f): sound.
Discussion 139. CI passed at 3161be2 (after reruns past two unrelated
flakes, TestOperatorCommandActualBinaryDispatch and TestNativeProofRefusals).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Squashed from feat/seal-derive into main.

Squashed from feat/seal-derive into main.
claude agent, operated by paul@keymail.dev 17cdcaff33452b49eb7bd38c999219a5bc49dcc4 parent 1f7b43c
2 files changed, +63 −0
  • handoff/seal/seal.go +20 −0
  • handoff/seal/seal_test.go +43 −0

CI passed — run details

CI log
amadan: .amadan/timeout: this job's budget is 20m0s (this runner allows up to 30m0s)
=== vet ===
CGO_ENABLED=0 go vet ./...
CGO_ENABLED=0 go vet -tags browser ./internal/home/
cd handoff && CGO_ENABLED=0 go vet ./...
cd roster && CGO_ENABLED=0 go vet ./...
cd dashboard && CGO_ENABLED=0 go vet ./...
cd connect && CGO_ENABLED=0 go vet ./...
cd teammode && CGO_ENABLED=0 go vet ./...
=== fmt ===
=== build ===
CGO_ENABLED=0 go build ./...
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build ./cmd/home ./cmd/oficina ./internal/operatorlaunch
cd handoff && CGO_ENABLED=0 go build ./...
cd roster && CGO_ENABLED=0 go build ./...
cd dashboard && CGO_ENABLED=0 go build ./...
cd connect && CGO_ENABLED=0 go build ./...
cd teammode && CGO_ENABLED=0 go build ./...
=== go-test ===
CGO_ENABLED=0 go test -count=1 ./...
?   	amadan.net/oficina/home/cmd/home	[no test files]
ok  	amadan.net/oficina/home/cmd/home-placeholder	0.791s
?   	amadan.net/oficina/home/cmd/oficina	[no test files]
ok  	amadan.net/oficina/home/internal/guardstest	0.101s
ok  	amadan.net/oficina/home/internal/home	54.635s
ok  	amadan.net/oficina/home/internal/home/directorydata	1.451s
ok  	amadan.net/oficina/home/internal/home/logtest	0.003s
ok  	amadan.net/oficina/home/internal/home/seal	0.004s
ok  	amadan.net/oficina/home/internal/home/sealtest	0.003s
ok  	amadan.net/oficina/home/internal/home/wisp	0.061s
ok  	amadan.net/oficina/home/internal/operator	6.549s
ok  	amadan.net/oficina/home/internal/operatorlaunch	4.120s
ok  	amadan.net/oficina/home/internal/provisioner	50.074s
cd handoff && CGO_ENABLED=0 go test -count=1 ./...
ok  	amadan.net/oficina/home/handoff	0.676s
ok  	amadan.net/oficina/home/handoff/seal	0.003s
cd roster && CGO_ENABLED=0 go test -count=1 ./...
ok  	amadan.net/oficina/home/roster	5.125s
cd dashboard && CGO_ENABLED=0 go test ./...
ok  	amadan.net/oficina/home/dashboard	(cached)
cd connect && CGO_ENABLED=0 go test -count=1 ./...
ok  	amadan.net/oficina/home/connect	1.440s
ok  	amadan.net/oficina/home/connect/linkstore	0.484s
cd teammode && CGO_ENABLED=0 go test -count=1 ./...
ok  	amadan.net/oficina/home/teammode	0.108s
?   	amadan.net/oficina/home/teammode/teammodetest	[no test files]
=== race ===
go test -race -count=1 ./internal/home/ ./internal/operator/ ./internal/operatorlaunch/ ./internal/home/directorydata/
ok  	amadan.net/oficina/home/internal/home	327.846s
ok  	amadan.net/oficina/home/internal/operator	83.488s
ok  	amadan.net/oficina/home/internal/operatorlaunch	22.403s
ok  	amadan.net/oficina/home/internal/home/directorydata	24.417s
ok  	amadan.net/oficina/home/internal/provisioner	401.512s
cd handoff && CGO_ENABLED=1 go test -race -count=1 ./...
ok  	amadan.net/oficina/home/handoff	12.943s
ok  	amadan.net/oficina/home/handoff/seal	1.015s
cd roster && CGO_ENABLED=1 go test -race -count=1 ./...
ok  	amadan.net/oficina/home/roster	6.969s
cd dashboard && CGO_ENABLED=1 go test -race -count=1 ./...
ok  	amadan.net/oficina/home/dashboard	1.065s
cd connect && CGO_ENABLED=1 go test -race -count=1 ./...
ok  	amadan.net/oficina/home/connect	6.302s
ok  	amadan.net/oficina/home/connect/linkstore	6.340s
cd teammode && CGO_ENABLED=1 go test -race -count=1 ./...
ok  	amadan.net/oficina/home/teammode	1.956s
?   	amadan.net/oficina/home/teammode/teammodetest	[no test files]
diff --git a/handoff/seal/seal.go b/handoff/seal/seal.go
index 56fadc9..62bcd49 100644
--- a/handoff/seal/seal.go
+++ b/handoff/seal/seal.go
@@ -8,6 +8,7 @@ import (
"crypto/rand"
"crypto/sha256"
"errors"
+ "strings"
)
// Sealer owns a copy of the key and an application namespace.
@@ -48,6 +49,25 @@ func (s *Sealer) aeadFor(purpose string) (cipher.AEAD, error) {
return cipher.NewGCM(block)
}
+// deriveInfo starts every Derive label's HKDF info. Purpose keys use
+// namespace+":"+purpose, and a namespace may not contain a NUL, so no
+// purpose key can equal a derived key whatever the label: the two are
+// separate domains under one DEK.
+const deriveInfo = "oficina/seal/derive/v1\x00"
+
+// Derive returns a 32-byte key for label, for a consumer that keys its
+// own construction rather than sealing through this Sealer (Home's
+// remembered-sign-in cookie, under "home/lastsignin/v1"). Each label gets
+// its own key, and none is any purpose's sealing key. The label must be
+// 1 to 128 bytes with no NUL; keep it stable, because changing it changes
+// the key.
+func (s *Sealer) Derive(label string) ([]byte, error) {
+ if len(label) == 0 || len(label) > 128 || strings.ContainsRune(label, 0) {
+ return nil, errors.New("seal: invalid derive label")
+ }
+ return hkdf.Key(sha256.New, s.dek, nil, deriveInfo+s.namespace+"\x00"+label, 32)
+}
+
// Seal uses the historical Oficina envelope: random nonce followed by GCM
// ciphertext and authentication tag. The namespace and purpose bind the key.
func (s *Sealer) Seal(purpose string, plaintext []byte) ([]byte, error) {
diff --git a/handoff/seal/seal_test.go b/handoff/seal/seal_test.go
index 14e1b96..cefddd0 100644
--- a/handoff/seal/seal_test.go
+++ b/handoff/seal/seal_test.go
@@ -121,3 +121,46 @@ func TestConstructorCopiesKeyAndRejectsAmbiguousNamespaces(t *testing.T) {
}
}
}
+
+// Derive is domain-separated: different labels give different keys, the
+// same label the same key, a different DEK or namespace a different key,
+// and no derived key is any purpose's sealing key, even for a label that
+// spells a purpose.
+func TestDeriveIsDomainSeparated(t *testing.T) {
+ s, _ := New(testKey(), "oficina/home")
+ a, err := s.Derive("home/lastsignin/v1")
+ if err != nil {
+ t.Fatal(err)
+ }
+ again, _ := s.Derive("home/lastsignin/v1")
+ b, _ := s.Derive("home/lastsignin/v2")
+ if len(a) != 32 || !bytes.Equal(a, again) {
+ t.Fatal("derive is not a stable 32-byte key")
+ }
+ if bytes.Equal(a, b) {
+ t.Fatal("different labels gave the same key")
+ }
+ other, _ := New(testKey(), "oficina/docs")
+ if c, _ := other.Derive("home/lastsignin/v1"); bytes.Equal(a, c) {
+ t.Fatal("namespace not bound into the derived key")
+ }
+ otherKey, _ := New(bytes.Repeat([]byte{7}, 32), "oficina/home")
+ if c, _ := otherKey.Derive("home/lastsignin/v1"); bytes.Equal(a, c) {
+ t.Fatal("DEK not bound into the derived key")
+ }
+ for _, purpose := range []string{"home/lastsignin/v1", "signin.address", "id.direct"} {
+ pk, err := s.purposeKey(purpose)
+ if err != nil {
+ t.Fatal(err)
+ }
+ dk, _ := s.Derive(purpose)
+ if bytes.Equal(pk, dk) {
+ t.Fatalf("derived key for %q equals that purpose's sealing key", purpose)
+ }
+ }
+ for _, bad := range []string{"", "a\x00b", string(bytes.Repeat([]byte{'a'}, 129))} {
+ if _, err := s.Derive(bad); err == nil {
+ t.Fatalf("invalid label %q accepted", bad)
+ }
+ }
+}