@@ -4,26 +4,26 @@ use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
44use hemtt_common:: io:: { ReadExt , WriteExt } ;
55use hemtt_pbo:: { BISignVersion , ReadablePbo } ;
66use rsa:: {
7- BigUint , RsaPrivateKey ,
7+ BoxedUint , RsaPrivateKey ,
88 traits:: { PrivateKeyParts , PublicKeyParts } ,
99} ;
1010
11- use crate :: { error:: Error , generate_hashes, public:: BIPublicKey , signature:: BISign } ;
11+ use crate :: { error:: Error , generate_hashes, modpow , public:: BIPublicKey , signature:: BISign } ;
1212
1313#[ allow( clippy:: module_name_repetitions) ]
1414#[ derive( Debug , Clone ) ]
1515/// A private key for signing PBOs
1616pub struct BIPrivateKey {
1717 authority : String ,
1818 length : u32 ,
19- exponent : BigUint ,
20- n : BigUint ,
21- p : BigUint ,
22- q : BigUint ,
23- dp : BigUint ,
24- dq : BigUint ,
25- qinv : BigUint ,
26- d : BigUint ,
19+ exponent : BoxedUint ,
20+ n : BoxedUint ,
21+ p : BoxedUint ,
22+ q : BoxedUint ,
23+ dp : BoxedUint ,
24+ dq : BoxedUint ,
25+ qinv : BoxedUint ,
26+ d : BoxedUint ,
2727}
2828
2929impl BIPrivateKey {
@@ -35,20 +35,18 @@ impl BIPrivateKey {
3535 /// # Errors
3636 /// If RSA generation fails.
3737 pub fn generate ( length : u32 , authority : & str ) -> Result < Self , Error > {
38- let mut rng = rand:: thread_rng ( ) ;
38+ let mut rng = rand:: rng ( ) ;
3939 let mut rsa = RsaPrivateKey :: new ( & mut rng, length as usize ) ?;
4040 rsa. precompute ( ) ?;
4141 let primes = rsa. primes ( ) ;
42- let Some ( qinv) = rsa. qinv ( ) . expect (
42+ let qinv = rsa. qinv ( ) . expect (
4343 "qinv should be precomputed, if it's not, the precompute failed and we should return" ,
44- ) . to_biguint ( ) else {
45- return Err ( Error :: Rsa ( rsa:: errors:: Error :: Internal ) ) ;
46- } ;
44+ ) . to_montgomery ( ) ;
4745 Ok ( Self {
4846 authority : authority. to_string ( ) ,
4947 length,
5048 exponent : rsa. e ( ) . clone ( ) ,
51- n : rsa. n ( ) . clone ( ) ,
49+ n : rsa. n ( ) . clone ( ) . get ( ) ,
5250 p : primes[ 0 ] . clone ( ) ,
5351 q : primes[ 1 ] . clone ( ) ,
5452 dp : rsa. dp ( ) . expect (
@@ -92,49 +90,49 @@ impl BIPrivateKey {
9290 let exponent = {
9391 let mut buffer = vec ! [ 0 ; 4 ] ;
9492 input. read_exact ( & mut buffer) ?;
95- BigUint :: from_bytes_le ( & buffer)
93+ BoxedUint :: from_le_slice_vartime ( & buffer)
9694 } ;
9795
9896 let n = {
9997 let mut buffer = vec ! [ 0 ; ( length / 8 ) as usize ] ;
10098 input. read_exact ( & mut buffer) ?;
101- BigUint :: from_bytes_le ( & buffer)
99+ BoxedUint :: from_le_slice_vartime ( & buffer)
102100 } ;
103101
104102 let p = {
105103 let mut buffer = vec ! [ 0 ; ( length / 16 ) as usize ] ;
106104 input. read_exact ( & mut buffer) ?;
107- BigUint :: from_bytes_le ( & buffer)
105+ BoxedUint :: from_le_slice_vartime ( & buffer)
108106 } ;
109107
110108 let q = {
111109 let mut buffer = vec ! [ 0 ; ( length / 16 ) as usize ] ;
112110 input. read_exact ( & mut buffer) ?;
113- BigUint :: from_bytes_le ( & buffer)
111+ BoxedUint :: from_le_slice_vartime ( & buffer)
114112 } ;
115113
116114 let dp = {
117115 let mut buffer = vec ! [ 0 ; ( length / 16 ) as usize ] ;
118116 input. read_exact ( & mut buffer) ?;
119- BigUint :: from_bytes_le ( & buffer)
117+ BoxedUint :: from_le_slice_vartime ( & buffer)
120118 } ;
121119
122120 let dq = {
123121 let mut buffer = vec ! [ 0 ; ( length / 16 ) as usize ] ;
124122 input. read_exact ( & mut buffer) ?;
125- BigUint :: from_bytes_le ( & buffer)
123+ BoxedUint :: from_le_slice_vartime ( & buffer)
126124 } ;
127125
128126 let qinv = {
129127 let mut buffer = vec ! [ 0 ; ( length / 16 ) as usize ] ;
130128 input. read_exact ( & mut buffer) ?;
131- BigUint :: from_bytes_le ( & buffer)
129+ BoxedUint :: from_le_slice_vartime ( & buffer)
132130 } ;
133131
134132 let d = {
135133 let mut buffer = vec ! [ 0 ; ( length / 8 ) as usize ] ;
136134 input. read_exact ( & mut buffer) ?;
137- BigUint :: from_bytes_le ( & buffer)
135+ BoxedUint :: from_le_slice_vartime ( & buffer)
138136 } ;
139137
140138 Ok ( Self {
@@ -162,9 +160,9 @@ impl BIPrivateKey {
162160 ) -> Result < BISign , Error > {
163161 let ( hash1, hash2, hash3) = generate_hashes ( pbo, version, self . length ) ?;
164162
165- let sig1 = hash1 . modpow ( & self . d , & self . n ) ;
166- let sig2 = hash2 . modpow ( & self . d , & self . n ) ;
167- let sig3 = hash3 . modpow ( & self . d , & self . n ) ;
163+ let sig1 = modpow ( & hash1 , & self . d , & self . n ) ;
164+ let sig2 = modpow ( & hash2 , & self . d , & self . n ) ;
165+ let sig3 = modpow ( & hash3 , & self . d , & self . n ) ;
168166
169167 Ok ( BISign {
170168 version,
@@ -191,21 +189,21 @@ impl BIPrivateKey {
191189 output. write_all ( b"\x07 \x02 \x00 \x00 \x00 \x24 \x00 \x00 " ) ?;
192190 output. write_all ( b"RSA2" ) ?;
193191 output. write_u32 :: < LittleEndian > ( self . length ) ?;
194- super :: write_biguint ( output, & self . exponent , 4 ) ?;
192+ super :: write_boxeduint ( output, & self . exponent , 4 ) ?;
195193 // output.write_all(&self.exponent.to_bytes_le())?;
196- super :: write_biguint ( output, & self . n , ( self . length / 8 ) as usize ) ?;
194+ super :: write_boxeduint ( output, & self . n , ( self . length / 8 ) as usize ) ?;
197195 // output.write_all(&self.n.to_bytes_le())?;
198- super :: write_biguint ( output, & self . p , ( self . length / 16 ) as usize ) ?;
196+ super :: write_boxeduint ( output, & self . p , ( self . length / 16 ) as usize ) ?;
199197 // output.write_all(&self.p.to_bytes_le())?;
200- super :: write_biguint ( output, & self . q , ( self . length / 16 ) as usize ) ?;
198+ super :: write_boxeduint ( output, & self . q , ( self . length / 16 ) as usize ) ?;
201199 // output.write_all(&self.q.to_bytes_le())?;
202- super :: write_biguint ( output, & self . dp , ( self . length / 16 ) as usize ) ?;
200+ super :: write_boxeduint ( output, & self . dp , ( self . length / 16 ) as usize ) ?;
203201 // output.write_all(&self.dp.to_bytes_le())?;
204- super :: write_biguint ( output, & self . dq , ( self . length / 16 ) as usize ) ?;
202+ super :: write_boxeduint ( output, & self . dq , ( self . length / 16 ) as usize ) ?;
205203 // output.write_all(&self.dq.to_bytes_le())?;
206- super :: write_biguint ( output, & self . qinv , ( self . length / 16 ) as usize ) ?;
204+ super :: write_boxeduint ( output, & self . qinv , ( self . length / 16 ) as usize ) ?;
207205 // output.write_all(&self.qinv.to_bytes_le())?;
208- super :: write_biguint ( output, & self . d , ( self . length / 8 ) as usize ) ?;
206+ super :: write_boxeduint ( output, & self . d , ( self . length / 8 ) as usize ) ?;
209207 // output.write_all(&self.d.to_bytes_le())?;
210208 Ok ( ( ) )
211209 }
0 commit comments