@@ -4,7 +4,7 @@ use core::cmp::Ordering;
44
55use crypto_bigint:: modular:: { BoxedMontyForm , BoxedMontyParams } ;
66use crypto_bigint:: { BoxedUint , Gcd , NonZero , Odd , RandomMod , Wrapping } ;
7- use rand_core:: CryptoRng ;
7+ use rand_core:: TryCryptoRng ;
88use zeroize:: Zeroize ;
99
1010use crate :: errors:: { Error , Result } ;
@@ -31,8 +31,8 @@ pub fn rsa_encrypt<K: PublicKeyParts>(key: &K, m: &BoxedUint) -> Result<BoxedUin
3131/// Use this function with great care! Raw RSA should never be used without an appropriate padding
3232/// or signature scheme. See the [module-level documentation][crate::hazmat] for more information.
3333#[ inline]
34- pub fn rsa_decrypt < R : CryptoRng + ?Sized > (
35- mut rng : Option < & mut R > ,
34+ pub fn rsa_decrypt < R : TryCryptoRng + ?Sized > (
35+ rng : Option < & mut R > ,
3636 priv_key : & impl PrivateKeyParts ,
3737 c : & BoxedUint ,
3838) -> Result < BoxedUint > {
@@ -48,8 +48,8 @@ pub fn rsa_decrypt<R: CryptoRng + ?Sized>(
4848 let n_params = priv_key. n_params ( ) ;
4949 let bits = d. bits_precision ( ) ;
5050
51- let c = if let Some ( ref mut rng) = rng {
52- let ( blinded, unblinder) = blind ( rng, priv_key, c, n_params) ;
51+ let c = if let Some ( rng) = rng {
52+ let ( blinded, unblinder) = blind ( rng, priv_key, c, n_params) ? ;
5353 ir = Some ( unblinder) ;
5454 blinded. widen ( bits)
5555 } else {
@@ -123,7 +123,7 @@ pub fn rsa_decrypt<R: CryptoRng + ?Sized>(
123123/// Use this function with great care! Raw RSA should never be used without an appropriate padding
124124/// or signature scheme. See the [module-level documentation][crate::hazmat] for more information.
125125#[ inline]
126- pub fn rsa_decrypt_and_check < R : CryptoRng + ?Sized > (
126+ pub fn rsa_decrypt_and_check < R : TryCryptoRng + ?Sized > (
127127 priv_key : & impl PrivateKeyParts ,
128128 rng : Option < & mut R > ,
129129 c : & BoxedUint ,
@@ -142,12 +142,12 @@ pub fn rsa_decrypt_and_check<R: CryptoRng + ?Sized>(
142142}
143143
144144/// Returns the blinded c, along with the unblinding factor.
145- fn blind < R : CryptoRng , K : PublicKeyParts > (
145+ fn blind < R : TryCryptoRng + ? Sized , K : PublicKeyParts > (
146146 rng : & mut R ,
147147 key : & K ,
148148 c : & BoxedUint ,
149149 n_params : & BoxedMontyParams ,
150- ) -> ( BoxedUint , BoxedUint ) {
150+ ) -> Result < ( BoxedUint , BoxedUint ) > {
151151 // Blinding involves multiplying c by r^e.
152152 // Then the decryption operation performs (m^e * r^e)^d mod n
153153 // which equals mr mod n. The factor of r can then be removed
@@ -158,7 +158,7 @@ fn blind<R: CryptoRng, K: PublicKeyParts>(
158158 let mut r: BoxedUint = BoxedUint :: one_with_precision ( bits) ;
159159 let mut ir: Option < BoxedUint > = None ;
160160 while ir. is_none ( ) {
161- r = BoxedUint :: random_mod ( rng, key. n ( ) ) ;
161+ r = BoxedUint :: try_random_mod ( rng, key. n ( ) ) . map_err ( |_| Error :: Rng ) ? ;
162162 if r. is_zero ( ) . into ( ) {
163163 r = BoxedUint :: one_with_precision ( bits) ;
164164 }
@@ -181,7 +181,7 @@ fn blind<R: CryptoRng, K: PublicKeyParts>(
181181 debug_assert_eq ! ( blinded. bits_precision( ) , bits) ;
182182 debug_assert_eq ! ( ir. bits_precision( ) , bits) ;
183183
184- ( blinded, ir)
184+ Ok ( ( blinded, ir) )
185185}
186186
187187/// Given an m and and unblinding factor, unblind the m.
0 commit comments