#rsa-key #aes-key #encryption #openssl #security #data-integrity

cryptlib

A simple cryptography library for encrypting and decrypting data based on openssl

5 releases

0.6.3 Oct 25, 2024
0.5.3 Oct 25, 2024
0.5.2 Oct 25, 2024
0.5.1 Oct 25, 2024
0.5.0 Oct 25, 2024

#697 in Cryptography

Download history 324/week @ 2024-10-20 38/week @ 2024-10-27

362 downloads per month

MIT license

94KB
2K SLoC

CryptLib

CryptLib is a simple to use cryptography crate for Rust that uses OpenSSL.

Capabilities

  • RSA encryption
  • AES encryption
  • AES encryption for streams
  • SHA 224, 256, 384, 512
  • SHA3 224, 256, 384, 512
  • SHA 224, 256, 384, 512 for streams
  • SHA3 224, 256, 384, 512 for streams
  • Composite encryption that uses AES for data and RSA for the AES key

lib.rs:

CryptLib

CryptLib is a simple cryptographic library that provides functionalities for RSA and AES encryption and decryption, as well as SHA hash functions. The library supports:

  • Creating instances with various RSA key sizes.
  • Encrypting and decrypting data using a composite method, where data is encrypted with AES and the AES key is encrypted with RSA.
  • Signing and verifying data using RSA digital signatures.
  • Generating SHA hashes for data integrity and verification.

Features

  • RSA Encryption/Decryption: Securely encrypt and decrypt data using RSA keys of different sizes.
  • AES Encryption/Decryption: Efficiently encrypt and decrypt data using AES keys.
  • Composite Encryption: Combine AES and RSA encryption to securely transmit data and keys.
  • Digital Signatures: Sign data to ensure authenticity and verify signatures to confirm data integrity.
  • SHA Hashing: Generate SHA hashes for data integrity checks.

Modules

  • rsa: RSA encryption, decryption, and digital signature functionalities.
  • aes: AES encryption and decryption functionalities.
  • sha: SHA hashing functionalities.
  • bits: Utility module for handling RSA key sizes.
  • error: Error handling for cryptographic operations.
  • responses: Structures for handling encrypted data and responses.

Usage

To use CryptLib, create an instance with the desired RSA key size and AES key, and then use the provided methods to encrypt, decrypt, sign, verify, and hash data.

You can also use the different methods seperately by creating an instance of the desired module. CryptLib is a wrapper around the different functionalities and provides a more convenient way to use them together.

Example usage of the composite encryption method:

use cryptlib::{CryptLib, rsa::Bits};

let crypt_lib = CryptLib::new(Bits::Bits2048).unwrap();
let data = b"Sensitive data";
let aad = b"Additional authenticated data. This data is not encrypted but it is safe from tampering.";

// Encrypt data
let public_key = crypt_lib.get_public_keys().unwrap();
let ciphertext = crypt_lib.encrypt_composite(&public_key, data, aad).unwrap();

// Decrypt data
let decrypted_data = crypt_lib.decrypt_composite(ciphertext).unwrap();

Example usage of signatures:

use cryptlib::{CryptLib, rsa::Bits};

let crypt_lib = CryptLib::new(Bits::Bits2048).unwrap();
let public_key = crypt_lib.get_public_keys().unwrap();
let data = b"Data to sign";

// Sign data
let signature = crypt_lib.sign(data).unwrap();

// Verify signature
let is_valid = crypt_lib.verify(&public_key, data, signature).unwrap();
assert!(is_valid);

Testing

The library includes comprehensive tests to ensure the correctness of encryption, decryption, signing, and verification functionalities. The tests cover different RSA key sizes, serialization and deserialization, and error handling for tampered data and incorrect signatures.

Dependencies

~2.2–3.5MB
~80K SLoC