From 2497a2e47dad3fafe00fde7a04da805369e3ba4e Mon Sep 17 00:00:00 2001 From: Arthur Gautier Date: Mon, 25 Dec 2023 00:56:59 +0000 Subject: [PATCH] x509-cert: drop `generate_with_length` and `generate_with_dyn_length` Signed-off-by: Arthur Gautier --- x509-cert/src/serial_number.rs | 107 +++++++++++++++------------------ 1 file changed, 49 insertions(+), 58 deletions(-) diff --git a/x509-cert/src/serial_number.rs b/x509-cert/src/serial_number.rs index 6aff10897..7897eface 100644 --- a/x509-cert/src/serial_number.rs +++ b/x509-cert/src/serial_number.rs @@ -12,7 +12,7 @@ use { core::ops::Add, generic_array::{ typenum::{ - consts::{self, U17, U19, U8}, + consts::{U17, U19, U8}, marker_traits::Unsigned, type_operators::{Max, Min}, uint::UTerm, @@ -88,71 +88,62 @@ impl SerialNumber

{ /// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/ #[cfg(feature = "builder")] pub fn generate(rng: &mut impl CryptoRngCore) -> Result { - Self::generate_with_length::(rng) + Self::generate_with_prefix::(GenericArray::default(), rng) } - /// Generates a random serial number from RNG. + /// Generates a random serial number from RNG. Include a prefix value. /// /// This follows the recommendation the CAB forum [ballot 164] and uses a minimum of 64 bits /// of output from the CSPRNG. /// - /// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/ - #[cfg(feature = "builder")] - pub fn generate_with_length(rng: &mut impl CryptoRngCore) -> Result - where - N: Unsigned + ArrayLength, - // Rand minimum is 64 bits - N: Min, - // Max length is 20 bytes, encoding of sign bit might - // requires an additional byte, so the limit is 19. - N: Max, - { - Self::generate_with_prefix::(GenericArray::default(), rng) - } - - /// Generates a random serial number from RNG. + /// # Dynamic API /// - /// This follows the recommendation the CAB forum [ballot 164] and uses a minimum of 64 bits - /// of output from the CSPRNG. - /// - /// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/ - #[cfg(feature = "builder")] - #[allow(unused_qualifications)] - pub fn generate_with_dyn_length(rng: &mut impl CryptoRngCore, len: usize) -> Result { - macro_rules! impl_generate { - ($len:literal => $u:ty) => { - if (len == $len) { - return Self::generate_with_prefix::(GenericArray::default(), rng); - } - }; - ($len:literal => $u:ty, $($rest:tt)*) => { - impl_generate!($len => $u); - impl_generate!($($rest)*); - }; - } - - impl_generate!( - 8 => consts::U8, - 9 => consts::U9, - 10 => consts::U10, - 11 => consts::U11, - 12 => consts::U12, - 13 => consts::U13, - 14 => consts::U14, - 15 => consts::U15, - 16 => consts::U16, - 17 => consts::U17, - 18 => consts::U18, - 19 => consts::U19 - ); - - Err(ErrorKind::Failed.into()) - } - - /// Generates a random serial number from RNG. Include a prefix value. + /// While this only provides a const-generic API. A dynamic generation can be implemented + /// like: + /// ```rust + /// use der::{ErrorKind, Result}; + /// use generic_array::{ + /// typenum::{ + /// consts, + /// uint::UTerm, + /// }, + /// GenericArray, + /// }; + /// use signature::rand_core::CryptoRngCore; + /// use x509_cert::serial_number::SerialNumber; /// - /// This follows the recommendation the CAB forum [ballot 164] and uses a minimum of 64 bits - /// of output from the CSPRNG. + /// /// Generates a random serial number from RNG. + /// n generate_with_dyn_length(rng: &mut impl CryptoRngCore, len: usize) -> Result { + /// macro_rules! impl_generate { + /// ($len:literal => $u:ty) => { + /// if (len == $len) { + /// return SerialNumber::generate_with_prefix::(GenericArray::default(), rng); + /// } + /// }; + /// ($len:literal => $u:ty, $($rest:tt)*) => { + /// impl_generate!($len => $u); + /// impl_generate!($($rest)*); + /// }; + /// } + + /// impl_generate!( + /// 8 => consts::U8, + /// 9 => consts::U9, + /// 10 => consts::U10, + /// 11 => consts::U11, + /// 12 => consts::U12, + /// 13 => consts::U13, + /// 14 => consts::U14, + /// 15 => consts::U15, + /// 16 => consts::U16, + /// 17 => consts::U17, + /// 18 => consts::U18, + /// 19 => consts::U19 + /// ); + + /// Err(ErrorKind::Failed.into()) + /// } + /// ``` /// /// [ballot 164]: https://cabforum.org/2016/03/31/ballot-164/ #[cfg(feature = "builder")]