// Copyright (c) 2026 Xessenger // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. // Package storeseal seals what bridgekit and its extensions keep in the bridge database about // encrypted rooms (QA-289): the keys of encrypted pictures, the network IDs behind hashed ghosts, // and fingerprints of the metadata sent into encrypted rooms. The key is the bridge's store key, // which only the bridge's enclave can derive (bridgekit's keys.go), so whoever reads or writes the // database cannot open a sealed value, move it to another row, or test a guess against a // fingerprint. package storeseal import ( "crypto/aes" "crypto/cipher" "crypto/hkdf" "crypto/hmac" "crypto/rand" "crypto/sha256" "encoding/base64" "encoding/hex" "errors" "fmt" "strings" ) // KeySize is the length of the store key. const KeySize = 32 // prefix marks the format of a sealed value: v1 is AES-256-GCM, base64 (standard, unpadded) of the // 12-byte nonce followed by the ciphertext. const prefix = "v1:" // ErrNoKey is returned by a Sealer made without a key. var ErrNoKey = errors.New("storeseal: no store key") // Sealer seals and fingerprints values with one bridge's store key. Its zero value, and a nil // *Sealer, have no key: Ready reports false and every other method fails with ErrNoKey. type Sealer struct { aead cipher.AEAD macKey []byte } // New makes a Sealer from the store key, which must be KeySize bytes or empty (no key). Sealing and // fingerprinting use separate keys derived from it. func New(key []byte) (*Sealer, error) { if len(key) == 0 { return &Sealer{}, nil } else if len(key) != KeySize { return nil, fmt.Errorf("storeseal: the store key must be %d bytes, not %d", KeySize, len(key)) } sealKey, err := hkdf.Key(sha256.New, key, nil, "xessenger/bridgekit/store/v1/seal", 32) if err != nil { return nil, err } defer clear(sealKey) macKey, err := hkdf.Key(sha256.New, key, nil, "xessenger/bridgekit/store/v1/mac", 32) if err != nil { return nil, err } block, err := aes.NewCipher(sealKey) if err != nil { return nil, err } aead, err := cipher.NewGCM(block) if err != nil { return nil, err } return &Sealer{aead: aead, macKey: macKey}, nil } // Ready reports whether the Sealer has a key. func (s *Sealer) Ready() bool { return s != nil && s.aead != nil } // Seal encrypts plain, bound to ad: the value opens only with the same ad, which should name what // the value is and where it is kept (for example its kind, the bridge and the row's key). func (s *Sealer) Seal(ad string, plain []byte) (string, error) { if !s.Ready() { return "", ErrNoKey } nonce := make([]byte, s.aead.NonceSize()) if _, err := rand.Read(nonce); err != nil { return "", err } return prefix + base64.RawStdEncoding.EncodeToString(s.aead.Seal(nonce, nonce, plain, []byte(ad))), nil } // Open decrypts a value Seal made with the same ad. func (s *Sealer) Open(ad string, sealed string) ([]byte, error) { if !s.Ready() { return nil, ErrNoKey } encoded, ok := strings.CutPrefix(sealed, prefix) if !ok { return nil, errors.New("storeseal: unknown sealed value format") } data, err := base64.RawStdEncoding.DecodeString(encoded) if err != nil { return nil, fmt.Errorf("storeseal: %w", err) } if len(data) < s.aead.NonceSize()+s.aead.Overhead() { return nil, errors.New("storeseal: sealed value is too short") } nonce, ciphertext := data[:s.aead.NonceSize()], data[s.aead.NonceSize():] plain, err := s.aead.Open(nil, nonce, ciphertext, []byte(ad)) if err != nil { return nil, errors.New("storeseal: the value does not open with this key and context") } return plain, nil } // MAC fingerprints data under ad, as hex: the same data and ad give the same fingerprint, and // without the store key nobody can compute one, so a fingerprint kept in the database does not let // its reader check guesses of the data. func (s *Sealer) MAC(ad string, data []byte) (string, error) { if !s.Ready() { return "", ErrNoKey } mac := hmac.New(sha256.New, s.macKey) // The length of ad keeps (ad, data) pairs apart that would concatenate to the same bytes. fmt.Fprintf(mac, "%d:%s", len(ad), ad) mac.Write(data) return hex.EncodeToString(mac.Sum(nil)), nil }