pub struct Cipher { /* private fields */ }Expand description
Provides encryption and decryption functions for AES in modes ECB, CTR, and GCM. Instantiated with an AES Key, which is expanded into round keys and stored in the instance.
§Examples
use aesp::{Key, Cipher};
// Instantiate random key:
let rk_256 = Key::rand_key_256()?;
// Instantiate AESP cipher using the key:
let cipher = Cipher::new(&rk_256);Implementations§
Source§impl Cipher
impl Cipher
Sourcepub fn new(key: &Key) -> Self
pub fn new(key: &Key) -> Self
Generates round keys from provided key and stores in the returned instance.
Sourcepub fn round_keys(&self) -> &[[u8; 16]]
pub fn round_keys(&self) -> &[[u8; 16]]
Getter for internal round keys. Returned as a slice of 16-byte arrays.
Sourcepub fn encrypt_ecb(&self, plaintext: &[u8]) -> Vec<u8> ⓘ
pub fn encrypt_ecb(&self, plaintext: &[u8]) -> Vec<u8> ⓘ
Electronic codebook encryption.
Encrypts each 16-byte block entirely independently and chains them together. Pads input to a multiple of 16 bytes using PKCS#7 padding. Vulnerable to pattern emergence in the ciphertext.
§Examples
let plaintext = ("Hello, World!").as_bytes();
let ciphertext = cipher.encrypt_ecb(&plaintext);Sourcepub fn decrypt_ecb(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
pub fn decrypt_ecb(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
Electronic codebook decryption.
Assumes plaintext was PKCS#7 padded before encryption and unpads automatically. Throws error if last block does not match PKCS#7 format or input is not a multiple of 16 bytes.
§Examples
let plaintext = ("Hello, World!").as_bytes();
let ciphertext = cipher.encrypt_ecb(&plaintext);
let decrypted = cipher.decrypt_ecb(&ciphertext)?;
assert_eq!(decrypted, plaintext);Sourcepub fn encrypt_ctr(&self, plaintext: &[u8]) -> Result<Vec<u8>>
pub fn encrypt_ctr(&self, plaintext: &[u8]) -> Result<Vec<u8>>
Counter mode encryption.
Generates a random 12-byte initialisation vector (IV). For each 16-byte block of plaintext:
- 4-byte counter is incremented (starts at zero).
- Counter is appended to 12-byte IV to form a 16-byte block.
- The
IV || Counterblock is encrypted using the round keys. - The plaintext block is
XOR’d with the encrypted counter block.
Important: the same IV must never be reused with the same key. 96 bits is sufficiently large to assume uniqueness when randomly generated.
Output is formatted as IV (12 bytes) || Ciphertext
§Examples
let plaintext = ("Hello, World!").as_bytes();
let ciphertext = cipher.encrypt_ctr(&plaintext)?;Sourcepub fn decrypt_ctr(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
pub fn decrypt_ctr(&self, ciphertext: &[u8]) -> Result<Vec<u8>>
Counter mode decryption.
Assumes format matches output of encryption: IV (12 bytes) || Ciphertext
§Examples
let plaintext = ("Hello, World!").as_bytes();
let ciphertext = cipher.encrypt_ctr(&plaintext)?;
let decrypted = cipher.decrypt_ctr(&ciphertext)?;
assert_eq!(decrypted, plaintext);Sourcepub fn encrypt_gcm(
&self,
plaintext: &[u8],
aad: Option<&[u8]>,
) -> Result<Vec<u8>>
pub fn encrypt_gcm( &self, plaintext: &[u8], aad: Option<&[u8]>, ) -> Result<Vec<u8>>
Galois/counter mode encryption.
Encrypts using counter mode and generates a cryptographic tag to verify the message has not been modified.
Also accepts optional additional authenticated data (AAD), which is included in the computation of the tag but not encrypted.
Output is formatted as IV (12 bytes) || AAD length (4 bytes) || AAD || Ciphertext || Tag (16 bytes)
§Examples
let plaintext = ("Hello, World!").as_bytes();
let aad = ("Some data to be authenticated but not encrypted").as_bytes();
let ciphertext_with_aad = cipher.encrypt_gcm(plaintext, Some(aad))?;
let ciphertext_no_aad = cipher.encrypt_gcm(plaintext, None)?;Sourcepub fn decrypt_gcm(
&self,
ciphertext: &[u8],
) -> Result<(Vec<u8>, Option<Vec<u8>>)>
pub fn decrypt_gcm( &self, ciphertext: &[u8], ) -> Result<(Vec<u8>, Option<Vec<u8>>)>
Galois/counter mode decryption.
Assumes input follows the same format as encryption:
IV (12 bytes) || AAD length (4 bytes) || AAD || Ciphertext || Tag (16 bytes)
Returns:
(plaintext, AAD)if tag was authenticated and decryption was successful.- AuthFailed error if computed tag did not match input tag.
- CounterOverflow error if more than 2^32 blocks were provided.
- InvalidCiphertext error if ciphertext does not match expected format.
§Examples
let plaintext = ("Hello, World!").as_bytes();
let aad = ("Some data to be authenticated but not encrypted").as_bytes();
// Decryption with AAD
let ciphertext = cipher.encrypt_gcm(plaintext, Some(aad))?;
let (decrypted, returned_aad) = cipher.decrypt_gcm(&ciphertext)?;
assert_eq!(decrypted, plaintext);
assert_eq!(returned_aad, Some(aad.to_vec()));
// Decryption without AAD
let ciphertext = cipher.encrypt_gcm(plaintext, None)?;
let (_, returned_aad) = cipher.decrypt_gcm(&ciphertext)?;
assert!(returned_aad.is_none());Trait Implementations§
impl Eq for Cipher
impl StructuralPartialEq for Cipher
Auto Trait Implementations§
impl Freeze for Cipher
impl RefUnwindSafe for Cipher
impl Send for Cipher
impl Sync for Cipher
impl Unpin for Cipher
impl UnsafeUnpin for Cipher
impl UnwindSafe for Cipher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more