basic_usage/
basic_usage.rs1use ever_crypto::{
9 XChaChaCrypto, XChaChaKey, XChaChaNonce,
10 KyberCrypto,
11 Result,
12};
13
14fn main() -> Result<()> {
15 println!("=== Ever Crypto Library Example ===\n");
16
17 println!("1. XChaCha20Poly1305 Encryption");
19 xchacha_example()?;
20
21 println!("\n2. Kyber1024 Key Encapsulation");
23 kyber_example()?;
24
25 println!("\n3. Hybrid Encryption (Kyber1024 + XChaCha20Poly1305)");
27 hybrid_example()?;
28
29 println!("\n=== All examples completed successfully! ===");
30 Ok(())
31}
32
33fn xchacha_example() -> Result<()> {
34 let key = XChaChaKey::generate();
36 let nonce = XChaChaNonce::generate();
37
38 let plaintext = b"Hello, World! This is a secret message.";
40 let aad = b"Additional authenticated data";
41
42 println!(" Original message: {:?}", std::str::from_utf8(plaintext).unwrap());
43 println!(" Key size: {} bytes", key.as_bytes().len());
44 println!(" Nonce size: {} bytes", nonce.as_bytes().len());
45
46 let ciphertext = XChaChaCrypto::encrypt(&key, &nonce, plaintext, Some(aad))?;
48 println!(" Ciphertext size: {} bytes", ciphertext.len());
49
50 let decrypted = XChaChaCrypto::decrypt(&key, &nonce, &ciphertext, Some(aad))?;
52 println!(" Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
53
54 assert_eq!(plaintext, &decrypted[..]);
56 println!(" ✓ Encryption/Decryption successful!");
57
58 Ok(())
59}
60
61fn kyber_example() -> Result<()> {
62 let alice_keypair = KyberCrypto::generate_keypair();
64
65 println!(" Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
66 println!(" Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
67
68 let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
70
71 println!(" Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
72 println!(" Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
73
74 let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
76
77 println!(" Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
78
79 assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
81 println!(" ✓ Key encapsulation successful!");
82
83 let sizes = KyberCrypto::sizes();
85 println!(" Component sizes:");
86 println!(" - Public key: {} bytes", sizes.public_key);
87 println!(" - Secret key: {} bytes", sizes.secret_key);
88 println!(" - Ciphertext: {} bytes", sizes.ciphertext);
89 println!(" - Shared secret: {} bytes", sizes.shared_secret);
90
91 Ok(())
92}
93
94fn hybrid_example() -> Result<()> {
95 println!(" Setting up hybrid encryption...");
96
97 let alice_keypair = KyberCrypto::generate_keypair();
99 let (shared_secret, kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
100
101 let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
103
104 let plaintext = b"This is a secret message using hybrid post-quantum encryption!";
106 println!(" Message to encrypt: {:?}", std::str::from_utf8(plaintext).unwrap());
107
108 let (nonce, ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
109 &xchacha_key,
110 plaintext,
111 None,
112 )?;
113
114 println!(" Hybrid encryption complete!");
115 println!(" - Kyber ciphertext size: {} bytes", kyber_ciphertext.as_bytes().len());
116 println!(" - XChaCha nonce size: {} bytes", nonce.as_bytes().len());
117 println!(" - XChaCha ciphertext size: {} bytes", ciphertext.len());
118
119 println!(" Decrypting...");
121
122 let alice_shared_secret = KyberCrypto::decapsulate(&kyber_ciphertext, &alice_keypair.secret_key);
124 let alice_xchacha_key = XChaChaKey::from_bytes(alice_shared_secret.as_bytes())?;
125
126 let decrypted = XChaChaCrypto::decrypt(&alice_xchacha_key, &nonce, &ciphertext, None)?;
128
129 println!(" Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
130
131 assert_eq!(plaintext, &decrypted[..]);
133 println!(" ✓ Hybrid encryption/decryption successful!");
134
135 Ok(())
136}