@@ -8,8 +8,8 @@ use crate::{Error, WrapperErrorKind};
88
99use core:: convert:: TryFrom ;
1010use elliptic_curve:: {
11- generic_array :: typenum:: Unsigned ,
12- sec1:: { EncodedPoint , FromEncodedPoint , ModulusSize , ToEncodedPoint } ,
11+ array :: typenum:: Unsigned ,
12+ sec1:: { FromSec1Point , ModulusSize , Sec1Point , ToSec1Point } ,
1313 AffinePoint , CurveArithmetic , FieldBytesSize , PublicKey ,
1414} ;
1515
@@ -18,7 +18,7 @@ use x509_cert::spki::SubjectPublicKeyInfoOwned;
1818#[ cfg( feature = "rsa" ) ]
1919use {
2020 crate :: structures:: RsaExponent ,
21- rsa:: { BigUint , RsaPublicKey } ,
21+ rsa:: { BoxedUint , RsaPublicKey } ,
2222} ;
2323
2424#[ cfg( any(
@@ -41,7 +41,7 @@ impl<C> TryFrom<&Public> for PublicKey<C>
4141where
4242 C : CurveArithmetic + AssociatedTpmCurve ,
4343 FieldBytesSize < C > : ModulusSize ,
44- AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
44+ AffinePoint < C > : FromSec1Point < C > + ToSec1Point < C > ,
4545{
4646 type Error = Error ;
4747
@@ -57,15 +57,13 @@ where
5757 let x = unique. x ( ) . as_bytes ( ) ;
5858 let y = unique. y ( ) . as_bytes ( ) ;
5959
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 = Sec1Point :: < 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+ ) ;
6967 let public_key = PublicKey :: < C > :: try_from ( & encoded_point)
7068 . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
7169
@@ -86,10 +84,10 @@ impl TryFrom<&Public> for RsaPublicKey {
8684 unique, parameters, ..
8785 } => {
8886 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 ( ) ) ,
9189 } ;
92- let modulus = BigUint :: from_bytes_be ( unique. as_bytes ( ) ) ;
90+ let modulus = BoxedUint :: from_be_slice_vartime ( unique. as_bytes ( ) ) ;
9391
9492 let public_key = RsaPublicKey :: new ( modulus, exponent)
9593 . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
@@ -163,7 +161,7 @@ impl<C> TryFrom<&TpmPublicKey> for PublicKey<C>
163161where
164162 C : CurveArithmetic + AssociatedTpmCurve ,
165163 FieldBytesSize < C > : ModulusSize ,
166- AffinePoint < C > : FromEncodedPoint < C > + ToEncodedPoint < C > ,
164+ AffinePoint < C > : FromSec1Point < C > + ToSec1Point < C > ,
167165{
168166 type Error = Error ;
169167
@@ -173,17 +171,21 @@ where
173171 let x = x. as_slice ( ) ;
174172 let y = y. as_slice ( ) ;
175173
176- // TODO: When elliptic_curve bumps to 0.14, we can use the TryFrom implementation instead
177- // of checking lengths manually
178174 if x. len ( ) != FieldBytesSize :: < C > :: USIZE {
179175 return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
180176 }
181177 if y. len ( ) != FieldBytesSize :: < C > :: USIZE {
182178 return Err ( Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ;
183179 }
184180
185- let encoded_point =
186- EncodedPoint :: < C > :: from_affine_coordinates ( x. into ( ) , y. into ( ) , false ) ;
181+ let encoded_point = Sec1Point :: < C > :: from_affine_coordinates (
182+ x. try_into ( )
183+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?,
184+ y. try_into ( )
185+ . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?,
186+ false ,
187+ ) ;
188+
187189 let public_key = PublicKey :: < C > :: try_from ( & encoded_point)
188190 . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
189191
@@ -201,8 +203,8 @@ impl TryFrom<&TpmPublicKey> for RsaPublicKey {
201203 fn try_from ( value : & TpmPublicKey ) -> Result < Self , Self :: Error > {
202204 match value {
203205 TpmPublicKey :: Rsa ( modulus) => {
204- let exponent = BigUint :: from ( RSA_DEFAULT_EXP ) ;
205- let modulus = BigUint :: from_bytes_be ( modulus. as_slice ( ) ) ;
206+ let exponent = BoxedUint :: from ( RSA_DEFAULT_EXP ) ;
207+ let modulus = BoxedUint :: from_be_slice_vartime ( modulus. as_slice ( ) ) ;
206208
207209 let public_key = RsaPublicKey :: new ( modulus, exponent)
208210 . map_err ( |_| Error :: local_error ( WrapperErrorKind :: InvalidParam ) ) ?;
0 commit comments