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

generic array / stream encryptor type mismatch #542

Closed
ousado opened this issue Jul 18, 2023 · 4 comments
Closed

generic array / stream encryptor type mismatch #542

ousado opened this issue Jul 18, 2023 · 4 comments

Comments

@ousado
Copy link

ousado commented Jul 18, 2023

The following code

use chacha20poly1305::{
    aead::{stream, OsRng},
    XChaCha20Poly1305, XNonce, Key, KeyInit, AeadCore
};

fn main() {
    let key   : Key               = XChaCha20Poly1305::generate_key(&mut OsRng);
    let nonce : XNonce            = XChaCha20Poly1305::generate_nonce(&mut OsRng);
    let aead  : XChaCha20Poly1305 = XChaCha20Poly1305::new(&key);
    let stream_encryptor          = stream::EncryptorBE32::from_aead(aead,&nonce);
}

produces the following error:

error[E0308]: mismatched types
   --> src/main.rs:11:78
    |
11  |     let stream_encryptor             = stream::EncryptorBE32::from_aead(aead,&nonce);
    |                                        --------------------------------      ^^^^^^ expected `B0`, found `B1`
    |                                        |
    |                                        arguments to this function are incorrect
    |
    = note: expected reference `&GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B0>, B1>, B1>>`
               found reference `&GenericArray<u8, UInt<UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>, B0>>`
note: associated function defined here
   --> / .. .. .. /cargo/registry/src/index.crates.io-6f17d22bba15001f/aead-0.5.2/src/stream.rs:337:1
    |
337 | / impl_stream_object!(
338 | |     Encryptor,
339 | |     encrypt_next,
340 | |     encrypt_next_in_place,
...   |
346 | |     "ℰ STREAM encryptor"
347 | | );
    | |_^
    = note: this error originates in the macro `impl_stream_object` (in Nightly builds, run with -Z macro-backtrace for more info)

Here's a repository with this example:
https://github.com/ousado/xcc20poly1305issue/blob/main/src/main.rs

@tarcieri
Copy link
Member

tarcieri commented Jul 18, 2023

The nonce size for *BE32 is 5-bytes smaller (32-bit counter + last block flag byte) than the 24-byte XChaCha20Poly1305 nonce so 24 - 5 = 19 bytes.

See also RustCrypto/traits#1306

@ousado
Copy link
Author

ousado commented Jul 18, 2023

I assumed it's somehow generic over the cipher's NonceSize due to these definitions:

pub type Nonce<A, S> = GenericArray<u8, NonceSize<A, S>>;

pub type NonceSize<A, S> = <<A as AeadCore>::NonceSize as Sub<<S as StreamPrimitive>::NonceOverhead>>::Output;

Does that mean there is currently no stream::Encryptor that can be used with XChaCha20Poly1305?

@tarcieri
Copy link
Member

No, it just means you're passing the wrong nonce size.

You need to use GenericArray<u8, U19>.

You can use those type aliases if you'd like, but right now you're using XNonce which is 24-byte.

@ousado
Copy link
Author

ousado commented Jul 18, 2023

Ah, I see, thanks for the quick clarification!

@ousado ousado closed this as completed Jul 18, 2023
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

No branches or pull requests

2 participants