| 1 | //! [Electronic Codebook][1] (ECB) mode. |
| 2 | //! |
| 3 | //! <img src="https://user-images.githubusercontent.com/7829098/171395128-0ff53e16-1969-4848-8db4-3fc4fd0cbbb4.svg" width="49%" /> |
| 4 | //! <img src="https://user-images.githubusercontent.com/7829098/171395113-219f6995-4e2d-4f4a-bb10-d6a229c10989.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, BlockDecryptMut, BlockEncryptMut, 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_mut::<Pkcs7>(&mut buf, pt_len) |
| 37 | //! .unwrap(); |
| 38 | //! assert_eq!(ct, &ciphertext[..]); |
| 39 | //! |
| 40 | //! let pt = Aes128EcbDec::new(&key.into()) |
| 41 | //! .decrypt_padded_mut::<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_mut::<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_mut::<Pkcs7>(&ct, &mut buf) |
| 55 | //! .unwrap(); |
| 56 | //! assert_eq!(pt, &plaintext); |
| 57 | //! # } |
| 58 | //! ``` |
| 59 | //! |
| 60 | //! With enabled `alloc` (or `std`) feature you also can use allocating |
| 61 | //! convinience methods: |
| 62 | //! ``` |
| 63 | //! # #[cfg(all(feature = "alloc", feature = "block-padding"))] { |
| 64 | //! # use aes::cipher::{block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, KeyInit}; |
| 65 | //! # use hex_literal::hex; |
| 66 | //! # type Aes128EcbEnc = ecb::Encryptor<aes::Aes128>; |
| 67 | //! # type Aes128EcbDec = ecb::Decryptor<aes::Aes128>; |
| 68 | //! # let key = [0x42; 16]; |
| 69 | //! # let plaintext = *b"hello world! this is my plaintext."; |
| 70 | //! # let ciphertext = hex!( |
| 71 | //! # "42b153410851a931eb3e6c048867ae5f" |
| 72 | //! # "95eb20b42e176b07840db75688be9c70" |
| 73 | //! # "e4670ea0d87a71be5f9f3099b4fff3dc" |
| 74 | //! # ); |
| 75 | //! let res = Aes128EcbEnc::new(&key.into()) |
| 76 | //! .encrypt_padded_vec_mut::<Pkcs7>(&plaintext); |
| 77 | //! assert_eq!(res[..], ciphertext[..]); |
| 78 | //! let res = Aes128EcbDec::new(&key.into()) |
| 79 | //! .decrypt_padded_vec_mut::<Pkcs7>(&res) |
| 80 | //! .unwrap(); |
| 81 | //! assert_eq!(res[..], plaintext[..]); |
| 82 | //! # } |
| 83 | //! ``` |
| 84 | //! |
| 85 | //! [1]: https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#ECB |
| 86 | |
| 87 | #![no_std] |
| 88 | #![doc( |
| 89 | html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg", |
| 90 | html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/26acc39f/logo.svg", |
| 91 | html_root_url = "https://docs.rs/ECB/0.1.2" |
| 92 | )] |
| 93 | #![forbid(unsafe_code)] |
| 94 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 95 | #![warn(missing_docs, rust_2018_idioms)] |
| 96 | |
| 97 | mod decrypt; |
| 98 | mod encrypt; |
| 99 | |
| 100 | pub use cipher; |
| 101 | pub use decrypt::Decryptor; |
| 102 | pub use encrypt::Encryptor; |