From 306e40b7967ad11670cc1c9403b5a88073fecc4b Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 18 Oct 2026 15:02:38 -0700 Subject: [PATCH] Use `os2ip` for decoding signatures Uses the helper function introduced in #719 for signature decoding --- src/pkcs1v15.rs | 7 +++---- src/pss.rs | 13 +++++-------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/pkcs1v15.rs b/src/pkcs1v15.rs index 6b2178e2..85f95621 100644 --- a/src/pkcs1v15.rs +++ b/src/pkcs1v15.rs @@ -42,7 +42,6 @@ pub use self::{ use alloc::{boxed::Box, vec::Vec}; use const_oid::AssociatedOid; use core::fmt::Debug; -use crypto_bigint::BoxedUint; use digest::Digest; use rand_core::TryCryptoRng; @@ -207,7 +206,7 @@ fn sign( ) -> Result> { let em = pkcs1v15_sign_pad(prefix, hashed, priv_key.size())?; - let em = BoxedUint::from_be_slice(&em, priv_key.n_bits_precision())?; + let em = os2ip(&em, priv_key.n_bits_precision())?; // The signature itself is public; only the computation is secret Ok(i2osp( rsa_decrypt_and_check(priv_key, rng, &em)?, @@ -222,8 +221,7 @@ fn verify(pub_key: &RsaPublicKey, prefix: &[u8], hashed: &[u8], sig: &[u8]) -> R if sig.len() != pub_key.size() { return Err(Error::Verification); } - let s = BoxedUint::from_be_slice(sig, pub_key.n_bits_precision()) - .map_err(|_| Error::Verification)?; + let s = os2ip(sig, pub_key.n_bits_precision()).map_err(|_| Error::Verification)?; if s >= *pub_key.n().as_ref() { return Err(Error::Verification); } @@ -284,6 +282,7 @@ mod tests { SignatureEncoding, Signer, Verifier, }; use base64ct::{Base64, Encoding}; + use crypto_bigint::BoxedUint; use hex_literal::hex; use rand::rngs::ChaCha8Rng; use rand_core::{Rng, SeedableRng}; diff --git a/src/pss.rs b/src/pss.rs index e4ed2c1f..4841ffe7 100644 --- a/src/pss.rs +++ b/src/pss.rs @@ -21,12 +21,11 @@ pub use self::{ use alloc::vec::Vec; use core::fmt::{self, Debug}; -use crypto_bigint::BoxedUint; use digest::{Digest, FixedOutputReset}; use rand_core::TryCryptoRng; -use crate::algorithms::pad::i2osp; +use crate::algorithms::pad::{i2osp, os2ip}; use crate::algorithms::pss::*; use crate::algorithms::rsa::{rsa_decrypt_and_check, rsa_encrypt}; use crate::errors::{Error, Result}; @@ -148,8 +147,7 @@ where if sig.len() != pub_key.size() { return Err(Error::Verification); } - let s = BoxedUint::from_be_slice(sig, pub_key.n_bits_precision()) - .map_err(|_| Error::Verification)?; + let s = os2ip(sig, pub_key.n_bits_precision()).map_err(|_| Error::Verification)?; let raw = rsa_encrypt(pub_key, &s)?; let mut em = i2osp(raw, pub_key.size()); @@ -168,8 +166,7 @@ where if sig.len() != pub_key.size() { return Err(Error::Verification); } - let s = BoxedUint::from_be_slice(sig, pub_key.n_bits_precision()) - .map_err(|_| Error::Verification)?; + let s = os2ip(sig, pub_key.n_bits_precision()).map_err(|_| Error::Verification)?; if s >= *pub_key.n().as_ref() { return Err(Error::Verification); } @@ -239,7 +236,7 @@ where let em = emsa_pss_encode(hashed, em_bits as _, salt, digest)?; - let em = BoxedUint::from_be_slice(&em, priv_key.n_bits_precision())?; + let em = os2ip(&em, priv_key.n_bits_precision())?; let raw = rsa_decrypt_and_check(priv_key, blind_rng, &em)?; Ok(i2osp(raw, priv_key.size())) } @@ -257,7 +254,7 @@ where let em_bits = priv_key.n().bits() - 1; let em = emsa_pss_encode_digest::(hashed, em_bits as _, salt)?; - let em = BoxedUint::from_be_slice(&em, priv_key.n_bits_precision())?; + let em = os2ip(&em, priv_key.n_bits_precision())?; Ok(i2osp( rsa_decrypt_and_check(priv_key, blind_rng, &em)?, priv_key.size(),