| 1 | // Package seal encrypts purpose-bound values for app handoffs and local storage. |
| 2 | package seal |
| 3 | |
| 4 | import ( |
| 5 | "crypto/aes" |
| 6 | "crypto/cipher" |
| 7 | "crypto/hkdf" |
| 8 | "crypto/rand" |
| 9 | "crypto/sha256" |
| 10 | "errors" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | // Sealer owns a copy of the key and an application namespace. |
| 15 | type Sealer struct { |
| 16 | dek []byte |
| 17 | namespace string |
| 18 | } |
| 19 | |
| 20 | // New requires a 32-byte key and an explicit namespace. Keep the namespace |
| 21 | // stable: changing it makes previously sealed values unreadable. |
| 22 | func New(dek []byte, namespace string) (*Sealer, error) { |
| 23 | if len(dek) != 32 { |
| 24 | return nil, errors.New("seal: DEK must be 32 bytes") |
| 25 | } |
| 26 | if len(namespace) == 0 || len(namespace) > 128 { |
| 27 | return nil, errors.New("seal: invalid namespace") |
| 28 | } |
| 29 | for _, b := range []byte(namespace) { |
| 30 | if !(b >= 'a' && b <= 'z' || b >= 'A' && b <= 'Z' || b >= '0' && b <= '9' || b == '/' || b == '_' || b == '-') { |
| 31 | return nil, errors.New("seal: invalid namespace") |
| 32 | } |
| 33 | } |
| 34 | return &Sealer{dek: append([]byte(nil), dek...), namespace: namespace}, nil |
| 35 | } |
| 36 | |
| 37 | func (s *Sealer) purposeKey(purpose string) ([]byte, error) { |
| 38 | return hkdf.Key(sha256.New, s.dek, nil, s.namespace+":"+purpose, 32) |
| 39 | } |
| 40 | func (s *Sealer) aeadFor(purpose string) (cipher.AEAD, error) { |
| 41 | key, err := s.purposeKey(purpose) |
| 42 | if err != nil { |
| 43 | return nil, err |
| 44 | } |
| 45 | block, err := aes.NewCipher(key) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return cipher.NewGCM(block) |
| 50 | } |
| 51 | |
| 52 | // deriveInfo starts every Derive label's HKDF info. Purpose keys use |
| 53 | // namespace+":"+purpose, and a namespace may not contain a NUL, so no |
| 54 | // purpose key can equal a derived key whatever the label: the two are |
| 55 | // separate domains under one DEK. |
| 56 | const deriveInfo = "oficina/seal/derive/v1\x00" |
| 57 | |
| 58 | // Derive returns a 32-byte key for label, for a consumer that keys its |
| 59 | // own construction rather than sealing through this Sealer (Home's |
| 60 | // remembered-sign-in cookie, under "home/lastsignin/v1"). Each label gets |
| 61 | // its own key, and none is any purpose's sealing key. The label must be |
| 62 | // 1 to 128 bytes with no NUL; keep it stable, because changing it changes |
| 63 | // the key. |
| 64 | func (s *Sealer) Derive(label string) ([]byte, error) { |
| 65 | if len(label) == 0 || len(label) > 128 || strings.ContainsRune(label, 0) { |
| 66 | return nil, errors.New("seal: invalid derive label") |
| 67 | } |
| 68 | return hkdf.Key(sha256.New, s.dek, nil, deriveInfo+s.namespace+"\x00"+label, 32) |
| 69 | } |
| 70 | |
| 71 | // Seal uses the historical Oficina envelope: random nonce followed by GCM |
| 72 | // ciphertext and authentication tag. The namespace and purpose bind the key. |
| 73 | func (s *Sealer) Seal(purpose string, plaintext []byte) ([]byte, error) { |
| 74 | aead, err := s.aeadFor(purpose) |
| 75 | if err != nil { |
| 76 | return nil, err |
| 77 | } |
| 78 | nonce := make([]byte, aead.NonceSize()) |
| 79 | if _, err := rand.Read(nonce); err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | return aead.Seal(nonce, nonce, plaintext, nil), nil |
| 83 | } |
| 84 | |
| 85 | // Open refuses a different key, namespace or purpose, and altered ciphertext. |
| 86 | func (s *Sealer) Open(purpose string, ct []byte) ([]byte, error) { |
| 87 | aead, err := s.aeadFor(purpose) |
| 88 | if err != nil { |
| 89 | return nil, err |
| 90 | } |
| 91 | if len(ct) < aead.NonceSize() { |
| 92 | return nil, errors.New("seal: ciphertext too short") |
| 93 | } |
| 94 | return aead.Open(nil, ct[:aead.NonceSize()], ct[aead.NonceSize():], nil) |
| 95 | } |
| 96 | |