use darkbio_crypto::{cose, x509, xdsa, xhpke};
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let end = now + 86400;
println!("1. Creating Root Authority (xDSA)...");
let root_secret = xdsa::SecretKey::generate();
let root_public = root_secret.public_key();
println!(
" Root fingerprint: {}",
hex::encode(root_public.fingerprint().to_bytes())
);
println!("\n2. Creating Alice's xDSA identity (signed by root)...");
let alice_xdsa_secret = xdsa::SecretKey::generate();
let alice_xdsa_public = alice_xdsa_secret.public_key();
let alice_xdsa_template = x509::Certificate {
subject: x509::Name::new().cn("Alice Identity"),
issuer: x509::Name::new().cn("Root"),
not_before: now,
not_after: end,
role: x509::Role::Authority { path_len: Some(0) },
..Default::default()
};
let alice_xdsa_cert =
xdsa::issue_cert_pem(&alice_xdsa_public, &root_secret, &alice_xdsa_template).unwrap();
println!(
" Alice xDSA fingerprint: {}",
hex::encode(alice_xdsa_public.fingerprint().to_bytes())
);
println!("\n3. Creating Bob's xDSA identity (signed by root)...");
let bob_xdsa_secret = xdsa::SecretKey::generate();
let bob_xdsa_public = bob_xdsa_secret.public_key();
let bob_xdsa_template = x509::Certificate {
subject: x509::Name::new().cn("Bob Identity"),
issuer: x509::Name::new().cn("Root"),
not_before: now,
not_after: end,
role: x509::Role::Authority { path_len: Some(0) },
..Default::default()
};
let bob_xdsa_cert =
xdsa::issue_cert_pem(&bob_xdsa_public, &root_secret, &bob_xdsa_template).unwrap();
println!(
" Bob xDSA fingerprint: {}",
hex::encode(bob_xdsa_public.fingerprint().to_bytes())
);
println!("\n4. Alice generates xHPKE identity (signed by her xDSA)...");
let alice_xhpke_secret = xhpke::SecretKey::generate();
let alice_xhpke_public = alice_xhpke_secret.public_key();
let alice_xhpke_template = x509::Certificate {
subject: x509::Name::new().cn("Alice Encryption"),
issuer: x509::Name::new().cn("Alice Identity"),
not_before: now,
not_after: end,
role: x509::Role::Leaf,
..Default::default()
};
let alice_xhpke_cert = xhpke::issue_cert_pem(
&alice_xhpke_public,
&alice_xdsa_secret,
&alice_xhpke_template,
)
.unwrap();
println!(
" Alice xHPKE fingerprint: {}",
hex::encode(alice_xhpke_public.fingerprint().to_bytes())
);
println!("\n5. Bob generates xHPKE identity (signed by his xDSA)...");
let bob_xhpke_secret = xhpke::SecretKey::generate();
let bob_xhpke_public = bob_xhpke_secret.public_key();
let bob_xhpke_template = x509::Certificate {
subject: x509::Name::new().cn("Bob Encryption"),
issuer: x509::Name::new().cn("Bob Identity"),
not_before: now,
not_after: end,
role: x509::Role::Leaf,
..Default::default()
};
let bob_xhpke_cert =
xhpke::issue_cert_pem(&bob_xhpke_public, &bob_xdsa_secret, &bob_xhpke_template).unwrap();
println!(
" Bob xHPKE fingerprint: {}",
hex::encode(bob_xhpke_public.fingerprint().to_bytes())
);
println!("\n6. Alice verifies Bob's identity chain...");
let verified_bob_xdsa =
xdsa::verify_cert_pem(&bob_xdsa_cert, &root_public, x509::ValidityCheck::Now)
.expect("Failed to verify Bob's xDSA cert against root");
println!(" ✓ Bob's xDSA cert verified against root");
let verified_bob_xhpke = xhpke::verify_cert_pem_with_issuer(
&bob_xhpke_cert,
&verified_bob_xdsa,
x509::ValidityCheck::Now,
)
.expect("Failed to verify Bob's xHPKE cert against his xDSA")
.public_key;
println!(" ✓ Bob's xHPKE cert verified against his xDSA");
println!("\n7. Bob verifies Alice's identity chain...");
let verified_alice_xdsa =
xdsa::verify_cert_pem(&alice_xdsa_cert, &root_public, x509::ValidityCheck::Now)
.expect("Failed to verify Alice's xDSA cert against root");
println!(" ✓ Alice's xDSA cert verified against root");
let _verified_alice_xhpke = xhpke::verify_cert_pem_with_issuer(
&alice_xhpke_cert,
&verified_alice_xdsa,
x509::ValidityCheck::Now,
)
.expect("Failed to verify Alice's xHPKE cert against her xDSA")
.public_key;
println!(" ✓ Alice's xHPKE cert verified against her xDSA");
println!("\n8. Alice sends a signed & encrypted message to Bob...");
let message = "Hello Bob! This is a secret message from Alice.";
println!(" Original message: {:?}", message);
let ciphertext = cose::seal(
&message,
&(),
&alice_xdsa_secret,
&verified_bob_xhpke,
b"demo-crypto-domain",
)
.expect("Failed to sign and encrypt message");
println!(
" ✓ Message signed & encrypted to Bob ({} bytes ciphertext)",
ciphertext.len()
);
println!("\n9. Bob receives and verifies the message...");
let decrypted: String = cose::open(
&ciphertext,
&(),
&bob_xhpke_secret,
&verified_alice_xdsa.public_key,
b"demo-crypto-domain",
None,
)
.expect("Failed to decrypt and verify message");
println!(" ✓ Message decrypted & verified");
println!(" Decrypted message: {:?}", decrypted);
}