1 unstable release

0.1.0 Sep 20, 2024

#844 in Cryptography

Download history 158/week @ 2024-10-14 152/week @ 2024-10-21 199/week @ 2024-10-28 116/week @ 2024-11-04 54/week @ 2024-11-11 104/week @ 2024-11-18 123/week @ 2024-11-25 141/week @ 2024-12-02 136/week @ 2024-12-09 327/week @ 2024-12-16 38/week @ 2024-12-23 44/week @ 2024-12-30 290/week @ 2025-01-06 694/week @ 2025-01-13 686/week @ 2025-01-20 861/week @ 2025-01-27

2,537 downloads per month
Used in 18 crates (2 directly)

Apache-2.0

135KB
2.5K SLoC

CBOR Object Signing and Encryption (COSE) implementation based on coset.

Usage

use std::borrow::Cow;
use serde::{Serialize, Deserialize};
use ssi_claims_core::{VerifiableClaims, ValidateClaims, VerificationParameters};
use ssi_cose::{CosePayload, ValidateCoseHeader, CoseSignatureBytes, DecodedCoseSign1, CoseKey, key::CoseKeyGenerate};

// Our custom payload type.
#[derive(Serialize, Deserialize)]
struct CustomPayload {
  data: String
}

// Define how the payload is encoded in COSE.
impl CosePayload for CustomPayload {
  // Serialize the payload as JSON.
  fn payload_bytes(&self) -> Cow<[u8]> {
    Cow::Owned(serde_json::to_vec(self).unwrap())
  }
}

// Define how to validate the COSE header (always valid by default).
impl<P> ValidateCoseHeader<P> for CustomPayload {}

// Define how to validate the payload (always valid by default).
impl<P> ValidateClaims<P, CoseSignatureBytes> for CustomPayload {}

// Create a payload.
let payload = CustomPayload {
  data: "Some Data".to_owned()
};

// Create a signature key.
let key = CoseKey::generate_p256(); // requires the `secp256r1` feature.

// Sign the payload!
let bytes = payload.sign(
  &key,
  true // should the `COSE_Sign1` object be tagged or not.
).await.unwrap();

// Decode the signed COSE object.
let decoded: DecodedCoseSign1<CustomPayload> = bytes
    .decode(true)
    .unwrap()
    .try_map(|_, bytes| serde_json::from_slice(bytes))
    .unwrap();

assert_eq!(decoded.signing_bytes.payload.data, "Some Data");

// Verify the signature.
let params = VerificationParameters::from_resolver(&key);
decoded.verify(&params).await.unwrap();

Dependencies

~19MB
~345K SLoC