Skip to content

Commit

Permalink
V1.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mm9942 authored Apr 15, 2024
1 parent 332d170 commit 105b71f
Show file tree
Hide file tree
Showing 14 changed files with 554 additions and 26 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.2"
version = "1.2.3"
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
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ An additional layer of security is provided through the appending of a HMAC (Has

### Current Release

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

- **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 Down Expand Up @@ -84,13 +84,18 @@ println!("{}", decrypted_text);

### New signature syntax for dilithium and falcon

#### Signing and opening with Falcon
#### Signing and opening from "messages" with Falcon

```rust
use crypt_guard::KDF::*;

// Create a new keypair
let (public_key, secret_key) = Falcon1024::keypair();

// Save the keys, in the case of Falcon1024, they are saved in the folder ./Falcon1024/key(.pub & .sec)
let _ = Falcon1024::save_public(&public_key);
let _ = Falcon1024::save_secret(&secret_key);

let data = b"Hello, world!".to_vec();
let sign = Signature::<Falcon1024, Message>::new();
// Sign the message
Expand All @@ -100,13 +105,15 @@ let signed_message = sign.signature(data.clone(), secret_key);
let opened_message = sign.open(signed_message, public_key);
```

#### Signing and verifying detached with Dilithium
#### Creating and verifying detached signature with Dilithium 5

```rust
use crypt_guard::KDF::*;

// Create a new keypair
let (public_key, secret_key) = Dilithium5::keypair();
// Load the public and secret dilithium 5 key
let public_key = Dilithium5::load(&PathBuf::from("./Dilithium5/key.pub"))?;
let secret_key = Dilithium5::load(&PathBuf::from("./Dilithium5/key.sec"))?;

let data = b"Hello, world!".to_vec();

let sign = Signature::<Dilithium5, Detached>::new();
Expand Down
20 changes: 20 additions & 0 deletions examples/signed_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crypt_guard::KDF::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let (public_key, secret_key) = Falcon1024::keypair()?;
let _ = Falcon1024::save_public(&public_key);
let _ = Falcon1024::save_secret(&secret_key);

let data = b"Hello, world!".to_vec();
let sign = Signature::<Falcon1024, Message>::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)?;

let message = String::from_utf8(opened_message).expect("Failed to convert decrypted message to string");
println!("{:?}", message);
Ok(())
}
105 changes: 104 additions & 1 deletion src/Core/KDF.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ use std::{
};
use pqcrypto_traits::sign::{PublicKey, SecretKey, SignedMessage, DetachedSignature};
use crate::{
FileMetadata,
FileTypes,
FileState,
KeyTypes,
Key,
error::SigningErr,
log_activity,
LOGGER,
Expand Down Expand Up @@ -41,6 +46,9 @@ pub trait SignatureFunctions {
pub trait KeyOperations {
/// Generates a public and secret key pair.
fn keypair() -> Result<(Vec<u8>, Vec<u8>), SigningErr>;
fn save_public(public_key: &[u8]) -> Result<(), SigningErr>;
fn save_secret(secret_key: &[u8]) -> Result<(), SigningErr>;
fn load(path: &PathBuf) -> Result<Vec<u8>, SigningErr>;
}

/// Implements Falcon1024 algorithm operations.
Expand All @@ -51,6 +59,25 @@ impl KeyOperations for Falcon1024 {
let (public_key, secret_key) = falcon1024::keypair();
Ok((public_key.as_bytes().to_owned(), secret_key.as_bytes().to_owned()))
}
fn save_public(public_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Falcon1024/key.pub"), FileTypes::public_key(), FileState::not_encrypted());
let _ = file.save(public_key);
Ok(())
}
fn save_secret(secret_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Falcon1024/key.sec"), FileTypes::secret_key(), FileState::not_encrypted());
let _ = file.save(secret_key);
Ok(())
}
fn load(path: &PathBuf) -> Result<Vec<u8>, SigningErr> {
let file = match path.extension().and_then(|s| s.to_str()) {
Some("pub") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::public_key(), FileState::not_encrypted()),
Some("sec") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::secret_key(), FileState::not_encrypted()),
_ => FileMetadata::new(),
};
let key = file.load().map_err(|e| SigningErr::UnsupportedFileType(path.extension().unwrap().to_str().unwrap().to_string()))?;
Ok(key)
}
}
impl SignatureFunctions for Falcon1024 {
/// Signs a given message with the provided key.
Expand Down Expand Up @@ -103,6 +130,25 @@ impl KeyOperations for Falcon512 {
let (public_key, secret_key) = falcon512::keypair();
Ok((public_key.as_bytes().to_owned(), secret_key.as_bytes().to_owned()))
}
fn save_public(public_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Falcon512/key.pub"), FileTypes::public_key(), FileState::not_encrypted());
let _ = file.save(public_key);
Ok(())
}
fn save_secret(secret_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Falcon512/key.sec"), FileTypes::secret_key(), FileState::not_encrypted());
let _ = file.save(secret_key);
Ok(())
}
fn load(path: &PathBuf) -> Result<Vec<u8>, SigningErr> {
let file = match path.extension().and_then(|s| s.to_str()) {
Some("pub") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::public_key(), FileState::not_encrypted()),
Some("sec") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::secret_key(), FileState::not_encrypted()),
_ => FileMetadata::new(),
};
let key = file.load().map_err(|e| SigningErr::UnsupportedFileType(path.extension().unwrap().to_str().unwrap().to_string()))?;
Ok(key)
}
}
impl SignatureFunctions for Falcon512 {
/// Signs a given message with the provided key.
Expand Down Expand Up @@ -156,6 +202,26 @@ impl KeyOperations for Dilithium2 {
let (public_key, secret_key) = dilithium2::keypair();
Ok((public_key.as_bytes().to_owned(), secret_key.as_bytes().to_owned()))
}

fn save_public(public_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Dilithium2/key.pub"), FileTypes::public_key(), FileState::not_encrypted());
let _ = file.save(public_key);
Ok(())
}
fn save_secret(secret_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Dilithium2/key.sec"), FileTypes::secret_key(), FileState::not_encrypted());
let _ = file.save(secret_key);
Ok(())
}
fn load(path: &PathBuf) -> Result<Vec<u8>, SigningErr> {
let file = match path.extension().and_then(|s| s.to_str()) {
Some("pub") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::public_key(), FileState::not_encrypted()),
Some("sec") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::secret_key(), FileState::not_encrypted()),
_ => FileMetadata::new(),
};
let key = file.load().map_err(|e| SigningErr::UnsupportedFileType(path.extension().unwrap().to_str().unwrap().to_string()))?;
Ok(key)
}
}
impl SignatureFunctions for Dilithium2 {
/// Signs a given message with the provided key.
Expand Down Expand Up @@ -208,6 +274,25 @@ impl KeyOperations for Dilithium3 {
let (public_key, secret_key) = dilithium3::keypair();
Ok((public_key.as_bytes().to_owned(), secret_key.as_bytes().to_owned()))
}
fn save_public(public_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Dilithium3/key.pub"), FileTypes::public_key(), FileState::not_encrypted());
let _ = file.save(public_key);
Ok(())
}
fn save_secret(secret_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Dilithium3/key.sec"), FileTypes::secret_key(), FileState::not_encrypted());
let _ = file.save(secret_key);
Ok(())
}
fn load(path: &PathBuf) -> Result<Vec<u8>, SigningErr> {
let file = match path.extension().and_then(|s| s.to_str()) {
Some("pub") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::public_key(), FileState::not_encrypted()),
Some("sec") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::secret_key(), FileState::not_encrypted()),
_ => FileMetadata::new(),
};
let key = file.load().map_err(|e| SigningErr::UnsupportedFileType(path.extension().unwrap().to_str().unwrap().to_string()))?;
Ok(key)
}
}
impl SignatureFunctions for Dilithium3 {
/// Signs a given message with the provided key.
Expand All @@ -228,7 +313,6 @@ impl SignatureFunctions for Dilithium3 {
}
/// Opens (or verifies) a signed message with the provided key.
fn open_message(signed_data: Vec<u8>, key: Vec<u8>) -> Result<Vec<u8>, SigningErr> {

log_activity!("Starting with signing of the message.", "\nUsed key: Dilithium3");
let key = dilithium3::PublicKey::from_bytes(&key).unwrap();
let signed_message = dilithium3::SignedMessage::from_bytes(&signed_data).unwrap();
Expand Down Expand Up @@ -262,6 +346,25 @@ impl KeyOperations for Dilithium5 {
let (public_key, secret_key) = dilithium5::keypair();
Ok((public_key.as_bytes().to_owned(), secret_key.as_bytes().to_owned()))
}
fn save_public(public_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Dilithium5/key.pub"), FileTypes::public_key(), FileState::not_encrypted());
let _ = file.save(public_key);
Ok(())
}
fn save_secret(secret_key: &[u8]) -> Result<(), SigningErr> {
let file = FileMetadata::from(PathBuf::from("./Dilithium5/key.sec"), FileTypes::secret_key(), FileState::not_encrypted());
let _ = file.save(secret_key);
Ok(())
}
fn load(path: &PathBuf) -> Result<Vec<u8>, SigningErr> {
let file = match path.extension().and_then(|s| s.to_str()) {
Some("pub") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::public_key(), FileState::not_encrypted()),
Some("sec") => FileMetadata::from(PathBuf::from(path.as_os_str().to_str().unwrap()), FileTypes::secret_key(), FileState::not_encrypted()),
_ => FileMetadata::new(),
};
let key = file.load().map_err(|e| SigningErr::UnsupportedFileType(path.extension().unwrap().to_str().unwrap().to_string()))?;
Ok(key)
}
}
impl SignatureFunctions for Dilithium5 {
/// Signs a given message with the provided key.
Expand Down
Loading

0 comments on commit 105b71f

Please sign in to comment.