Skip to content

Commit

Permalink
rsa/der: Avoid as conversions when serializing ASN.1 tags.
Browse files Browse the repository at this point in the history
Work around the lack of `const fn` for `From` implementations.
  • Loading branch information
briansmith committed Feb 19, 2024
1 parent e1730e5 commit c8995cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
13 changes: 10 additions & 3 deletions src/io/der.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,21 @@ pub enum Tag {

impl From<Tag> for usize {
fn from(tag: Tag) -> Self {
tag as Self
Self::from(Tag::into(tag))
}
}

impl From<Tag> for u8 {
fn from(tag: Tag) -> Self {
tag as Self
} // XXX: narrowing conversion.
Tag::into(tag)
}
}

// `impl From<Tag> for u8` but as a `const fn`.
impl Tag {
pub const fn into(self) -> u8 {
self as u8
}
}

pub fn expect_tag_and_get_value<'a>(
Expand Down
10 changes: 5 additions & 5 deletions src/rsa/padding/pkcs1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ macro_rules! pkcs1_digestinfo_prefix {
( $name:ident, $digest_len:expr, $digest_oid_len:expr,
[ $( $digest_oid:expr ),* ] ) => {
static $name: [u8; 2 + 8 + $digest_oid_len] = [
der::Tag::Sequence as u8, 8 + $digest_oid_len + $digest_len,
der::Tag::Sequence as u8, 2 + $digest_oid_len + 2,
der::Tag::OID as u8, $digest_oid_len, $( $digest_oid ),*,
der::Tag::Null as u8, 0,
der::Tag::OctetString as u8, $digest_len,
der::Tag::Sequence.into(), 8 + $digest_oid_len + $digest_len,
der::Tag::Sequence.into(), 2 + $digest_oid_len + 2,
der::Tag::OID.into(), $digest_oid_len, $( $digest_oid ),*,
der::Tag::Null.into(), 0,
der::Tag::OctetString.into(), $digest_len,
];
}
}
Expand Down

0 comments on commit c8995cd

Please sign in to comment.