Skip to content

RC5 Block cipher implementation in rust with variable length word and multiple operation modes i.e (ECB, CBC, CTR)

License

Notifications You must be signed in to change notification settings

SyedAnees21/RC5-Rust

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

RC5 Block cipher implementation in rust with variable length word and multiple operation modes i.e (ECB, CBC, CTR)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages