Documentation
¶
Overview ¶
Package crypto provides envelope encryption primitives shared by components that need to protect data at rest that includes a pluggable key-encryption-key provider, and the envelope format that wraps a data-encryption key alongside its ciphertext.
The split exists so that a deployment configures key management once. The same provider that protects persisted state can protect any other recoverable secret a component holds.
Index ¶
- Constants
- Variables
- func EnvelopeKeyID(sealed []byte) (string, error)
- func GenerateLocalKey() ([]byte, error)
- func GenerateSalt() ([]byte, error)
- func IsEnvelope(payload []byte) bool
- func Open(ctx context.Context, provider KeyProvider, sealed []byte, ...) ([]byte, error)
- func Seal(ctx context.Context, provider KeyProvider, scope string, plaintext []byte, ...) ([]byte, error)
- type KeyProvider
Constants ¶
const DEKSize = 32
DEKSize is the length in bytes of the data-encryption keys this package generates and expects, matching AES-256.
const ( // SaltSize is the length of the salt a passphrase provider needs. A deployment persists // it alongside the data it protects. A salt is not secret, and losing it loses the data. SaltSize = 16 )
Argon2id parameters for passphrase-derived keys, at the OWASP-recommended minimum of 19MiB memory with two iterations. Deriving happens once at startup rather than per object, so the cost is paid once and does not bear on write throughput.
Variables ¶
var ErrNotEnvelope = errors.New("crypto: payload is not an envelope")
ErrNotEnvelope is returned when a payload does not carry the envelope magic, which means it was written before encryption was enabled.
Functions ¶
func EnvelopeKeyID ¶
EnvelopeKeyID returns the id of the key-encryption key an envelope was sealed with, without decrypting it or reaching a key provider.
This lets a caller tell which key protects an object, which is what makes re-encrypting a store resumable, an object already sealed under the key being moved to can be left alone.
Returns ErrNotEnvelope for a payload written before encryption was enabled.
func GenerateLocalKey ¶
GenerateLocalKey returns a random key-encryption key suitable for a local key file.
func GenerateSalt ¶
GenerateSalt returns a random salt suitable for a passphrase provider.
func IsEnvelope ¶
IsEnvelope reports whether a payload carries the envelope magic.
Lets a caller tell data written under encryption from data written before it was enabled, without attempting to decrypt.
func Open ¶
func Open( ctx context.Context, provider KeyProvider, sealed []byte, associatedData []byte, ) ([]byte, error)
Open decrypts an envelope produced by Seal.
Returns ErrNotEnvelope for a payload without the magic prefix, which a caller reading a store written before encryption was enabled treats as plaintext rather than an error.
func Seal ¶
func Seal( ctx context.Context, provider KeyProvider, scope string, plaintext []byte, associatedData []byte, ) ([]byte, error)
Seal encrypts plaintext under a freshly generated data-encryption key, wrapped by the provider's key-encryption key, and returns the serialised envelope.
scope is passed to the provider unchanged; see KeyProvider.GenerateDataKey.
associatedData is authenticated but not encrypted, so a reader passing different associated data gets a failure rather than plaintext. Callers pass something that names where the ciphertext belongs, so an envelope moved elsewhere no longer decrypts. Open needs no scope, since the envelope records which key-encryption key sealed it.
Types ¶
type KeyProvider ¶
type KeyProvider interface {
// ID identifies the active KEK.
//
// Recorded in each envelope so a key can be rotated without rewriting everything already
// encrypted, an envelope names the KEK it was sealed with, and a provider that recognises
// more than one can still unwrap the older ones.
ID() string
// GenerateDataKey returns a fresh data-encryption key, in plaintext and wrapped form,
// for data belonging to the given scope.
//
// The intention is one key per object, and the envelope layer calls this for every
// object it seals. How much a caller puts in an object, and whether it needs keys
// grouped more coarsely, varies by use case.
//
// The scope is that coarser grouping, naming what the data belongs to, whether that be
// the instance for blueprint state or another category. This way, a provider can select
// a per-scope key-encryption key, which is what lets one scope's data be made
// unreadable by destroying a key.
//
// An implementation may reuse a key across objects within one scope to save round
// trips, but must never share one across scopes, and must bound what a key covers by
// object count, bytes and age: the envelope's nonce is then the only thing preventing
// keystream reuse.
GenerateDataKey(
ctx context.Context,
scope string,
) (plaintext []byte, wrapped []byte, err error)
// UnwrapKey decrypts a data-encryption key that was wrapped with the named KEK.
//
// The kekID comes from the envelope rather than from the provider's current state, so an
// implementation must fail cleanly when asked for a key it does not have rather than
// assume the active one.
UnwrapKey(ctx context.Context, kekID string, wrapped []byte) ([]byte, error)
}
KeyProvider wraps and unwraps data-encryption keys with a key-encryption key.
Implementations range from a passphrase-derived key held in the process to a cloud key management service that never releases the KEK at all. Callers generate a fresh DEK per object, encrypt with it, and persist only the wrapped form, so the KEK is the single thing a deployment has to protect.
func NewKeySet ¶
func NewKeySet(active KeyProvider, retired ...KeyProvider) (KeyProvider, error)
NewKeySet returns a provider that seals with the active key and unwraps with the active key or any retired one.
func NewLocalFileKeyProvider ¶
func NewLocalFileKeyProvider(path string) (KeyProvider, error)
NewLocalFileKeyProvider reads a raw 32-byte key-encryption key from a file.
The file's permissions are checked rather than assumed, a key readable by other users is not deemed a secure key, and failing at startup is better than encrypting against it.
func NewPassphraseKeyProvider ¶
func NewPassphraseKeyProvider(passphrase string, salt []byte) (KeyProvider, error)
NewPassphraseKeyProvider derives a key-encryption key from a passphrase using Argon2id.
The salt must be stable for the life of the data, the same passphrase with a different salt derives a different key and cannot unwrap anything previously wrapped.