Skip to content

Commit

Permalink
Add getrandom-based AeadCore::generate_nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Nov 12, 2023
1 parent 8f4c751 commit 963a8cf
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions aead/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ pub use crypto_common::{

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

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

#[cfg(feature = "getrandom")]
pub use crypto_common::rand_core::OsRng;

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

Expand All @@ -45,10 +42,10 @@ use crypto_common::array::{typenum::Unsigned, ArraySize, ByteArray};

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "bytes")]
use bytes::BytesMut;

#[cfg(feature = "getrandom")]
use crypto_common::getrandom;
#[cfg(feature = "rand_core")]
use rand_core::CryptoRngCore;

Expand Down Expand Up @@ -127,6 +124,21 @@ pub trait AeadCore {
/// See the [`stream`] module for a ready-made implementation of the latter.
///
/// [NIST SP 800-38D]: https://csrc.nist.gov/publications/detail/sp/800-38d/final
#[cfg(feature = "getrandom")]
fn generate_nonce() -> core::result::Result<Nonce<Self>, getrandom::Error>
where
Nonce<Self>: Default,
{
let mut nonce = Nonce::<Self>::default();
getrandom::getrandom(&mut nonce)?;
Ok(nonce)
}

/// Generate a random nonce for this AEAD algorithm using the specified
/// [`CryptoRngCore`].
///
/// See [`AeadCore::generate_nonce`] documentation for requirements for
/// random nonces.
#[cfg(feature = "rand_core")]
fn generate_nonce_with_rng(
rng: &mut impl CryptoRngCore,
Expand Down

0 comments on commit 963a8cf

Please sign in to comment.