Documentation
¶
Overview ¶
Package cose implements COSE (RFC 9052, RFC 9053) encryption. The COSE_Encrypt structure with direct ECDH-ES key agreement (ECDH-ES + HKDF-256) and AES-GCM content encryption is supported; additional content-encryption algorithms can be added via RegisterContentEncryption.
Index ¶
- Constants
- Variables
- func Encrypt(plaintext []byte, recipientPublicKey *ecdh.PublicKey, options *EncryptOptions) ([]byte, error)
- func ParsePublicKey(data []byte) (crypto.PublicKey, error)
- func PublicKey(keyMap map[any]any) (crypto.PublicKey, error)
- func RegisterContentEncryption(algorithm Algorithm, contentEncryption *ContentEncryption)
- type Algorithm
- type ContentEncryption
- type DecryptOptions
- type DecryptResult
- type EncryptOptions
Constants ¶
const ( HeaderLabelAlgorithm int64 = 1 HeaderLabelCritical int64 = 2 HeaderLabelContentType int64 = 3 HeaderLabelKeyIdentifier int64 = 4 HeaderLabelIv int64 = 5 HeaderLabelEphemeralKey int64 = -1 )
Header parameter labels from the IANA COSE Header Parameters registry.
const ( CurveP256 int64 = 1 CurveP384 int64 = 2 CurveP521 int64 = 3 )
Elliptic curve identifiers from the IANA COSE Elliptic Curves registry.
const CurveEd25519 int64 = 6
CurveEd25519 is the Ed25519 identifier from the IANA COSE Elliptic Curves registry.
Variables ¶
Functions ¶
func Encrypt ¶
func Encrypt(plaintext []byte, recipientPublicKey *ecdh.PublicKey, options *EncryptOptions) ([]byte, error)
Encrypt produces a COSE_Encrypt message (CBOR tag 96) for a single recipient, using direct ECDH-ES key agreement with HKDF-256 and the configured content-encryption algorithm.
func ParsePublicKey ¶ added in v1.1.0
ParsePublicKey decodes CBOR-encoded COSE_Key bytes and converts them with PublicKey.
func PublicKey ¶ added in v1.1.0
PublicKey converts a decoded COSE_Key map into the corresponding standard library public key: *ecdsa.PublicKey for EC2 keys, ed25519.PublicKey for OKP Ed25519 keys, and *rsa.PublicKey for RSA keys.
func RegisterContentEncryption ¶
func RegisterContentEncryption(algorithm Algorithm, contentEncryption *ContentEncryption)
RegisterContentEncryption makes a content-encryption algorithm available to Encrypt and Decrypt.
Types ¶
type Algorithm ¶
type Algorithm int64
Algorithm is a COSE algorithm identifier as registered in the IANA COSE Algorithms registry.
type ContentEncryption ¶
ContentEncryption describes a COSE content-encryption algorithm.
type DecryptOptions ¶
type DecryptOptions struct {
// ExternalAad is additional authenticated data not carried in the message.
ExternalAad []byte
}
type DecryptResult ¶
type DecryptResult struct {
Plaintext []byte
// ContentType is the content protected header's content type: a string, an int64, or nil if
// absent.
ContentType any
// KeyIdentifier is the key identifier of the recipient that was used for decryption, or nil
// if absent.
KeyIdentifier []byte
// Protected is the decoded content protected header map.
Protected map[any]any
}
func Decrypt ¶
func Decrypt(message []byte, privateKey *ecdh.PrivateKey, options *DecryptOptions) (*DecryptResult, error)
Decrypt decrypts a COSE_Encrypt message (CBOR tag 96, tagged or untagged) using direct ECDH-ES key agreement with HKDF-256.
type EncryptOptions ¶
type EncryptOptions struct {
// ContentEncryptionAlgorithm defaults to A256GCM.
ContentEncryptionAlgorithm Algorithm
// KeyIdentifier identifies the recipient public key and is placed in the recipient
// unprotected header.
KeyIdentifier []byte
// ContentType describes the plaintext and is placed in the content protected header. It must
// be a media type string or a CoAP Content-Format integer.
ContentType any
// ExternalAad is additional authenticated data not carried in the message.
ExternalAad []byte
// Rand defaults to crypto/rand.Reader.
Rand io.Reader
}