Documentation
¶
Index ¶
- Variables
- func MarshalEnvelope(env EncryptedEnvelope) ([]byte, error)
- type DataKey
- type EncryptedEnvelope
- type EnvelopeCipher
- func (c *EnvelopeCipher) NeedsRewrap(ctx context.Context, env EncryptedEnvelope) (bool, string, error)
- func (c *EnvelopeCipher) Open(ctx context.Context, env EncryptedEnvelope, aad []byte) ([]byte, error)
- func (c *EnvelopeCipher) OpenString(ctx context.Context, encoded string, aad []byte) ([]byte, error)
- func (c *EnvelopeCipher) Rewrap(ctx context.Context, env EncryptedEnvelope, aad []byte) (EncryptedEnvelope, bool, error)
- func (c *EnvelopeCipher) RewrapBatch(ctx context.Context, envs []EncryptedEnvelope, ...) ([]EncryptedEnvelope, RewrapStats, error)
- func (c *EnvelopeCipher) Seal(ctx context.Context, keyID string, plaintext, aad []byte) (EncryptedEnvelope, error)
- func (c *EnvelopeCipher) SealString(ctx context.Context, keyID string, plaintext, aad []byte) (string, error)
- type KeyMetadata
- type KeyProvider
- type KeyState
- type KeyVersionInfo
- type LocalKeySeed
- type LocalKeyring
- func (k *LocalKeyring) AddKeyVersion(seed LocalKeySeed) error
- func (k *LocalKeyring) Decrypt(ctx context.Context, keyID, keyVersionID string, ciphertext, aad []byte) ([]byte, error)
- func (k *LocalKeyring) DescribeKey(ctx context.Context, keyID string) (KeyMetadata, error)
- func (k *LocalKeyring) Encrypt(ctx context.Context, keyID string, plaintext, aad []byte) (string, []byte, error)
- func (k *LocalKeyring) GenerateDataKey(ctx context.Context, keyID string, aad []byte) (DataKey, error)
- func (k *LocalKeyring) ImportBase64Key(keyID, versionID, encoded string, state KeyState) error
- func (k *LocalKeyring) ImportHexKey(keyID, versionID, encoded string, state KeyState) error
- func (k *LocalKeyring) ProviderName() string
- func (k *LocalKeyring) ReEncrypt(ctx context.Context, keyID, sourceVersionID string, ciphertext, aad []byte) (string, []byte, error)
- func (k *LocalKeyring) RetireKeyVersion(ctx context.Context, keyID, versionID string) error
- func (k *LocalKeyring) RotateKey(ctx context.Context, keyID string, newKeyMaterial []byte) (KeyMetadata, error)
- func (k *LocalKeyring) RotateKeyWithVersion(ctx context.Context, keyID, versionID string, newKeyMaterial []byte) (KeyMetadata, error)
- type ProviderRegistry
- type RewrapStats
- type RotatableKeyProvider
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidInput = errors.New("kms: invalid input") ErrKeyNotFound = errors.New("kms: key not found") ErrVersionNotFound = errors.New("kms: key version not found") ErrKeyInactive = errors.New("kms: key version is not active for encryption") ErrVersionRetired = errors.New("kms: key version is retired") ErrDecryptionFailed = errors.New("kms: decryption failed") ErrInvalidEnvelope = errors.New("kms: invalid encrypted envelope") )
Sentinel errors returned by KMS providers and envelope helpers.
Functions ¶
func MarshalEnvelope ¶
func MarshalEnvelope(env EncryptedEnvelope) ([]byte, error)
MarshalEnvelope serializes an EncryptedEnvelope to JSON.
Types ¶
type DataKey ¶
DataKey holds a freshly generated data encryption key (DEK) and its KMS-wrapped form.
type EncryptedEnvelope ¶
type EncryptedEnvelope struct {
Version int `json:"v"`
Algorithm string `json:"alg"`
Provider string `json:"provider"`
KeyID string `json:"key_id"`
KeyVersionID string `json:"key_version_id"`
EncryptedDEK []byte `json:"encrypted_dek"`
Nonce []byte `json:"nonce"`
Ciphertext []byte `json:"ciphertext"`
CreatedAt time.Time `json:"created_at"`
}
EncryptedEnvelope is a self-describing, JSON-serializable envelope-encrypted payload containing the KMS-wrapped DEK, key version metadata, and AES-256-GCM ciphertext.
func UnmarshalEnvelope ¶
func UnmarshalEnvelope(data []byte) (EncryptedEnvelope, error)
UnmarshalEnvelope parses and validates a JSON-serialized EncryptedEnvelope.
type EnvelopeCipher ¶
type EnvelopeCipher struct {
// contains filtered or unexported fields
}
EnvelopeCipher performs AES-256-GCM envelope encryption, decryption, and zero-downtime DEK re-wrapping using a pluggable KeyProvider.
func NewEnvelopeCipher ¶
func NewEnvelopeCipher(provider KeyProvider) (*EnvelopeCipher, error)
NewEnvelopeCipher constructs an EnvelopeCipher backed by provider.
func (*EnvelopeCipher) NeedsRewrap ¶
func (c *EnvelopeCipher) NeedsRewrap(ctx context.Context, env EncryptedEnvelope) (bool, string, error)
NeedsRewrap checks whether env's KeyVersionID differs from the active version reported by the KMS provider, returning (needsRewrap, activeVersionID, err).
func (*EnvelopeCipher) Open ¶
func (c *EnvelopeCipher) Open(ctx context.Context, env EncryptedEnvelope, aad []byte) ([]byte, error)
Open unwraps the envelope's DEK via the KMS provider and decrypts the payload.
func (*EnvelopeCipher) OpenString ¶
func (c *EnvelopeCipher) OpenString(ctx context.Context, encoded string, aad []byte) ([]byte, error)
OpenString decodes a base64-encoded JSON envelope string and decrypts its payload.
func (*EnvelopeCipher) Rewrap ¶
func (c *EnvelopeCipher) Rewrap(ctx context.Context, env EncryptedEnvelope, aad []byte) (EncryptedEnvelope, bool, error)
Rewrap re-encrypts the envelope's DEK under the current active key version without modifying the payload ciphertext or exposing the plaintext DEK. If the envelope is already encrypted under the active version, it returns (env, false, nil) idempotently.
func (*EnvelopeCipher) RewrapBatch ¶
func (c *EnvelopeCipher) RewrapBatch( ctx context.Context, envs []EncryptedEnvelope, aadFn func(index int, env EncryptedEnvelope) []byte, ) ([]EncryptedEnvelope, RewrapStats, error)
RewrapBatch re-wraps a slice of envelopes onto the current active key version. If aadFn is nil, empty AAD is used for every item.
func (*EnvelopeCipher) Seal ¶
func (c *EnvelopeCipher) Seal(ctx context.Context, keyID string, plaintext, aad []byte) (EncryptedEnvelope, error)
Seal generates a fresh DEK via the KMS provider, encrypts plaintext locally using AES-256-GCM, wipes the plaintext DEK, and returns an EncryptedEnvelope.
func (*EnvelopeCipher) SealString ¶
func (c *EnvelopeCipher) SealString(ctx context.Context, keyID string, plaintext, aad []byte) (string, error)
SealString is a convenience helper that encrypts plaintext and returns a base64-encoded JSON envelope string.
type KeyMetadata ¶
type KeyMetadata struct {
KeyID string `json:"key_id"`
Provider string `json:"provider"`
Algorithm string `json:"algorithm"`
ActiveVersionID string `json:"active_version_id"`
Versions []KeyVersionInfo `json:"versions"`
}
KeyMetadata describes a logical KMS key and its version history.
type KeyProvider ¶
type KeyProvider interface {
// ProviderName returns a stable identifier for the backend (e.g. "local", "aws-kms", "vault").
ProviderName() string
// GenerateDataKey creates a fresh 256-bit DEK and wraps it under the active version of keyID.
GenerateDataKey(ctx context.Context, keyID string, aad []byte) (DataKey, error)
// Encrypt encrypts/wraps plaintext under the active version of keyID and returns the version used.
Encrypt(ctx context.Context, keyID string, plaintext, aad []byte) (keyVersionID string, ciphertext []byte, err error)
// Decrypt decrypts/unwraps ciphertext using keyID and keyVersionID (or embedded version if empty).
Decrypt(ctx context.Context, keyID, keyVersionID string, ciphertext, aad []byte) ([]byte, error)
// ReEncrypt unwraps ciphertext under sourceVersionID and re-wraps it under the current active
// version of keyID without returning plaintext key material to the caller.
ReEncrypt(ctx context.Context, keyID, sourceVersionID string, ciphertext, aad []byte) (newVersionID string, newCiphertext []byte, err error)
// DescribeKey returns metadata including the current active version and known key versions.
DescribeKey(ctx context.Context, keyID string) (KeyMetadata, error)
}
KeyProvider is the pluggable KMS abstraction designed to support local keyrings as well as external KMS backends (e.g., AWS KMS, HashiCorp Vault Transit).
type KeyState ¶
type KeyState string
KeyState represents the lifecycle state of a specific key version.
const ( // KeyStateActive indicates the current primary version used for Encrypt and GenerateDataKey. KeyStateActive KeyState = "active" // KeyStateDecryptOnly indicates a previous version retained for zero-downtime Decrypt and ReEncrypt. KeyStateDecryptOnly KeyState = "decrypt_only" // KeyStateRetired indicates a decommissioned version that can no longer decrypt or re-encrypt. KeyStateRetired KeyState = "retired" )
type KeyVersionInfo ¶
type KeyVersionInfo struct {
VersionID string `json:"version_id"`
State KeyState `json:"state"`
CreatedAt time.Time `json:"created_at"`
}
KeyVersionInfo describes a single immutable version of a master/KEK key.
type LocalKeySeed ¶
type LocalKeySeed struct {
KeyID string
VersionID string
Key []byte
State KeyState
CreatedAt time.Time
}
LocalKeySeed seeds a versioned key in LocalKeyring during initialization.
type LocalKeyring ¶
type LocalKeyring struct {
// contains filtered or unexported fields
}
LocalKeyring is an in-memory, thread-safe, versioned AES-256-GCM KeyProvider suitable for local development, deterministic testing, and config-injected KEKs.
func NewLocalKeyring ¶
func NewLocalKeyring(seeds ...LocalKeySeed) (*LocalKeyring, error)
NewLocalKeyring constructs a LocalKeyring initialized with optional key seeds.
func NewLocalKeyringWithGeneratedKey ¶
func NewLocalKeyringWithGeneratedKey(keyID string) (*LocalKeyring, error)
NewLocalKeyringWithGeneratedKey constructs a LocalKeyring containing one freshly generated active 256-bit key under keyID (version "v1").
func (*LocalKeyring) AddKeyVersion ¶
func (k *LocalKeyring) AddKeyVersion(seed LocalKeySeed) error
AddKeyVersion registers a specific key version into the keyring. If state is empty or KeyStateActive, it becomes the active version for keyID and any previous active version is demoted to KeyStateDecryptOnly.
func (*LocalKeyring) Decrypt ¶
func (k *LocalKeyring) Decrypt(ctx context.Context, keyID, keyVersionID string, ciphertext, aad []byte) ([]byte, error)
Decrypt implements KeyProvider. If keyVersionID is empty, the version ID is extracted from the wrapped ciphertext header.
func (*LocalKeyring) DescribeKey ¶
func (k *LocalKeyring) DescribeKey(ctx context.Context, keyID string) (KeyMetadata, error)
DescribeKey implements KeyProvider.
func (*LocalKeyring) Encrypt ¶
func (k *LocalKeyring) Encrypt(ctx context.Context, keyID string, plaintext, aad []byte) (string, []byte, error)
Encrypt implements KeyProvider using the active version of keyID.
func (*LocalKeyring) GenerateDataKey ¶
func (k *LocalKeyring) GenerateDataKey(ctx context.Context, keyID string, aad []byte) (DataKey, error)
GenerateDataKey implements KeyProvider.
func (*LocalKeyring) ImportBase64Key ¶
func (k *LocalKeyring) ImportBase64Key(keyID, versionID, encoded string, state KeyState) error
ImportBase64Key decodes a base64-encoded 32-byte key and registers it.
func (*LocalKeyring) ImportHexKey ¶
func (k *LocalKeyring) ImportHexKey(keyID, versionID, encoded string, state KeyState) error
ImportHexKey decodes a hex-encoded 32-byte key and registers it.
func (*LocalKeyring) ProviderName ¶
func (k *LocalKeyring) ProviderName() string
ProviderName implements KeyProvider.
func (*LocalKeyring) ReEncrypt ¶
func (k *LocalKeyring) ReEncrypt(ctx context.Context, keyID, sourceVersionID string, ciphertext, aad []byte) (string, []byte, error)
ReEncrypt implements KeyProvider by unwrapping under sourceVersionID and re-wrapping under the currently active version of keyID in a single locked step.
func (*LocalKeyring) RetireKeyVersion ¶
func (k *LocalKeyring) RetireKeyVersion(ctx context.Context, keyID, versionID string) error
RetireKeyVersion marks a non-active key version as KeyStateRetired so it can no longer be used for Decrypt or ReEncrypt.
func (*LocalKeyring) RotateKey ¶
func (k *LocalKeyring) RotateKey(ctx context.Context, keyID string, newKeyMaterial []byte) (KeyMetadata, error)
RotateKey creates or registers a new active key version for keyID while keeping prior active versions in KeyStateDecryptOnly for zero-downtime rotation. If newKeyMaterial is empty, a cryptographically random 256-bit key is generated.
func (*LocalKeyring) RotateKeyWithVersion ¶
func (k *LocalKeyring) RotateKeyWithVersion(ctx context.Context, keyID, versionID string, newKeyMaterial []byte) (KeyMetadata, error)
RotateKeyWithVersion behaves like RotateKey with an optional explicit versionID.
type ProviderRegistry ¶
type ProviderRegistry struct {
// contains filtered or unexported fields
}
ProviderRegistry resolves named KeyProvider instances at runtime without coupling callers to concrete KMS implementations.
func NewProviderRegistry ¶
func NewProviderRegistry() *ProviderRegistry
NewProviderRegistry creates an empty ProviderRegistry.
func (*ProviderRegistry) Get ¶
func (r *ProviderRegistry) Get(name string) (KeyProvider, bool)
Get retrieves a registered provider by name.
func (*ProviderRegistry) Register ¶
func (r *ProviderRegistry) Register(p KeyProvider) error
Register adds or replaces a KeyProvider under its ProviderName().
type RewrapStats ¶
type RewrapStats struct {
Total int `json:"total"`
Rewrapped int `json:"rewrapped"`
Unchanged int `json:"unchanged"`
}
RewrapStats summarizes a batch key-rotation re-wrap pass.
type RotatableKeyProvider ¶
type RotatableKeyProvider interface {
KeyProvider
RotateKey(ctx context.Context, keyID string, newKeyMaterial []byte) (KeyMetadata, error)
RetireKeyVersion(ctx context.Context, keyID, versionID string) error
}
RotatableKeyProvider is an optional capability implemented by KMS providers that support programmatic key rotation and version retirement.