Documentation
¶
Overview ¶
Package crypto provides AES-GCM-256 encryption primitives and master-key helpers for Relay's stored-secret subsystem, plus the EdDSA JWT format inference tokens are signed with (jwt.go). All functions are pure; the package has no state.
The inference token's claim set lives here rather than in a caller so the minting and verifying sides cannot drift apart.
Index ¶
- Constants
- Variables
- func Decrypt(masterKey, ciphertext, nonce []byte) ([]byte, error)
- func Encrypt(masterKey, plaintext []byte) (ciphertext, nonce []byte, err error)
- func GenerateMasterKey() (string, error)
- func KeyID(pub ed25519.PublicKey) string
- func ParseMasterKey(raw string) ([]byte, error)
- func SignToken(priv ed25519.PrivateKey, kid string, claims TokenClaims) (string, error)
- func TokenKeyID(token string) string
- type TokenClaims
Constants ¶
const TokenIssuer = "relay"
TokenIssuer is the only `iss` a relay-minted token carries.
Variables ¶
var ( ErrTokenMalformed = errors.New("crypto: malformed token") ErrTokenSignature = errors.New("crypto: token signature does not verify") )
ErrTokenMalformed is returned for anything that isn't a well-formed EdDSA JWT; ErrTokenSignature for a well-formed one that doesn't verify.
Functions ¶
func Decrypt ¶
Decrypt decrypts ciphertext (with GCM auth tag) using masterKey and nonce. Returns an error if the tag is invalid or inputs are malformed; never panics.
func Encrypt ¶
Encrypt encrypts plaintext with AES-GCM-256 using masterKey. masterKey must be exactly 32 bytes. A fresh 12-byte nonce is read from crypto/rand on each call. Returns ciphertext (with GCM auth tag appended by gcm.Seal) and nonce separately; callers store them in distinct DB columns.
func GenerateMasterKey ¶
GenerateMasterKey generates 32 random bytes and returns them as a base64.StdEncoding string suitable for use as RELAY_MASTER_KEY.
func KeyID ¶ added in v0.10.0
KeyID names a public key in a token's `kid` header. Truncated sha256 of the key bytes: stable across processes, and it reveals nothing the token signature doesn't already.
func ParseMasterKey ¶
ParseMasterKey base64-decodes (StdEncoding) raw and validates the result is exactly 32 bytes. Returns the raw key bytes or a structured error.
func SignToken ¶ added in v0.10.0
func SignToken(priv ed25519.PrivateKey, kid string, claims TokenClaims) (string, error)
SignToken returns the compact JWT for claims, signed with priv. kid names the verification key so a rotation can keep the previous one live; an empty kid writes the bare header.
func TokenKeyID ¶ added in v0.10.0
TokenKeyID reports the `kid` a token names, or "" when it carries none. Reads the header only — nothing here is a trust decision.
Types ¶
type TokenClaims ¶ added in v0.10.0
type TokenClaims struct {
Iss string `json:"iss"`
Sub string `json:"sub"` // "user:<id>"
Prj string `json:"prj"` // project id
Grp []string `json:"grp,omitempty"`
Ver int `json:"ver"` // the user's token version at mint time
Jti string `json:"jti"`
Iat int64 `json:"iat"`
Exp int64 `json:"exp"`
}
TokenClaims is the inference token's payload. Every field is required except Grp, which is empty for a user in no groups.
func ParseToken ¶ added in v0.10.0
func ParseToken(pub ed25519.PublicKey, token string) (TokenClaims, error)
ParseToken verifies the signature with pub and returns the claims. It checks nothing else — expiry, issuer and version are the caller's policy, not the format's.
func (TokenClaims) UserID ¶ added in v0.10.0
func (c TokenClaims) UserID() string
UserID returns the user id the "user:<id>" subject names, or "" when the subject is malformed or names another principal kind.