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
impl BlockCipher
Sourcepub fn new(name: &str) -> Result<BlockCipher>
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());Sourcepub fn block_size(&self) -> Result<usize>
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);Sourcepub fn algo_name(&self) -> Result<String>
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");Sourcepub fn key_spec(&self) -> Result<KeySpec>
pub fn key_spec(&self) -> Result<KeySpec>
Return information about the key lengths supported by this object
Sourcepub fn encrypt_blocks(&self, input: &[u8]) -> Result<Vec<u8>>
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());Sourcepub fn encrypt_in_place(&self, buf: &mut [u8]) -> Result<()>
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.
Sourcepub fn decrypt_blocks(&self, input: &[u8]) -> Result<Vec<u8>>
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());Sourcepub fn decrypt_in_place(&self, buf: &mut [u8]) -> Result<()>
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.
Sourcepub fn clear(&mut self) -> Result<()>
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());