Skip to main content

Cipher

Struct Cipher 

Source
pub struct Cipher<B, W, const N: usize>
where W: Word, B: BlockCipher<W, N>,
{ /* private fields */ }
Expand description

§Cipher

A high‐level cipher wrapper type that contains a control block It provides byte‐stream handling and cryptographic operation modes dispatch.

§Generics

  • B: Control-Block, e.g. RC5ControlBlock<W>.
  • W: Underlying type which implements a Word trait.
  • N: number of words per block (for RC5, always 2).

Implementations§

Source§

impl<B, W, const N: usize> Cipher<B, W, N>
where W: Word, B: BlockCipher<W, N>,

Source

pub fn new(block: B) -> Self

Create a new Cipher wrapping the given block‐cipher instance.

§Example
use rc5_block::{RC5ControlBlock, Cipher};

let rc5_control_block = RC5ControlBlock::<u32>::new("SECRET_KEY", 12).unwrap();
let cipher = Cipher::new(rc5_control_block);
Source

pub fn encrypt( &self, pt: &[u8], mode: OperationMode<W, N>, ) -> Result<Vec<u8>, Reason>

Encrypt plain-text bytes under selected cryptographic operation mode and returns cipher-text bytes.

This takes the plain-text as their bytes reference. It supports various encryption flows based on operation modes such as:

  • ECB : Electronic-code-book mode.
  • CBC : Cipher-block-chain mode.
  • CTR : Counter mode.

Encryption might fail for various reasons, either due to padding or etc, that’s why this function is fallible.

It returns ciphered bytes, or Reason of failure as an err.

Source

pub fn decrypt( &self, ct: &[u8], mode: OperationMode<W, N>, ) -> Result<Vec<u8>, Reason>

Decrypt cipher-text bytes under selected cryptographic operation mode and returns plain-text bytes.

This takes the plain-text as their bytes reference. It supports various encryption flows based on operation modes such as:

  • ECB : Electronic-code-book mode.
  • CBC : Cipher-block-chain mode.
  • CTR : Counter mode.

Decryption might fail for various reasons, either due to padding or etc, that’s why this function is fallible.

It returns plain bytes, or Reason of failure as an err.

Source

pub fn parse_iv_from_hex<V>(&self, iv_hex: V) -> Result<[W; N], Reason>
where V: AsRef<[u8]>,

Parse an IV from a hex‐encoded string, validating length = block size. Parsing may fail if the hex-string is not equal to blcok size.

Returns a result contain iv block or failure reason as an err.

Source

pub fn parse_nonce_counter_from_hex<V>( &self, nonce_hex: V, counter_hex: V, ) -> Result<[W; N], Reason>
where V: AsRef<[u8]>,

Parses nonce and counter from their respective hex-encoded strings.

Parse may fail if the hex-string is not equal to word-size.

Source

pub fn control_block(&self) -> &B

Returns an immutable access to control-block of block-cipher underlying the cipher.

Auto Trait Implementations§

§

impl<B, W, const N: usize> Freeze for Cipher<B, W, N>
where B: Freeze,

§

impl<B, W, const N: usize> RefUnwindSafe for Cipher<B, W, N>

§

impl<B, W, const N: usize> Send for Cipher<B, W, N>
where B: Send, W: Send,

§

impl<B, W, const N: usize> Sync for Cipher<B, W, N>
where B: Sync, W: Sync,

§

impl<B, W, const N: usize> Unpin for Cipher<B, W, N>
where B: Unpin, W: Unpin,

§

impl<B, W, const N: usize> UnsafeUnpin for Cipher<B, W, N>
where B: UnsafeUnpin,

§

impl<B, W, const N: usize> UnwindSafe for Cipher<B, W, N>
where B: UnwindSafe, W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V