envelope

package
v0.0.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package envelope implements KMS-native envelope encryption using a two-tier key hierarchy: Key Encryption Keys (KEKs) protect per-scope Data Encryption Keys (DEKs), and DEKs protect application data via local AEAD.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Envelope added in v0.0.5

type Envelope struct {
	// contains filtered or unexported fields
}

Envelope coordinates DEK generation, KEK-based wrapping/unwrapping/rewrapping with mandatory KeyScope AAD binding, and local AEAD data encryption/decryption. It is safe for concurrent use.

func New added in v0.0.5

func New(keyring kek.Keyring, c cipher.Cipher) *Envelope

New returns an Envelope wired to the given KEK keyring and symmetric data cipher. It panics if keyring or c is nil.

func (*Envelope) ActiveKEKID added in v0.0.6

func (e *Envelope) ActiveKEKID() string

ActiveKEKID returns the active KEK identifier from the keyring.

func (*Envelope) Decrypt added in v0.0.5

func (e *Envelope) Decrypt(ctx context.Context, key *encryption.EncryptedKey, ciphertext, aad []byte) ([]byte, error)

Decrypt unwraps key, decrypts ciphertext verifying optional payload aad, and scrubs the plaintext DEK from memory before returning.

func (*Envelope) DecryptString added in v0.0.6

func (e *Envelope) DecryptString(ctx context.Context, key *encryption.EncryptedKey, ciphertext string, aad []byte) (string, error)

DecryptString unwraps key, decrypts base64-encoded ciphertext verifying optional payload aad, and returns the UTF-8 plaintext string.

func (*Envelope) Encrypt added in v0.0.5

func (e *Envelope) Encrypt(ctx context.Context, key *encryption.EncryptedKey, plaintext, aad []byte) ([]byte, error)

Encrypt unwraps key, encrypts plaintext with optional payload aad, and scrubs the plaintext DEK from memory before returning.

func (*Envelope) EncryptString added in v0.0.6

func (e *Envelope) EncryptString(ctx context.Context, key *encryption.EncryptedKey, plaintext string, aad []byte) (string, error)

EncryptString unwraps key, encrypts a UTF-8 plaintext string with optional payload aad, and returns base64-encoded ciphertext.

func (*Envelope) GenerateDEK added in v0.0.5

func (e *Envelope) GenerateDEK() ([]byte, error)

GenerateDEK generates a new cryptographically secure random DEK matching the data cipher's key size. The caller is responsible for scrubbing memory with encryption.ZeroBytes(dek) when done.

func (*Envelope) OpenSession added in v0.0.6

func (e *Envelope) OpenSession(ctx context.Context, key *encryption.EncryptedKey) (*Session, error)

OpenSession unwraps key once and returns a reusable in-memory Session for high-throughput batch or stream encryption and decryption without repeated KMS calls. Callers must call defer session.Close() to scrub the plaintext DEK from memory.

func (*Envelope) RewrapDEK added in v0.0.6

func (e *Envelope) RewrapDEK(ctx context.Context, key *encryption.EncryptedKey, targetKEKID string) ([]byte, error)

RewrapDEK re-encrypts a stored DEK from key.KEKID to targetKEKID, verifying and preserving the canonical KeyScope AAD.

func (*Envelope) UnwrapDEK added in v0.0.5

func (e *Envelope) UnwrapDEK(ctx context.Context, key *encryption.EncryptedKey) ([]byte, error)

UnwrapDEK decrypts an EncryptedKey's DEK using the KEK identified by key.KEKID, verifying the canonical KeyScope AAD. The caller is responsible for scrubbing the returned slice with encryption.ZeroBytes(dek) when done.

func (*Envelope) WrapDEK added in v0.0.5

func (e *Envelope) WrapDEK(ctx context.Context, scope encryption.KeyScope, dek []byte) (kekID string, encryptedDEK []byte, err error)

WrapDEK encrypts a raw plaintext DEK using the keyring's active KEK, cryptographically binding it to scope.AAD().

type Session added in v0.0.6

type Session struct {
	// contains filtered or unexported fields
}

Session holds an unwrapped DEK in memory for performing multiple local AEAD encrypt and decrypt operations without making repeated KMS unwrap calls.

A Session is safe for concurrent use and must be closed via Close when the unit of work finishes to scrub the plaintext DEK from memory.

func (*Session) Close added in v0.0.6

func (s *Session) Close()

Close scrubs the plaintext DEK from memory and marks the session as closed. Subsequent calls to Close are no-ops.

func (*Session) Decrypt added in v0.0.6

func (s *Session) Decrypt(ciphertext, aad []byte) ([]byte, error)

Decrypt decrypts ciphertext with the session's DEK and verifies optional aad.

func (*Session) DecryptString added in v0.0.6

func (s *Session) DecryptString(ciphertext string, aad []byte) (string, error)

DecryptString decodes base64-encoded ciphertext, decrypts it, and returns the UTF-8 plaintext string.

func (*Session) Encrypt added in v0.0.6

func (s *Session) Encrypt(plaintext, aad []byte) ([]byte, error)

Encrypt encrypts plaintext with the session's DEK and binds it to optional aad.

func (*Session) EncryptString added in v0.0.6

func (s *Session) EncryptString(plaintext string, aad []byte) (string, error)

EncryptString encrypts a UTF-8 plaintext string and returns base64-encoded ciphertext.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL