Skip to main content

ecb/
lib.rs

1//! [Electronic Codebook][1] (ECB) mode.
2//!
3//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/ecb_enc.svg" width="49%" />
4//! <img src="https://raw.githubusercontent.com/RustCrypto/media/26acc39f/img/block-modes/ecb_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//!
13//! # Example
14//! ```
15//! # #[cfg(feature = "block-padding")] {
16//! use aes::cipher::{block_padding::Pkcs7, BlockModeDecrypt, BlockModeEncrypt, KeyInit};
17//! use hex_literal::hex;
18//!
19//! type Aes128EcbEnc = ecb::Encryptor<aes::Aes128>;
20//! type Aes128EcbDec = ecb::Decryptor<aes::Aes128>;
21//!
22//! let key = [0x42; 16];
23//! let plaintext = *b"hello world! this is my plaintext.";
24//! let ciphertext = hex!(
25//!     "42b153410851a931eb3e6c048867ae5f"
26//!     "95eb20b42e176b07840db75688be9c70"
27//!     "e4670ea0d87a71be5f9f3099b4fff3dc"
28//! );
29//!
30//! // encrypt/decrypt in-place
31//! // buffer must be big enough for padded plaintext
32//! let mut buf = [0u8; 48];
33//! let pt_len = plaintext.len();
34//! buf[..pt_len].copy_from_slice(&plaintext);
35//! let ct = Aes128EcbEnc::new(&key.into())
36//!     .encrypt_padded::<Pkcs7>(&mut buf, pt_len)
37//!     .unwrap();
38//! assert_eq!(ct, &ciphertext[..]);
39//!
40//! let pt = Aes128EcbDec::new(&key.into())
41//!     .decrypt_padded::<Pkcs7>(&mut buf)
42//!     .unwrap();
43//! assert_eq!(pt, &plaintext);
44//!
45//! // encrypt/decrypt from buffer to buffer
46//! let mut buf = [0u8; 48];
47//! let ct = Aes128EcbEnc::new(&key.into())
48//!     .encrypt_padded_b2b::<Pkcs7>(&plaintext, &mut buf)
49//!     .unwrap();
50//! assert_eq!(ct, &ciphertext[..]);
51//!
52//! let mut buf = [0u8; 48];
53//! let pt = Aes128EcbDec::new(&key.into())
54//!     .decrypt_padded_b2b::<Pkcs7>(&ct, &mut buf)
55//!     .unwrap();
56//! assert_eq!(pt, &plaintext);
57//! # }
58//! ```
59//!
60//! With enabled `alloc` feature you also can use allocating convenience methods:
61//! ```
62//! # #[cfg(all(feature = "alloc", feature = "block-padding"))] {
63//! # use aes::cipher::{block_padding::Pkcs7, BlockModeDecrypt, BlockModeEncrypt, KeyInit};
64//! # use hex_literal::hex;
65//! # type Aes128EcbEnc = ecb::Encryptor<aes::Aes128>;
66//! # type Aes128EcbDec = ecb::Decryptor<aes::Aes128>;
67//! # let key = [0x42; 16];
68//! # let plaintext = *b"hello world! this is my plaintext.";
69//! # let ciphertext = hex!(
70//! #     "42b153410851a931eb3e6c048867ae5f"
71//! #     "95eb20b42e176b07840db75688be9c70"
72//! #     "e4670ea0d87a71be5f9f3099b4fff3dc"
73//! # );
74//! let res = Aes128EcbEnc::new(&key.into())
75//!     .encrypt_padded_vec::<Pkcs7>(&plaintext);
76//! assert_eq!(res[..], ciphertext[..]);
77//! let res = Aes128EcbDec::new(&key.into())
78//!     .decrypt_padded_vec::<Pkcs7>(&res)
79//!     .unwrap();
80//! assert_eq!(res[..], plaintext[..]);
81//! # }
82//! ```
83//!
84//! [1]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#ECB
85
86#![no_std]
87#![doc(
88    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
89    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
90)]
91#![cfg_attr(docsrs, feature(doc_cfg))]
92
93mod decrypt;
94mod encrypt;
95
96pub use cipher;
97pub use decrypt::Decryptor;
98pub use encrypt::Encryptor;