logo

Crate pcbc

source · []
Expand description

Propagating Cipher Block Chaining (PCBC) mode.

Mode functionality is accessed using traits from re-exported cipher crate.

⚠️ Security Warning: Hazmat!

This crate does not ensure ciphertexts are authentic! Thus ciphertext integrity is not verified, which can lead to serious vulnerabilities!

Example

use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyIvInit};
use hex_literal::hex;

type Aes128PcbcEnc = pcbc::Encryptor<aes::Aes128>;
type Aes128PcbcDec = pcbc::Decryptor<aes::Aes128>;

let key = [0x42; 16];
let iv = [0x24; 16];
let plaintext = *b"hello world! this is my plaintext.";
let ciphertext = hex!(
    "c7fe247ef97b21f07cbdd26cb5d346bf"
    "ab13156d0b2f05f91c4837db5157bad5"
    "62cb0b6fa7816e254a2fc8d852fb4315"
);

// encrypt/decrypt in-place
// buffer must be big enough for padded plaintext
let mut buf = vec![0u8; 48];
let pt_len = plaintext.len();
buf[..pt_len].copy_from_slice(&plaintext);
let ct = Aes128PcbcEnc::new(&key.into(), &iv.into())
    .encrypt_padded_mut::<Pkcs7>(&mut buf, pt_len)
    .unwrap();
assert_eq!(ct, &ciphertext[..]);

let pt = Aes128PcbcDec::new(&key.into(), &iv.into())
    .decrypt_padded_mut::<Pkcs7>(&mut buf)
    .unwrap();
assert_eq!(pt, &plaintext);

// encrypt/decrypt from buffer to buffer
let mut buf = vec![0u8; 48];
let ct = Aes128PcbcEnc::new(&key.into(), &iv.into())
    .encrypt_padded_b2b_mut::<Pkcs7>(&plaintext, &mut buf)
    .unwrap();
assert_eq!(ct, &ciphertext[..]);

let mut buf = vec![0u8; 48];
let pt = Aes128PcbcDec::new(&key.into(), &iv.into())
    .decrypt_padded_b2b_mut::<Pkcs7>(&ct, &mut buf)
    .unwrap();
assert_eq!(pt, &plaintext);

With enabled alloc (or std) feature you also can use allocating convinience methods:

let res = Aes128PcbcEnc::new(&key.into(), &iv.into())
    .encrypt_padded_vec_mut::<Pkcs7>(&plaintext);
assert_eq!(res[..], ciphertext[..]);
let res = Aes128PcbcDec::new(&key.into(), &iv.into())
    .decrypt_padded_vec_mut::<Pkcs7>(&res)
    .unwrap();
assert_eq!(res[..], plaintext[..]);

Re-exports

pub use cipher;

Structs

PCBC mode decryptor.

PCBC mode encryptor.