Crate rc5_block

Source
Expand description

§RC5-RS Cipher Library

This crate provides a generic, parametric implementation of the RC5 block cipher, supporting variable word sizes (u16, u32, u64) and multiple modes of operation (ECB, CBC, CTR). It includes PKCS#7 padding helpers, IV/nonce generators, and convenient parsing of hex‐encoded parameters.

§Features

  • Variable word length: 16-bit, 32-bit, 64-bit.
  • Various operation modes:
    • ECB
    • CBC
    • CTR
  • Strict padding using PKCS#7 standard.
  • Pseudo-random IV/nonce generation utitlities , see random_iv, random_nonce_and_counter.
  • Hex‐string parsing for IVs and nonces.

§Example

use rc5_block::{rc5_cipher, OperationMode};

// Build a 32‐bit word RC5 cipher with 12 rounds:
let cipher = rc5_cipher::<u32>(b"mykey", 12).unwrap();

let plaintext = b"Secret message";

// Encrypt in CBC mode with a random IV:
let iv = rc5_block::random_iv::<u32, 2>();
let ciphertext = cipher.encrypt(plaintext, OperationMode::CBC { iv }).unwrap();

// Decrypt using the same IV:
let recovered = cipher.decrypt(&ciphertext, OperationMode::CBC { iv }).unwrap();
assert_eq!(recovered, plaintext);

§Utilities

This crate provide some extra utilities such as, pseudo-random iv and nonce generation and PKCS#7 padding function:

// generate a pseudo-random iv-block of block size [u32;2]
let iv = rc5_block::random_iv::<u32, 2>();

// generate a pseudo-random nonce and counter initialized to zero
// of block size [u32;2]
// Note: Higher part of this block conatins nonce and lower part
// contains counter with initial value set to zero.
let nonce_counter = rc5_block::random_nonce_and_counter::<u32, 2>();

Macros§

bail
Helper macro to bail out early with a Reason error if any condition is true.

Structs§

Cipher
Cipher
RC5ControlBlock
RC5 control block
Version
RC5 version identifier

Enums§

OperationMode
Modes of operation for a block cipher.
Reason
Errors returned by the Cipher as reasons during cipher operations.

Traits§

BlockCipher
A core trait that any block-cipher must implement to work with Cipher.
Word
A core trait to define a word in N-sized blocks of a block cipher. This word must support arithmatic and binary operations required for cryptographic functions.

Functions§

pkcs7
Apply or remove PKCS#7 padding on the given buffer in place.
random_iv
Generate a pseudo‑random IV (Initialization-Vector) of [W; N].
random_nonce_and_counter
Generate a pseudo‑random block of N words where the last word is zero (suitable for use as a CTR‐mode nonce + counter seed).
rc5_cipher
Construct a new RC5 cipher from a raw key and round count.

Type Aliases§

RC5Cipher