Crate crypt_guard_lite

Source
Expand description

§CryptGuard Lite

Crates.io MIT licensed Documentation Hashnode Blog GitHub Library

§Introduction

CryptGuard Lite is a cryptographic library that provides a generic interface for cryptographic operations, wrapping around the crypt_guard crate to offer easy access to its core functionality.

This library allows seamless interaction with underlying cryptographic operations through provided instances of type T (which could be the Crypto or Sign struct instances). It offers methods to create and manage cryptographic operations, including keypair generation, data signing, and encryption/decryption using AES and XChaCha20 cryptographic algorithms. Supported key sizes for Kyber are 1024, 768, and 512, while for Sign, Falcon supports 1024 and 512, and Dilithium supports 5, 3, and 2.

A detached signature is a signature that is separate from the data it signs. This means that the signature is not included within the data itself but is provided separately for verification purposes. In such cases, data is returned upon verification instead of directly modifying and retrieving the signature.

§Examples

§Signing

§Dilithium Signing
use crypt_guard_lite::{CryptGuard, KeyVariants, Sign};
use crypt_guard::error::SigningErr;

pub fn main() -> Result<(), SigningErr> {
    let key_size = 5;
    let (public_key, secret_key) = Sign::keypair(KeyVariants::Dilithium, key_size).unwrap();
    let mut guard = CryptGuard::signature(secret_key, KeyVariants::Dilithium, key_size);

    let data = b"hey, how are you".to_vec();
    let signing_data = data.clone();

    let signature = guard.signed_data(signing_data.clone())?;
    println!("Signature: {:?}", signature);

    let mut guard = CryptGuard::signature(public_key, KeyVariants::Dilithium, key_size);
    let opened_data = guard.open(signature.clone())?;
    println!("Opened data: {:?}", opened_data);

    Ok(())
}
§Falcon Detached Signature
use crypt_guard_lite::{CryptGuard, KeyVariants, Sign};
use crypt_guard::error::SigningErr;

pub fn main() -> Result<(), SigningErr> {
    let key_size = 512;
    let (public_key, secret_key) = Sign::keypair(KeyVariants::Falcon, key_size).unwrap();
    let mut guard = CryptGuard::signature(secret_key, KeyVariants::Falcon, key_size);

    let data = vec![1, 2, 3, 4, 5];
    let signature = guard.detached(data.clone())?;
    println!("Signature: {:?}", signature);

    let mut guard = CryptGuard::signature(public_key, KeyVariants::Falcon, key_size);
    let verified = guard.verify(data.clone(), signature.clone())?;
    println!("Verification: {:?}", verified);

    Ok(())
}

§Encryption

§AES Encryption
use crypt_guard_lite::{CryptGuard, Crypto};
use crypt_guard::error::CryptError;

pub fn main() -> Result<(), CryptError> {
    let key_size = 1024;
    let passphrase = "password".to_string();
    let (secret_key, public_key) = Crypto::keypair(key_size).unwrap();
    let mut guard = CryptGuard::cryptography(secret_key, key_size, passphrase.clone(), None, None);

    let data = b"hey, how are you".to_vec();
    let (encrypted_data, cipher) = guard.aencrypt(data.clone()).unwrap();
    println!("Encrypted data: {:?}", encrypted_data);

    let mut guard = CryptGuard::cryptography(public_key, key_size, passphrase.clone(), Some(cipher), None);
    let decrypted_data = guard.adecrypt(encrypted_data.clone()).unwrap();
    println!("Decrypted data: {:?}", decrypted_data);

    Ok(())
}
§XChaCha20 Encryption
use crypt_guard_lite::{CryptGuard, Crypto};
use crypt_guard::error::CryptError;

pub fn main() -> Result<(), CryptError> {
    let key_size = 1024;
    let passphrase = "password".to_string();
    let (secret_key, public_key) = Crypto::keypair(key_size).unwrap();
    let mut guard = CryptGuard::cryptography(secret_key, key_size, passphrase.clone(), None, None);

    let data = b"hey, how are you".to_vec();
    let (encrypted_data, cipher, nonce) = guard.xencrypt(data.clone()).unwrap();
    println!("Encrypted data: {:?}", encrypted_data);

    let mut guard = CryptGuard::cryptography(public_key, key_size, passphrase.clone(), Some(cipher), Some(nonce.clone()));
    let decrypted_data = guard.xdecrypt(encrypted_data.clone(), nonce).unwrap();
    println!("Decrypted data: {:?}", decrypted_data);

    Ok(())
}

Structs§

CryptGuard
CryptGuard struct is a generic wrapper around cryptographic operations, such as signing and encryption. It allows easy interaction with the underlying cryptographic operations through the provided instance of type T.
Crypto
Crypto struct represents an encryption operation
Sign
Sign struct represents a signing operation

Enums§

KeyVariants
KeyVariants enum represents different types of keys.