Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions pkcs1/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ impl<'a> RsaPssParamsRef<'a> {
impl<Params> RsaPssParams<Params>
where
AlgorithmIdentifier<Params>: AsAlgorithmIdentifierRef,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifier<AnyRef<'static>>>,
Params: PartialEq,
{
fn context_specific_hash(&self) -> Option<ContextSpecificRef<'_, AlgorithmIdentifier<Params>>> {
if self.hash.as_algo_ref() == SHA_1_AI {
Expand All @@ -170,7 +168,7 @@ where
fn context_specific_mask_gen(
&self,
) -> Option<ContextSpecificRef<'_, AlgorithmIdentifier<AlgorithmIdentifier<Params>>>> {
if self.mask_gen == default_mgf1_sha1::<Params>() {
if is_default_mgf1_sha1(&self.mask_gen) {
None
} else {
Some(ContextSpecificRef {
Expand Down Expand Up @@ -201,7 +199,7 @@ where
AlgorithmIdentifier<Params>: DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<AlgorithmIdentifier<Params>>:
DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'a>>,
Params: 'a,
{
type Error = der::Error;
Expand Down Expand Up @@ -230,8 +228,6 @@ where
for<'b> Option<ContextSpecificRef<'b, AlgorithmIdentifier<Params>>>: Encode,
for<'b> Option<ContextSpecificRef<'b, AlgorithmIdentifier<AlgorithmIdentifier<Params>>>>:
Encode,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
Params: PartialEq,
{
fn value_len(&self) -> der::Result<Length> {
self.context_specific_hash().encoded_len()?
Expand All @@ -256,7 +252,7 @@ where
AlgorithmIdentifier<Params>: DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<AlgorithmIdentifier<Params>>:
DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'a>>,
Params: 'a,
{
type Error = Error;
Expand All @@ -267,16 +263,29 @@ where
}

/// Default Mask Generation Function (MGF): SHA-1.
fn default_mgf1_sha1<Params>() -> AlgorithmIdentifier<AlgorithmIdentifier<Params>>
fn default_mgf1_sha1<'a, Params>() -> AlgorithmIdentifier<AlgorithmIdentifier<Params>>
where
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'a>>,
{
AlgorithmIdentifier::<AlgorithmIdentifier<Params>> {
oid: OID_MGF_1,
parameters: Some(SHA_1_AI.into()),
}
}

/// Is `mask_gen` the default MGF (MGF1 with SHA-1)?
fn is_default_mgf1_sha1<Params>(mask_gen: &AlgorithmIdentifier<AlgorithmIdentifier<Params>>) -> bool
where
AlgorithmIdentifier<Params>: AsAlgorithmIdentifierRef,
{
mask_gen.oid == OID_MGF_1
&& mask_gen
.parameters
.as_ref()
.map(AsAlgorithmIdentifierRef::as_algo_ref)
== Some(SHA_1_AI)
}

/// [`RsaOaepParams`] with [`AlgorithmIdentifier<AnyRef>`]
pub type RsaOaepParamsRef<'a> = RsaOaepParams<AnyRef<'a>>;

Expand Down Expand Up @@ -345,13 +354,11 @@ where
impl<Params> RsaOaepParams<Params>
where
AlgorithmIdentifier<Params>: AsAlgorithmIdentifierRef,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
Params: PartialEq,
{
fn context_specific_mask_gen(
&self,
) -> Option<ContextSpecificRef<'_, AlgorithmIdentifier<AlgorithmIdentifier<Params>>>> {
if self.mask_gen == default_mgf1_sha1::<Params>() {
if is_default_mgf1_sha1(&self.mask_gen) {
None
} else {
Some(ContextSpecificRef {
Expand Down Expand Up @@ -412,7 +419,7 @@ where
AlgorithmIdentifier<Params>: DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<AlgorithmIdentifier<Params>>:
DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'a>>,
Params: 'a,
{
type Error = der::Error;
Expand All @@ -438,8 +445,6 @@ where
for<'b> Option<ContextSpecificRef<'b, AlgorithmIdentifier<Params>>>: Encode,
for<'b> Option<ContextSpecificRef<'b, AlgorithmIdentifier<AlgorithmIdentifier<Params>>>>:
Encode,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
Params: PartialEq,
{
fn value_len(&self) -> der::Result<Length> {
self.context_specific_hash().encoded_len()?
Expand All @@ -462,7 +467,7 @@ where
AlgorithmIdentifier<Params>: DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<AlgorithmIdentifier<Params>>:
DecodeValue<'a, Error = der::Error> + FixedTag,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'static>>,
AlgorithmIdentifier<Params>: From<AlgorithmIdentifierRef<'a>>,
Params: 'a,
{
type Error = Error;
Expand Down
26 changes: 26 additions & 0 deletions pkcs1/tests/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,32 @@ fn new_oaep_param() {
);
}

/// The `*Ref` types borrow from their input, which need not be `'static`.
#[test]
fn ref_params_non_static_input() {
fn pss(der: &[u8], buf: &mut [u8]) -> usize {
let param = RsaPssParamsRef::try_from(der).unwrap();
param.encode_to_slice(buf).unwrap().len()
}
fn oaep(der: &[u8], buf: &mut [u8]) -> usize {
let param = RsaOaepParamsRef::try_from(der).unwrap();
param.encode_to_slice(buf).unwrap().len()
}

let mut buf = [0_u8; 256];
let mut der = [0_u8; 64];

let n = RSA_PSS_PARAMETERS_SHA2_256.len();
der[..n].copy_from_slice(RSA_PSS_PARAMETERS_SHA2_256);
assert_eq!(pss(&der[..n], &mut buf), n);
assert_eq!(&buf[..n], RSA_PSS_PARAMETERS_SHA2_256);

let n = RSA_OAEP_PARAMETERS_SHA2_256.len();
der[..n].copy_from_slice(RSA_OAEP_PARAMETERS_SHA2_256);
assert_eq!(oaep(&der[..n], &mut buf), n);
assert_eq!(&buf[..n], RSA_OAEP_PARAMETERS_SHA2_256);
}

#[cfg(feature = "alloc")]
mod test_alloc_trait_bounds {
use der::{Decode, Encode};
Expand Down
Loading