11#![ no_std]
2- #![ cfg_attr( docsrs, feature( doc_cfg) ) ]
32#![ doc = include_str ! ( "../README.md" ) ]
43#![ doc(
54 html_logo_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg" ,
65 html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/meta/master/logo.svg"
76) ]
8- #![ warn( missing_docs, rust_2018_idioms) ]
7+ #![ cfg_attr( docsrs, feature( doc_auto_cfg) ) ]
8+ #![ warn( missing_docs) ]
99
1010//! # Usage
1111//!
1212//! Simple usage (allocating, no associated data):
1313//!
14- #![ cfg_attr( all( feature = "os_rng" , feature = "heapless" ) , doc = "```" ) ]
15- #![ cfg_attr( not( all( feature = "os_rng" , feature = "heapless" ) ) , doc = "```ignore" ) ]
14+ //! ```
1615//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
16+ //! # #[cfg(all(feature = "os_rng", feature = "heapless"))] {
1717//! use belt_dwp::{
1818//! aead::{Aead, AeadCore, KeyInit}, Nonce, BeltDwp
1919//! };
2424//! let ciphertext = cipher.encrypt(&nonce, b"plaintext message".as_ref())?;
2525//! let plaintext = cipher.decrypt(&nonce, ciphertext.as_ref())?;
2626//! assert_eq!(&plaintext, b"plaintext message");
27- //! # Ok(())
28- //! # }
27+ //! # }; Ok(()) }
2928//! ```
3029//!
3130//! ## In-place Usage (eliminates `alloc` requirement)
4342//! which can then be passed as the `buffer` parameter to the in-place encrypt
4443//! and decrypt methods:
4544//!
46- #![ cfg_attr( all( feature = "os_rng" , feature = "heapless" ) , doc = "```" ) ]
47- #![ cfg_attr( not( all( feature = "os_rng" , feature = "heapless" ) ) , doc = "```ignore" ) ]
45+ //! ```
4846//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
47+ //! # #[cfg(all(feature = "os_rng", feature = "heapless"))] {
4948//! use belt_dwp::{
5049//! aead::{AeadInPlace, AeadInPlaceDetached, KeyInit, heapless::Vec},
5150//! Nonce, BeltDwp
6766//! // Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
6867//! cipher.decrypt_in_place(&nonce, b"", &mut buffer)?;
6968//! assert_eq!(&buffer, b"plaintext message");
70- //! # Ok(())
71- //! # }
69+ //! # }; Ok(()) }
7270//! ```
7371//!
7472//! Similarly, enabling the `arrayvec` feature of this crate will provide an impl of
7573//! [`aead::Buffer`] for `arrayvec::ArrayVec` (re-exported from the [`aead`] crate as
7674//! [`aead::arrayvec::ArrayVec`]).
7775
78- use aead:: consts:: { U8 , U16 } ;
7976pub use aead:: { self , AeadCore , AeadInPlace , Error , Key , KeyInit , KeySizeUser } ;
80- use aead:: { AeadInPlaceDetached , PostfixTagged } ;
8177pub use belt_block:: BeltBlock ;
78+
79+ use aead:: consts:: { U8 , U16 } ;
80+ use aead:: { AeadInPlaceDetached , PostfixTagged } ;
8281use belt_block:: cipher:: crypto_common:: InnerUser ;
8382use belt_block:: cipher:: { Block , BlockCipherEncrypt , StreamCipher } ;
8483use belt_ctr:: cipher:: InnerIvInit ;
8584use belt_ctr:: { BeltCtr , BeltCtrCore } ;
8685use universal_hash:: UniversalHash ;
8786use universal_hash:: crypto_common:: { BlockSizeUser , InnerInit } ;
8887
89- use crate :: ghash:: GHash ;
90-
9188/// Nonce type for [`Dwp`]
9289pub type Nonce = aead:: Nonce < Dwp > ;
9390
@@ -97,15 +94,17 @@ pub type Tag = aead::Tag<Dwp>;
9794mod gf;
9895mod ghash;
9996
100- /// T from the STB 34.101.31-2020
97+ use ghash:: GHash ;
98+
99+ /// Constant `T` from the STB 34.101.31-2020
101100const T : u128 = 0xE45D_4A58_8E00_6D36_3BF5_080A_C8BA_94B1 ;
102101
103- /// Belt-DWP authenticated encryption with associated data (AEAD) cipher, defined in
104- /// STB 34.101.31-2020
102+ /// `belt-dwp` authenticated encryption with associated data (AEAD) cipher,
103+ /// defined in STB 34.101.31-2020.
105104pub type BeltDwp = Dwp < BeltBlock > ;
106105
107- /// Belt-DWP authenticated encryption with associated data (AEAD) cipher, defined in
108- /// STB 34.101.31-2020
106+ /// `belt-dwp` authenticated encryption with associated data (AEAD) cipher
107+ /// defined in STB 34.101.31-2020 generic over block cipher implementation.
109108pub struct Dwp < C = BeltBlock >
110109where
111110 C : BlockCipherEncrypt + BlockSizeUser < BlockSize = U16 > ,
@@ -124,8 +123,8 @@ impl<C> InnerInit for Dwp<C>
124123where
125124 C : BlockCipherEncrypt + BlockSizeUser < BlockSize = U16 > ,
126125{
127- fn inner_init ( inner : Self :: Inner ) -> Self {
128- Self { cipher : inner }
126+ fn inner_init ( cipher : Self :: Inner ) -> Self {
127+ Self { cipher }
129128 }
130129}
131130
@@ -150,10 +149,7 @@ where
150149 self . cipher . encrypt_block ( & mut r) ;
151150
152151 // Initialize GHash
153- let mut ghash = GHash :: new_with_init_block (
154- & Key :: < GHash > :: try_from ( & r[ ..] ) . expect ( "Key is always 16 bytes" ) ,
155- T ,
156- ) ;
152+ let mut ghash = GHash :: new_with_init_block ( & r, T ) ;
157153
158154 // Initialize CTR mode
159155 let core = BeltCtrCore :: inner_iv_init ( & self . cipher , nonce) ;
@@ -169,10 +165,8 @@ where
169165 // 4.2 𝑌𝑖 ← 𝑋𝑖 ⊕ Lo(belt-block(𝑠, 𝐾), |𝑋𝑖|)
170166 // 4.3 𝑡 ← 𝑡 ⊕ (𝑌𝑖 ‖ 0^{128−|𝑌𝑖|})
171167 // 4.4 𝑡 ← 𝑡 * 𝑟.
172- buffer. chunks_mut ( 16 ) . for_each ( |block| {
173- enc_cipher. apply_keystream ( block) ;
174- ghash. update_padded ( block) ;
175- } ) ;
168+ enc_cipher. apply_keystream ( buffer) ;
169+ ghash. update_padded ( buffer) ;
176170
177171 // 5. 𝑡 ← 𝑡 ⊕ (⟨|𝐼|⟩_64 ‖ ⟨|𝑋|⟩_64)
178172 ghash. xor_s ( & sizes_block) ;
@@ -202,10 +196,7 @@ where
202196 self . cipher . encrypt_block ( & mut r) ;
203197
204198 // Initialize GHash
205- let mut ghash = GHash :: new_with_init_block (
206- & Key :: < GHash > :: try_from ( & r[ ..] ) . expect ( "Key is always 16 bytes" ) ,
207- T ,
208- ) ;
199+ let mut ghash = GHash :: new_with_init_block ( & r, T ) ;
209200
210201 // 3. For 𝑖 = 1, 2, . . . , 𝑚 do:
211202 // 3.1 𝑡 ← 𝑡 ⊕ (𝐼𝑖 ‖ 0^{128−|𝐼𝑖|})
@@ -264,4 +255,7 @@ fn get_sizes_block(aad_len: usize, msg_len: usize) -> Block<GHash> {
264255}
265256
266257#[ cfg( feature = "zeroize" ) ]
267- impl zeroize:: ZeroizeOnDrop for Dwp { }
258+ impl < C > zeroize:: ZeroizeOnDrop for Dwp < C > where
259+ C : zeroize:: ZeroizeOnDrop + BlockCipherEncrypt + BlockSizeUser < BlockSize = U16 >
260+ {
261+ }
0 commit comments