rc5-block

Crates.iorc5-block
lib.rsrc5-block
version0.1.0
created_at2025-07-03 17:25:17.672033+00
updated_at2025-07-03 17:25:17.672033+00
descriptionRust implementation of RC5 Block cipher with ECB, CBC and CTR operation modes.
homepage
repositoryhttps://github.com/SyedAnees21/RC5-Rust
max_upload_size
id1736603
size50,180
(SyedAnees21)

documentation

README

RC5-Block

crates.io
docs.rs
license

A pure‑Rust implementation of the RC5 block cipher, supporting variable word‑sizes (u16, u32, u64, u128),
PKCS#7 padding and the three classic modes of operation: ECB, CBC, and CTR.

This implementation is inspired by the original paper on RC5-Block-Cipher by Ronald L. Rivest.


Features

This library is generic over word-size per block and suppoerts multiple operation modes.

  • Supported Word sizes:

    • 16‑bit
    • 32‑bit
    • 64‑bit
    • 128 bit
  • Modes

    • ECB: Electronic Codebook
    • CBC: Cipher Block Chaining (with PKCS#7 padding)
    • CTR: Counter mode (no padding)
  • Helpers

    • PKCS#7 padding/unpadding (Strict)
    • Random IV / nonce+counter generators
    • Parse hex strings for IV and nonce

Installation

Add to your Cargo.toml:

[dependencies]
rc5-block = "0.1"

And start using it in your application

use rc5_block::{
    rc5_cipher,                     // builder
    OperationMode,                  // enum for mode
    random_iv,                      // for CBC
    random_nonce_and_counter,       // for CTR
    utils::pkcs7,                   // padding helper
};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 1) Build an RC5 cipher: 32‑bit words, 12 rounds
    let cipher = rc5_cipher::<u32>("my secret key", 12)?;

    let plaintext = b"The quick brown fox jumps over the lazy dog";

    // --- ECB ---
    let ct_ecb = cipher.encrypt(plaintext, OperationMode::ECB)?;
    let pt_ecb = cipher.decrypt(&ct_ecb, OperationMode::ECB)?;
    assert_eq!(pt_ecb, plaintext);

    // --- CBC ---
    let iv = random_iv::<u32, 2>();
    let ct_cbc = cipher.encrypt(plaintext, OperationMode::CBC { iv })?;
    let pt_cbc = cipher.decrypt(&ct_cbc, OperationMode::CBC { iv })?;
    assert_eq!(pt_cbc, plaintext);

    // --- CTR ---
    let nc = random_nonce_and_counter::<u32, 2>();
    let ct_ctr = cipher.encrypt(plaintext, OperationMode::CTR { nonce_and_counter: nc })?;
    // CTR decryption is same call:
    let pt_ctr = cipher.decrypt(&ct_ctr, OperationMode::CTR { nonce_and_counter: nc })?;
    assert_eq!(pt_ctr, plaintext);

    Ok(())
}

Testing

This library is tested against some of the standard test vectors and round trip tests. These tests are define here. Standard test vector are picked from here.


RC5-CLI

This is a command-line application developed using this library, see it readme for more.

Commit count: 0

cargo fmt