Struct BlockCipher

Source
pub struct BlockCipher { /* private fields */ }
Expand description

A raw block cipher interface (ie ECB mode)

Warning: you almost certainly want an AEAD cipher mode instead

Implementations§

Source§

impl BlockCipher

Source

pub fn new(name: &str) -> Result<BlockCipher>

Create a new block cipher instance, failing if the cipher is unknown

§Examples
let cipher = botan::BlockCipher::new("AES-128");
assert!(cipher.is_ok());
let no_such_cipher = botan::BlockCipher::new("SuperCipher9000");
assert!(no_such_cipher.is_err());
Source

pub fn block_size(&self) -> Result<usize>

Return the block size of the cipher, in bytes

§Examples
let cipher = botan::BlockCipher::new("AES-128").unwrap();
assert_eq!(cipher.block_size().unwrap(), 16);
Source

pub fn algo_name(&self) -> Result<String>

Return the name of this algorithm which may or may not exactly match what was provided to new()

§Examples
let cipher = botan::BlockCipher::new("AES-128").unwrap();
assert_eq!(cipher.algo_name().unwrap(), "AES-128");
Source

pub fn key_spec(&self) -> Result<KeySpec>

Return information about the key lengths supported by this object

Source

pub fn set_key(&mut self, key: &[u8]) -> Result<()>

Set the key for the cipher.

§Errors

Fails if the key is not a valid length for the cipher

§Examples
let mut cipher = botan::BlockCipher::new("AES-128").unwrap();
assert!(cipher.set_key(&vec![0; 32]).is_err());
assert!(cipher.set_key(&vec![0; 16]).is_ok());
Source

pub fn encrypt_blocks(&self, input: &[u8]) -> Result<Vec<u8>>

Encrypt some blocks of data

§Errors

Fails if the input is not a multiple of the block size, or if the key was not set on the object.

§Examples
let mut cipher = botan::BlockCipher::new("AES-128").unwrap();
// Key is not set
assert!(cipher.encrypt_blocks(&vec![0; 16]).is_err());
assert!(cipher.set_key(&vec![0; 16]).is_ok());
// Not a multiple of block size
assert!(cipher.encrypt_blocks(&vec![0; 17]).is_err());
// Key is set and multiple of block size - ok
assert!(cipher.encrypt_blocks(&vec![0; 16]).is_ok());
Source

pub fn encrypt_in_place(&self, buf: &mut [u8]) -> Result<()>

Encrypt in place

§Errors

Fails if the input is not a multiple of the block size, or if the key was not set on the object.

Source

pub fn decrypt_blocks(&self, input: &[u8]) -> Result<Vec<u8>>

Decrypt some blocks of data

§Errors

Fails if the input is not a multiple of the block size, or if the key was not set on the object.

§Examples
let mut cipher = botan::BlockCipher::new("AES-128").unwrap();
// Key is not set
assert!(cipher.decrypt_blocks(&vec![0; 16]).is_err());
assert!(cipher.set_key(&vec![0; 16]).is_ok());
// Not a multiple of block size
assert!(cipher.decrypt_blocks(&vec![0; 17]).is_err());
// Key is set and multiple of block size - ok
assert!(cipher.decrypt_blocks(&vec![0; 16]).is_ok());
Source

pub fn decrypt_in_place(&self, buf: &mut [u8]) -> Result<()>

Decrypt in place

§Errors

Fails if the input is not a multiple of the block size, or if the key was not set on the object.

Source

pub fn clear(&mut self) -> Result<()>

Clear the key set on the cipher from memory. After this, the object is un-keyed and must be re-keyed before use.

§Examples
let mut cipher = botan::BlockCipher::new("AES-128").unwrap();
assert!(cipher.set_key(&vec![0; 16]).is_ok());
assert!(cipher.encrypt_blocks(&vec![0; 16]).is_ok());
assert!(cipher.clear().is_ok());
assert!(cipher.encrypt_blocks(&vec![0; 16]).is_err());

Trait Implementations§

Source§

impl Debug for BlockCipher

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for BlockCipher

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for BlockCipher

Source§

impl Sync for BlockCipher

Auto Trait Implementations§

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.