Crate secured_cipher

Source
Expand description

§Secured-Cipher Library

secured-cipher is a Rust library offering an implementation of the ChaCha20 and XChaCha20 encryption algorithms. It provides both high-level and low-level cryptographic functionalities through a common interface.

§Overview

The library includes the following key components:

  • core: A module containing essential ChaCha20 cryptographic functionalities.
  • ChaCha20: A struct for the ChaCha20 stream cipher algorithm.
  • Cipher: A struct that provides a common interface for cryptographic operations, focusing on encryption and decryption.
  • CipherMode: An enum to specify the mode of the cipher (only ChaCha20 for now).

§Features

  • High-level interfaces for ChaCha20 and XChaCha20 ciphers.
  • Common Cipher interface for encryption and decryption operations.
  • Flexible usage with support for both raw and high-level cryptographic operations.

§Usage

§Basic Encryption and Decryption

This example demonstrates encrypting and decrypting data using the ChaCha20 cipher.

use secured_cipher::Cipher;

let key: [u8; 32] = [0; 32]; // Your key
let nonce: [u8; 12] = [0; 12]; // Your nonce
let data: &[u8] = b"Your data here"; // Data to be encrypted

let mut cipher = Cipher::default();
cipher.init(&key, &nonce);

// Encrypt and decrypt
let encrypted_data = cipher.encrypt(data);
let decrypted_data = cipher.decrypt(&encrypted_data);

// Sign - the secret evelope contains the encrypted data and its MAC (message authentication code)
let signed_secret_envelope = cipher.sign(b"your readable header", &encrypted_data);

// Decrypt and verify - the verified decrypted data is returned if the MAC is valid
let verified_decrypted_data = cipher.decrypt_and_verify(&signed_secret_envelope);

// if the MAC is invalid, the decryption will fail
let is_decryption_ok = verified_decrypted_data.is_ok();

println!("Decrypted and verified data: {:?}", verified_decrypted_data.unwrap());

§Modules

  • core: Core functionalities and algorithmic implementations.
  • stream: Internal stream cipher operations, including ChaChaStream.

Re-exports§

pub use algorithm::AEADAlgorithm;
pub use algorithm::AlgorithmKeyIVInit;
pub use algorithm::AlgorithmKeyInit;
pub use algorithm::AlgorithmProcess;
pub use algorithm::AlgorithmProcessInPlace;
pub use algorithm::ChaCha20;
pub use algorithm::EncryptionAlgorithm;
pub use algorithm::Poly1305;
pub use algorithm::SignedEnvelope;

Modules§

algorithm

Structs§

Cipher
The Cipher struct provides a common interface for cryptographic operations, specifically focusing on encryption and decryption.
Key
Key holds a public key and a salt value. This struct is specifically designed for use in symmetric encryption, and is compatible with multiple encryption algorithms.

Enums§

CipherError
CipherMode
KeyDerivationStrategy

Functions§

random_bytes
Generates a random byte array of a specified size.