@@ -8,7 +8,7 @@ use crate::{Error, WrapperErrorKind};
8
8
9
9
use core:: convert:: TryFrom ;
10
10
use elliptic_curve:: {
11
- generic_array :: typenum:: Unsigned ,
11
+ array :: typenum:: Unsigned ,
12
12
sec1:: { EncodedPoint , FromEncodedPoint , ModulusSize , ToEncodedPoint } ,
13
13
AffinePoint , CurveArithmetic , FieldBytesSize , PublicKey ,
14
14
} ;
@@ -18,7 +18,7 @@ use x509_cert::spki::SubjectPublicKeyInfoOwned;
18
18
#[ cfg( feature = "rsa" ) ]
19
19
use {
20
20
crate :: structures:: RsaExponent ,
21
- rsa:: { BigUint , RsaPublicKey } ,
21
+ rsa:: { BoxedUint , RsaPublicKey } ,
22
22
} ;
23
23
24
24
#[ cfg( any(
@@ -57,15 +57,13 @@ where
57
57
let x = unique. x ( ) . as_bytes ( ) ;
58
58
let y = unique. y ( ) . as_bytes ( ) ;
59
59
60
- if x. len ( ) != FieldBytesSize :: < C > :: USIZE {
61
- return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
62
- }
63
- if y. len ( ) != FieldBytesSize :: < C > :: USIZE {
64
- return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
65
- }
66
-
67
- let encoded_point =
68
- EncodedPoint :: < C > :: from_affine_coordinates ( x. into ( ) , y. into ( ) , false ) ;
60
+ let encoded_point = EncodedPoint :: < C > :: from_affine_coordinates (
61
+ x. try_into ( )
62
+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?,
63
+ y. try_into ( )
64
+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?,
65
+ false ,
66
+ ) ;
69
67
let public_key = PublicKey :: < C > :: try_from ( & encoded_point)
70
68
. map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
71
69
@@ -86,10 +84,11 @@ impl TryFrom<&Public> for RsaPublicKey {
86
84
unique, parameters, ..
87
85
} => {
88
86
let exponent = match parameters. exponent ( ) {
89
- RsaExponent :: ZERO_EXPONENT => BigUint :: from ( RSA_DEFAULT_EXP ) ,
90
- _ => BigUint :: from ( parameters. exponent ( ) . value ( ) ) ,
87
+ RsaExponent :: ZERO_EXPONENT => BoxedUint :: from ( RSA_DEFAULT_EXP ) ,
88
+ _ => BoxedUint :: from ( parameters. exponent ( ) . value ( ) ) ,
91
89
} ;
92
- let modulus = BigUint :: from_bytes_be ( unique. as_bytes ( ) ) ;
90
+ let modulus = BoxedUint :: from_be_slice ( unique. as_bytes ( ) , 8192 )
91
+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: WrongParamSize ) ) ?;
93
92
94
93
let public_key = RsaPublicKey :: new ( modulus, exponent)
95
94
. map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
@@ -173,17 +172,21 @@ where
173
172
let x = x. as_slice ( ) ;
174
173
let y = y. as_slice ( ) ;
175
174
176
- // TODO: When elliptic_curve bumps to 0.14, we can use the TryFrom implementation instead
177
- // of checking lengths manually
178
175
if x. len ( ) != FieldBytesSize :: < C > :: USIZE {
179
176
return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
180
177
}
181
178
if y. len ( ) != FieldBytesSize :: < C > :: USIZE {
182
179
return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
183
180
}
184
181
185
- let encoded_point =
186
- EncodedPoint :: < C > :: from_affine_coordinates ( x. into ( ) , y. into ( ) , false ) ;
182
+ let encoded_point = EncodedPoint :: < C > :: from_affine_coordinates (
183
+ x. try_into ( )
184
+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?,
185
+ y. try_into ( )
186
+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?,
187
+ false ,
188
+ ) ;
189
+
187
190
let public_key = PublicKey :: < C > :: try_from ( & encoded_point)
188
191
. map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
189
192
@@ -201,8 +204,9 @@ impl TryFrom<&TpmPublicKey> for RsaPublicKey {
201
204
fn try_from ( value : & TpmPublicKey ) -> Result < Self , Self :: Error > {
202
205
match value {
203
206
TpmPublicKey :: Rsa ( modulus) => {
204
- let exponent = BigUint :: from ( RSA_DEFAULT_EXP ) ;
205
- let modulus = BigUint :: from_bytes_be ( modulus. as_slice ( ) ) ;
207
+ let exponent = BoxedUint :: from ( RSA_DEFAULT_EXP ) ;
208
+ let modulus = BoxedUint :: from_be_slice ( modulus. as_slice ( ) , 8192 )
209
+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: WrongParamSize ) ) ?;
206
210
207
211
let public_key = RsaPublicKey :: new ( modulus, exponent)
208
212
. map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
0 commit comments