cbc/lib.rs
1//! [Cipher Block Chaining][1] (CBC) mode.
2//!
3//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/cbc_enc.svg" width="49%" />
4//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/cbc_dec.svg" width="49%"/>
5//!
6//! Mode functionality is accessed using traits from re-exported [`cipher`] crate.
7//!
8//! # ⚠️ Security Warning: Hazmat!
9//!
10//! This crate does not ensure ciphertexts are authentic! Thus ciphertext integrity
11//! is not verified, which can lead to serious vulnerabilities!
12//! [RustCrypto/AEADs] provide simple authenticated encryption,
13//! which is much less error-prone than manual integrity verification.
14//!
15//! [RustCrypto/AEADs]: https://github.com/RustCrypto/AEADs
16//!
17//! # Example
18//! ```
19//! # #[cfg(feature = "block-padding")] {
20//! use aes::cipher::{block_padding::Pkcs7, BlockModeEncrypt, BlockModeDecrypt, KeyIvInit};
21//! use hex_literal::hex;
22//!
23//! type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
24//! type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
25//!
26//! let key = [0x42; 16];
27//! let iv = [0x24; 16];
28//! let plaintext = *b"hello world! this is my plaintext.";
29//! let ciphertext = hex!(
30//! "c7fe247ef97b21f07cbdd26cb5d346bf"
31//! "d27867cb00d9486723e159978fb9a5f9"
32//! "14cfb228a710de4171e396e7b6cf859e"
33//! );
34//!
35//! // encrypt/decrypt in-place
36//! // buffer must be big enough for padded plaintext
37//! let mut buf = [0u8; 48];
38//! let pt_len = plaintext.len();
39//! buf[..pt_len].copy_from_slice(&plaintext);
40//! let ct = Aes128CbcEnc::new(&key.into(), &iv.into())
41//! .encrypt_padded::<Pkcs7>(&mut buf, pt_len)
42//! .unwrap();
43//! assert_eq!(ct, &ciphertext[..]);
44//!
45//! let pt = Aes128CbcDec::new(&key.into(), &iv.into())
46//! .decrypt_padded::<Pkcs7>(&mut buf)
47//! .unwrap();
48//! assert_eq!(pt, &plaintext);
49//!
50//! // encrypt/decrypt from buffer to buffer
51//! let mut buf = [0u8; 48];
52//! let ct = Aes128CbcEnc::new(&key.into(), &iv.into())
53//! .encrypt_padded_b2b::<Pkcs7>(&plaintext, &mut buf)
54//! .unwrap();
55//! assert_eq!(ct, &ciphertext[..]);
56//!
57//! let mut buf = [0u8; 48];
58//! let pt = Aes128CbcDec::new(&key.into(), &iv.into())
59//! .decrypt_padded_b2b::<Pkcs7>(&ct, &mut buf)
60//! .unwrap();
61//! assert_eq!(pt, &plaintext);
62//! # }
63//! ```
64//!
65//! With enabled `alloc` (or `std`) feature you also can use allocating
66//! convenience methods:
67//! ```
68//! # #[cfg(all(feature = "alloc", feature = "block-padding"))] {
69//! # use aes::cipher::{block_padding::Pkcs7, BlockModeEncrypt, BlockModeDecrypt, KeyIvInit};
70//! # use hex_literal::hex;
71//! # type Aes128CbcEnc = cbc::Encryptor<aes::Aes128>;
72//! # type Aes128CbcDec = cbc::Decryptor<aes::Aes128>;
73//! # let key = [0x42; 16];
74//! # let iv = [0x24; 16];
75//! # let plaintext = *b"hello world! this is my plaintext.";
76//! # let ciphertext = hex!(
77//! # "c7fe247ef97b21f07cbdd26cb5d346bf"
78//! # "d27867cb00d9486723e159978fb9a5f9"
79//! # "14cfb228a710de4171e396e7b6cf859e"
80//! # );
81//! let res = Aes128CbcEnc::new(&key.into(), &iv.into())
82//! .encrypt_padded_vec::<Pkcs7>(&plaintext);
83//! assert_eq!(res[..], ciphertext[..]);
84//! let res = Aes128CbcDec::new(&key.into(), &iv.into())
85//! .decrypt_padded_vec::<Pkcs7>(&res)
86//! .unwrap();
87//! assert_eq!(res[..], plaintext[..]);
88//! # }
89//! ```
90//!
91//! [1]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#CBC
92
93#![no_std]
94#![doc(
95 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
96 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
97)]
98#![cfg_attr(docsrs, feature(doc_cfg))]
99
100mod decrypt;
101mod encrypt;
102
103pub use cipher;
104pub use decrypt::Decryptor;
105pub use encrypt::Encryptor;
106
107use cipher::array::{Array, ArraySize};
108
109#[inline(always)]
110fn xor<N: ArraySize>(out: &mut Array<u8, N>, buf: &Array<u8, N>) {
111 for (a, b) in out.iter_mut().zip(buf) {
112 *a ^= *b;
113 }
114}