Skip to main content

cfb_mode/
lib.rs

1//! [Cipher feedback][1] (CFB) mode with full block feedback.
2//!
3//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/cfb_enc.svg" width="49%" />
4//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/cfb_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//! use aes::cipher::KeyIvInit;
20//! use hex_literal::hex;
21//!
22//! type Aes128CfbEnc = cfb_mode::Encryptor<aes::Aes128>;
23//! type Aes128CfbDec = cfb_mode::Decryptor<aes::Aes128>;
24//!
25//! let key = [0x42; 16];
26//! let iv = [0x24; 16];
27//! let plaintext = *b"hello world! this is my plaintext.";
28//! let ciphertext = hex!(
29//!     "3357121ebb5a29468bd861467596ce3d6f99e251cc2d9f0a598032ae386d0ab995b3"
30//! );
31//!
32//! // encrypt/decrypt in-place
33//! let mut buf = plaintext.to_vec();
34//! Aes128CfbEnc::new(&key.into(), &iv.into()).encrypt(&mut buf);
35//! assert_eq!(buf[..], ciphertext[..]);
36//!
37//! Aes128CfbDec::new(&key.into(), &iv.into()).decrypt(&mut buf);
38//! assert_eq!(buf[..], plaintext[..]);
39//!
40//! // encrypt/decrypt from buffer to buffer
41//! // buffer length must be equal to input length
42//! let mut buf1 = [0u8; 34];
43//! Aes128CfbEnc::new(&key.into(), &iv.into())
44//!     .encrypt_b2b(&plaintext, &mut buf1)
45//!     .unwrap();
46//! assert_eq!(buf1[..], ciphertext[..]);
47//!
48//! let mut buf2 = [0u8; 34];
49//! Aes128CfbDec::new(&key.into(), &iv.into())
50//!     .decrypt_b2b(&buf1, &mut buf2)
51//!     .unwrap();
52//! assert_eq!(buf2[..], plaintext[..]);
53//! ```
54//!
55//! [1]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_feedback_(CFB)
56
57#![no_std]
58#![doc(
59    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
60    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
61)]
62#![cfg_attr(docsrs, feature(doc_cfg))]
63
64mod decrypt;
65mod encrypt;
66
67pub use cipher;
68pub use decrypt::{BufDecryptor, Decryptor};
69pub use encrypt::{BufEncryptor, Encryptor};