Contiki-NG
Loading...
Searching...
No Matches
A COSE implementation (RFC 9052)

Stateless helpers for the small subset of COSE used by EDHOC: COSE_Encrypt0 with AES-CCM* and COSE_Sign1 with ES256. More...

Functions

size_t cose_encrypt0_seal (uint8_t alg, const uint8_t *key, const uint8_t *nonce, const uint8_t *external_aad, size_t external_aad_len, uint8_t *buf, size_t plaintext_len, size_t buf_capacity)
 AEAD-encrypt a buffer in place as a COSE_Encrypt0.
size_t cose_encrypt0_open (uint8_t alg, const uint8_t *key, const uint8_t *nonce, const uint8_t *external_aad, size_t external_aad_len, uint8_t *buf, size_t ciphertext_len)
 AEAD-decrypt a buffer in place as a COSE_Encrypt0.
size_t cose_sign1_sign (int8_t alg, const uint8_t *private_key, const uint8_t *protected_hdr, size_t protected_hdr_len, const uint8_t *external_aad, size_t external_aad_len, const uint8_t *payload, size_t payload_len, uint8_t *signature)
 Produce a COSE_Sign1 signature.
bool cose_sign1_verify (int8_t alg, const uint8_t *public_key, const uint8_t *protected_hdr, size_t protected_hdr_len, const uint8_t *external_aad, size_t external_aad_len, const uint8_t *payload, size_t payload_len, const uint8_t *signature, size_t signature_len)
 Verify a COSE_Sign1 signature.
uint8_t cose_get_key_len (uint8_t alg_id)
 Get the symmetric key length for a COSE AEAD algorithm.
uint8_t cose_get_iv_len (uint8_t alg_id)
 Get the nonce/IV length for a COSE AEAD algorithm.
uint8_t cose_get_tag_len (uint8_t alg_id)
 Get the authentication tag length for a COSE AEAD algorithm.

Detailed Description

Stateless helpers for the small subset of COSE used by EDHOC: COSE_Encrypt0 with AES-CCM* and COSE_Sign1 with ES256.

Function Documentation

◆ cose_encrypt0_open()

size_t cose_encrypt0_open ( uint8_t alg,
const uint8_t * key,
const uint8_t * nonce,
const uint8_t * external_aad,
size_t external_aad_len,
uint8_t * buf,
size_t ciphertext_len )

AEAD-decrypt a buffer in place as a COSE_Encrypt0.

Parameters
algCOSE AEAD algorithm identifier.
keySymmetric key (length implied by alg).
nonceNonce/IV (length implied by alg).
external_aadCBOR-encoded external Additional Authenticated Data.
external_aad_lenLength of external_aad in bytes.
bufIn/out buffer; on entry holds plaintext || tag, on return holds the recovered plaintext.
ciphertext_lenLength of plaintext || tag in buf.
Returns
Plaintext length on success, 0 on failure (including authentication failure).

The protected header is treated as empty, matching how EDHOC uses COSE_Encrypt0.

Warning
Decryption happens in place before the tag is verified, so on failure (return 0) buf holds unauthenticated, attacker-influenced data. Callers must discard buf unless the return value is nonzero.

Definition at line 149 of file cose.c.

References cose_get_iv_len(), cose_get_key_len(), and cose_get_tag_len().

◆ cose_encrypt0_seal()

size_t cose_encrypt0_seal ( uint8_t alg,
const uint8_t * key,
const uint8_t * nonce,
const uint8_t * external_aad,
size_t external_aad_len,
uint8_t * buf,
size_t plaintext_len,
size_t buf_capacity )

AEAD-encrypt a buffer in place as a COSE_Encrypt0.

Parameters
algCOSE AEAD algorithm identifier.
keySymmetric key (length implied by alg).
nonceNonce/IV (length implied by alg).
external_aadCBOR-encoded external Additional Authenticated Data.
external_aad_lenLength of external_aad in bytes.
bufIn/out buffer; on entry holds the plaintext, on return holds plaintext || authentication tag.
plaintext_lenLength of the plaintext in buf.
buf_capacityTotal number of bytes available in buf. Must be at least plaintext_len plus the tag length for alg, or the call fails without writing the tag.
Returns
Total ciphertext length on success, 0 on failure.

The protected header is treated as empty, matching how EDHOC uses COSE_Encrypt0. The Enc_structure built internally is [ "Encrypt0", h'', external_aad ].

Definition at line 103 of file cose.c.

References cose_get_iv_len(), cose_get_key_len(), and cose_get_tag_len().

◆ cose_get_iv_len()

uint8_t cose_get_iv_len ( uint8_t alg_id)

Get the nonce/IV length for a COSE AEAD algorithm.

Parameters
alg_idCOSE algorithm identifier.
Returns
IV length in bytes, 0 if the algorithm is unknown.

Definition at line 294 of file cose.c.

Referenced by cose_encrypt0_open(), and cose_encrypt0_seal().

◆ cose_get_key_len()

uint8_t cose_get_key_len ( uint8_t alg_id)

Get the symmetric key length for a COSE AEAD algorithm.

Parameters
alg_idCOSE algorithm identifier.
Returns
Key length in bytes, 0 if the algorithm is unknown.

Definition at line 280 of file cose.c.

Referenced by cose_encrypt0_open(), and cose_encrypt0_seal().

◆ cose_get_tag_len()

uint8_t cose_get_tag_len ( uint8_t alg_id)

Get the authentication tag length for a COSE AEAD algorithm.

Parameters
alg_idCOSE algorithm identifier.
Returns
Tag length in bytes, 0 if the algorithm is unknown.

Definition at line 308 of file cose.c.

Referenced by cose_encrypt0_open(), and cose_encrypt0_seal().

◆ cose_sign1_sign()

size_t cose_sign1_sign ( int8_t alg,
const uint8_t * private_key,
const uint8_t * protected_hdr,
size_t protected_hdr_len,
const uint8_t * external_aad,
size_t external_aad_len,
const uint8_t * payload,
size_t payload_len,
uint8_t * signature )

Produce a COSE_Sign1 signature.

Parameters
algSigning algorithm identifier (only ES256 is supported).
private_keyECDSA private key.
protected_hdrCBOR-encoded protected header bytes.
protected_hdr_lenLength of protected_hdr.
external_aadCBOR-encoded external Additional Authenticated Data.
external_aad_lenLength of external_aad.
payloadPayload bytes to sign.
payload_lenLength of payload.
signatureOutput buffer for the signature (P256_SIGNATURE_LEN bytes for ES256).
Returns
Signature length on success, 0 on failure.

Definition at line 209 of file cose.c.

References ecc_sign_hash(), and P256_SIGNATURE_LEN.

Referenced by edhoc_generate_message_2(), and edhoc_generate_message_3().

◆ cose_sign1_verify()

bool cose_sign1_verify ( int8_t alg,
const uint8_t * public_key,
const uint8_t * protected_hdr,
size_t protected_hdr_len,
const uint8_t * external_aad,
size_t external_aad_len,
const uint8_t * payload,
size_t payload_len,
const uint8_t * signature,
size_t signature_len )

Verify a COSE_Sign1 signature.

Parameters
algSigning algorithm identifier (only ES256 is supported).
public_keyECDSA public key (2 * ECC_KEY_LEN bytes for ES256).
protected_hdrCBOR-encoded protected header bytes.
protected_hdr_lenLength of protected_hdr.
external_aadCBOR-encoded external Additional Authenticated Data.
external_aad_lenLength of external_aad.
payloadPayload bytes that were signed.
payload_lenLength of payload.
signatureSignature to verify.
signature_lenLength of signature.
Returns
true on a valid signature, false otherwise.

Definition at line 242 of file cose.c.

References ecc_verify_hash(), and P256_SIGNATURE_LEN.

Referenced by edhoc_authenticate_msg().