Skip to content

deoxys: rewrite the internals to use inout - #666

Merged
newpavlov merged 6 commits into
RustCrypto:masterfrom
baloo:baloo/deoxys/inout-internals
Mar 24, 2025
Merged

deoxys: rewrite the internals to use inout#666
newpavlov merged 6 commits into
RustCrypto:masterfrom
baloo:baloo/deoxys/inout-internals

Conversation

@baloo

@baloobaloo commented Mar 16, 2025

Copy link
Copy Markdown
Member

This is to prepare the migration to AeadInOut, following RustCrypto/traits#1793

Note: there is a strong chance this could actually use the StreamCipherCore api, but I couldn't not make it fit.

Comment threaddeoxys/Cargo.toml
[dependencies]
aead = { version = "0.6.0-rc.0", default-features = false }
aes = { version = "=0.9.0-pre.3", features = ["hazmat"], default-features = false }
inout = { version = "0.2.0-rc.4", default-features = false }

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually this should drop and use aead::inout

@baloo
balooforce-pushed the baloo/deoxys/inout-internals branch 2 times, most recently from 70de0a6 to 787a1d3CompareMarch 16, 2025 07:48
@tarcieri

Copy link
Copy Markdown
Member

@baloo can you add one or more tests that actually exercise InOutBuf with separate backing buffers?

@baloo
balooforce-pushed the baloo/deoxys/inout-internals branch from c517aa5 to 3396c54CompareMarch 16, 2025 18:35
@baloo

Copy link
Copy Markdown
MemberAuthor

Yeah, found a misuse of get_in haha ><

Comment threaddeoxys/src/lib.rs Outdated
@tarcieri
tarcieri requested a review from newpavlovMarch 16, 2025 20:50
@baloobaloo mentioned this pull request Mar 17, 2025
@baloo
balooforce-pushed the baloo/deoxys/inout-internals branch from 3396c54 to 3549927CompareMarch 17, 2025 06:22

@newpavlovnewpavlov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR includes a fair amount of changes which are not directly relevant to the inout support. Ideally, I would prefer to have them in a separate PR, but it's fine to merge it as-is, if you don't wan to bother with the split.

Comment threaddeoxys/src/lib.rs
Comment threaddeoxys/src/lib.rs
Comment threadCargo.toml Outdated
Comment threaddeoxys/src/modes.rs Outdated
Comment threaddeoxys/src/modes.rs Outdated
Comment threaddeoxys/src/modes.rs Outdated
Comment threaddeoxys/src/modes.rs Outdated
@baloo
balooforce-pushed the baloo/deoxys/inout-internals branch from 3549927 to 29fbddbCompareMarch 17, 2025 15:27
@baloo
balooforce-pushed the baloo/deoxys/inout-internals branch 3 times, most recently from d6a9bd2 to c7bc9a5CompareMarch 17, 2025 15:40
@baloo
balooforce-pushed the baloo/deoxys/inout-internals branch 2 times, most recently from a79861d to c9c5606CompareMarch 17, 2025 19:25
tarcieri pushed a commit that referenced this pull request Mar 17, 2025
@baloo

Copy link
Copy Markdown
MemberAuthor

I think this one is ready to merge, unless you have anything other to say.

Comment threaddeoxys/src/modes.rs
block[0..data.len()].copy_from_slice(data);
let mut data = tail;
let index = data_blocks_len;
if !data.is_empty() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffer_len % 16 == 0 is always true when !data.is_empty() is false. I think it would be better to write this part like this:

if tail.is_empty(){// Without incomplete last block
...}else{// With incomplete last block
...}

Same for decrypt_inout.

@baloobalooMar 24, 2025

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the best I can do will look this:

diff --git a/deoxys/src/modes.rs b/deoxys/src/modes.rs
index 0f91ec36e0..e6ed2cd000 100644
--- a/deoxys/src/modes.rs
+++ b/deoxys/src/modes.rs
@@ -111,12 +111,11 @@
tweak[8] = nonce[7] << 4;
// Message authentication and encryption
- if !buffer.is_empty() {
+ let (data_blocks, mut tail) = buffer.into_chunks();
+ let data_blocks_len = data_blocks.len();
+ if !data_blocks.is_empty() || !tail.is_empty() {
tweak[0] = (tweak[0] & 0xf) | TWEAK_M;
- let (data_blocks, tail) = buffer.into_chunks();
- let data_blocks_len = data_blocks.len();
-
for (index, data) in data_blocks.into_iter().enumerate() {
// Copy block number
let tmp = tweak[8] & 0xf0;
@@ -130,9 +129,8 @@
B::encrypt_inout(data, &tweak, subkeys);
}
- let mut data = tail;
let index = data_blocks_len;
- if !data.is_empty() {
+ if !tail.is_empty() {
// Last block, incomplete
// Copy block number
@@ -144,9 +142,9 @@
tweak[0] = (tweak[0] & 0xf) | TWEAK_M_LAST;
let mut block = Block::default();
- block[0..data.len()].copy_from_slice(data.get_in());
+ block[0..tail.len()].copy_from_slice(tail.get_in());
- block[data.len()] = 0x80;
+ block[tail.len()] = 0x80;
for (c, d) in checksum.iter_mut().zip(block.iter()) {
*c ^= d;
@@ -157,24 +155,23 @@
// Last block encryption
B::encrypt_inout((&mut block).into(), &tweak, subkeys);
- data.xor_in2out((block[..data.len()]).into());
-
- // Tag computing.
- tweak[0] = (tweak[0] & 0xf) | TWEAK_CHKSUM;
-
- let tmp = tweak[8] & 0xf0;
- tweak[8..].copy_from_slice(&((index + 1) as u64).to_be_bytes());
- tweak[8] = (tweak[8] & 0xf) | tmp;
-
- B::encrypt_inout((&mut checksum).into(), tweak.as_ref(), subkeys);
-
- for (t, c) in tag.iter_mut().zip(checksum.iter()) {
- *t ^= c;
- }
+ tail.xor_in2out((block[..tail.len()]).into());
}
}
- if buffer_len % 16 == 0 {
+ if !tail.is_empty() {
+ tweak[0] = (tweak[0] & 0xf) | TWEAK_CHKSUM;
+
+ let tmp = tweak[8] & 0xf0;
+ tweak[8..].copy_from_slice(&((data_blocks_len + 1) as u64).to_be_bytes());
+ tweak[8] = (tweak[8] & 0xf) | tmp;
+
+ B::encrypt_inout((&mut checksum).into(), tweak.as_ref(), subkeys);
+
+ for (t, c) in tag.iter_mut().zip(checksum.iter()) {
+ *t ^= c;
+ }
+ } else {
// Tag computing without last block
tweak[0] = (tweak[0] & 0xf) | TWEAK_TAG;

That makes the whole patch series a lot less readable in my opinion (even when ignoring whitespace).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, we can do it in a different PR then.

@newpavlov
newpavlov merged commit 557d864 into RustCrypto:masterMar 24, 2025
@baloo
baloo deleted the baloo/deoxys/inout-internals branch March 24, 2025 21:51
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@baloo@tarcieri@newpavlov