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

xaes: initial implementation #612

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

SergioBenitez
Copy link

This is an initial implementation of XAES-256-GCM (re: #1) which passes the test vectors.

Would love a review, especially as it pertains to constant-time and zeroing (why isn't zeroize used to zero IVs?). I don't see an obvious constant-time byte-slice XOR in use elsewhere in Rust-Crypto, but please point to a canonical reference if possible. I also have not placed this behind any feature flags, yet. Finally, the primary structure XaesGcm256 is not parameterized in any way. If it's desirable to parameterize it in a similar fashion to AesGcm, please let me know.

pub const P_MAX: u64 = 1 << 36;

/// Maximum length of associated data.
// pub const A_MAX: u64 = 1 << 61;

Choose a reason for hiding this comment

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

probably forgot to delete ?

Copy link
Author

Choose a reason for hiding this comment

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

I left the code here to inspire this comment. :)

According to the C2SP spec, the associated data max size for XAES-256-GCM is 2EiB = 2^61 bytes. However the max size for AD for AES-GCM is 2^64 and yet it's been restricted to 2^36 by this crate, so specifying the "true" AD would have no affect. I didn't explore why the AD is limited in this manner, however, so I left the code there in hopes that that would be clarified.

pub use aead::{self, AeadCore, AeadInPlace, Error, Key, KeyInit, KeySizeUser};

#[cfg(feature = "aes")]
pub use aes;

#[cfg(feature = "aes")]
Copy link

@pinkforest pinkforest Jun 29, 2024

Choose a reason for hiding this comment

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

Maybe feature = "xaes" extending "aes" ?

Copy link
Author

Choose a reason for hiding this comment

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

Whatever is desired!

use common::TestVector;
use hex_literal::hex;

/// C2SP XAES-256-GCM test vectors
Copy link

@pinkforest pinkforest Jun 29, 2024

Choose a reason for hiding this comment

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

sidebar - would love if C2SP test vectors would be embedded in crate a-like of wycheproof-rs so test vectors can be updated independently to all associated impl's and no messing updating multiple places

// Kₓ = Kₘ || Kₙ = AES-256ₖ(M1 ⊕ K1) || AES-256ₖ(M2 ⊕ K1)
let mut key: Key<Aes256Gcm> = Array::default();
let (km, kn) = key.split_ref_mut::<<KeySize as Div<U2>>::Output>();
for i in 0..km.len() {

Choose a reason for hiding this comment

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

Would be good to comment why conditional km.len() is okay to potentially leak on all conditionals ?

Copy link
Author

Choose a reason for hiding this comment

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

Not sure I follow. km.len() isn't a conditional. Perhaps I'm misunderstanding?


// If MSB₁(L) = 0 then K1 = L << 1 Else K1 = (L << 1) ⊕ 0¹²⁰10000111
let mut msb = 0;
for i in (0..k1.len()).rev() {

Choose a reason for hiding this comment

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

Also probably good to document k1.len() conditional here

Copy link
Author

Choose a reason for hiding this comment

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

Same as above.

@pinkforest
Copy link

gave you few nits - take em if you like ☺️

if you feel like it and if worried about non-ct anywhere:

// M1 = 0x00 || 0x01 || X || 0x00 || N[:12]
let mut m1 = Block::default();
m1[..4].copy_from_slice(&[0, 1, b'X', 0]);
m1[4..].copy_from_slice(n1);

Choose a reason for hiding this comment

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

conditional length on ? would be great to clarify maybe why perhaps

Copy link
Author

Choose a reason for hiding this comment

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

I don't understand the comment. What is ??

@@ -4,7 +4,7 @@
#[derive(Debug)]
pub struct TestVector<K: 'static> {
pub key: &'static K,
pub nonce: &'static [u8; 12],
pub nonce: &'static [u8],

Choose a reason for hiding this comment

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

Is there a reason to make this non-fixed length ?

Copy link
Author

Choose a reason for hiding this comment

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

Now we have nonces of two different lengths, so this is needed to be able to reuse the structure.

let mut msb = 0;
for i in (0..k1.len()).rev() {
let new_msb = k1[i] >> 7;
k1[i] = (k1[i] << 1) | msb;

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

I compiled this with various flags, and at least of the nightly as of 3 days, the assembly generated is identical and doesn't leak any secrets via timing side-channels. That may change with future compilers, of course.

Where precisely are you suggesting Blackbox be used? Could you provide a GitHub suggestion of the change, perhaps?

km[i] = m1[i] ^ self.k1[i];
}
for i in 0..kn.len() {
kn[i] = m2[i] ^ self.k1[i];

Choose a reason for hiding this comment

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

Also should these be in subtle::BlackBox

@SergioBenitez
Copy link
Author

@newpavlov @tarcieri Any chance for a review here?

@tarcieri
Copy link
Member

tarcieri commented Jul 8, 2024

@SergioBenitez sorry, I've been on vacation. I'll look at this soon.

@SergioBenitez
Copy link
Author

Checking in. Any chance to push this forward?

@tarcieri
Copy link
Member

@SergioBenitez haven't had a whole lot of free time lately for code review but I still hope to review it soon

@tarcieri
Copy link
Member

tarcieri commented Aug 1, 2024

Sorry for the belated review.

On #1 we had discussed an xaes-gcm crate, but I now see there is already an xaes-gcm crate which implements the Derive-Key-AES-GCM construction, and I have not carefully looked at the differences between that and this construction.

I am a bit wary including the construction in the aes-gcm crate itself, which otherwise implements NIST standard constructions.

However, I'd also note the construction in the spec is called XAES-256-GCM, so how about an xaes-256-gcm crate instead, which is currently available?

@SergioBenitez
Copy link
Author

Sure! Went ahead and published a -pre version to reserve the name. I also added you as an owner; feel free to use that as you wish. Looking forward to a review of the crate.

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