Skip to content

Add support for arrayvec in aead - #1219

Merged
tarcieri merged 1 commit into
RustCrypto:masterfrom
antonok-edm:arrayvec
Jan 27, 2023
Merged

Add support for arrayvec in aead#1219
tarcieri merged 1 commit into
RustCrypto:masterfrom
antonok-edm:arrayvec

Conversation

@antonok-edm

@antonok-edmantonok-edm commented Jan 27, 2023

Copy link
Copy Markdown
Contributor

arrayvec is a popular alternative to the heapless crate; it has over 65 million all-time downloads and is used in several high-profile crates.

I've added an arrayvec feature to aead. When enabled, the aead::Buffer trait is implemented for ArrayVec<u8, N>.

Example usage

This usage is modeled off of the heapless example from the aes-gcm-siv crate documentation.

fnmain() -> Result<(),Box<dyn std::error::Error>>{use aes_gcm_siv::{
aead::{AeadInPlace,KeyInit,OsRng, arrayvec::ArrayVec},Aes256GcmSiv,Nonce,// Or `Aes128GcmSiv`};let key = Aes256GcmSiv::generate_key(&mutOsRng);let cipher = Aes256GcmSiv::new(&key);let nonce = Nonce::from_slice(b"unique nonce");// 96-bits; unique per messageletmut buffer:ArrayVec<u8,128> = ArrayVec::new();// Note: buffer needs 16-bytes overhead for auth tag tag
buffer.try_extend_from_slice(b"plaintext message")?;// Encrypt `buffer` in-place, replacing the plaintext contents with ciphertext
cipher.encrypt_in_place(nonce,b"",&mut buffer)?;// `buffer` now contains the message ciphertextassert_ne!(&buffer[..],b"plaintext message");// Decrypt `buffer` in-place, replacing its ciphertext context with the original plaintext
cipher.decrypt_in_place(nonce,b"",&mut buffer)?;assert_eq!(&buffer[..],b"plaintext message");Ok(())}

Until this is merged and aes-gcm-siv is updated with a corresponding arrayvec feature, this would need to be run with a Cargo.toml with the following dependencies/patch:

[dependencies]
aes-gcm-siv = { version = "0.11.1", features = [ "std" ] }
arrayvec = { version = "0.7.2", features = [ "std" ] }
aead = { version = "*", features = [ "arrayvec" ] }
[patch.crates-io]
aead = { path = "/path/to/this/branch/of/traits/aead" }

Comment threadaead/Cargo.toml Outdated
blobby = { version = "0.3", optional = true }
bytes = { version = "1", optional = true, default-features = false }
heapless = { version = "0.7", optional = true, default-features = false }
arrayvec = { version = "0.7", optional = true, default-features = false }

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.

The other dependencies are in alphabetical order

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

Ah, thanks! Updated.

@tarcieri
tarcieri merged commit 942902f into RustCrypto:masterJan 27, 2023
@tarcieri

Copy link
Copy Markdown
Member

Thanks!

@antonok-edm

Copy link
Copy Markdown
ContributorAuthor

@tarcieri I have a branch ready for a PR in https://github.com/RustCrypto/AEADs; let me know once this makes it to a new release on crates.io and I'll get a PR opened for that too

@tarcieritarcieri mentioned this pull request Apr 2, 2023
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.

2 participants

@antonok-edm@tarcieri