Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deoxys: rewrite the internals to use inout #666

Merged
merged 6 commits into from
Mar 24, 2025

Conversation

baloo
Copy link
Member

@baloo baloo commented Mar 16, 2025

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.

@@ -20,6 +20,7 @@ rust-version = "1.85"
[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
Member Author

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 baloo force-pushed the baloo/deoxys/inout-internals branch 2 times, most recently from 70de0a6 to 787a1d3 Compare March 16, 2025 07:48
@tarcieri
Copy link
Member

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

@baloo baloo force-pushed the baloo/deoxys/inout-internals branch from c517aa5 to 3396c54 Compare March 16, 2025 18:35
@baloo
Copy link
Member Author

baloo commented Mar 16, 2025

Yeah, found a misuse of get_in haha ><

@tarcieri tarcieri requested a review from newpavlov March 16, 2025 20:50
@baloo baloo mentioned this pull request Mar 17, 2025
@baloo baloo force-pushed the baloo/deoxys/inout-internals branch from 3396c54 to 3549927 Compare March 17, 2025 06:22
Copy link
Member

@newpavlov newpavlov left a comment

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.

@baloo baloo force-pushed the baloo/deoxys/inout-internals branch from 3549927 to 29fbddb Compare March 17, 2025 15:27
@baloo baloo force-pushed the baloo/deoxys/inout-internals branch 3 times, most recently from d6a9bd2 to c7bc9a5 Compare March 17, 2025 15:40
@baloo baloo force-pushed the baloo/deoxys/inout-internals branch 2 times, most recently from a79861d to c9c5606 Compare March 17, 2025 19:25
tarcieri pushed a commit that referenced this pull request Mar 17, 2025
@baloo
Copy link
Member Author

baloo commented Mar 24, 2025

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

block[0..data.len()].copy_from_slice(data);
let mut data = tail;
let index = data_blocks_len;
if !data.is_empty() {
Copy link
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.

Copy link
Member Author

@baloo baloo Mar 24, 2025

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
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:master Mar 24, 2025
108 checks passed
@baloo baloo deleted the baloo/deoxys/inout-internals branch March 24, 2025 21:51
Sign up for free to 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