ever-crypto 0.1.0

A simple Rust library providing easy-to-use APIs for XChaCha20Poly1305 and Kyber1024 post-quantum cryptography at the same time. Based on chacha20poly1305 and pqcrypto-kyber.
Documentation
//! Basic usage example for Ever Crypto library
//! 
//! This example demonstrates:
//! 1. XChaCha20Poly1305 encryption/decryption
//! 2. Kyber1024 key encapsulation
//! 3. Hybrid encryption combining both

use ever_crypto::{
    XChaChaCrypto, XChaChaKey, XChaChaNonce,
    KyberCrypto,
    Result,
};

fn main() -> Result<()> {
    println!("=== Ever Crypto Library Example ===\n");
    
    // Example 1: XChaCha20Poly1305 Encryption
    println!("1. XChaCha20Poly1305 Encryption");
    xchacha_example()?;
    
    // Example 2: Kyber1024 Key Encapsulation
    println!("\n2. Kyber1024 Key Encapsulation");
    kyber_example()?;
    
    // Example 3: Hybrid Encryption
    println!("\n3. Hybrid Encryption (Kyber1024 + XChaCha20Poly1305)");
    hybrid_example()?;
    
    println!("\n=== All examples completed successfully! ===");
    Ok(())
}

fn xchacha_example() -> Result<()> {
    // Generate a key and nonce
    let key = XChaChaKey::generate();
    let nonce = XChaChaNonce::generate();
    
    // Message to encrypt
    let plaintext = b"Hello, World! This is a secret message.";
    let aad = b"Additional authenticated data";
    
    println!("  Original message: {:?}", std::str::from_utf8(plaintext).unwrap());
    println!("  Key size: {} bytes", key.as_bytes().len());
    println!("  Nonce size: {} bytes", nonce.as_bytes().len());
    
    // Encrypt the message
    let ciphertext = XChaChaCrypto::encrypt(&key, &nonce, plaintext, Some(aad))?;
    println!("  Ciphertext size: {} bytes", ciphertext.len());
    
    // Decrypt the message
    let decrypted = XChaChaCrypto::decrypt(&key, &nonce, &ciphertext, Some(aad))?;
    println!("  Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
    
    // Verify they match
    assert_eq!(plaintext, &decrypted[..]);
    println!("  ✓ Encryption/Decryption successful!");
    
    Ok(())
}

fn kyber_example() -> Result<()> {
    // Alice generates a key pair
    let alice_keypair = KyberCrypto::generate_keypair();
    
    println!("  Alice's public key size: {} bytes", alice_keypair.public_key.as_bytes().len());
    println!("  Alice's secret key size: {} bytes", alice_keypair.secret_key.as_bytes().len());
    
    // Bob encapsulates a shared secret for Alice
    let (bob_shared_secret, ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
    
    println!("  Encapsulated ciphertext size: {} bytes", ciphertext.as_bytes().len());
    println!("  Bob's shared secret: {} bytes", bob_shared_secret.as_bytes().len());
    
    // Alice decapsulates the shared secret
    let alice_shared_secret = KyberCrypto::decapsulate(&ciphertext, &alice_keypair.secret_key);
    
    println!("  Alice's shared secret: {} bytes", alice_shared_secret.as_bytes().len());
    
    // Verify they match
    assert_eq!(bob_shared_secret.as_bytes(), alice_shared_secret.as_bytes());
    println!("  ✓ Key encapsulation successful!");
    
    // Display key sizes
    let sizes = KyberCrypto::sizes();
    println!("  Component sizes:");
    println!("    - Public key: {} bytes", sizes.public_key);
    println!("    - Secret key: {} bytes", sizes.secret_key);
    println!("    - Ciphertext: {} bytes", sizes.ciphertext);
    println!("    - Shared secret: {} bytes", sizes.shared_secret);
    
    Ok(())
}

fn hybrid_example() -> Result<()> {
    println!("  Setting up hybrid encryption...");
    
    // Step 1: Key establishment with Kyber1024
    let alice_keypair = KyberCrypto::generate_keypair();
    let (shared_secret, kyber_ciphertext) = KyberCrypto::encapsulate(&alice_keypair.public_key);
    
    // Step 2: Use shared secret as XChaCha20Poly1305 key
    let xchacha_key = XChaChaKey::from_bytes(shared_secret.as_bytes())?;
    
    // Step 3: Encrypt data with XChaCha20Poly1305
    let plaintext = b"This is a secret message using hybrid post-quantum encryption!";
    println!("  Message to encrypt: {:?}", std::str::from_utf8(plaintext).unwrap());
    
    let (nonce, ciphertext) = XChaChaCrypto::encrypt_with_random_nonce(
        &xchacha_key,
        plaintext,
        None,
    )?;
    
    println!("  Hybrid encryption complete!");
    println!("  - Kyber ciphertext size: {} bytes", kyber_ciphertext.as_bytes().len());
    println!("  - XChaCha nonce size: {} bytes", nonce.as_bytes().len());
    println!("  - XChaCha ciphertext size: {} bytes", ciphertext.len());
    
    // Decryption process:
    println!("  Decrypting...");
    
    // Step 1: Alice decapsulates the shared secret
    let alice_shared_secret = KyberCrypto::decapsulate(&kyber_ciphertext, &alice_keypair.secret_key);
    let alice_xchacha_key = XChaChaKey::from_bytes(alice_shared_secret.as_bytes())?;
    
    // Step 2: Decrypt the message
    let decrypted = XChaChaCrypto::decrypt(&alice_xchacha_key, &nonce, &ciphertext, None)?;
    
    println!("  Decrypted message: {:?}", std::str::from_utf8(&decrypted).unwrap());
    
    // Verify they match
    assert_eq!(plaintext, &decrypted[..]);
    println!("  ✓ Hybrid encryption/decryption successful!");
    
    Ok(())
}