1 个不稳定版本
0.2.0 | 2024年2月8日 |
---|
在加密学类别中排名1318
260KB
5K SLoC
cose-rust
cose-rust 是一个 Rust 包,用于将加密数据(签名、加密或带有MAC的)编码和解码为 CBOR 对象签名和加密(COSE)格式,参考RFC 8152。
此包使用 rust-openssl 和 rand 进行加密操作,以及 cbor-codec 进行 CBOR 编码/解码。
COSE
COSE 是一种简洁的二进制数据格式,通过一系列加密操作保护消息的有效负载。
COSE 的 RFC 8152 规定了以下 6 种 COSE 消息类型
- cose-sign1:带有单个签名的数字签名 COSE 消息。
- cose-sign:带有签名字段的数字签名 COSE 消息。
- cose-encrypt0:带有单个接收者的加密 COSE 消息。
- cose-encrypt:带有接收者字段的加密 COSE 消息。
- cose-mac0:带有单个接收者的 MAC 标记 COSE 消息。
- cose-mac:带有接收者字段的 MAC 标记 COSE 消息。
示例
以下示例演示了如何编码和解码基本 COSE 消息(cose-sign1、cose-encrypt0、cose-mac0),其他用例和 COSE 消息类型的示例可以在相应文档中找到。
cose-sign1
编码 cose-sign1 消息
use cose::message::CoseMessage;
use cose::keys;
use cose::algs;
use hex;
fn main() {
let msg = b"This is the content.".to_vec();
let kid = b"11".to_vec();
// cose-key to encode the message
let mut key = keys::CoseKey::new();
key.kty(keys::EC2);
key.alg(algs::ES512);
key.crv(keys::P_256);
key.x(hex::decode("bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a09eff").unwrap());
key.y(hex::decode("20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbfc117e").unwrap());
key.d(hex::decode("57c92077664146e876760c9520d054aa93c3afb04e306705db6090308507b4d3").unwrap());
key.key_ops(vec![keys::KEY_OPS_SIGN]);
// Prepare cose-sign1 message
let mut sign1 = CoseMessage::new_sign();
sign1.header.alg(algs::ES512, true, false);
sign1.header.kid(kid, true, false);
sign1.payload(msg);
sign1.key(&key).unwrap();
// Generate the signature
sign1.secure_content(None).unwrap();
// Encode the message with the payload
sign1.encode(true).unwrap();
}
解码 cose-sign1 消息
use cose::message::CoseMessage;
use cose::keys;
use cose::algs;
use hex;
fn main() {
// cose-key to decode the message
let mut key = keys::CoseKey::new();
key.kty(keys::EC2);
key.alg(algs::ES256);
key.crv(keys::P_256);
key.x(hex::decode("bac5b11cad8f99f9c72b05cf4b9e26d244dc189f745228255a219a86d6a09eff").unwrap());
key.y(hex::decode("20138bf82dc1b6d562be0fa54ab7804a3a64b6d72ccfed6b6fb6ed28bbfc117e").unwrap());
key.key_ops(vec![keys::KEY_OPS_VERIFY]);
// Generate CoseSign struct with the cose-sign1 message to decode
let mut verify = CoseMessage::new_sign();
verify.bytes =
hex::decode("d28447a2012604423131a054546869732069732074686520636f6e74656e742e5840dc93ddf7d5aff58131589087eaa65eeffa0baf2e72201ee91c0ca876ec42fdfb2a67dbc6ea1a95d2257cec645cf789808c0a392af045e2bc1bdb6746d80f221b").unwrap();
// Initial decoding
verify.init_decoder(None).unwrap();
// Add key and verify the signature
verify.key(&key).unwrap();
verify.decode(None, None).unwrap();
}
cose-encrypt0
编码 cose-encrypt0 消息
use cose::message::CoseMessage;
use cose::keys;
use cose::algs;
use hex;
fn main() {
let msg = b"This is the content.".to_vec();
let kid = b"secret".to_vec();
// Prepare the cose-key
let mut key = keys::CoseKey::new();
key.kty(keys::SYMMETRIC);
key.alg(algs::CHACHA20);
key.k(hex::decode("849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188").unwrap());
key.key_ops(vec![keys::KEY_OPS_ENCRYPT]);
// Prepare cose-encrypt0 message
let mut enc0 = CoseMessage::new_encrypt();
enc0.header.alg(algs::CHACHA20, true, false);
enc0.header.iv(hex::decode("89f52f65a1c580933b5261a7").unwrap(), true, false);
enc0.payload(msg);
enc0.key(&key).unwrap();
// Generate the ciphertext with no AAD.
enc0.secure_content(None).unwrap();
// Encode the cose-encrypt0 message with the ciphertext included
enc0.encode(true).unwrap();
}
解码 cose-encrypt0 消息
use cose::message::CoseMessage;
use cose::keys;
use cose::algs;
use hex;
fn main() {
let expected_msg = b"This is the content.".to_vec();
// Prepare the cose-key
let mut key = keys::CoseKey::new();
key.kty(keys::SYMMETRIC);
key.alg(algs::CHACHA20);
key.k(hex::decode("849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188").unwrap());
key.key_ops(vec![keys::KEY_OPS_DECRYPT]);
// Generate CoseEncrypt struct with the cose-encryt0 message to decode
let mut dec0 = CoseMessage::new_encrypt();
dec0.bytes =
hex::decode("d08352a2011818054c89f52f65a1c580933b5261a7a0582481c32c048134989007b3b5b932811ea410eeab15bd0de5d5ac5be03c84dce8c88871d6e9").unwrap();
// Initial decoding of the message
dec0.init_decoder(None).unwrap();
// Add cose-key
dec0.key(&key).unwrap();
// Decrypt the cose-encrypt0 message
let msg = dec0.decode(None, None).unwrap();
assert_eq!(msg, expected_msg);
}
cose-mac0
编码 cose-mac0 消息
use cose::message::CoseMessage;
use cose::keys;
use cose::algs;
use hex;
fn main() {
let msg = b"This is the content.".to_vec();
// Prepare the cose-key
let mut key = keys::CoseKey::new();
key.kty(keys::SYMMETRIC);
key.alg(algs::AES_MAC_256_128);
key.k(hex::decode("849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188").unwrap());
key.key_ops(vec![keys::KEY_OPS_MAC]);
// Prepare the cose-mac0 message
let mut mac0 = CoseMessage::new_mac();
mac0.header.alg(algs::AES_MAC_256_128, true, false);
// Add the payload
mac0.payload(msg);
// Add cose-key
mac0.key(&key).unwrap();
// Generate MAC tag without AAD
mac0.secure_content(None).unwrap();
// Encode the cose-mac0 message with the payload included
mac0.encode(true).unwrap();
}
解码 cose-mac0 消息
use cose::message::CoseMessage;
use cose::keys;
use cose::algs;
use hex;
fn main() {
// Prepare the cose-key
let mut key = keys::CoseKey::new();
key.kty(keys::SYMMETRIC);
key.alg(algs::AES_MAC_256_128);
key.k(hex::decode("849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188").unwrap());
key.key_ops(vec![keys::KEY_OPS_MAC_VERIFY]);
// Generate CoseMAC struct with the cose-mac0 message to decode
let mut verify = CoseMessage::new_mac();
verify.bytes =
hex::decode("d18444a101181aa054546869732069732074686520636f6e74656e742e50403152cc208c1d501e1dc2a789ae49e4").unwrap();
// Initial decoding of the message
verify.init_decoder(None).unwrap();
// Add cose-key
verify.key(&key).unwrap();
// Verify the MAC tag of the cose-mac0 message
verify.decode(None, None).unwrap();
}
许可证
此包 cose-rust 由 MIT 许可证授权。
注意
此包正在开发中,尚未经过测试。
依赖关系
~2.2–6.5MB
~147K SLoC