Skip to content

Commit cd1acaa

Browse files
committed
Wire inout in the public traits
1 parent c3fda09 commit cd1acaa

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

deoxys/src/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ use aead::{
118118
consts::U16,
119119
};
120120
use core::marker::PhantomData;
121-
use inout::InOut;
121+
use inout::{InOut, InOutBuf};
122122

123123
/// Deoxys-I with 128-bit keys
124124
pub type DeoxysI128 = Deoxys<modes::DeoxysI<deoxys_bc::DeoxysBc256>, deoxys_bc::DeoxysBc256>;
@@ -157,19 +157,19 @@ where
157157

158158
/// Encrypts the data in place with the specified parameters
159159
/// Returns the tag
160-
fn encrypt_in_place(
160+
fn encrypt_inout(
161161
nonce: &Array<u8, Self::NonceSize>,
162162
associated_data: &[u8],
163-
buffer: &mut [u8],
163+
buffer: InOutBuf<'_, '_, u8>,
164164
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
165165
) -> Tag;
166166

167167
/// Decrypts the data in place with the specified parameters
168168
/// Returns an error if the tag verification fails
169-
fn decrypt_in_place(
169+
fn decrypt_inout(
170170
nonce: &Array<u8, Self::NonceSize>,
171171
associated_data: &[u8],
172-
buffer: &mut [u8],
172+
buffer: InOutBuf<'_, '_, u8>,
173173
tag: &Tag,
174174
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
175175
) -> Result<(), aead::Error>;
@@ -185,7 +185,7 @@ pub trait DeoxysBcType: deoxys_bc::DeoxysBcInternal {
185185
fn precompute_subkeys(key: &Array<u8, Self::KeySize>) -> Array<DeoxysKey, Self::SubkeysSize>;
186186

187187
/// Encrypts a block of data in place.
188-
fn encrypt_in_place(
188+
fn encrypt_inout(
189189
mut block: InOut<'_, '_, Block>,
190190
tweak: &Tweak,
191191
subkeys: &Array<DeoxysKey, Self::SubkeysSize>,
@@ -200,7 +200,7 @@ pub trait DeoxysBcType: deoxys_bc::DeoxysBcInternal {
200200
}
201201

202202
/// Decrypts a block of data in place.
203-
fn decrypt_in_place(
203+
fn decrypt_inout(
204204
mut block: InOut<'_, '_, Block>,
205205
tweak: &Tweak,
206206
subkeys: &Array<DeoxysKey, Self::SubkeysSize>,
@@ -282,10 +282,10 @@ where
282282
associated_data: &[u8],
283283
buffer: &mut [u8],
284284
) -> Result<Tag, Error> {
285-
Ok(Tag::from(M::encrypt_in_place(
285+
Ok(Tag::from(M::encrypt_inout(
286286
nonce,
287287
associated_data,
288-
buffer,
288+
buffer.into(),
289289
&self.subkeys,
290290
)))
291291
}
@@ -297,7 +297,7 @@ where
297297
buffer: &mut [u8],
298298
tag: &Tag,
299299
) -> Result<(), Error> {
300-
M::decrypt_in_place(nonce, associated_data, buffer, tag, &self.subkeys)
300+
M::decrypt_inout(nonce, associated_data, buffer.into(), tag, &self.subkeys)
301301
}
302302
}
303303

deoxys/src/modes.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ where
4848
let mut block = Block::default();
4949
block.copy_from_slice(ad);
5050

51-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
51+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
5252

5353
for (t, b) in tag.iter_mut().zip(block.iter()) {
5454
*t ^= b;
@@ -62,7 +62,7 @@ where
6262

6363
block[ad.len()] = 0x80;
6464

65-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
65+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
6666

6767
for (t, b) in tag.iter_mut().zip(block.iter()) {
6868
*t ^= b;
@@ -83,16 +83,15 @@ where
8383
{
8484
type NonceSize = U8;
8585

86-
fn encrypt_in_place(
86+
fn encrypt_inout(
8787
nonce: &Array<u8, Self::NonceSize>,
8888
associated_data: &[u8],
89-
buffer: &mut [u8],
89+
buffer: InOutBuf<'_, '_, u8>,
9090
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
9191
) -> Tag {
9292
let mut tag = Tag::default();
9393
let mut checksum = Checksum::default();
9494
let mut tweak = Tweak::default();
95-
let buffer: InOutBuf<'_, '_, u8> = buffer.into();
9695
let buffer_len = buffer.len();
9796

9897
// Associated Data
@@ -128,7 +127,7 @@ where
128127
*c ^= d;
129128
}
130129

131-
B::encrypt_in_place(data, tweak.as_ref(), subkeys);
130+
B::encrypt_inout(data, &tweak, subkeys);
132131
}
133132

134133
let mut data = tail;
@@ -156,7 +155,7 @@ where
156155
block.fill(0);
157156

158157
// Last block encryption
159-
B::encrypt_in_place((&mut block).into(), tweak.as_ref(), subkeys);
158+
B::encrypt_inout((&mut block).into(), &tweak, subkeys);
160159

161160
data.xor_in2out((block[..data.len()]).into());
162161

@@ -167,7 +166,7 @@ where
167166
tweak[8..].copy_from_slice(&((index + 1) as u64).to_be_bytes());
168167
tweak[8] = (tweak[8] & 0xf) | tmp;
169168

170-
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);
169+
B::encrypt_inout((&mut checksum).into(), tweak.as_ref(), subkeys);
171170

172171
for (t, c) in tag.iter_mut().zip(checksum.iter()) {
173172
*t ^= c;
@@ -183,7 +182,7 @@ where
183182
tweak[8..].copy_from_slice(&((buffer_len / 16) as u64).to_be_bytes());
184183
tweak[8] = (tweak[8] & 0xf) | tmp;
185184

186-
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);
185+
B::encrypt_inout((&mut checksum).into(), tweak.as_ref(), subkeys);
187186

188187
for (t, c) in tag.iter_mut().zip(checksum.iter()) {
189188
*t ^= c;
@@ -193,17 +192,16 @@ where
193192
tag
194193
}
195194

196-
fn decrypt_in_place(
195+
fn decrypt_inout(
197196
nonce: &Array<u8, Self::NonceSize>,
198197
associated_data: &[u8],
199-
buffer: &mut [u8],
198+
buffer: InOutBuf<'_, '_, u8>,
200199
tag: &Tag,
201200
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
202201
) -> Result<(), aead::Error> {
203202
let mut computed_tag = Tag::default();
204203
let mut checksum = Checksum::default();
205204
let mut tweak = Tweak::default();
206-
let buffer: InOutBuf<'_, '_, u8> = buffer.into();
207205
let buffer_len = buffer.len();
208206

209207
// Associated Data
@@ -235,7 +233,7 @@ where
235233
tweak[8..].copy_from_slice(&(index as u64).to_be_bytes());
236234
tweak[8] = (tweak[8] & 0xf) | tmp;
237235

238-
B::decrypt_in_place(data.reborrow(), tweak.as_ref(), subkeys);
236+
B::decrypt_inout(data.reborrow(), tweak.as_ref(), subkeys);
239237

240238
for (c, d) in checksum.iter_mut().zip(data.get_out().iter()) {
241239
*c ^= d;
@@ -254,7 +252,7 @@ where
254252
tweak[0] = (tweak[0] & 0xf) | TWEAK_M_LAST;
255253

256254
let mut block = Block::default();
257-
B::encrypt_in_place((&mut block).into(), tweak.as_ref(), subkeys);
255+
B::encrypt_inout((&mut block).into(), tweak.as_ref(), subkeys);
258256

259257
data.xor_in2out((block[..data.len()]).into());
260258

@@ -274,7 +272,7 @@ where
274272
tweak[8..].copy_from_slice(&((index + 1) as u64).to_be_bytes());
275273
tweak[8] = (tweak[8] & 0xf) | tmp;
276274

277-
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);
275+
B::encrypt_inout((&mut checksum).into(), tweak.as_ref(), subkeys);
278276

279277
for (t, c) in computed_tag.iter_mut().zip(checksum.iter()) {
280278
*t ^= c;
@@ -290,7 +288,7 @@ where
290288
tweak[8..].copy_from_slice(&((buffer_len / 16) as u64).to_be_bytes());
291289
tweak[8] = (tweak[8] & 0xf) | tmp;
292290

293-
B::encrypt_in_place((&mut checksum).into(), tweak.as_ref(), subkeys);
291+
B::encrypt_inout((&mut checksum).into(), tweak.as_ref(), subkeys);
294292

295293
for (t, c) in computed_tag.iter_mut().zip(checksum.iter()) {
296294
*t ^= c;
@@ -329,7 +327,7 @@ where
329327

330328
let mut block = data.clone();
331329

332-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
330+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
333331

334332
for (t, b) in tag.iter_mut().zip(block.iter()) {
335333
*t ^= b;
@@ -353,7 +351,7 @@ where
353351

354352
block[data.len()] = 0x80;
355353

356-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
354+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
357355

358356
for (t, b) in tag.iter_mut().zip(block.iter()) {
359357
*t ^= b;
@@ -385,7 +383,7 @@ where
385383
let mut block = Block::default();
386384
block[1..].copy_from_slice(nonce);
387385

388-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
386+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
389387

390388
xor(&block);
391389

@@ -424,10 +422,10 @@ where
424422
{
425423
type NonceSize = U15;
426424

427-
fn encrypt_in_place(
425+
fn encrypt_inout(
428426
nonce: &Array<u8, Self::NonceSize>,
429427
associated_data: &[u8],
430-
buffer: &mut [u8],
428+
buffer: InOutBuf<'_, '_, u8>,
431429
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
432430
) -> Tag {
433431
let mut tag = Tag::default();
@@ -442,22 +440,22 @@ where
442440
);
443441

444442
// Message authentication
445-
Self::authenticate_message(buffer, &mut tweak, subkeys, &mut tag);
443+
Self::authenticate_message(buffer.get_in(), &mut tweak, subkeys, &mut tag);
446444

447445
tweak[0] = TWEAK_TAG;
448446
tweak[1..].copy_from_slice(nonce);
449-
B::encrypt_in_place((&mut tag).into(), &tweak, subkeys);
447+
B::encrypt_inout((&mut tag).into(), &tweak, subkeys);
450448

451449
// Message encryption
452-
Self::encrypt_decrypt_message(buffer.into(), &mut tweak, subkeys, &tag, nonce);
450+
Self::encrypt_decrypt_message(buffer, &mut tweak, subkeys, &tag, nonce);
453451

454452
tag
455453
}
456454

457-
fn decrypt_in_place(
455+
fn decrypt_inout(
458456
nonce: &Array<u8, Self::NonceSize>,
459457
associated_data: &[u8],
460-
buffer: &mut [u8],
458+
mut buffer: InOutBuf<'_, '_, u8>,
461459
tag: &Tag,
462460
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
463461
) -> Result<(), aead::Error> {
@@ -473,16 +471,16 @@ where
473471
);
474472

475473
// Message decryption
476-
Self::encrypt_decrypt_message(buffer.into(), &mut tweak, subkeys, tag, nonce);
474+
Self::encrypt_decrypt_message(buffer.reborrow(), &mut tweak, subkeys, tag, nonce);
477475

478476
tweak.fill(0);
479477

480478
// Message authentication
481-
Self::authenticate_message(buffer, &mut tweak, subkeys, &mut computed_tag);
479+
Self::authenticate_message(buffer.get_out(), &mut tweak, subkeys, &mut computed_tag);
482480

483481
tweak[0] = TWEAK_TAG;
484482
tweak[1..].copy_from_slice(nonce);
485-
B::encrypt_in_place((&mut computed_tag).into(), &tweak, subkeys);
483+
B::encrypt_inout((&mut computed_tag).into(), &tweak, subkeys);
486484

487485
if tag.ct_eq(&computed_tag).into() {
488486
Ok(())

0 commit comments

Comments
 (0)