- OCaml 96%
- Python 3%
- Shell <1%
cose #
CBOR Object Signing and Encryption (COSE) for OCaml.
COSE (RFC 9052) is the CBOR
counterpart of JOSE: it signs and encrypts compact binary messages, and
supply-chain systems such as SCITT transparency logs and Notary v2 signatures
use it as their envelope. cose implements the parts those systems need:
COSE_Sign1 for single-signer signatures, including the detached-payload hash
envelope of RFC 9995; COSE_Sign for several signers over one payload, such
as a classical and a post-quantum key together; COSE_Encrypt0 for
single-recipient encryption with AES-GCM; COSE_Encrypt for encryption to
several recipients, each receiving the content key by AES Key Wrap, by ECDH-ES
on P-256 then AES Key Wrap, or by ML-KEM-768 or ML-KEM-1024 then AES Key Wrap;
and COSE_Key for Ed25519 (OKP), ECDSA on P-256, P-384 and P-521 (EC2, RFC
9053 section 7.1.1), RSA (RFC 8230 section 4) and ML-DSA (AKP) keys and for
symmetric keys (RFC 9053 section 7.3), with RFC 9679 thumbprints. OKP (Octet
Key Pair) is the COSE key type for curves whose public key is one byte string,
such as Ed25519; EC2 carries a point as its x and y coordinates, or x and
the sign of y; AKP (Algorithm Key Pair, RFC 9964) is the type for keys, such
as ML-DSA's, whose bytes only their algorithm defines. A WebAuthn credential
public key is an EC2 or RSA COSE_Key. A thumbprint is the 32-byte SHA-256 of a
COSE_Key that holds only the parameters required to represent the key (key
type, crv and x for OKP; key type, crv, x and y for EC2; key type,
n and e for RSA; key type, alg and pub for AKP), encoded
deterministically, so two documents for the same public key have the same
thumbprint whatever kid they carry.
Signed messages are written in the deterministic CBOR encoding, so that a log can name a message by the digest of its bytes. CBOR allows several encodings of one value: an integer in more bytes than it needs, map keys in any order, lengths left indefinite. The deterministic encoding of RFC 8949 Section 4.2.1 keeps one of them: the shortest form of every integer and length, definite lengths only, and map keys sorted by the bytes of their own encoding. Two encoders then write the same bytes for the same value, and so the same digest. Keys held in an HSM or an OS keychain sign through a callback.
Install #
$ opam install cose
If opam cannot find the package, add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install cose
Usage #
Sign a payload with an ECDSA P-256 key and verify it:
let () = Crypto_rng_unix.use_default ()
let roundtrip payload =
let priv, _pub = Crypto_ec.P256.Dsa.generate () in
let key = `P256 priv in
let pub = X509.Private_key.public key in
let protected_header =
Cose.Header.v ~algorithm:Cose.Algorithm.ES256
~content_type:"application/vnd.example+json" ()
in
match Cose.Sign1.sign ~protected_header ~payload ~key:(Cose.X509 key) () with
| Error e -> Fmt.failwith "sign: %a" Cose.pp_error e
| Ok sign1 -> Cose.Sign1.verify ~key:(Cose.X509 pub) sign1
let () = assert (roundtrip "hello world" = Ok "hello world")
Algorithms #
| Algorithm | COSE ID | Use | Scheme |
|---|---|---|---|
| ES256 | -7 | signature | ECDSA P-256, SHA-256 |
| ES384 | -35 | signature | ECDSA P-384, SHA-384 |
| ES512 | -36 | signature | ECDSA P-521, SHA-512 |
| EdDSA | -8 | signature | Ed25519 |
| PS256 | -37 | signature | RSA-PSS, SHA-256 |
| RS256 | -257 | signature | RSASSA-PKCS1-v1_5, SHA-256 (RFC 8812) |
| ML-DSA-65 | -49 | signature | FIPS 204 (RFC 9964) |
| A128GCM | 1 | encryption | AES-128-GCM |
| A256GCM | 3 | encryption | AES-256-GCM |
| MLKEM768_A256GCM | -70001 | encryption | ML-KEM-768 and AES-256-GCM (private use) |
| A128KW, A192KW, A256KW | -3, -4, -5 | recipient | AES Key Wrap (RFC 3394) |
| ECDH-ES + A128KW, A192KW, A256KW | -29, -30, -31 | recipient | ECDH on P-256, HKDF-SHA-256, AES Key Wrap |
| ML-KEM-768+A192KW | -70005 | recipient | draft-ietf-jose-pqc-kem-06 (IANA TBD5, private use) |
| ML-KEM-1024+A256KW | -70006 | recipient | draft-ietf-jose-pqc-kem-06 (IANA TBD6, private use) |
The ML-KEM recipients follow
draft-ietf-jose-pqc-kem-06:
the ML-KEM ciphertext rides the ek header parameter of draft-ietf-cose-hpke,
and the key wrap key is KMAC256 of the shared secret over the RFC 9053
section 5.2 context. The draft's algorithms and ek have no IANA code points
yet, so they take Private Use values (-70005, -70006, label -70007) until IANA
assigns them; an OpenSSL-built recipient opens in the interop tests. The
MLKEM768_A256GCM construction of COSE_Encrypt0 fuses the key
encapsulation with the content algorithm, which RFC 9052 section 8.5 keeps
apart, and only this library reads it. Nested recipient layers and
COSE_Mac are not implemented.
License #
ISC. See LICENSE.md.