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 its own prefix and a NUL, so no label can yield a sealing key. Home derives its remembered-sign-in cookie key under "home/lastsignin/v1".
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+63
−0
handoff/seal/seal.go+20 −0handoff/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.825s ? amadan.net/oficina/home/cmd/oficina [no test files] ok amadan.net/oficina/home/internal/guardstest 0.125s ok amadan.net/oficina/home/internal/home 51.562s ok amadan.net/oficina/home/internal/home/directorydata 1.416s ok amadan.net/oficina/home/internal/home/logtest 0.005s 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.047s ok amadan.net/oficina/home/internal/operator 6.399s ok amadan.net/oficina/home/internal/operatorlaunch 4.124s ok amadan.net/oficina/home/internal/provisioner 48.508s cd handoff && CGO_ENABLED=0 go test -count=1 ./... ok amadan.net/oficina/home/handoff 0.659s ok amadan.net/oficina/home/handoff/seal 0.002s cd roster && CGO_ENABLED=0 go test -count=1 ./... ok amadan.net/oficina/home/roster 5.118s 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.391s ok amadan.net/oficina/home/connect/linkstore 0.451s cd teammode && CGO_ENABLED=0 go test -count=1 ./... ok amadan.net/oficina/home/teammode 0.097s ? 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 310.390s ok amadan.net/oficina/home/internal/operator 82.103s ok amadan.net/oficina/home/internal/operatorlaunch 22.381s ok amadan.net/oficina/home/internal/home/directorydata 23.228s ok amadan.net/oficina/home/internal/provisioner 393.988s cd handoff && CGO_ENABLED=1 go test -race -count=1 ./... ok amadan.net/oficina/home/handoff 12.717s ok amadan.net/oficina/home/handoff/seal 1.014s cd roster && CGO_ENABLED=1 go test -race -count=1 ./... ok amadan.net/oficina/home/roster 6.943s cd dashboard && CGO_ENABLED=1 go test -race -count=1 ./... ok amadan.net/oficina/home/dashboard 1.060s cd connect && CGO_ENABLED=1 go test -race -count=1 ./... ok amadan.net/oficina/home/connect 6.092s ok amadan.net/oficina/home/connect/linkstore 6.263s cd teammode && CGO_ENABLED=1 go test -race -count=1 ./... ok amadan.net/oficina/home/teammode 1.905s ? 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) |
| + } |
| + } |
| +} |