diff --git a/README.md b/README.md index 034e8f7..746ec41 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ The implementation of the logging logic is taking a bit longer as we're delibera ### Current Release -The present version, **1.1.5**, emphasizes detailed cryptographic operations. This version is ideal for those who want a fast but not too complicated, elaborate approach to cryptography and don't want to use asynchronous code. Asynchronous capabilities will be reimplemented in a later update (but this time as a feature). For those who prefer using async implementation, use version 1.0.3 until a later update is released. This version's syntax is more user-friendly and does not require the definition of too many structs like in 1.1.1 or 1.1.0 but allows for precise control over the encryption and decryption algorithm as well as the Kyber key size. It allows the usage of Kyber1024, Kyber768, and Kyber512. +The present version, **1.1.6**, emphasizes detailed cryptographic operations. This version is ideal for those who want a fast but not too complicated, elaborate approach to cryptography and don't want to use asynchronous code. Asynchronous capabilities will be reimplemented in a later update (but this time as a feature). For those who prefer using async implementation, use version 1.0.3 until a later update is released. This version's syntax is more user-friendly and does not require the definition of too many structs like in 1.1.1 or 1.1.0 but allows for precise control over the encryption and decryption algorithm as well as the Kyber key size. It allows the usage of Kyber1024, Kyber768, and Kyber512. ### Future Release @@ -63,35 +63,35 @@ For those considering the transition to the updated version upon its release, fa #### Signing and opening with Falcon ```rust - use crypt_guard::KDF::*; - - // Create a new keypair - let (public_key, secret_key) = Falcon1024::keypair(); - let data = b"Hello, world!".to_vec(); - let sign = Signature::::new(); - // Sign the message - let signed_message = sign.signature(data.clone(), secret_key); - - // Open the message - let opened_message = sign.open(signed_message, public_key); +use crypt_guard::KDF::*; + +// Create a new keypair +let (public_key, secret_key) = Falcon1024::keypair(); +let data = b"Hello, world!".to_vec(); +let sign = Signature::::new(); +// Sign the message +let signed_message = sign.signature(data.clone(), secret_key); + +// Open the message +let opened_message = sign.open(signed_message, public_key); ``` #### Signing and verifying detached with Dilithium ```rust - use crypt_guard::KDF::*; +use crypt_guard::KDF::*; - // Create a new keypair - let (public_key, secret_key) = Dilithium5::keypair(); - let data = b"Hello, world!".to_vec(); +// Create a new keypair +let (public_key, secret_key) = Dilithium5::keypair(); +let data = b"Hello, world!".to_vec(); - let sign = Signature::::new(); +let sign = Signature::::new(); - // Create a detached signature - let signature = sign.signature(data.clone(), secret_key); +// Create a detached signature +let signature = sign.signature(data.clone(), secret_key); - // Verify the detached signature - let is_valid = sign.verify(data, signature, public_key); +// Verify the detached signature +let is_valid = sign.verify(data, signature, public_key); ``` ### Cryptographic Operations @@ -101,89 +101,89 @@ For those considering the transition to the updated version upon its release, fa This example illustrates generating a key pair and saving it to files, leveraging the `KeyControKyber1024::keypair()` method for key pair generation and the `KeyControl::` instance for setting and saving the keys. ```rust - // Generate a keypair - let (public_key, secret_key) = KeyControKyber1024::keypair().unwrap(); +// Generate a keypair +let (public_key, secret_key) = KeyControKyber1024::keypair().unwrap(); - let keycontrol = KeyControl::::new(); +let keycontrol = KeyControl::::new(); - // Save Public and Secret key while defining the folder (./key). - keycontrol.set_public_key(public_key.clone()).unwrap(); - keycontrol.save(KeyTypes::PublicKey, "./key".into()).unwrap(); +// Save Public and Secret key while defining the folder (./key). +keycontrol.set_public_key(public_key.clone()).unwrap(); +keycontrol.save(KeyTypes::PublicKey, "./key".into()).unwrap(); - keycontrol.set_secret_key(secret_key.clone()).unwrap(); - keycontrol.save(KeyTypes::SecretKey, "./key".into()).unwrap(); +keycontrol.set_secret_key(secret_key.clone()).unwrap(); +keycontrol.save(KeyTypes::SecretKey, "./key".into()).unwrap(); ``` ### Encryption of a File using AES ```rust - let message = "Hey, how are you doing?"; - let passphrase = "Test Passphrase"; - - // Instantiate Kyber for encryption of a message with Kyber1024 and AES - // Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! - let mut encryptor = Kyber::::new(public_key.clone(), None)?; - - // Encrypt message - let (encrypt_message, cipher) = encryptor.encrypt_msg(message.clone(), passphrase.clone())?; - - // Save the ciphertext for decryption in folder ./key - key_control.set_ciphertext(cipher.clone()).unwrap(); - key_control.save(KeyTypes::Ciphertext, "./key".into()).unwrap(); +let message = "Hey, how are you doing?"; +let passphrase = "Test Passphrase"; + +// Instantiate Kyber for encryption of a message with Kyber1024 and AES +// Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! +let mut encryptor = Kyber::::new(public_key.clone(), None)?; + +// Encrypt message +let (encrypt_message, cipher) = encryptor.encrypt_msg(message.clone(), passphrase.clone())?; + +// Save the ciphertext for decryption in folder ./key +key_control.set_ciphertext(cipher.clone()).unwrap(); +key_control.save(KeyTypes::Ciphertext, "./key".into()).unwrap(); ``` ### Decryption of a File using AES ```rust - let cipher = key_control.load(KeyTypes::Ciphertext, Path::new("./key/ciphertext.ct")); - let secret_key = key_control.load(KeyTypes::SecretKey, Path::new("./key/secret_key.sec")); - - // Instantiate Kyber for decryption of a message with Kyber1024 and AES - // Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! - let mut decryptor = Kyber::::new(secret_key, None)?; - - // Decrypt message - let decrypt_message = decryptor.decrypt_msg(encrypt_message.clone(), passphrase.clone(), cipher)?; - - // Print the decrypted text - println!("{:?}", String::from_utf8(decrypt_message)); +let cipher = key_control.load(KeyTypes::Ciphertext, Path::new("./key/ciphertext.ct")); +let secret_key = key_control.load(KeyTypes::SecretKey, Path::new("./key/secret_key.sec")); + +// Instantiate Kyber for decryption of a message with Kyber1024 and AES +// Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! +let mut decryptor = Kyber::::new(secret_key, None)?; + +// Decrypt message +let decrypt_message = decryptor.decrypt_msg(encrypt_message.clone(), passphrase.clone(), cipher)?; + +// Print the decrypted text +println!("{:?}", String::from_utf8(decrypt_message)); ``` #### Encryption and decryption of a message written into a file with XChaCha20 ```rust - let message = "Hey, how are you doing?"; +let message = "Hey, how are you doing?"; + +let tmp_dir = TempDir::new().map_err(|e| CryptError::from(e))?; +let tmp_dir = Builder::new().prefix("messages").tempdir().map_err(|e| CryptError::from(e))?; + +let enc_path = tmp_dir.path().clone().join("message.txt"); +let dec_path = tmp_dir.path().clone().join("message.txt.enc"); + +fs::write(&enc_path, message.as_bytes())?; - let tmp_dir = TempDir::new().map_err(|e| CryptError::from(e))?; - let tmp_dir = Builder::new().prefix("messages").tempdir().map_err(|e| CryptError::from(e))?; - - let enc_path = tmp_dir.path().clone().join("message.txt"); - let dec_path = tmp_dir.path().clone().join("message.txt.enc"); - - fs::write(&enc_path, message.as_bytes())?; +let passphrase = "Test Passphrase"; - let passphrase = "Test Passphrase"; +// Generate key pair +let (public_key, secret_key) = KeyControKyber768::keypair().expect("Failed to generate keypair"); - // Generate key pair - let (public_key, secret_key) = KeyControKyber768::keypair().expect("Failed to generate keypair"); +// Instantiate Kyber for encryption of a file with Kyber768 and XChaCha20 +// Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! +let mut encryptor = Kyber::::new(public_key.clone(), None)?; - // Instantiate Kyber for encryption of a file with Kyber768 and XChaCha20 - // Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! - let mut encryptor = Kyber::::new(public_key.clone(), None)?; +// Encrypt message +let (encrypt_message, cipher) = encryptor.encrypt_file(enc_path.clone(), passphrase.clone())?; - // Encrypt message - let (encrypt_message, cipher) = encryptor.encrypt_file(enc_path.clone(), passphrase.clone())?; +let nonce = encryptor.get_nonce(); - let nonce = encryptor.get_nonce(); +fs::remove_file(enc_path.clone()); - fs::remove_file(enc_path.clone()); +// Instantiate Kyber for decryption of a file with Kyber768 and XChaCha20 +// Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! +let mut decryptor = Kyber::::new(secret_key, Some(nonce?.to_string()))?; - // Instantiate Kyber for decryption of a file with Kyber768 and XChaCha20 - // Fails when not using either of these properties since it would be the wrong type of algorithm, data, keysize or process! - let mut decryptor = Kyber::::new(secret_key, Some(nonce?.to_string()))?; - - // Decrypt message - let decrypt_message = decryptor.decrypt_file(dec_path.clone(), passphrase.clone(), cipher)?; +// Decrypt message +let decrypt_message = decryptor.decrypt_file(dec_path.clone(), passphrase.clone(), cipher)?; ``` ### Conclusion and Looking Forward