Skip to content

Commit

Permalink
V1.2.6
Browse files Browse the repository at this point in the history
The versions 1.2.4 and 1.2.5 had errors which is why they weren't uploaded
  • Loading branch information
mm9942 authored Apr 17, 2024
1 parent 72019f6 commit ae68e7e
Show file tree
Hide file tree
Showing 15 changed files with 806 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "crypt_guard"
version = "1.2.3"
version = "1.2.6"
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
156 changes: 148 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,43 @@ An additional layer of security is provided through the appending of a HMAC (Has

### Current Release

The present version, **1.2.3**, 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.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.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

We've introduced new macros to make the encryption and decryption processes more straightforward since we only separate into encryption of bytes and automated encryption of files, thus providing an alternative to the need of manually invoking specific functions such as `encrypt_msg`, `encrypt_file`, `encrypt_data`, and their decryption equivalents. Here’s a guide on how to effectively utilize these macros:

- **Encryption Macro**: Use the `encrypt!` macro for seamless encryption tasks. Provide it with an instance of Kyber configured for encryption, the data you want to encrypt (as a `Vec<u8>`), and a passphrase (as a string slice `&str`).

**Syntax**:
```rust
encrypt!(kyber_encryption_instance, data: Vec<u8>, passphrase)
```

- **Decryption Macro**: The `decrypt!` macro simplifies the decryption process. Supply it with an instance of Kyber configured for decryption, the encrypted data (as `Vec<u8>`), the passphrase, and the ciphertext.

**Syntax**:
```rust
decrypt!(kyber_decryption_instance, data: Vec<u8>, passphrase, cipher)
```

- **File Encryption Macro**: We've also implemented a macro specifically for file encryption, `encrypt_file!()`. This macro is similar to `encrypt!` but takes a `PathBuf` for file paths instead of `Vec<u8>`.

**Syntax**:
```rust
encrypt_file!(kyber_encryption_instance, data: PathBuf, passphrase)
```

- **File Decryption Macro**: Corresponding to the file encryption macro, the `decrypt_file!()` macro is designed for file decryption, accepting a `PathBuf` instead of `Vec<u8>`.

**Syntax**:
```rust
decrypt_file!(kyber_decryption_instance, data: PathBuf, passphrase, cipher)
```

These macros are intended to make your cryptographic operations more intuitive and less prone to errors, by removing the complexities associated with selecting the appropriate function for different data types. Note that with these macros, it is necessary to convert messages into `Vec<u8>` before encryption.

#### Other Changes

- **Simplified Syntax**: We've re-engineered the use of Dilithium and Falcon, adopting a straightforward, modular, and flexible approach akin to our encryption and decryption syntax. This enhancement aims to streamline operations for developers.

Expand All @@ -49,6 +85,92 @@ The present version, **1.2.3**, emphasizes detailed cryptographic operations. Th

## Usage Examples

### New encryption and decryption macros

```rust
use crypt_guard::{
encrypt,
decrypt,
KyberFunctions,
KeyControKyber1024,
KyberKeyFunctions,
error::*,
Encryption,
Decryption,
Kyber1024,
Message,
AES,
Kyber,
};

// Since we only allow encryption/ decryption of Vec<u8> or files through selecting a path as &str, please use
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");

// Instantiate Kyber for encryption with Kyber1024
let mut encryptor = Kyber::<Encryption, Kyber1024, Message, AES>::new(public_key.clone(), None)?;

// Encrypt message with new encryption macro
// Provide it with an instance of Kyber configured for encryption, the data you want to encrypt (this can be a `PathBuf`, a string slice `&str`, or a byte vector `Vec<u8>`), a passphrase (as a string slice `&str`) and boolean checking if it is a file
let (encrypt_message, cipher) = encrypt!(encryptor, message, passphrase)?;

// Instantiate Kyber for decryption with Kyber1024
let mut decryptor = Kyber::<Decryption, Kyber1024, Message, AES>::new(secret_key, None)?;

// Decrypt message with new decryption macro
// Provide it with an instance of Kyber configured for decryption, the data you want to decrypt (this can be a `PathBuf`, a string slice `&str`, or a byte vector `Vec<u8>`), a passphrase (as a string slice `&str`) as well as a ciphertext and boolean checking if it is a file
let decrypt_message = decrypt!(decryptor, encrypt_message, passphrase, cipher);
println!("{}", String::from_utf8(decrypt_message?).expect("Failed to convert decrypted message to string"));
Ok(())

```

#### Usage of the new macros with a file

```rust
use crypt_guard::{
encrypt,
decrypt,
KyberFunctions,
KeyControKyber1024,
KyberKeyFunctions,
error::*,
Encryption,
Decryption,
Kyber1024,
Message,
AES,
Kyber,
};

// Since we only allow encryption/ decryption of Vec<u8> or files through selecting a path as &str
let path = "./message.txt";
let passphrase = "Test Passphrase";

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

// Instantiate Kyber for encryption with Kyber1024
let mut encryptor = Kyber::<Encryption, Kyber1024, Message, AES>::new(public_key.clone(), None)?;

// Encrypt message with new encryption macro
// Provide it with an instance of Kyber configured for encryption, the data you want to encrypt (this can be a `PathBuf`, a string slice `&str`, or a byte vector `Vec<u8>`), a passphrase (as a string slice `&str`) and boolean checking if it is a file
let (encrypt_message, cipher) = encrypt_file!(encryptor, PathBuf::from(&path), passphrase)?;

// Instantiate Kyber for decryption with Kyber1024
let mut decryptor = Kyber::<Decryption, Kyber1024, Message, AES>::new(secret_key, None)?;

// Decrypt message with new decryption macro
// Provide it with an instance of Kyber configured for decryption, the data you want to decrypt (this can be a `PathBuf`, a string slice `&str`, or a byte vector `Vec<u8>`), a passphrase (as a string slice `&str`) as well as a ciphertext and boolean checking if it is a file
let decrypt_message = decrypt_file!(decryptor, PathBuf::from(format!("{}.enc", path)), passphrase, cipher);
println!("{}", String::from_utf8(decrypt_message?).expect("Failed to convert decrypted message to string"));
Ok(())

```

### The new Logging feature

CryptGuard's latest release introduces a logging feature, meticulously designed to offer comprehensive insights into cryptographic operations while prioritizing security and privacy.
Expand All @@ -71,13 +193,13 @@ let passphrase = "Test Passphrase";
let (public_key, secret_key) = KeyControKyber1024::keypair().expect("Failed to generate keypair");

// Instantiate Kyber for encryption with Kyber1024
let mut encryptor = Kyber::<Encryption, Kyber1024, File, AES>::new(public_key.clone(), None)?;
let mut encryptor = Kyber::<Encryption, Kyber1024, Files, AES>::new(public_key.clone(), None)?;

// Encrypt message
let (encrypt_message, cipher) = encryptor.encrypt_msg(message.clone(), passphrase.clone())?;

// Instantiate Kyber for decryption with Kyber1024
let mut decryptor = Kyber::<Decryption, Kyber1024, File, AES>::new(secret_key, None)?;
let mut decryptor = Kyber::<Decryption, Kyber1024, Files, AES>::new(secret_key, None)?;

// Decrypt message
let decrypt_message = decryptor.decrypt_msg(encrypt_message.clone(), passphrase.clone(), cipher)?;
Expand Down Expand Up @@ -150,7 +272,7 @@ keycontrol.set_secret_key(secret_key.clone()).unwrap();
keycontrol.save(KeyTypes::SecretKey, "./key".into()).unwrap();
```

### Encryption of a File using AES
### Encryption of a Message using AES

```rust
let message = "Hey, how are you doing?";
Expand All @@ -168,6 +290,24 @@ key_control.set_ciphertext(cipher.clone()).unwrap();
key_control.save(KeyTypes::Ciphertext, "./key".into()).unwrap();
```

### Encryption of a Data using AES

```rust
let message = "Hey, how are you doing?".as_bytes().to_owned();
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::<Encryption, Kyber1024, Data, AES>::new(public_key.clone(), None)?;

// Encrypt message
let (encrypt_message, cipher) = encryptor.encrypt_data(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
Expand All @@ -176,7 +316,7 @@ let secret_key = key_control.load(KeyTypes::SecretKey, Path::new("./key/secret_k

// 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::<Decryption, Kyber1024, File, AES>::new(secret_key, None)?;
let mut decryptor = Kyber::<Decryption, Kyber1024, Files, AES>::new(secret_key, None)?;

// Decrypt message
let decrypt_message = decryptor.decrypt_msg(encrypt_message.clone(), passphrase.clone(), cipher)?;
Expand Down Expand Up @@ -205,7 +345,7 @@ let (public_key, secret_key) = KeyControKyber768::keypair().expect("Failed to ge

// 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::<Encryption, Kyber768, File, XChaCha20>::new(public_key.clone(), None)?;
let mut encryptor = Kyber::<Encryption, Kyber768, Files, XChaCha20>::new(public_key.clone(), None)?;

// Encrypt message
let (encrypt_message, cipher) = encryptor.encrypt_file(enc_path.clone(), passphrase.clone())?;
Expand All @@ -216,7 +356,7 @@ 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::<Decryption, Kyber768, File, XChaCha20>::new(secret_key, Some(nonce?.to_string()))?;
let mut decryptor = Kyber::<Decryption, Kyber768, Files, XChaCha20>::new(secret_key, Some(nonce?.to_string()))?;

// Decrypt message
let decrypt_message = decryptor.decrypt_file(dec_path.clone(), passphrase.clone(), cipher)?;
Expand All @@ -229,4 +369,4 @@ We appreciate your engagement with our cryptographic library. As we strive to im
Thank you for your support and for making security a priority in your projects.

## License
CryptGuard is licensed under the MIT LICENSE. The full license text is available in the `LICENSE` file in the repository.
CryptGuard is licensed under the MIT LICENSE. The full license text is available in the `LICENSE` file in the repository.
7 changes: 6 additions & 1 deletion examples/encrypt_aes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//use crypt_guard::KeyKyber::KeyControl;
use crypt_guard::KeyControler::KeyControl;
use crypt_guard::{*, error::*};
use std::{
fs::{self, File},
Expand All @@ -13,6 +13,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let message = "Hey, how are you doing?";
let passphrase = "Test Passphrase";

let mut key_control = KeyControl::<KeyControKyber1024>::new();

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

Expand All @@ -23,6 +25,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// Encrypt message
let (encrypt_message, cipher) = encryptor.encrypt_msg(message.clone(), passphrase.clone())?;

key_control.set_ciphertext(cipher.clone()).unwrap();
key_control.save(KeyTypes::Ciphertext, "./key".into()).unwrap();

// 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::<Decryption, Kyber1024, File, AES>::new(secret_key, None)?;
Expand Down
35 changes: 35 additions & 0 deletions examples/macro_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crypt_guard::{
encrypt,
decrypt,
KyberFunctions,
KeyControKyber1024,
KyberKeyFunctions,
error::*,
Encryption,
Decryption,
Kyber1024,
Message,
AES,
Kyber,
};
fn main() -> 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");

// Instantiate Kyber for encryption with Kyber1024
let mut encryptor = Kyber::<Encryption, Kyber1024, Message, AES>::new(public_key.clone(), None)?;

// Encrypt message
let (encrypt_message, cipher) = encrypt!(encryptor, message, passphrase)?;

// Instantiate Kyber for decryption with Kyber1024
let mut decryptor = Kyber::<Decryption, Kyber1024, Message, AES>::new(secret_key, None)?;

// Decrypt message
let decrypt_message = decrypt!(decryptor, encrypt_message, passphrase, cipher);
println!("{}", String::from_utf8(decrypt_message?).expect("Failed to convert decrypted message to string"));
Ok(())
}
10 changes: 7 additions & 3 deletions src/Core/cipher_aes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use super::*;
use crate::{
*,
error::CryptError,
hmac_sign::*,
cryptography::{
CryptographicInformation,
CipherAES,
hmac_sign::*,
},
Core::{
CryptographicFunctions,
KeyControl,
KeyControKyber512,
KeyControKyber768,
Expand Down Expand Up @@ -166,7 +170,7 @@ impl CipherAES {
let verified_data = verifier.hmac();

self.infos.set_data(&verified_data)?;
//println!("{:?}", verified_data);
// println!("{:?}", verified_data);
let data = self.decrypt_aes()?;
if self.infos.safe()? {
let _ = self.infos.set_data(&data)?;
Expand Down
9 changes: 6 additions & 3 deletions src/Core/cipher_xchacha.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use super::*;
use crate::{
*,
cryptography::*,
cryptography::{
CryptographicInformation,
CipherChaCha,
hmac_sign::*,
},
error::*,
hmac_sign::*,
Core::{
CryptographicFunctions,
KeyControl,
KeyControKyber512,
KeyControKyber768,
Expand Down
1 change: 1 addition & 0 deletions src/Core/kyber/KeyControler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub trait KyberKeyFunctions {
/// Decapsulates a secret using a secret key and a ciphertext.
fn decap(secret_key: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, CryptError>;
}

/// Implementation for Kyber 1024 variant.
pub struct KeyControKyber1024;
impl KyberKeyFunctions for KeyControKyber1024{
Expand Down
Loading

0 comments on commit ae68e7e

Please sign in to comment.