cose

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2026 License: MIT Imports: 18 Imported by: 0

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

View Source
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.

View Source
const (
	CurveP256 int64 = 1
	CurveP384 int64 = 2
	CurveP521 int64 = 3
)

Elliptic curve identifiers from the IANA COSE Elliptic Curves registry.

View Source
const CurveEd25519 int64 = 6

CurveEd25519 is the Ed25519 identifier from the IANA COSE Elliptic Curves registry.

Variables

View Source
var (
	ErrUnsupportedAlgorithm = errors.New("unsupported algorithm")
	ErrMalformedMessage     = errors.New("malformed message")
	ErrMalformedKey         = errors.New("malformed key")
	ErrNoUsableRecipient    = errors.New("no usable recipient")
)

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

func ParsePublicKey(data []byte) (crypto.PublicKey, error)

ParsePublicKey decodes CBOR-encoded COSE_Key bytes and converts them with PublicKey.

func PublicKey added in v1.1.0

func PublicKey(keyMap map[any]any) (crypto.PublicKey, error)

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.

const (
	AlgorithmA128GCM       Algorithm = 1
	AlgorithmA192GCM       Algorithm = 2
	AlgorithmA256GCM       Algorithm = 3
	AlgorithmEcdhEsHkdf256 Algorithm = -25
)

func KeyAlgorithm added in v1.1.0

func KeyAlgorithm(keyMap map[any]any) (Algorithm, bool)

KeyAlgorithm returns the alg parameter of a COSE_Key map.

type ContentEncryption

type ContentEncryption struct {
	KeyBits int
	NewAead func(key []byte) (cipher.AEAD, error)
}

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
}

Jump to

Keyboard shortcuts

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