Skip to content

Commit

Permalink
V1.2.11
Browse files Browse the repository at this point in the history
  • Loading branch information
mm9942 committed Jun 17, 2024
1 parent d9c0c40 commit d888dd3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
workspace = { members = ["crypt_guard_proc"] }
[package]
name = "crypt_guard"
version = "1.2.9"
version = "1.2.11"
edition = "2021"
description = "CryptGuardLib is a comprehensive Rust library designed for strong encryption and decryption, incorporating post-quantum cryptography to safeguard against quantum threats. It's geared towards developers who need to embed advanced cryptographic capabilities in their Rust applications."
license = "MIT"
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ An additional layer of security is provided through the appending of a HMAC (Has

### Current Release

The present version, **1.2.9**, 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.X 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. Now you also can use logging cappabilitys.
The present version, **1.2.11**, 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.X 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. Now you also can use logging cappabilitys.

### Simplifying Encryption and Decryption with Macros

Expand Down Expand Up @@ -122,8 +122,6 @@ let decrypt_message = DecryptOpen!(secret_key, public, encrypt_message, "hey, ho

```rust
use crypt_guard::{
encrypt,
decrypt,
KyberFunctions,
KeyControKyber1024,
KyberKeyFunctions,
Expand Down Expand Up @@ -159,8 +157,6 @@ Ok(())

```rust
use crypt_guard::{
encrypt,
decrypt,
KyberFunctions,
KeyControKyber1024,
KyberKeyFunctions,
Expand Down
102 changes: 72 additions & 30 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ macro_rules! DilithiumKeypair {
Dilithium2::keypair().expect("Failed to generate keypair")
},
_ => {
return eprintln!("Unsupported key size")
}
Err(Box::new(CryptError::new("Wrong key size!")) as Box<dyn std::error::Error>)
}.unwrap()
};
(public_key, secret_key)
}};
Expand All @@ -309,21 +309,21 @@ macro_rules! Encryption {
// XChaCha20
($key:expr, 1024, $data:expr, $passphrase:expr, XChaCha20) => {{
let mut encryptor = Kyber::<Encryption, Kyber1024, Data, XChaCha20>::new($key, None).expect("");
let (encrypt_message, cipher) = encryptor.encrypt_data($data, $passphrase);
let nonce = encryptor.get_nonce();
(encrypt_message, cipher, nonce)
let (encrypt_message, cipher) = encryptor.encrypt_data($data, $passphrase).expect("");
let nonce = encryptor.get_nonce().expect("");
(encrypt_message, cipher, nonce.to_string())
}};
($key:expr, 768, $data:expr, $passphrase:expr, XChaCha20) => {{
let mut encryptor = Kyber::<Encryption, Kyber768, Data, XChaCha20>::new($key, None).expect("");
let (encrypt_message, cipher) = encryptor.encrypt_data($data, $passphrase);
let nonce = encryptor.get_nonce();
(encrypt_message, cipher, nonce)
let (encrypt_message, cipher) = encryptor.encrypt_data($data, $passphrase).expect("");
let nonce = encryptor.get_nonce().expect("");
(encrypt_message, cipher, nonce.to_string())
}};
($key:expr, 512, $data:expr, $passphrase:expr, XChaCha20) => {{
let mut encryptor = Kyber::<Encryption, Kyber512, Data, XChaCha20>::new($key, None).expect("");
let (encrypt_message, cipher) = encryptor.encrypt_data($data, $passphrase);
let nonce = encryptor.get_nonce();
(encrypt_message, cipher, nonce)
let (encrypt_message, cipher) = encryptor.encrypt_data($data, $passphrase).expect("");
let nonce = encryptor.get_nonce().expect("");
(encrypt_message, cipher, nonce.to_string())
}};
}

Expand All @@ -345,34 +345,18 @@ macro_rules! Decryption {
// XChaCha20
($key:expr, 1024, $data:expr, $passphrase:expr, $cipher:expr, $nonce:expr, XChaCha20) => {{
let mut decryptor = Kyber::<Decryption, Kyber1024, Data, XChaCha20>::new($key, $nonce).expect("");
decryptor.decrypt_data($data, $passphrase)
decryptor.decrypt_data($data, $passphrase, $cipher)
}};
($key:expr, 768, $data:expr, $passphrase:expr, $cipher:expr, $nonce:expr, XChaCha20) => {{
let mut decryptor = Kyber::<Decryption, Kyber768, Data, XChaCha20>::new($key, $nonce).expect("");
decryptor.decrypt_data($data, $passphrase)
decryptor.decrypt_data($data, $passphrase, $cipher)
}};
($key:expr, 512, $data:expr, $passphrase:expr, $cipher:expr, $nonce:expr, XChaCha20) => {{
let mut decryptor = Kyber::<Decryption, Kyber512, Data, XChaCha20>::new($key, $nonce).expect("");
decryptor.decrypt_data($data, $passphrase)
}};
}

/* /// Macro for encryption of data, taking a Kyber encryption instance, a `Vec<u8>` as well as a passphrase and ciphertext as arguments
#[macro_export]
macro_rules! encrypt {
($kyber:expr, $data:expr, $passphrase:expr) => {{
$kyber.encrypt_data($data, $passphrase)
decryptor.decrypt_data($data, $passphrase, $cipher)
}};
}

/// Macro for decryption of data, taking a Kyber decryption instance, a `Vec<u8>` as well as a passphrase and ciphertext as arguments
#[macro_export]
macro_rules! decrypt {
($kyber:expr, $encrypted_data:expr, $passphrase:expr, $cipher:expr) => {{
$kyber.decrypt_data($encrypted_data, $passphrase, $cipher)
}};
}*/

/// Macro for encryption of a file, taking a Kyber decryption instance, a `PathBuf` as well as a passphrase and ciphertext as arguments
#[macro_export]
macro_rules! EncryptFile {
Expand Down Expand Up @@ -439,4 +423,62 @@ macro_rules! DecryptFile {
let mut decryptor = Kyber::<Decryption, Kyber512, Data, XChaCha20>::new($key, $nonce).expect("");
decryptor.decrypt_file($path, $passphrase, $cipher)
}};
}


#[macro_export]
macro_rules! Signature {

// Falcon
// 1024
(Falcon, $key:expr, 1024, $content:expr, Message) => {{
let sign = Signature::<Falcon1024, Message>;
sign.signature($content, $key).unwrap()
}};
(Falcon, $key:expr, 1024, $content:expr, Detached) => {{
let sign = Signature::<Falcon1024, Detached>;
sign.signature($content, $key).unwrap()
}};

// 512
(Falcon, $key:expr, 512, $content:expr, Message) => {{
let sign = Signature::<Falcon512, Message>;
sign.signature($content, key).unwrap()
}};
(Falcon, $key:expr, 512, $content:expr, Detached) => {{
let sign = Signature::<Falcon512, Detached>;
sign.signature($content, $key).unwrap()
}};

// Dilithium
// 5
(Dilithium, $key:expr, 5, $content:expr, Message) => {{
let sign = Signature::<Dilithium5, Message>;
sign.signature($content, $key).unwrap()
}};
(Dilithium, $key:expr, 5, $content:expr, Detached) => {{
let sign = Signature::<Dilithium5, Detached>;
sign.signature($content, $key).unwrap()
}};

// 3
(Dilithium, $key:expr, 3, $content:expr, Message) => {{
let sign = Signature::<Dilithium3, Message>;
sign.signature($content, $key).unwrap()
}};
(Dilithium, $key:expr, 3, $content:expr, Detached) => {{
let sign = Signature::<Dilithium3, Detached>;
sign.signature($content, $key).unwrap()
}};

// 2
(Dilithium, $key:expr, 2, $content:expr, Message) => {{
let sign = Signature::<Dilithium2, Message>;
sign.signature($content, $key).unwrap()
}};
(Dilithium, $key:expr, 2, $content:expr, Detached) => {{
let sign = Signature::<Dilithium2, Detached>;
sign.signature($content, $key).unwrap()
}};

}
21 changes: 21 additions & 0 deletions src/tests/KyberTests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,27 @@ fn encrypt_decrypt_msg_macro_AES_Kyber1024() -> Result<(), Box<dyn std::error::E
Ok(())
}

#[test]
fn encrypt_decrypt_msg_macro_XChaCha20_Kyber1024() -> Result<(), Box<dyn std::error::Error>> {
let message = "Hey, how are you doing?".as_bytes().to_owned();
let passphrase = "Test Passphrase";

// Generate key pair
let (public_key, secret_key) = KeyControKyber1024::keypair().expect("Failed to generate keypair");

// Encrypt message
let (encrypt_message, cipher, nonce) = Encryption!(public_key.clone(), 1024, message.clone(), passphrase, XChaCha20);

// Decrypt message
let decrypt_message = Decryption!(secret_key, 1024, encrypt_message, passphrase, cipher, Some(nonce.clone()), XChaCha20);

// Assert that the decrypted message matches the original message
assert_eq!(decrypt_message?, message);

Ok(())
}


#[test]
fn encrypt_decrypt_data_macro_AES_Kyber1024() -> Result<(), Box<dyn std::error::Error>> {
let data = "Hey, how are you doing?".as_bytes().to_owned();
Expand Down

0 comments on commit d888dd3

Please sign in to comment.