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

How to serialize a Nonce in aes_gcm #621

Closed
Antosser opened this issue Aug 5, 2024 · 3 comments
Closed

How to serialize a Nonce in aes_gcm #621

Antosser opened this issue Aug 5, 2024 · 3 comments

Comments

@Antosser
Copy link

Antosser commented Aug 5, 2024

When creating a random nonce, Rust automagically gives it the following type:

use aes_gcm::AeadCore;

fn main() {
    let mut rng = rand::thread_rng();

    let _nonce = aes_gcm::Aes256Gcm::generate_nonce(&mut rng);
}
// size = 12 (0xC), align = 0x1
let _nonce: GenericArray<u8, UInt<UInt<UInt<UInt<UTerm, B1>, B1>, B0>, B0>>

My goal is to put the Nonce into a struct that implements both serde::Serialize and serde::Deserialize. And actually writing that whole type doesn't seem intended.

The library provides two Nonce types: aes_gcm::Nonce and aes_gcm::aead::Nonce with the latter one being returned from the generate_nonce function.

Signature of generate_nonce:

fn generate_nonce(mut rng: impl CryptoRng + RngCore) -> Nonce<Self> where Nonce<Self>: Default

Type Nonce:

pub type Nonce<A> = GenericArray<u8, <A as AeadCore>::NonceSize>;

However, when I try to give the nonce the type myself, like in the following, I get the following error:

use aes_gcm::{aead::Nonce, AeadCore};

fn main() {
    let mut rng = rand::thread_rng();

    let _nonce: Nonce<AeadCore> = aes_gcm::Aes256Gcm::generate_nonce(&mut rng);
}
error[E0191]: the value of the associated types `TagSize`, `NonceSize` and `CiphertextOverhead` in `AeadCore` must be specified
 --> src/main.rs:6:23
  |
6 |     let _nonce: Nonce<AeadCore> = aes_gcm::Aes256Gcm::generate_nonce(&mut rng);
  |                       ^^^^^^^^ help: specify the associated types: `AeadCore<NonceSize = Type, TagSize = Type, CiphertextOverhead = Type>`

All this is getting way too complicated, which I hope is not intended. What would be the intended solution to (de)?serialize a Nonce?

@tarcieri
Copy link
Member

tarcieri commented Aug 5, 2024

Nonce is just a type alias for GenericArray<u8, AeadCore::NonceSize>: https://docs.rs/generic-array/0.14.7/generic_array/struct.GenericArray.html

There are several ways to convert normal core arrays like [0u8; 12] to/from GenericArray, such as the From impls:

https://docs.rs/generic-array/0.14.7/generic_array/struct.GenericArray.html#impl-From%3C%26'a+%5BT;+12%5D%3E-for-%26'a+GenericArray%3CT,+U12%3E

https://docs.rs/generic-array/0.14.7/generic_array/struct.GenericArray.html#impl-From%3CGenericArray%3CT,+UInt%3CUInt%3CUInt%3CUInt%3CUTerm,+B1%3E,+B1%3E,+B0%3E,+B0%3E%3E%3E-for-%5BT;+12%5D

You can also use the AsRef/AsMut impls to obtain &[u8; 12]: https://docs.rs/generic-array/0.14.7/generic_array/struct.GenericArray.html#impl-AsRef%3C%5BT;+12%5D%3E-for-GenericArray%3CT,+U12%3E

aes_gcm::aead is a re-export of the aead crate. aead::Nonce is generic around an AeadCore algorithm such as AesGcm.

To avoid you having to specify the algorithm, aes_gcm::Nonce is a type alias for AesGcm: https://docs.rs/aes-gcm/latest/aes_gcm/type.Nonce.html

So you should really work with that, not aes_gcm::aead::Nonce, and you can use any of the above converts to go to/from [u8; 12].

@tarcieri tarcieri closed this as completed Aug 5, 2024
@Antosser
Copy link
Author

Antosser commented Aug 5, 2024

This crate uses a very old version of generic_array (0.14.7), and not the latest stable version (1.1.0). That means, you have to add that specific version to your project because the different versions are incompatible. Would re-exporting the generic_array crate make sense? Or even upgrading?

And thanks for helping!

@tarcieri
Copy link
Member

tarcieri commented Aug 5, 2024

We re-export generic_array from the aead crate: https://docs.rs/aead/latest/aead/

It's available as aes_gcm::aead::generic_array

In our next breaking releases, we're migrating to hybrid-array, which should have better ergonomics: https://github.com/RustCrypto/hybrid-array

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