COSE signing and encryption: Sign, Sign1, Mac, Mac0, Encrypt, Encrypt0, COSE_Key, HKDF, and X25519/X448 ECDH
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/mainmoon 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/agreementlet 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)| 类型 | 作用 |
|---|---|
| Header / Protected | 未保护头图,以及 bstr 包裹的受保护头。验签使用原始 protected 字节,不重新编码。 |
| CoseAlg | ES256、EdDSA、HMAC_256_256、A128GCM、A256GCM,以及其它 int / text。 |
| CoseKey | Okp、Ec2、Sym。encode / decode 读写 COSE_Key。 |
| Sign1 | sign、encode、decode、verify。标签 18,也接受无标签数组。 |
| Sign | 多名签名者。verify_signer 按序号验签。标签 98。 |
| Mac0 | HMAC-256/256。标签 17。 |
| Encrypt0 | A128GCM / A256GCM。密文为 ciphertext \|\| 16 字节 tag。标签 16。 |
| sig_structure / mac_structure / enc_structure | RFC 9052 的待签名、待 MAC、AEAD AAD 字节串。 |
| 构造器 | 含义 |
|---|---|
| BadStructure | 数组长度、缺字段、IV 长度、重复 label、不理解的 crit |
| UnsupportedAlg | 算法或曲线不在本库范围内 |
| AlgMismatch | 头里的 alg 与密钥上的 alg,或 protected 与 unprotected 的 alg 不一致;密钥类型与算法不匹配 |
| VerificationFailed | 签名或 MAC 校验失败 |
| DecryptionFailed | GCM 标签校验失败 |
| MalformedCbor | CBOR 截断、保留的附加信息、非法 UTF-8、超深嵌套或超大长度 |
| 结构 | 算法 | 标签 |
|---|---|---|
| COSE_Sign1 | EdDSA (Ed25519 crv 6、Ed448 crv 7, -8)、ES256 (-7) | 18 |
| COSE_Sign | 同上,可多名签名者 | 98 |
| COSE_Mac0 / COSE_Mac | HMAC 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_Encrypt | A128GCM (1)、A192GCM (2)、A256GCM (3)、ChaCha20/Poly1305 (24)、AES-CCM 八个参数组 (10–13、30–33) | 16 / 96 |
| recipient | direct (-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_Key | OKP (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 | 无 |
| 目录 | 执行 | 验签/解密通过 | 失败向量被拒绝 | 不支持的算法被拒绝 | 与规范不一致的示例被拒绝 |
|---|---|---|---|---|---|
| sign1-tests | 9 | 2 | 4 | 2 | 1 |
| sign-tests | 10 | 3 | 4 | 2 | 1 |
| eddsa-examples | 4 | 4 | 0 | 0 | 0 |
| ecdsa-examples | 8 | 2 | 0 | 6 | 0 |
| mac0-tests | 10 | 3 | 4 | 2 | 1 |
| mac-tests | 10 | 3 | 4 | 2 | 1 |
| hmac-examples | 10 | 8 | 2 | 0 | 0 |
| encrypted-tests | 10 | 3 | 4 | 2 | 1 |
| enveloped-tests | 10 | 3 | 4 | 2 | 1 |
| aes-gcm-examples | 9 | 7 | 2 | 0 | 0 |
| aes-ccm-examples | 16 | 16 | 0 | 0 | 0 |
| aes-wrap-examples | 15 | 15 | 0 | 0 | 0 |
| chacha-poly-examples | 2 | 2 | 0 | 0 | 0 |
| 合计 | 123 | 71 | 28 | 18 | 6 |
| 编码 | 本库行为 | 依据 | 测试 |
|---|---|---|---|
| 非最短整数、非最短长度前缀 | 接受 | RFC 8949 §3;§4.2.1 只约束确定性编码 | non-shortest integers and lengths are accepted |
| 不定长 bstr / tstr,含分块字符串 | 接受。分块本身必须是定长、类型一致 | RFC 8949 §3.2.2、§3.2.3 | indefinite strings, arrays, and maps are accepted |
| 不定长 array / map | 接受 | RFC 8949 §3.2.4、§3.2.5 | indefinite strings, arrays, and maps are accepted;noncanonical protected Mac0 verifies and tamper fails |
| map 键任意顺序 | 接受。生成时才排序 | RFC 8949 §4.2.1、§5.6 | map key order does not matter |
| 半精度、单精度、双精度浮点(header 值) | 接受 | RFC 8949 §3.3 | half, single, and double floats are accepted |
| 带正确的 COSE 外层 tag,或完全没有 tag | 接受 | RFC 9052 §2 | COSE tag may be present or absent |
| 无关 tag 包在正确的 COSE tag 外面 | 接受,剥掉外层 tag | RFC 8949 tag;RFC 9052 §2 | unrelated 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 §3 | duplicate CBOR map key is rejected |
| protected 与 unprotected 出现同一个 label | 拒绝 | RFC 9052 §3 | header label in both buckets is rejected |
| 类型错误(header 不是 map、kty 不是整数) | 拒绝 | RFC 9052 的结构 | header type error is rejected |
| 截断、保留的附加信息、非法 UTF-8、尾随字节 | 拒绝 | RFC 8949 §3 | truncated and huge CBOR is rejected |
| 超深嵌套、超大长度声明、随机字节 | 拒绝或不接受,不崩溃、不按声明长度分配 | 深度 64,单项 100000 | deep 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.3 | noncanonical 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 9052 | noncanonical protected Sign1 verifies and tamper fails |
| 本库自己生成的消息 | 确定性编码 | RFC 8949 §4.2.1 | generated messages stay deterministically encoded |
| RFC 8949 附录 A 的合法值 | 接受 | RFC 8949 附录 A | RFC 8949 appendix A well-formed values decode |
| 非最短或不定长的 COSE_Key | 接受,再编码时改为确定性形式 | RFC 9052 §7;RFC 8949 §4.2.1 | noncanonical COSE_Key is accepted |
| cose-wg 六条 pass-01(protected 为 h'A0',校验材料按 h'' 计算) | 拒绝 | 示例与 RFC 9052 冲突,不是编码形式问题 | cose-wg examples for supported structures |
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 对照字节| 能力 | moon-cose | mooncred 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 文档范围内 |
pub(all) suberror CoseError {
BadStructure(String)
UnsupportedAlg(String)
AlgMismatch(String)
VerificationFailed
DecryptionFailed
MalformedCbor(String)
} derive(Eq)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)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)fn enc_structure(context : String, prot : Bytes, external_aad : Bytes) -> Bytesfn mac_structure(context : String, prot : Bytes, external_aad : Bytes, payload : Bytes) -> Bytesfn sig_structure(context : String, body_protected : Bytes, sign_protected : Bytes?, external_aad : Bytes, payload : Bytes) -> BytesInstall
Download zipCOSE signing and encryption: Sign, Sign1, Mac, Mac0, Encrypt, Encrypt0, COSE_Key, HKDF, and X25519/X448 ECDH