Files
pixa/internal/seal/keys.go
T
sneak 534b453a81
check / check (push) Canceled after 0s
Derive every key from the signing key in one place (closes #315)
seal.DeriveKeys derives the session hash and block keys, the encrypted URL
key and the CSRF key from the signing key, with their salts declared beside
it, and returns them as one seal.Keys. internal/providers calls it once at
startup and provides the value to the fx graph. The session manager, the
encrypted URL generator and the CSRF protection each take their own key.
session.NewManager, encurl.NewGenerator, newCSRFProtect and handlers.New
return no error, as nothing in them can fail now. Each key is derived as
before, with seal.DeriveKey and the same salt. The signing key itself stays
the HMAC key of signed URLs and the login password. README.md says under
signing_key what it protects.

Model: opus-5-5
2026-10-26 13:01:57 +00:00

58 lines
1.6 KiB
Go

package seal
// The salt of each key that DeriveKeys derives. Each key has a salt of its
// own, so no two uses share a key.
const (
sessionHashKeySalt = "pixa-session-hash-v1"
sessionBlockKeySalt = "pixa-session-block-v1"
encryptedURLKeySalt = "pixa-urlenc-v1"
csrfKeySalt = "pixa-csrf-v1"
)
// Keys holds the keys derived from the signing key, one for each use. The
// signing key itself is used for two things only: it is the HMAC key of signed
// URLs and the password of the login page.
type Keys struct {
// SessionHashKey signs the session cookie.
SessionHashKey [KeySize]byte
// SessionBlockKey encrypts the session cookie.
SessionBlockKey [KeySize]byte
// EncryptedURLKey encrypts the token of an encrypted URL.
EncryptedURLKey [KeySize]byte
// CSRFKey signs the CSRF token cookie of the login and generator pages.
CSRFKey [KeySize]byte
}
// DeriveKeys derives each key of Keys from signingKey, with DeriveKey and the
// salt of that key.
func DeriveKeys(signingKey string) (Keys, error) {
masterKey := []byte(signingKey)
sessionHashKey, err := DeriveKey(masterKey, sessionHashKeySalt)
if err != nil {
return Keys{}, err
}
sessionBlockKey, err := DeriveKey(masterKey, sessionBlockKeySalt)
if err != nil {
return Keys{}, err
}
encryptedURLKey, err := DeriveKey(masterKey, encryptedURLKeySalt)
if err != nil {
return Keys{}, err
}
csrfKey, err := DeriveKey(masterKey, csrfKeySalt)
if err != nil {
return Keys{}, err
}
return Keys{
SessionHashKey: sessionHashKey,
SessionBlockKey: sessionBlockKey,
EncryptedURLKey: encryptedURLKey,
CSRFKey: csrfKey,
}, nil
}