Skip to content

Commit d009899

Browse files
committed
belt-dwp: migrate to AeadInOut
1 parent 5169455 commit d009899

File tree

4 files changed

+64
-30
lines changed

4 files changed

+64
-30
lines changed

Cargo.lock

Lines changed: 46 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ members = [
55
"aes-gcm-siv",
66
"aes-siv",
77
#"ascon-aead128",
8-
#"belt-dwp",
8+
"belt-dwp",
99
"ccm",
1010
"chacha20poly1305",
1111
"deoxys",

belt-dwp/src/lib.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! This crate has an optional `alloc` feature which can be disabled in e.g.
3333
//! microcontroller environments that don't have a heap.
3434
//!
35-
//! The [`AeadInPlace::encrypt_in_place`] and [`AeadInPlace::decrypt_in_place`]
35+
//! The [`AeadInOut::encrypt_in_place`] and [`AeadInOut::decrypt_in_place`]
3636
//! methods accept any type that impls the [`aead::Buffer`] trait which
3737
//! contains the plaintext for encryption or ciphertext for decryption.
3838
//!
@@ -46,7 +46,7 @@
4646
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
4747
//! # #[cfg(all(feature = "os_rng", feature = "heapless"))] {
4848
//! use belt_dwp::{
49-
//! aead::{AeadInPlace, AeadInPlaceDetached, KeyInit, heapless::Vec},
49+
//! aead::{AeadInOut, KeyInit, heapless::Vec},
5050
//! Nonce, BeltDwp
5151
//! };
5252
//!
@@ -73,11 +73,11 @@
7373
//! [`aead::Buffer`] for `arrayvec::ArrayVec` (re-exported from the [`aead`] crate as
7474
//! [`aead::arrayvec::ArrayVec`]).
7575
76-
pub use aead::{self, AeadCore, AeadInPlace, Error, Key, KeyInit, KeySizeUser};
76+
pub use aead::{self, AeadCore, AeadInOut, Error, Key, KeyInit, KeySizeUser};
7777
pub use belt_block::BeltBlock;
7878

7979
use aead::consts::{U8, U16};
80-
use aead::{AeadInPlaceDetached, PostfixTagged};
80+
use aead::{TagPosition, inout::InOutBuf};
8181
use belt_block::cipher::crypto_common::InnerUser;
8282
use belt_block::cipher::{Block, BlockCipherEncrypt, StreamCipher};
8383
use belt_ctr::cipher::InnerIvInit;
@@ -128,15 +128,15 @@ where
128128
}
129129
}
130130

131-
impl<C> AeadInPlaceDetached for Dwp<C>
131+
impl<C> AeadInOut for Dwp<C>
132132
where
133133
C: BlockCipherEncrypt + BlockSizeUser<BlockSize = U16>,
134134
{
135-
fn encrypt_in_place_detached(
135+
fn encrypt_inout_detached(
136136
&self,
137137
nonce: &Nonce,
138138
associated_data: &[u8],
139-
buffer: &mut [u8],
139+
mut buffer: InOutBuf<'_, '_, u8>,
140140
) -> aead::Result<Tag> {
141141
let sizes_block = get_sizes_block(associated_data.len(), buffer.len());
142142

@@ -165,8 +165,8 @@ where
165165
// 4.2 𝑌𝑖 ← 𝑋𝑖 ⊕ Lo(belt-block(𝑠, 𝐾), |𝑋𝑖|)
166166
// 4.3 𝑡 ← 𝑡 ⊕ (𝑌𝑖 ‖ 0^{128−|𝑌𝑖|})
167167
// 4.4 𝑡 ← 𝑡 * 𝑟.
168-
enc_cipher.apply_keystream(buffer);
169-
ghash.update_padded(buffer);
168+
enc_cipher.apply_keystream_inout(buffer.reborrow());
169+
ghash.update_padded(buffer.get_out());
170170

171171
// 5. 𝑡 ← 𝑡 ⊕ (⟨|𝐼|⟩_64 ‖ ⟨|𝑋|⟩_64)
172172
ghash.xor_s(&sizes_block);
@@ -178,11 +178,11 @@ where
178178
Ok(Tag::try_from(&tag[..8]).expect("Tag is always 8 bytes"))
179179
}
180180

181-
fn decrypt_in_place_detached(
181+
fn decrypt_inout_detached(
182182
&self,
183183
nonce: &Nonce,
184184
associated_data: &[u8],
185-
buffer: &mut [u8],
185+
buffer: InOutBuf<'_, '_, u8>,
186186
tag: &Tag,
187187
) -> aead::Result<()> {
188188
let sizes_block = get_sizes_block(associated_data.len(), buffer.len());
@@ -206,7 +206,7 @@ where
206206
// 4. For 𝑖 = 1, 2, . . . , 𝑛 do:
207207
// 4.1 𝑡 ← 𝑡 ⊕ (𝑌𝑖 ‖ 0^{128−|𝑌𝑖|})
208208
// 4.2 𝑡 ← 𝑡 * 𝑟.
209-
ghash.update_padded(buffer);
209+
ghash.update_padded(buffer.get_in());
210210

211211
// 5. 𝑡 ← 𝑡 ⊕ (⟨|𝐼|⟩_64 ‖ ⟨|𝑋|⟩_64)
212212
ghash.xor_s(&sizes_block);
@@ -223,22 +223,21 @@ where
223223
// 8.2. 𝑋𝑖 ← 𝑌𝑖 ⊕ Lo(belt-block(𝑠, 𝐾), |𝑌𝑖|)
224224
let core = BeltCtrCore::inner_iv_init(&self.cipher, nonce);
225225
let mut enc_cipher = BeltCtr::from_core(core);
226-
enc_cipher.apply_keystream(buffer);
226+
enc_cipher.apply_keystream_inout(buffer);
227227
Ok(())
228228
} else {
229229
Err(Error)
230230
}
231231
}
232232
}
233233

234-
impl<C> PostfixTagged for Dwp<C> where C: BlockCipherEncrypt + BlockSizeUser<BlockSize = U16> {}
235-
236234
impl<C> AeadCore for Dwp<C>
237235
where
238236
C: BlockCipherEncrypt + BlockSizeUser<BlockSize = U16>,
239237
{
240238
type NonceSize = C::BlockSize;
241239
type TagSize = U8;
240+
const TAG_POSITION: TagPosition = TagPosition::Postfix;
242241
}
243242

244243
/// Get the sizes block for the GHASH

belt-dwp/tests/belt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use aead::AeadInPlaceDetached;
1+
use aead::AeadInOut;
22
use belt_dwp::{BeltDwp, KeyInit};
33
use hex_literal::hex;
44

@@ -37,11 +37,11 @@ fn test_belt_dwp() {
3737
for vec in test_vectors {
3838
let mut x = vec.x;
3939
let belt_dwp = BeltDwp::new_from_slice(&vec.k).unwrap();
40-
let tag = belt_dwp.encrypt_in_place_detached(&vec.s.into(), &vec.i, &mut x);
40+
let tag = belt_dwp.encrypt_inout_detached(&vec.s.into(), &vec.i, (&mut x[..]).into());
4141
assert_eq!(vec.t, *tag.unwrap());
4242
assert_eq!(vec.y, x);
4343
belt_dwp
44-
.decrypt_in_place_detached(&vec.s.into(), &vec.i, &mut x, &tag.unwrap())
44+
.decrypt_inout_detached(&vec.s.into(), &vec.i, (&mut x[..]).into(), &tag.unwrap())
4545
.unwrap();
4646
assert_eq!(x, vec.x);
4747
}

0 commit comments

Comments
 (0)