Documentation ¶
Index ¶
Constants ¶
const ( // SchemeName is the name of the encryption scheme. SchemeName = "dapr.io/enc/v1" // Size of each segment in the encrypted message. // Each segment is exactly 64KB in length, except the last one which could be shorter. SegmentSize = 64 << 10 // Overhead of each segment in bytes. // This is equivalent to the size of the authentication tag for AES-GCM and ChaCha20-Poly1305. SegmentOverhead = 16 // Length of the nonce prefix. NoncePrefixLength = 7 )
Variables ¶
var ( // Error returned when trying to decrypt a document whose manifest does not contain a key name, and the caller did not provide an explicit key name. ErrDecryptionKeyMissing = errors.New("document's manifest does not contain a key name, and no key name was provided") // Error returned when the signature of the document could not be validated. ErrDecryptionSignature = errors.New("failed to validate the document's signature") // Error returned when the deryption fails. // Most commonly this happens when a segment has been tampered with. ErrDecryptionFailed = errors.New("failed to decrypt segment") )
var BufPool = sync.Pool{ New: func() any { const bufSize = SegmentSize + SegmentOverhead + 1 b := make([]byte, bufSize) return &b }, }
BufPool is a sync.Pool that returns buffers of SegmentSize+SegmentOverhead, plus one extra byte
Functions ¶
Types ¶
type Cipher ¶
type Cipher string
Cipher used to encrypt the file.
func NewCipherFromID ¶
NewCipherFromID returns a Cipher from its ID.
func (Cipher) MarshalJSON ¶
MarhsalJSON implements json.Marshaler.
func (*Cipher) UnmarshalJSON ¶
UnmarshalJSON implements json.Unmarshaler.
type DecryptOptions ¶
type DecryptOptions struct { // Function that is invoked to unwrap the key UnwrapKeyFn UnwrapKeyFn // If set, uses this value as key name rather than the one included in the manifest KeyName string }
DecryptOptions contains the options passed to the Decrypt method
type EncryptOptions ¶
type EncryptOptions struct { // Function that is invoked to wrap the key WrapKeyFn WrapKeyFn // Algorithm used to wrap the file key // This must be one of the supported KeyAlgorithm constants, and must be usable by the kind of key provided Algorithm KeyAlgorithm // Name of the key to use KeyName string // Name of the key to include as decryption key // If empty, uses KeyName DecryptionKeyName string // If true, does not include the key name in the manifest OmitKeyName bool // Cipher used to encrypt the data // If nil, defaults to AES-GCM Cipher *Cipher }
EncryptOptions contains the options passed to the Encrypt method
type KeyAlgorithm ¶
type KeyAlgorithm string
Algorithm used to wrap the file key.
const ( KeyAlgorithmAES256KW KeyAlgorithm = "A256KW" KeyAlgorithmAES128CBC KeyAlgorithm = "A128CBC-NOPAD" KeyAlgorithmAES192CBC KeyAlgorithm = "A192CBC-NOPAD" KeyAlgorithmAES256CBC KeyAlgorithm = "A256CBC-NOPAD" KeyAlgorithmRSAOAEP256 KeyAlgorithm = "RSA-OAEP-256" KeyAlgorithmAES KeyAlgorithm = "AES" // Alias for A256KW KeyAlgorithmRSA KeyAlgorithm = "RSA" // Alias for RSA-OAEP-256 )
func NewKeyAlgorithmFromID ¶
func NewKeyAlgorithmFromID(id int) (KeyAlgorithm, error)
NewKeyAlgorithmFromID returns a KeyAlgorithm from its ID.
func (KeyAlgorithm) ID ¶
func (a KeyAlgorithm) ID() int
ID returns the numeric ID for the algorithm.
func (KeyAlgorithm) MarshalJSON ¶
func (a KeyAlgorithm) MarshalJSON() ([]byte, error)
MarhsalJSON implements json.Marshaler.
func (*KeyAlgorithm) UnmarshalJSON ¶
func (a *KeyAlgorithm) UnmarshalJSON(dataB []byte) error
UnmarshalJSON implements json.Unmarshaler.
func (KeyAlgorithm) Validate ¶
func (a KeyAlgorithm) Validate() (KeyAlgorithm, error)
Validate the passed algorithm and resolves aliases.
type Manifest ¶
type Manifest struct { // Name of the key that can be used to decrypt the message. // This is optional, and if specified can be in the format `key` or `key/version`. KeyName string `json:"k,omitempty"` // ID of the wrapping algorithm used. KeyWrappingAlgorithm KeyAlgorithm `json:"kw"` // The Wrapped File Key. WFK []byte `json:"wfk"` // ID of the cipher used. Cipher Cipher `json:"cph"` // Random sequence of 7 bytes generated by a CSPRNG NoncePrefix []byte `json:"np"` }
Manifest contains the properties for the clear-text manifest which is added at the beginning of the encrypted document.
type UnwrapKeyFn ¶
type UnwrapKeyFn = func(wrappedKey []byte, algorithm string, keyName string, nonce []byte, tag []byte) (plaintextKey []byte, err error)
Signature of the method that unwraps keys. This does not accept a context, which needs to be provided by the caller of the Decrypt method inside the lambda.
type WrapKeyFn ¶
type WrapKeyFn = func(plaintextKey []byte, algorithm string, keyName string, nonce []byte) (wrappedKey []byte, tag []byte, err error)
Signature of the method that wraps keys. This does not accept a context, which needs to be provided by the caller of the Encrypt method inside the lambda.