Skip to content

Commit 787a1d3

Browse files
committed
Wire inout in the public traits
1 parent cdb2221 commit 787a1d3

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: &Array<u8, U16>,
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;
@@ -326,7 +324,7 @@ where
326324
let mut block = Block::default();
327325
block.copy_from_slice(data);
328326

329-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
327+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
330328

331329
for (t, b) in tag.iter_mut().zip(block.iter()) {
332330
*t ^= b;
@@ -340,7 +338,7 @@ where
340338

341339
block[data.len()] = 0x80;
342340

343-
B::encrypt_in_place((&mut block).into(), tweak, subkeys);
341+
B::encrypt_inout((&mut block).into(), tweak, subkeys);
344342

345343
for (t, b) in tag.iter_mut().zip(block.iter()) {
346344
*t ^= b;
@@ -369,7 +367,7 @@ where
369367
let mut block = Block::default();
370368
block[1..].copy_from_slice(nonce);
371369

372-
B::encrypt_in_place((&mut block).into(), tweak.as_ref(), subkeys);
370+
B::encrypt_inout((&mut block).into(), tweak.as_ref(), subkeys);
373371

374372
$xor(&block);
375373

@@ -404,10 +402,10 @@ where
404402
{
405403
type NonceSize = U15;
406404

407-
fn encrypt_in_place(
405+
fn encrypt_inout(
408406
nonce: &Array<u8, Self::NonceSize>,
409407
associated_data: &[u8],
410-
buffer: &mut [u8],
408+
buffer: InOutBuf<'_, '_, u8>,
411409
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
412410
) -> Tag {
413411
let mut tag = Tag::default();
@@ -422,22 +420,22 @@ where
422420
);
423421

424422
// Message authentication
425-
Self::authenticate_message(buffer, &mut tweak, subkeys, &mut tag);
423+
Self::authenticate_message(buffer.get_in(), &mut tweak, subkeys, &mut tag);
426424

427425
tweak[0] = TWEAK_TAG;
428426
tweak[1..].copy_from_slice(nonce);
429-
B::encrypt_in_place((&mut tag).into(), &tweak, subkeys);
427+
B::encrypt_inout((&mut tag).into(), &tweak, subkeys);
430428

431429
// Message encryption
432-
Self::encrypt_decrypt_message(buffer.into(), &mut tweak, subkeys, &tag, nonce);
430+
Self::encrypt_decrypt_message(buffer, &mut tweak, subkeys, &tag, nonce);
433431

434432
tag
435433
}
436434

437-
fn decrypt_in_place(
435+
fn decrypt_inout(
438436
nonce: &Array<u8, Self::NonceSize>,
439437
associated_data: &[u8],
440-
buffer: &mut [u8],
438+
mut buffer: InOutBuf<'_, '_, u8>,
441439
tag: &Array<u8, U16>,
442440
subkeys: &Array<DeoxysKey, B::SubkeysSize>,
443441
) -> Result<(), aead::Error> {
@@ -453,16 +451,16 @@ where
453451
);
454452

455453
// Message decryption
456-
Self::encrypt_decrypt_message(buffer.into(), &mut tweak, subkeys, tag, nonce);
454+
Self::encrypt_decrypt_message(buffer.reborrow(), &mut tweak, subkeys, tag, nonce);
457455

458456
tweak.fill(0);
459457

460458
// Message authentication
461-
Self::authenticate_message(buffer, &mut tweak, subkeys, &mut computed_tag);
459+
Self::authenticate_message(buffer.get_out(), &mut tweak, subkeys, &mut computed_tag);
462460

463461
tweak[0] = TWEAK_TAG;
464462
tweak[1..].copy_from_slice(nonce);
465-
B::encrypt_in_place((&mut computed_tag).into(), &tweak, subkeys);
463+
B::encrypt_inout((&mut computed_tag).into(), &tweak, subkeys);
466464

467465
if tag.ct_eq(&computed_tag).into() {
468466
Ok(())

0 commit comments

Comments
 (0)