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

Modules

Structs

  • The Cipher struct provides a common interface for cryptographic operations, specifically focusing on encryption and decryption.
  • 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

Functions

  • Generates a random byte array of a specified size.