Expand description
Utilities to help the process of encoding/decoding a COSE message.
§Examples
Examples on how to encode a COSE message by providing the respective parameters and cose-key in JSON format.
The functions decode_json_key
and decode_json
used in this examples are from the optional feature json
of this crate.
§cose-sign1
use cose::utils;
use cose::message::{CoseMessage, SIG1_TAG};
fn main() {
// cose-sign1 message in JSON format
let data: &str = r#"
{
"protected": {"alg": "EDDSA" },
"unprotected": {"kid": "11" },
"payload": "signed message"
}"#;
// cose-key in JSON format
let key_json: &str = r#"
{
"kty": "OKP",
"alg": "EDDSA",
"crv": "Ed25519",
"x": "d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a",
"d": "9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60",
"key ops": ["sign", "verify"]
}"#;
// Decode the cose-key JSON to CoseKey structure
let key = utils::decode_json_key(key_json).unwrap();
// Encode the cose-sign1 with the decoded cose-key and cose-sign1 JSON
let res = utils::decode_json(data, &key, SIG1_TAG).unwrap();
// Verify the signature
let mut verify = CoseMessage::new_sign();
verify.bytes = res;
verify.init_decoder(None).unwrap();
verify.key(&key).unwrap();
verify.decode(None, None).unwrap();
}
§cose-encrypt0
use cose::utils;
use cose::message::{CoseMessage, ENC0_TAG};
fn main() {
// cose-encrypt0 message in JSON format
let data: &str = r#"
{
"protected": {"alg": 24 },
"unprotected": {"kid": "11", "iv": "000102030405060700010203" },
"payload": "This is the content."
}"#;
// cose-key in JSON format
let key_json: &str = r#"
{
"kty": 4,
"alg": 24,
"k": "849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188",
"key ops": [3, 4]
}"#;
// Decode the cose-key JSON to CoseKey structure
let key = utils::decode_json_key(key_json).unwrap();
// Encode the cose-encrypt0 with the decoded cose-key and cose-encrypt0 JSON
let res = utils::decode_json(data, &key, ENC0_TAG).unwrap();
// Decrypt and verify
let mut dec0 = CoseMessage::new_encrypt();
dec0.bytes = res;
dec0.init_decoder(None).unwrap();
dec0.key(&key).unwrap();
let resp = dec0.decode(None, None).unwrap();
assert_eq!(resp, b"This is the content.".to_vec());
}
§cose-mac0
use cose::utils;
use cose::message::{CoseMessage, MAC0_TAG};
pub fn mac0_json() {
// cose-mac0 message in JSON format
let data: &str = r#"
{
"protected": {"crit": [1, 2], "alg": 26 },
"unprotected": {"kid": "11"},
"payload": "This is the content."
}"#;
// cose-key in JSON format
let key_json: &str = r#"
{
"kty": 4,
"alg": 26,
"k": "849b57219dae48de646d07dbb533566e976686457c1491be3a76dcea6c427188",
"key ops": [9, 10]
}"#;
// Decode the cose-key JSON to CoseKey structure
let key = utils::decode_json_key(key_json).unwrap();
// Encode the cose-mac0 with the decoded cose-key and cose-mac0 JSON
let res = utils::decode_json(data, &key, MAC0_TAG).unwrap();
// Verify the MAC tag
let mut verify = CoseMessage::new_mac();
verify.bytes = res;
verify.init_decoder(None).unwrap();
verify.key(&key).unwrap();
verify.decode(None, None).unwrap();
}
Functions§
- cose_
type_ finder - Function that with a given COSE message bytes, identifies the corresponding message type.