cose

    COSE signing and encryption: Sign, Sign1, Mac, Mac0, Encrypt, Encrypt0, COSE_Key, HKDF, and X25519/X448 ECDH

    cose
    cbor
    rfc9052
    rfc9053
    signature
    encryption
    hmac
    aes-gcm
    aes-ccm
    aes-cbc-mac
    chacha20
    hkdf
    ecdh
    rsa
    Download zip
    Author
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    3 hours ago
    Downloads
    7

    #moon-cose

    [English] COSE (RFC 9052 / RFC 9053) signing and encryption for MoonBit. Generated messages use RFC 8949 deterministic CBOR. Parsing accepts every well-formed CBOR encoding.

    MoonBit 上的 COSE 签名与加密库。除 COSE_Sign1、COSE_Mac0、COSE_Encrypt0 外,还实现多名签名者的 COSE_Sign,以及带 direct / AES Key Wrap recipient 的 COSE_Mac 和 COSE_Encrypt,recipient 可以多层嵌套。密码学调用 mooncry。生成消息时用 2515050242/cbor 做 RFC 8949 §4.2.1 确定性编码;解析接受所有合法 CBOR 编码。生成用确定性编码,解析接受所有合法编码。

    #安装

    工具链使用 MoonBit 0.10.19+97801ee1b(见 .moonbit-version)。

    curl -fsSL https://cli.moonbitlang.com/install/unix.sh | bash -s 0.10.19+97801ee1b export PATH="$HOME/.moon/bin:$PATH" git clone https://github.com/Hhsqoo/moon-cose cd moon-cose moon test --target native moon run --target native cmd/main

    依赖写在 moon.mod:2515050242/cbor@0.1.2、cc06b/mooncry@0.95.0。moon test 会自行下载。

    #快速开始

    cmd/main 对一段 CBOR 负载做 EdDSA COSE_Sign1 签名并验签,再做 HMAC-256/256 的 Mac0 和 A256GCM 的 Encrypt0 往返。

    示例都使用已经实现的算法:

    moon run --target native cmd/webauthn moon run --target native cmd/multi_sign moon run --target native cmd/encrypt_recipients moon run --target native cmd/noncanonical moon run --target native cmd/nested moon run --target native cmd/agreement

    cmd/webauthn 把 ES256 和 EdDSA 公钥编成 COSE_Key,再验对应的 Sign1。cmd/multi_sign 用 EdDSA 和 ES256 两个签名者签同一段负载。cmd/encrypt_recipients 用两把 A128KW 密钥包装同一把内容密钥,并分别解密。cmd/noncanonical 解析并验一条非规范编码的 COSE_Sign1 和一条不定长 protected 的 COSE_Mac0。cmd/nested 构造三层 AES-KW,外加一条 direct 分支,分别解密,并验一条三层 COSE_Mac。cmd/agreement 用 direct+HKDF-SHA-256 和 ECDH-ES+HKDF-256(X25519)各做一次 Encrypt 往返。

    库内签名一条文本:

    let key = @cose.CoseKey::Okp(@cose.OkpKey::ed25519(public_key, Some(seed)))
    let signed = @cose.Sign1::sign(
    @cose.Header::with_alg(@cose.EdDSA),
    @cose.Header::empty(),
    Some(payload),
    b"",
    key,
    )
    let bytes = signed.encode()
    let decoded = @cose.Sign1::decode(bytes)
    decoded.verify(b"", key)

    external_aad 传给 sign / verify / mac / encrypt / decrypt 的最后一个字节串参数。空 AAD 用 b""。

    #API

    类型作用
    Header / Protected未保护头图,以及 bstr 包裹的受保护头。验签使用原始 protected 字节,不重新编码。
    CoseAlgES256、EdDSA、HMAC_256_256、A128GCM、A256GCM,以及其它 int / text。
    CoseKeyOkp、Ec2、Sym。encode / decode 读写 COSE_Key。
    Sign1sign、encode、decode、verify。标签 18,也接受无标签数组。
    Sign多名签名者。verify_signer 按序号验签。标签 98。
    Mac0HMAC-256/256。标签 17。
    Encrypt0A128GCM / A256GCM。密文为 ciphertext \|\| 16 字节 tag。标签 16。
    sig_structure / mac_structure / enc_structureRFC 9052 的待签名、待 MAC、AEAD AAD 字节串。

    错误类型 CoseError:

    构造器含义
    BadStructure数组长度、缺字段、IV 长度、重复 label、不理解的 crit
    UnsupportedAlg算法或曲线不在本库范围内
    AlgMismatch头里的 alg 与密钥上的 alg,或 protected 与 unprotected 的 alg 不一致;密钥类型与算法不匹配
    VerificationFailed签名或 MAC 校验失败
    DecryptionFailedGCM 标签校验失败
    MalformedCborCBOR 截断、保留的附加信息、非法 UTF-8、超深嵌套或超大长度

    #支持的算法与结构

    结构算法标签
    COSE_Sign1EdDSA (Ed25519 crv 6、Ed448 crv 7, -8)、ES256 (-7)18
    COSE_Sign同上,可多名签名者98
    COSE_Mac0 / COSE_MacHMAC 256/64 (4)、256/256 (5)、384/384 (6)、512/512 (7);AES-CBC-MAC 128/64 (14)、256/64 (15)、128/128 (25)、256/128 (26)17 / 97
    COSE_Encrypt0 / COSE_EncryptA128GCM (1)、A192GCM (2)、A256GCM (3)、ChaCha20/Poly1305 (24)、AES-CCM 八个参数组 (10–13、30–33)16 / 96
    recipientdirect (-6)、direct+HKDF-SHA-256/512 (-10/-11)、direct+HKDF-AES-128/256 (-12/-13)、A128/192/256KW (-3/-4/-5)、ECDH-ES/SS+HKDF-256/512 (-25..-28)、ECDH-ES/SS+A128/192/256KW (-29..-34)、RSAES-OAEP SHA-1/256/512 (-40/-41/-42)无
    COSE_KeyOKP (kty 1, X25519 crv 4、X448 crv 5、Ed25519 crv 6、Ed448 crv 7)、EC2 (kty 2, P-256 crv 1)、RSA (kty 3)、Symmetric (kty 4),含 Base IV无

    头 label:alg (1)、crit (2)、content type (3)、kid (4)、IV (5)、partial IV (6),以及 KDF / ECDH 参数 salt (-20)、PartyU/PartyV (-21..-26)、ephemeral key (-1)、static key (-2)、static key id (-3)。crit 里出现这组以外的 label 会拒绝。创建空 protected 头时编码为零长度 bstr(h'');解码同时接受 h'' 和 h'a0'。

    ES256 签名是 RFC 9053 的 64 字节 r || s。本库签名使用 mooncry 的 RFC 6979 确定性 k;验签接受任意合法的 r || s,包括示例里的非确定性签名。

    #测试

    moon test --target native --deny-warn 当前 73 项全部通过。其中一项遍历 123 条 cose-wg 向量。多层 recipient 的密码学对照不是这 123 条,见下一节。

    目录执行验签/解密通过失败向量被拒绝不支持的算法被拒绝与规范不一致的示例被拒绝
    sign1-tests92421
    sign-tests103421
    eddsa-examples44000
    ecdsa-examples82060
    mac0-tests103421
    mac-tests103421
    hmac-examples108200
    encrypted-tests103421
    enveloped-tests103421
    aes-gcm-examples97200
    aes-ccm-examples1616000
    aes-wrap-examples1515000
    chacha-poly-examples22000
    合计1237128186

    六条「与规范不一致」的示例是 sign1-tests/sign-pass-01、sign-tests/sign-pass-01、mac0-tests/mac-pass-01、mac-tests/mac-pass-01、encrypted-tests/enc-pass-01、enveloped-tests/env-pass-01。重新检查后仍然拒绝,原因不是编码形式。这六条的 protected 桶是 h'A0'(空 map 的规范编码),而 ToBeSign、ToMac 或 Enc_structure 是对零长度 bstr h'' 计算的。宽松解码能读出 h'A0',但 RFC 9052 要求校验使用收到的原始字节,所以验签和解密失败。这是示例数据与规范冲突。测试断言库拒绝它们。

    eddsa-examples 的 eddsa-02 与 eddsa-sig-02 是 Ed448。实现 Ed448 之后它们从「不支持」改为通过。aes-wrap-examples 里六条 AES-CBC-MAC(aes-wrap-128/192/256-01 和 -02)同样从「不支持」改为通过。其余 115 条的期望分类没有改。

    密码学自测在 crypto_vectors_test.mbt、ed448_test.mbt 和 kdf_wbtest.mbt:RFC 8032 Ed25519 / Ed448、RFC 4231、RFC 6979、RFC 5869 HKDF-SHA-256 用例 1、RFC 7748 X25519、NIST SP 800-38D。direct+HKDF、ECDH 和 RSAES-OAEP 的期望字节来自 Python cryptography 41.0.7;AES-CBC-MAC 的完整标签与同一版本的 AES-CBC 一致。固定种子的 RSAES-OAEP 密文另有一份独立的 SHA-256 / MGF1 填充实现对照。

    aes-gcm-05 的消息只携带 partial IV。官方 JSON 的密钥没有 Base IV;测试用未发送的完整 IV 反推 Base IV 89F52F65A1C5809300000000,再按右对齐异或还原 nonce。

    #CBOR 编码兼容性与处理案例

    生成用确定性编码,解析接受所有合法编码。

    本库写出的 COSE 消息、header、COSE_Key 使用 RFC 8949 §4.2.1 确定性编码:整数和长度取最短形式,map 键按长度再按字节序排序,浮点取最短精度。这是推荐写法,不是互操作限制。解析 COSE 消息、header map、COSE_Key、recipient 和 signature 结构时,接受 RFC 8949 允许的全部合法编码。Sig_structure、MAC_structure、Enc_structure 使用收到的 protected bstr 原始字节,也原样使用 external_aad 和 payload,不把 protected 重新编码后再验。

    嵌套深度上限 64,单个数组、map 或字节串的项数上限 100000。声明的长度大于剩余输入时直接拒绝,不会按声明的长度分配。

    编码本库行为依据测试
    非最短整数、非最短长度前缀接受RFC 8949 §3;§4.2.1 只约束确定性编码non-shortest integers and lengths are accepted
    不定长 bstr / tstr,含分块字符串接受。分块本身必须是定长、类型一致RFC 8949 §3.2.2、§3.2.3indefinite strings, arrays, and maps are accepted
    不定长 array / map接受RFC 8949 §3.2.4、§3.2.5indefinite strings, arrays, and maps are accepted;noncanonical protected Mac0 verifies and tamper fails
    map 键任意顺序接受。生成时才排序RFC 8949 §4.2.1、§5.6map key order does not matter
    半精度、单精度、双精度浮点(header 值)接受RFC 8949 §3.3half, single, and double floats are accepted
    带正确的 COSE 外层 tag,或完全没有 tag接受RFC 9052 §2COSE tag may be present or absent
    无关 tag 包在正确的 COSE tag 外面接受,剥掉外层 tagRFC 8949 tag;RFC 9052 §2unrelated tag wrapping the COSE tag is accepted
    用别的 tag 代替 COSE tag,或换成另一种 COSE tag拒绝RFC 9052 §2。cose-wg sign-fail-01 属于这一类wrong COSE tag is rejected
    map 重复键拒绝RFC 8949 §5.6;RFC 9052 §3duplicate CBOR map key is rejected
    protected 与 unprotected 出现同一个 label拒绝RFC 9052 §3header label in both buckets is rejected
    类型错误(header 不是 map、kty 不是整数)拒绝RFC 9052 的结构header type error is rejected
    截断、保留的附加信息、非法 UTF-8、尾随字节拒绝RFC 8949 §3truncated and huge CBOR is rejected
    超深嵌套、超大长度声明、随机字节拒绝或不接受,不崩溃、不按声明长度分配深度 64,单项 100000deep nesting and huge lengths do not crash;random and truncated CBOR does not crash
    protected 用非最短整数、键乱序或不定长 map,签名 / MAC / AEAD 覆盖这些原始字节接受并验签或解密RFC 9052 §4.4、§5.3、§6.3noncanonical protected Sign1 verifies and tamper fails;noncanonical protected Mac0 verifies and tamper fails;noncanonical protected Encrypt0 decrypts and tamper fails;noncanonical signer protected COSE_Sign verifies
    把 protected 重新编码成确定性形式后仍用原来的签名、MAC 或密文拒绝RFC 9052:结构里必须是收到的 bstr同上四项里的篡改断言
    改 payload 或签名字节拒绝RFC 9052noncanonical protected Sign1 verifies and tamper fails
    本库自己生成的消息确定性编码RFC 8949 §4.2.1generated messages stay deterministically encoded
    RFC 8949 附录 A 的合法值接受RFC 8949 附录 ARFC 8949 appendix A well-formed values decode
    非最短或不定长的 COSE_Key接受,再编码时改为确定性形式RFC 9052 §7;RFC 8949 §4.2.1noncanonical COSE_Key is accepted
    cose-wg 六条 pass-01(protected 为 h'A0',校验材料按 h'' 计算)拒绝示例与 RFC 9052 冲突,不是编码形式问题cose-wg examples for supported structures

    cmd/noncanonical 用上面的 Sign1(非最短 tag、非最短数组长度、alg 用 0x3807)和 Mac0(protected 为不定长 map bf0105ff)做一次可运行的解析和校验。

    #完整标准兼容目标与多层接收方处理方案

    目标分两层,不能混成“已实现全部 COSE 标准”。

    RFC 9052 规定的顶层消息是 COSE_Sign、COSE_Sign1、COSE_Encrypt、COSE_Encrypt0、COSE_Mac、COSE_Mac0(§2 的 CDDL)。计数器签名不在这份清单里:RFC 9052 引言写明,推进到 Internet Standard 时已把计数器签名从本文删除,放到另行发表的文档。该文档是 RFC 9338,它更新 RFC 9052,并定义 CounterSignatureV2。因此未实现计数器签名,不等于 RFC 9052 的消息结构不完整。

    RFC 9053 是算法注册和用法,不是“实现了 COSE 就必须实现表里每一种算法”。本库交付的是下面点名的算法。表外算法返回 UnsupportedAlg,这不改变上面的结构处理目标。

    多层接收方按下面的规则处理:

    1. COSE_recipient 是三元或四元数组。没有第四元就是叶子;第四元是至少一个子 recipient。嵌套深度上限 8,节点上限 32。构造和解析用同一预算,所有分支加在一起计算。
    2. 解密或验 MAC 时从内向外。子层的输出是本层的 KEK,本层再用自己的 ciphertext 解出上一层的密钥。最外层解出的才是 CEK 或 MAC 密钥。
    3. direct 的 recipients 必须缺省(RFC 9052 §8.5.1),所以它只能是叶子。ciphertext 必须是零长度 bstr。识别出的对称密钥就是上一层的密钥。
    4. “direct 必须是消息上的唯一模式”指的是分发内容密钥的那一层(RFC 9052 §8.5.1)。AES-KW 的 recipients 可以存在(§8.5.2)。因此顶层可以是两个 AES-KW 分支,其中一个分支的唯一子接收方是 direct:direct 不是顶层的同级,也没有自己的子层。同一个 recipients 数组里不能既有 direct 又有 AES-KW,因为那一把密钥不能既是预共享密钥本身,又是被另一把密钥包住的随机密钥。
    5. direct 和 AES-KW 的 protected map 必须为空(RFC 9052 §5.4、§8.5.1、§8.5.2,RFC 9053 §6.1.1、§6.2.1)。空 map 的两种编码都接受:零长度 bstr h'',以及 bstr 里的 h'a0'(RFC 9052 §3:接收方必须两者都接受;发送方 SHOULD 用 h'')。收到的原始 bstr 会原样保留,不会重写成另一种空 map。map 里若有参数则拒绝。
    6. 叶子上的 AES-KW 用 kid 选中的预共享密钥解包。有子 recipient 时,KEK 只来自子层。同一层解开多把候选密钥时,内容认证失败的候选不会挡住下一把。
    7. 内容加密的 AAD 仍是 body 的 Enc_structure,使用 body protected 的原始字节。AES-KW 本身不带 AAD。

    cose-wg/Examples 里没有三层、且只用本库已实现算法的嵌套 recipient 向量。对照向量由 scripts/nested_vectors.py 生成,密码学来自 Python cryptography 41.0.7(Apache-2.0 或 BSD-3-Clause)的 AES-KW(RFC 3394)、AES-GCM 和标准库 HMAC-SHA-256,CBOR 按最短确定性形式手写。测试比对的是这组独立字节,不是本库自己加密再自己解密的结果。本库用同一组密钥重新构造时,字节与该脚本一致。

    #结构

    cbor_lenient.mbt 接受合法 CBOR 的解码器 alg.mbt header.mbt structures.mbt 算法、头、Sig/MAC/Enc 结构 key.mbt COSE_Key crypto_ops.mbt 调用 mooncry 的签名、MAC、AEAD sign1.mbt sign.mbt mac0.mbt mac.mbt encrypt0.mbt encrypt.mbt cmd/main cmd/webauthn cmd/multi_sign cmd/encrypt_recipients cmd/noncanonical cmd/nested testdata/cose-wg cose-wg/Examples scripts/gen_vectors.py 由 JSON 生成 vectors_gen_test.mbt scripts/nested_vectors.py 独立生成多层 recipient 对照字节

    验签路径读取消息里的 protected bstr,原样放进 Sig_structure / MAC_structure / Enc_structure,再交给 mooncry。AES-GCM 的 AAD 是 Enc_structure 的 CBOR,不是调用方的 external_aad 本身;external_aad 嵌在该结构里。

    #功能边界

    当前交付,已实现并有测试

    • 六种 COSE 消息:COSE_Sign1、COSE_Sign、COSE_Mac0、COSE_Mac、COSE_Encrypt0、COSE_Encrypt。
    • 递归 COSE_recipient,深度最多 8 层、节点最多 32 个。direct (-6) 与 A128KW / A192KW / A256KW 可以按上一节的规则组成多层和多分支。单层 recipient 仍可用。
    • 签名 EdDSA (Ed25519 与 Ed448)、ES256。MAC 为 HMAC 256/64、256/256、384/384、512/512,以及 AES-CBC-MAC 的四个参数组。内容加密为 A128GCM、A192GCM、A256GCM、ChaCha20/Poly1305、AES-CCM 的八个参数组。
    • recipient:direct、direct+HKDF(SHA-256、SHA-512、AES-128、AES-256)、A128/192/256KW、ECDH-ES/SS+HKDF-256/512、ECDH-ES/SS+A128/192/256KW(曲线只含 X25519 与 X448)、RSAES-OAEP(SHA-1、SHA-256、SHA-512,空 label)。AES-KW 仍可嵌套。direct、HKDF、ECDH 和 RSAES-OAEP 是叶子。
    • COSE_Key(OKP / EC2 / RSA / Symmetric,含 Base IV),以及 partial IV 与 Base IV 的组合。
    • cose-wg/Examples 的 123 条向量,统计见“测试”一节。多层 AES-KW / direct、HKDF、ECDH 和 RSAES-OAEP 另有 cryptography 41.0.7 对照。原生耗时见 bench/。

    尚未实现

    这些遇到时会拒绝,不能把它们说成已经兼容。

    • RFC 9338 的计数器签名(CounterSignatureV2 / CounterSignature0V2)。这不属于 RFC 9052 的六种顶层消息。
    • ES384、ES512。mooncry 0.95.0 没有公开的 P-384 / P-521 ECDSA。Ed448 已实现。
    • P-256、P-384、P-521 上的 ECDH。mooncry 0.95.0 没有单独的 NIST 曲线 ECDH 接口。X25519 与 X448 的 ECDH-ES/SS 已实现。
    • COSE_KDF_Context 的 SuppPubInfo.other 和 SuppPrivInfo。上下文目前只带算法、PartyU、PartyV、密钥位数和 recipient 的 protected bstr。
    • RSA 的 CRT 参数会保存,但 OAEP 只用 n 和 d,不要求两素数私钥带齐 p、q、dP、dQ、qInv。
    • RFC 9053 里上面没有点名的算法。本库不承诺实现该文档的全部算法表。

    不做

    • CWT、JOSE / JWT。本库只处理 COSE 消息和 COSE_Key。

    #和 mooncred 的差别

    检索时,最接近的已发布包是 moonbitstack/mooncred@0.6.7 的 cose 子包(Apache-2.0)。它的文档写明覆盖 COSE_Sign1、COSE_Mac0、COSE_Encrypt0 和 CWT,并服务于 JWT / JWK / X.509。

    能力moon-cosemooncred cose
    COSE_Sign1 / Mac0 / Encrypt0有有
    多名签名者的 COSE_Sign有文档未列
    带 recipient 的 COSE_Mac、COSE_Encrypt有。direct、HKDF、AES-KW(可多层)、X25519/X448 ECDH、RSAES-OAEP文档未列
    COSE_Key(OKP / EC2 / RSA / Symmetric,含 Base IV)有文档未列
    HMAC 256/64、384/384、512/512有未作为独立 COSE 算法表列出
    A192GCM、ChaCha20-Poly1305、AES-CCM 八个参数组有文档未列
    partial IV 与 Base IV 右对齐异或有文档未列
    CWT / JOSE无有
    Ed448、AES-CBC-MAC、direct+HKDF、X25519/X448 ECDH、RSAES-OAEP有不在其 COSE 文档范围内
    ES384 / ES512、P-256 ECDH无。mooncry 0.95.0 没有公开的 P-384 / P-521 ECDSA,也没有可直接调用的 P-256 ECDH不在其 COSE 文档范围内

    本库不实现 CWT。密码学来自 mooncry。写出消息时的确定性 CBOR 来自 2515050242/cbor;读入消息使用仓库里的宽松解码器。

    #来源与许可证

    本库 Apache-2.0。来源、许可证和移植范围见 NOTICE 与 docs/source-attribution.md。

    CoseError

    pub(all) suberror CoseError {
    BadStructure(String)
    UnsupportedAlg(String)
    AlgMismatch(String)
    VerificationFailed
    DecryptionFailed
    MalformedCbor(String)
    } derive(Eq)

    Failures produced by COSE encode, decode, and cryptographic operations.
    impl Show for CoseError

    CoseError::equal

    fn CoseError::equal(CoseError, CoseError) -> Bool
    automatically derived

    CoseError::output

    fn CoseError::output(self : CoseError, logger : &Logger) -> Unit

    CoseError::to_string

    fn CoseError::to_string(self : CoseError) -> String

    Default implementation for Show::to_string, uses a StringBuilder

    ContentType

    pub(all) enum ContentType {
    Int(Int)
    Text(String)
    } derive(Eq)

    Content type header (label 3): an integer or a text media type.

    ContentType::equal

    fn ContentType::equal(ContentType, ContentType) -> Bool
    automatically derived

    CoseAlg

    pub(all) enum CoseAlg {
    ES256
    EdDSA
    HMAC_256_64
    HMAC_256_256
    HMAC_384_384
    HMAC_512_512
    AesMac128_64
    AesMac256_64
    AesMac128_128
    AesMac256_128
    A128GCM
    A192GCM
    A256GCM
    ChaCha20Poly1305
    Direct
    DirectHkdfSha256
    DirectHkdfSha512
    DirectHkdfAes128
    DirectHkdfAes256
    A128KW
    A192KW
    A256KW
    EcdhEsHkdf256
    EcdhEsHkdf512
    EcdhSsHkdf256
    EcdhSsHkdf512
    EcdhEsA128Kw
    EcdhEsA192Kw
    EcdhEsA256Kw
    EcdhSsA128Kw
    EcdhSsA192Kw
    EcdhSsA256Kw
    RsaesOaepSha1
    RsaesOaepSha256
    RsaesOaepSha512
    AesCcm16_64_128
    AesCcm16_64_256
    AesCcm64_64_128
    AesCcm64_64_256
    AesCcm16_128_128
    AesCcm16_128_256
    AesCcm64_128_128
    AesCcm64_128_256
    Int(Int)
    Text(String)
    } derive(Eq)

    COSE algorithm identifier. Known algorithms use their RFC 9053 integer values on the wire (ES256 = -7, EdDSA = -8, HMAC_256_256 = 5, A128GCM = 1, A256GCM = 3). Text names are accepted on decode.

    CoseAlg::equal

    fn CoseAlg::equal(CoseAlg, CoseAlg) -> Bool
    automatically derived

    CoseAlg::from_cbor

    Read an algorithm from a CBOR int or text string.

    CoseAlg::name

    fn CoseAlg::name(self : CoseAlg) -> String

    Short name used in error messages.

    CoseAlg::normalize

    fn CoseAlg::normalize(self : CoseAlg) -> CoseAlg

    Map integer and text aliases onto the named algorithm variants.

    CoseAlg::to_cbor

    Encode the algorithm as its RFC 9053 integer, or as text if it has no integer form.

    CoseKey

    pub(all) enum CoseKey {
    Okp(OkpKey)
    Ec2(Ec2Key)
    Sym(SymKey)
    Rsa(RsaKey)
    } derive(Eq)

    A COSE_Key: an octet key pair, an EC2 point, a symmetric key, or RSA.

    CoseKey::algorithm

    fn CoseKey::algorithm(self : CoseKey) -> CoseAlg?

    Algorithm restriction stored on the key, if any.

    CoseKey::base_iv

    fn CoseKey::base_iv(self : CoseKey) -> Bytes?

    Base IV from the key. Combined with a partial IV by right-aligned XOR.

    CoseKey::decode

    fn CoseKey::decode(bytes : Bytes) -> CoseKey raise CoseError

    Decode one COSE_Key from CBOR bytes.

    CoseKey::encode

    fn CoseKey::encode(self : CoseKey) -> Bytes

    Encode this key as one CBOR data item.

    CoseKey::equal

    fn CoseKey::equal(CoseKey, CoseKey) -> Bool
    automatically derived

    CoseKey::from_cbor

    Read a COSE_Key map. kty selects OKP, EC2, or Symmetric.

    CoseKey::kid

    fn CoseKey::kid(self : CoseKey) -> Bytes?

    Key identifier, when the key carries one.

    CoseKey::to_cbor

    Build a deterministic COSE_Key map.

    Ec2Key

    pub(all) struct Ec2Key {
    crv : Int
    x : Bytes
    y : Bytes?
    d : Bytes?
    kid : Bytes?
    alg : CoseAlg?
    key_ops : Array[Int]
    base_iv : Bytes?
    } derive(Eq)

    Elliptic curve key (RFC 9053). crv 1 is P-256. d, x, and y are 32-byte big-endian coordinates.

    Ec2Key::equal

    fn Ec2Key::equal(Ec2Key, Ec2Key) -> Bool
    automatically derived

    Ec2Key::p256

    fn Ec2Key::p256(x : Bytes, y : Bytes?, d : Bytes?) -> Ec2Key

    A P-256 key. d may be absent when only verification is required.

    Encrypt

    pub(all) struct Encrypt {
    tagged : Bool
    protected_header : Protected
    unprotected : Header
    ciphertext : Bytes?
    recipients : Array[Recipient]
    } derive(Eq)

    COSE_Encrypt (RFC 9052 §5.1). Tag 96 when tagged is true.

    Encrypt::decode

    fn Encrypt::decode(bytes : Bytes) -> Encrypt raise CoseError

    Decode a tagged (96) or untagged COSE_Encrypt array.

    Encrypt::decrypt

    fn Encrypt::decrypt(self : Encrypt, external_aad : Bytes, key : CoseKey) -> Bytes raise CoseError

    Open the message with one recipient key. The key's kid must match when both sides have one.

    Encrypt::decrypt_with

    fn Encrypt::decrypt_with(self : Encrypt, external_aad : Bytes, keys : Array[CoseKey]) -> Bytes raise CoseError

    Open the message with any of keys. Nested recipients are unwrapped from the inside out; a leaf key is not used as the content key. Every successfully unwrapped CEK is tried. A path whose key unwraps but fails content authentication does not suppress another path.

    Encrypt::encode

    fn Encrypt::encode(self : Encrypt) -> Bytes

    Encode this COSE_Encrypt, using tag 96 when tagged is true.

    Encrypt::encrypt

    fn Encrypt::encrypt(protected_header : Header, unprotected : Header, plaintext : Bytes, external_aad : Bytes, cek : Bytes, recipients : Array[RecipientInput], tagged? : Bool) -> Encrypt raise CoseError

    Encrypt for one or more recipients. A recipient may itself contain recipients. Each layer wraps the key of the layer above it: the top layer wraps cek, and a nested layer wraps its parent's KEK. direct recipients must hold that next-layer key and cannot nest.

    Encrypt::equal

    fn Encrypt::equal(Encrypt, Encrypt) -> Bool
    automatically derived

    Encrypt0

    pub(all) struct Encrypt0 {
    tagged : Bool
    protected_header : Protected
    unprotected : Header
    ciphertext : Bytes?
    } derive(Eq)

    COSE_Encrypt0 (RFC 9052). Tag 16 when tagged is true. AES-GCM ciphertext is ciphertext || 16-byte tag.

    Encrypt0::decode

    fn Encrypt0::decode(bytes : Bytes) -> Encrypt0 raise CoseError

    Decode a tagged (16) or untagged COSE_Encrypt0 array.

    Encrypt0::decrypt

    fn Encrypt0::decrypt(self : Encrypt0, external_aad : Bytes, key : CoseKey) -> Bytes raise CoseError

    Decrypt and check the AEAD tag. A partial IV is expanded with the key's Base IV.

    Encrypt0::encode

    fn Encrypt0::encode(self : Encrypt0) -> Bytes

    Encode this COSE_Encrypt0, using tag 16 when tagged is true.

    Encrypt0::encrypt

    fn Encrypt0::encrypt(protected_header : Header, unprotected : Header, plaintext : Bytes, external_aad : Bytes, key : CoseKey, tagged? : Bool) -> Encrypt0 raise CoseError

    Encrypt with one key and no recipients. The content algorithm comes from the headers.

    Encrypt0::equal

    fn Encrypt0::equal(Encrypt0, Encrypt0) -> Bool
    automatically derived
    pub(all) struct Header {
    alg : CoseAlg?
    crit : Array[Label]
    content_type : ContentType?
    kid : Bytes?
    iv : Bytes?
    partial_iv : Bytes?
    salt : Bytes?
    party_u_id : Bytes?
    party_u_nonce : PartyNonce?
    party_u_other : Bytes?
    party_v_id : Bytes?
    party_v_nonce : PartyNonce?
    party_v_other : Bytes?
    epk : CoseKey?
    static_key : CoseKey?
    static_kid : Bytes?
    rest : Array[(Label,
    CborValue
    )]
    } derive(Eq)

    One COSE header map (protected or unprotected).

    rest keeps labels this library does not interpret, so a round trip can preserve them. Critical labels in crit must be in the known set: alg, crit, content type, kid, IV, partial IV, and the KDF parameters salt, PartyU/PartyV info, ephemeral key, and static key.

    Header::check_crit

    fn Header::check_crit(self : Header) -> Unit raise CoseError

    Reject a crit list that contains a label this library does not understand.

    Header::empty

    fn Header::empty() -> Header

    A header map with no fields.

    Header::encode

    fn Header::encode(self : Header) -> Bytes

    Encode this header map with deterministic CBOR (RFC 8949 section 4.2).

    Header::equal

    fn Header::equal(Header, Header) -> Bool
    automatically derived

    Header::from_cbor

    Read a header map. Duplicate labels are an error.

    Header::to_cbor

    Build the CBOR map for this header. The encoder sorts keys.

    Header::with_alg

    fn Header::with_alg(alg : CoseAlg) -> Header

    An otherwise empty header whose alg is alg.

    Label

    pub(all) enum Label {
    Int(Int)
    Text(String)
    } derive(Eq)

    Header map label: a CBOR int or text string (RFC 9052).

    Label::equal

    fn Label::equal(Label, Label) -> Bool
    automatically derived

    Label::from_cbor

    Read a header label from a CBOR int or text string.

    Label::to_cbor

    Encode a header label as a CBOR int or text string.

    Mac

    pub(all) struct Mac {
    tagged : Bool
    protected_header : Protected
    unprotected : Header
    payload : Bytes?
    tag : Bytes
    recipients : Array[Recipient]
    } derive(Eq)

    COSE_Mac (RFC 9052 §6.1). Tag 97 when tagged is true.

    Mac::decode

    fn Mac::decode(bytes : Bytes) -> Mac raise CoseError

    Decode a tagged (97) or untagged COSE_Mac array.

    Mac::encode

    fn Mac::encode(self : Mac) -> Bytes

    Encode this COSE_Mac, using tag 97 when tagged is true.

    Mac::equal

    fn Mac::equal(Mac, Mac) -> Bool
    automatically derived

    Mac::mac

    fn Mac::mac(protected_header : Header, unprotected : Header, payload : Bytes?, external_aad : Bytes, mac_key : Bytes, recipients : Array[RecipientInput], tagged? : Bool) -> Mac raise CoseError

    MAC payload and distribute mac_key through recipients. Nested recipients wrap the parent KEK, then the top layer wraps mac_key.

    Mac::verify

    fn Mac::verify(self : Mac, external_aad : Bytes, key : CoseKey) -> Unit raise CoseError

    Check the tag after unwrapping the MAC key from a matching recipient.

    Mac::verify_with

    fn Mac::verify_with(self : Mac, external_aad : Bytes, keys : Array[CoseKey]) -> Unit raise CoseError

    Check the tag using any key in keys. Each recipient layer is unwrapped before the recovered MAC key is compared.

    Mac0

    pub(all) struct Mac0 {
    tagged : Bool
    protected_header : Protected
    unprotected : Header
    payload : Bytes?
    tag : Bytes
    } derive(Eq)

    COSE_Mac0 (RFC 9052). Tag 17 when tagged is true.

    Mac0::decode

    fn Mac0::decode(bytes : Bytes) -> Mac0 raise CoseError

    Decode a tagged (17) or untagged COSE_Mac0 array.

    Mac0::encode

    fn Mac0::encode(self : Mac0) -> Bytes

    Encode this COSE_Mac0, using tag 17 when tagged is true.

    Mac0::equal

    fn Mac0::equal(Mac0, Mac0) -> Bool
    automatically derived

    Mac0::mac

    fn Mac0::mac(protected_header : Header, unprotected : Header, payload : Bytes?, external_aad : Bytes, key : CoseKey, tagged? : Bool) -> Mac0 raise CoseError

    Compute a MAC with one key and no recipients.

    Mac0::verify

    fn Mac0::verify(self : Mac0, external_aad : Bytes, key : CoseKey) -> Unit raise CoseError

    Check the tag over MAC_structure.

    OkpKey

    pub(all) struct OkpKey {
    crv : Int
    x : Bytes
    d : Bytes?
    kid : Bytes?
    alg : CoseAlg?
    key_ops : Array[Int]
    base_iv : Bytes?
    } derive(Eq)

    Octet key pair (RFC 9053). crv 4 is X25519, 5 is X448, 6 is Ed25519, and 7 is Ed448. d is the seed or scalar and may be absent.

    OkpKey::ed25519

    fn OkpKey::ed25519(x : Bytes, d : Bytes?) -> OkpKey

    An Ed25519 key. d is the 32-byte seed and may be absent for verify-only keys.

    OkpKey::ed448

    fn OkpKey::ed448(x : Bytes, d : Bytes?) -> OkpKey

    An Ed448 key. d is the 57-byte seed and may be absent for verify-only keys.

    OkpKey::equal

    fn OkpKey::equal(OkpKey, OkpKey) -> Bool
    automatically derived

    OkpKey::x25519

    fn OkpKey::x25519(x : Bytes, d : Bytes?) -> OkpKey

    An X25519 key. d is the 32-byte scalar and may be absent.

    OkpKey::x448

    fn OkpKey::x448(x : Bytes, d : Bytes?) -> OkpKey

    An X448 key. d is the 56-byte scalar and may be absent.

    PartyNonce

    pub(all) enum PartyNonce {
    Bytes(Bytes)
    Int(Int)
    } derive(Eq)

    PartyU or PartyV nonce (RFC 9053). It is a byte string or an integer.

    PartyNonce::equal

    fn PartyNonce::equal(PartyNonce, PartyNonce) -> Bool
    automatically derived

    Protected

    pub(all) struct Protected {
    raw : Bytes
    header : Header
    } derive(Eq)

    Protected header bucket: the raw bstr bytes are what Sig_structure, MAC_structure, and Enc_structure commit to. An empty bstr is an empty map.

    Protected::equal

    fn Protected::equal(Protected, Protected) -> Bool
    automatically derived

    Protected::from_header

    fn Protected::from_header(header : Header) -> Protected raise CoseError

    Encode header. An empty map becomes a zero-length bstr.

    Protected::from_raw

    fn Protected::from_raw(raw : Bytes) -> Protected raise CoseError

    Keep raw for signing and also parse it as a header map.

    Recipient

    pub(all) struct Recipient {
    protected_header : Protected
    unprotected : Header
    ciphertext : Bytes?
    recipients : Array[Recipient]
    } derive(Eq)

    One COSE_recipient (RFC 9052 §5.1). A leaf has no recipients. An intermediate recipient's ciphertext wraps the parent key with a KEK that is obtained from its nested recipients, one layer at a time.

    direct (-6) is only legal as a leaf: ciphertext is h'', and the identified key is the next layer's key. AES-KW (-3/-4/-5) wraps the next layer's key. Both require a zero-length protected bstr.

    Recipient::equal

    fn Recipient::equal(Recipient, Recipient) -> Bool
    automatically derived

    RecipientInput

    pub(all) struct RecipientInput {
    prot : Header
    unprotected : Header
    key : CoseKey
    recipients : Array[RecipientInput]
    agreement : CoseKey?
    seed : Bytes?
    }

    One layer of recipient construction.

    A leaf uses key as the preshared secret. An intermediate uses key as the KEK that wraps the parent key; recipients wrap that KEK and cannot be empty. direct cannot have nested recipients.

    RsaKey

    pub(all) struct RsaKey {
    n : Bytes
    e : Bytes
    d : Bytes?
    p : Bytes?
    q : Bytes?
    dp : Bytes?
    dq : Bytes?
    qinv : Bytes?
    kid : Bytes?
    alg : CoseAlg?
    key_ops : Array[Int]
    base_iv : Bytes?
    } derive(Eq)

    RSA key (RFC 8230). n and e are unsigned big-endian. d and the CRT fields may be absent on a public key.

    RsaKey::equal

    fn RsaKey::equal(RsaKey, RsaKey) -> Bool
    automatically derived

    RsaKey::new

    fn RsaKey::new(n : Bytes, e : Bytes, d : Bytes?) -> RsaKey

    An RSA public or private key. d may be absent for encryption-only keys.

    Sign

    pub(all) struct Sign {
    tagged : Bool
    protected_header : Protected
    unprotected : Header
    payload : Bytes?
    signatures : Array[Signer]
    } derive(Eq)

    COSE_Sign (RFC 9052), including the multi-signer form. Tag 98 when tagged.

    Sign::decode

    fn Sign::decode(bytes : Bytes) -> Sign raise CoseError

    Decode a tagged (98) or untagged COSE_Sign array.

    Sign::encode

    fn Sign::encode(self : Sign) -> Bytes

    Encode this COSE_Sign, using tag 98 when tagged is true.

    Sign::equal

    fn Sign::equal(Sign, Sign) -> Bool
    automatically derived

    Sign::sign

    fn Sign::sign(protected_header : Header, unprotected : Header, payload : Bytes?, signers : Array[SignerInput], external_aad : Bytes, tagged? : Bool) -> Sign raise CoseError

    Sign payload with every signer. The body protected header is shared; each signer's alg is protected separately.

    Sign::verify_signer

    fn Sign::verify_signer(self : Sign, index : Int, external_aad : Bytes, key : CoseKey) -> Unit raise CoseError

    Verify one signer. index selects the signature; key must match that signer's algorithm.

    Sign1

    pub(all) struct Sign1 {
    tagged : Bool
    protected_header : Protected
    unprotected : Header
    payload : Bytes?
    signature : Bytes
    } derive(Eq)

    COSE_Sign1 (RFC 9052). Tag 18 when tagged is true.

    Sign1::decode

    fn Sign1::decode(bytes : Bytes) -> Sign1 raise CoseError

    Decode a tagged (18) or untagged COSE_Sign1 array.

    Sign1::encode

    fn Sign1::encode(self : Sign1) -> Bytes

    Encode this COSE_Sign1, using tag 18 when tagged is true.

    Sign1::equal

    fn Sign1::equal(Sign1, Sign1) -> Bool
    automatically derived

    Sign1::sign

    fn Sign1::sign(protected_header : Header, unprotected : Header, payload : Bytes?, external_aad : Bytes, key : CoseKey, tagged? : Bool) -> Sign1 raise CoseError

    Sign payload with one key. alg must be in protected_header.

    Sign1::verify

    fn Sign1::verify(self : Sign1, external_aad : Bytes, key : CoseKey) -> Unit raise CoseError

    Check the signature over Sig_structure. The protected header bytes are used as stored.

    Signer

    pub(all) struct Signer {
    protected_header : Protected
    unprotected : Header
    signature : Bytes
    } derive(Eq)

    One signer inside COSE_Sign.

    Signer::equal

    fn Signer::equal(Signer, Signer) -> Bool
    automatically derived

    SignerInput

    pub(all) struct SignerInput {
    protected_header : Header
    unprotected : Header
    key : CoseKey
    }

    Material for one signature over a COSE_Sign body.

    SymKey

    pub(all) struct SymKey {
    k : Bytes
    kid : Bytes?
    alg : CoseAlg?
    key_ops : Array[Int]
    base_iv : Bytes?
    } derive(Eq)

    Symmetric key. k is the raw key bytes.

    SymKey::equal

    fn SymKey::equal(SymKey, SymKey) -> Bool
    automatically derived

    SymKey::new

    fn SymKey::new(k : Bytes) -> SymKey

    A symmetric key with no kid, alg, or Base IV.

    enc_structure

    fn enc_structure(context : String, prot : Bytes, external_aad : Bytes) -> Bytes

    Enc_structure from RFC 9052. This byte string is the AEAD AAD. context is "Encrypt0" or "Encrypt".

    mac_structure

    fn mac_structure(context : String, prot : Bytes, external_aad : Bytes, payload : Bytes) -> Bytes

    MAC_structure from RFC 9052. context is "MAC0" or "MAC".

    resolve_alg

    fn resolve_alg(prot : Header, unprot : Header) -> CoseAlg raise CoseError

    Read alg from the protected header, then the unprotected header. Differing values are an error.

    sig_structure

    fn sig_structure(context : String, body_protected : Bytes, sign_protected : Bytes?, external_aad : Bytes, payload : Bytes) -> Bytes

    Sig_structure from RFC 9052. sign_protected is present only for the "Signature" and "CounterSignature" contexts.