Deep dive

wolfCOSE

wolfCOSE signs, encrypts and attests messages with COSE (RFC 9052) on embedded devices, with no heap. It is one of the repositories I created at wolfSSL: I designed the API and wrote the algorithms, multi-party support, the attestation layer and post-quantum signing. In wolfSSL's comparison against t_cose, COSE-C, go-cose and coset, it is the smallest and fastest COSE library: 34.6 KB for sign and verify with wolfCrypt, about 14x faster verify than t_cose with Mbed TLS on an STM32H563, zero heap, and ML-DSA at about the cost of ES256 (footprint).

Role: creator C, COSE/CBOR, wolfCrypt, zero heap wolfSSL/wolfCOSE
01 / Problem and boundary

COSE for devices that cannot allocate

COSE is the CBOR-based signing and encryption format used by device attestation (RFC 9783 PSA/EAT), firmware manifests and IoT protocols. Most implementations allocate as they parse and build messages, which rules them out for secure boot, TrustZone partitions and certified firmware. wolfCOSE does everything in caller-provided memory on top of wolfCrypt. I created the repository and the design; wolfCrypt provides the underlying algorithms.

02 / Architecture and tradeoffs

Every byte comes from the caller

Every call takes its memory from the caller. wc_CoseSign1_Sign() takes a scratch buffer for the Sig_structure, an output buffer for the result, and returns the length it used. There is no internal buffer to grow, so RAM use is visible at the call site. The cost is that callers must size buffers correctly and the library must reject what does not fit instead of resizing.

That tradeoff got tested in public: a Hacker News reader pointed out that "zero allocation" hid stack use. The library was already zero-heap, so I made the claim precise with a WOLFCOSE_MIN_BUFFERS profile and a CI script that enforces bounded stack #38. Delegated signing is the same idea applied to keys: with WOLFCOSE_ENABLE_EXT_SIGN the caller passes a signing callback so the private key never enters wolfCOSE, which wolfBoot DICE and TF-M need #59.

03 / What I built

Main changes

04 / Debugging

A hostile key that picked its own importer

An outside security reporter showed that wc_CoseKey_Decode() could be pushed into memory corruption by a malicious COSE_Key, before any signature check ran. The caller attaches a wolfCrypt key object to a WOLFCOSE_KEY, then decodes the peer's CBOR into it.

The decoder chose its importer from the peer's kty and crv fields, not from the key the caller attached. WOLFCOSE_KEY held the attached key in an untagged union, so the key.ecc != NULL style guards could not tell an ed25519_key from an ed448_key or an ecc_key. A peer sending crv=Ed448 to an attached 112-byte ed25519_key ran the Ed448 importer and wrote past the object; kty=EC2 aimed the 4208-byte ecc_key importer at it.

The fix adds an attachedType field that every wc_CoseKey_Set*() function records, and each importer now matches on it instead of the union member. A check runs first: if the decoded type does not match the attached type, decode returns WOLFCOSE_E_COSE_KEY_TYPE before any import. Decoding with nothing attached still works and returns metadata only, which is how to inspect an untrusted key before choosing an object for it.

Attached key and peer inputBefore the fixAfter the fix
ed25519_key (112 B), peer sends crv=Ed448Ed448 importer writes past the objectWOLFCOSE_E_COSE_KEY_TYPE, no import
ed25519_key (112 B), peer sends kty=EC2ecc_key (4208 B) importer runs on itWOLFCOSE_E_COSE_KEY_TYPE, no import
Matching key typeimportsimports
No key attachedmetadata onlymetadata only

Fixed in #64.

05 / Verification

How it was checked

PREvidence
#64Reporter's proofs of concept rejected under ASan; full suite, cmdline 30/30, NO_EDDSA, NO_ED448 and LEAN pass
#59Delegated sign and local verify tests; TF-M v2.3.0 on QEMU mps2-an521 regression suite, 0 failures
#38CI enforces bounded stack with check_stack_usage.sh
06 / Limits and next steps

What is not done yet

Because nothing is allocated, sizing mistakes surface as error codes at run time rather than being absorbed. The type check only works when keys are attached through the Set functions; assigning the union directly records no type, which the API docs now state.

All my merged pull requests 61 in wolfSSL/wolfCOSE
  • #96Add an unchecked text string encoder option and share the PSA claim writer
  • #95Sign ECDSA without ASN and add an external-signer-only build
  • #93Delegate deterministic ECDSA enforcement to wolfSSL
  • #91Build the wolfCOSE manual with the shared documentation tooling
  • #92Use a shared GHCR image for wolfCOSE Linux CI
  • #76Bump version to 2.0.0 and add the 2.0.0 ChangeLog
  • #89Add release qualification workflow and feature scenarios
  • #88Restore README refresh documentation
  • #87Docs/readme refresh
  • #85Default to RFC 9864 algorithm IDs and gate deprecated RFC 9053 IDs
  • #86Guard COSE header label bounds
  • #75Add RFC 9783 PSA/EAT Support
  • #70COSE HPKE Support
  • #77Harden COSE recipient validation
  • #78Validate COSE key metadata
  • #79Tighten wolfCOSE type safety for MISRA complaince
  • #81Harden COSE input and cleanup paths
  • #74Add RFC 9338 countersignature support
  • #83Harden RSA COSE key handling
  • #82Harden CBOR state and API contracts
  • #80Validate attached wolfCrypt keys
  • #72Add RFC 8778 HSS/LMS signature and COSE_Key support
  • #71Expand COSE interop coverage
  • #73Split wolfcose.c into per-area source modules
  • #68Add experimental-settings acknowledgment gate for draft COSE features
  • #69Use pkg config for wolfSSL discovery
  • #67Harden COSE parsing, key handling, and cryptographic operations
  • #65Add wc_CoseSign1_Sign_ex with an untagged output option
  • #59Add delegated signing seam
  • #61Add STM32Cube pack support docs and on device test for wolfCOSE
  • #64Reject COSE_Key decode into a mismatched wolfCrypt key object
  • #62Stabilize flaky cppcheck nightly on test_cose.c
  • #51Release wolfCOSE 1.0.0
  • #58Fix Coverity DEADCODE findings in COSE MAC and CBOR decode paths
  • #57Open Issue on nightly CI failure
  • #56Various hardening, fixes, and testing
  • #55Hardening Fixes and Secure Edges
  • #50Fix RSA private COSE_Key: RFC 8230 dP/dQ + fixed-width d
  • #53Fix lean verify-only build: ECC sign helpers leak into WOLFCOSE_LEAN_VERIFY
  • #52Add automated security + house-style gates (CodeQL, Semgrep, conventions, Copilot)
  • #49Add RFC 9964 Support ML-DSA
  • #48Fix inject failure, private COSE_Key decode, word32 length guards, and ECDH zeroize test
  • #47Fix cppcheck unused/unassigned variable warnings in test_cose.c
  • #46Add RSA private-key COSE_Key round-trip (RFC 8230 d,p,q,qInv) (#34)
  • #43Add WOLFCOSE_LEAN_VERIFY and ML-DSA lean build profiles
  • #45Add wolfCOSE + t_cose wire-interop Conformance Interop Tests
  • #38Add bounded-stack CI, WOLFCOSE_MIN_BUFFERS, and updated minimal builds
  • #44Nightly CI: run at 2:00 AM Pacific instead of evening
  • #42Add WOLFCOSE_LEAN config layer with WOLFCOSE_HAVE_* feature gates
  • #40wolfCOSE test additions and hardening
  • #33Rename Dilithium API to ML-DSA and make CI track latest wolfSSL stable
  • #41Add docs/ as wiki source of truth with auto-sync workflow
  • #37Make wolfCOSE build and CI ISO C99 conformant
  • #32Sync CI and README refrences for wolfSSL/wolfCOSE
  • #30Harden COSE message paths and align RFC 9052/9053
  • #31Add empty brace scope CI scan
  • #27Add Github Pages for Blog Posts
  • #26Add Nightly CI Trigger and wolfSSL Version Builds
  • #16Add Misra-C Compliance Checker 2012/2023 Support
  • #10Add comprehensive algorithm coverage and multi-party COSE support
  • #1Add HMAC, ChaCha20, RSA-PSS, Ed448, ML-DSA, AES-CCM and tool test commands

all work view the repository