gose/cose

Typed accessors and builders for COSE message header parameters, plus the Key alias, COSE_Key CBOR serialization, and COSE algorithm integer ID mapping (RFC 9053).

Phantom-state vocabulary

Each COSE message module uses a phantom state type named after the RFC 9052 operation it performs: Sign1 uses Unsigned/Signed, Encrypt0 and Encrypt use Unencrypted/Encrypted, Mac0 uses Untagged/Tagged, and Sign uses Building/Signed for its builder body. The names match the RFC terminology rather than a single uniform vocabulary.

Types

Well-known CoAP content format identifiers and media type strings used in the COSE content type header (label 3).

pub type ContentType {
  TextPlain
  OctetStream
  Json
  Cbor
  Cwt
  CoseSign
  CoseSign1
  CoseEncrypt
  CoseEncrypt0
  CoseMac
  CoseMac0
  CoseKey
  CoseKeySet
  IntContentType(Int)
  TextContentType(String)
}

Constructors

  • TextPlain
  • OctetStream
  • Json
  • Cbor
  • Cwt
  • CoseSign
  • CoseSign1
  • CoseEncrypt
  • CoseEncrypt0
  • CoseMac
  • CoseMac0
  • CoseKey
  • CoseKeySet
  • IntContentType(Int)
  • TextContentType(String)

A COSE header parameter with typed labels and values for well-known headers, plus an Unknown fallback for non-standard parameters.

pub type Header {
  Alg(Int)
  Crit(List(Int))
  ContentType(ContentType)
  Kid(BitArray)
  Iv(BitArray)
  PartialIv(BitArray)
  Unknown(cbor.Value, cbor.Value)
}

Constructors

A key with a COSE-compatible binary kid.

pub type Key =
  gose.Key(BitArray)

Values

pub fn algorithm(
  headers: List(Header),
) -> Result(Int, gose.GoseError)

Extract the algorithm identifier (label 1).

pub fn content_alg_from_int(
  id: Int,
) -> Result(gose.ContentAlg, gose.GoseError)

Parse a content encryption algorithm from its COSE integer identifier.

pub fn content_alg_to_int(
  alg: gose.ContentAlg,
) -> Result(Int, gose.GoseError)

Convert a content encryption algorithm to its COSE integer identifier.

Some content encryption algorithms are JOSE-only and have no COSE identifier, in which case this returns an error.

pub fn content_type(
  headers: List(Header),
) -> Result(ContentType, gose.GoseError)

Extract the content type (label 3).

pub fn critical(
  headers: List(Header),
) -> Result(List(Int), gose.GoseError)

Extract the critical headers list (label 2).

pub fn iv(
  headers: List(Header),
) -> Result(BitArray, gose.GoseError)

Extract the IV (label 5).

pub fn key_encryption_alg_from_int(
  id: Int,
) -> Result(gose.KeyEncryptionAlg, gose.GoseError)

Parse a key encryption algorithm from its COSE integer identifier.

Both ECDH-ES+HKDF-256 (-25) and ECDH-ES+HKDF-512 (-26) map to EcdhEs(EcdhEsDirect) because the shared algorithm type does not distinguish the HKDF variant. The HKDF variant is preserved at the cose/encrypt layer via EcdhEsDirectVariant. Use new_ecdh_es_direct_recipient and ecdh_es_direct_decryptor for HKDF-512 support.

pub fn key_encryption_alg_to_int(
  alg: gose.KeyEncryptionAlg,
) -> Result(Int, gose.GoseError)

Convert a key encryption algorithm to its COSE integer identifier.

Some key encryption algorithms are JOSE-only and have no COSE identifier, in which case this returns an error.

pub fn key_from_cbor(
  data: BitArray,
) -> Result(gose.Key(BitArray), gose.GoseError)

Decode COSE_Key bytes to a Key.

pub fn key_from_cbor_map(
  map: List(#(cbor.Value, cbor.Value)),
) -> Result(gose.Key(BitArray), gose.GoseError)

Decode CBOR map entries to a Key.

pub fn key_to_cbor(
  k: gose.Key(BitArray),
) -> Result(BitArray, gose.GoseError)

Encode a Key to COSE_Key CBOR bytes (RFC 9052).

pub fn key_to_cbor_map(
  k: gose.Key(BitArray),
) -> Result(List(#(cbor.Value, cbor.Value)), gose.GoseError)

Encode a Key to its CBOR map entries, for embedding in larger CBOR structures.

pub fn kid(
  headers: List(Header),
) -> Result(BitArray, gose.GoseError)

Extract the key ID (label 4).

pub fn mac_alg_from_int(
  id: Int,
) -> Result(gose.MacAlg, gose.GoseError)

Parse a MAC algorithm from its COSE integer identifier.

pub fn mac_alg_to_int(alg: gose.MacAlg) -> Int

Convert a MAC algorithm to its COSE integer identifier.

pub fn partial_iv(
  headers: List(Header),
) -> Result(BitArray, gose.GoseError)

Extract the partial IV (label 6).

pub fn signature_alg_from_int(
  id: Int,
) -> Result(gose.DigitalSignatureAlg, gose.GoseError)

Parse a signature algorithm from its COSE integer identifier.

pub fn signature_alg_to_int(alg: gose.DigitalSignatureAlg) -> Int

Convert a signature algorithm to its COSE integer identifier.

pub fn signing_alg_from_int(
  id: Int,
) -> Result(gose.SigningAlg, gose.GoseError)

Parse a signing algorithm from its COSE integer identifier.

pub fn signing_alg_to_int(alg: gose.SigningAlg) -> Int

Convert a signing algorithm to its COSE integer identifier.

Search Document