Skip to content

Commit 7723996

Browse files
authored
belt-dwp: minor doc and code tweaks (#683)
1 parent d73fb8d commit 7723996

File tree

2 files changed

+32
-44
lines changed

2 files changed

+32
-44
lines changed

belt-dwp/README.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@
77
[![Project Chat][chat-image]][chat-link]
88
[![Build Status][build-image]][build-link]
99

10-
Pure Rust implementation of **BeltDwp** ([STB 34.101.31-2020][1]): an
11-
[Authenticated Encryption with Associated Data (AEAD)][2].
12-
13-
## About
14-
15-
BeltDwp is republic of Belarus standard for authenticated encryption with associated data.
10+
Pure Rust implementation of the `belt-dwp` [AEAD] algorithm
11+
specified in the republic of Belarus standard [STB 34.101.31-2020].
1612

1713
## Security Notes
1814

@@ -51,7 +47,5 @@ dual licensed as above, without any additional terms or conditions.
5147

5248
[//]: # (general links)
5349

54-
[1]: https://apmi.bsu.by/assets/files/std/belt-spec372.pdf
55-
[2]: https://en.wikipedia.org/wiki/Authenticated_encryption
56-
[3]: https://github.com/RustCrypto/stream-ciphers/tree/master/belt-dwp
57-
[4]: https://github.com/RustCrypto/universal-hashes/tree/master/belt-dwp
50+
[STB 34.101.31-2020]: https://apmi.bsu.by/assets/files/std/belt-spec372.pdf
51+
[AEAD]: https://en.wikipedia.org/wiki/Authenticated_encryption

belt-dwp/src/lib.rs

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
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
//! };
@@ -24,8 +24,7 @@
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)
@@ -43,9 +42,9 @@
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
@@ -67,27 +66,25 @@
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};
7976
pub use aead::{self, AeadCore, AeadInPlace, Error, Key, KeyInit, KeySizeUser};
80-
use aead::{AeadInPlaceDetached, PostfixTagged};
8177
pub use belt_block::BeltBlock;
78+
79+
use aead::consts::{U8, U16};
80+
use aead::{AeadInPlaceDetached, PostfixTagged};
8281
use belt_block::cipher::crypto_common::InnerUser;
8382
use belt_block::cipher::{Block, BlockCipherEncrypt, StreamCipher};
8483
use belt_ctr::cipher::InnerIvInit;
8584
use belt_ctr::{BeltCtr, BeltCtrCore};
8685
use universal_hash::UniversalHash;
8786
use universal_hash::crypto_common::{BlockSizeUser, InnerInit};
8887

89-
use crate::ghash::GHash;
90-
9188
/// Nonce type for [`Dwp`]
9289
pub type Nonce = aead::Nonce<Dwp>;
9390

@@ -97,15 +94,17 @@ pub type Tag = aead::Tag<Dwp>;
9794
mod gf;
9895
mod 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
101100
const 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.
105104
pub 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.
109108
pub struct Dwp<C = BeltBlock>
110109
where
111110
C: BlockCipherEncrypt + BlockSizeUser<BlockSize = U16>,
@@ -124,8 +123,8 @@ impl<C> InnerInit for Dwp<C>
124123
where
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

Comments
 (0)