@@ -12,10 +12,13 @@ use openssl::{
1212 error:: ErrorStack ,
1313} ;
1414use std:: ops:: Div ;
15- use zeroize:: ZeroizeOnDrop ;
15+ use zeroize:: { Zeroize , ZeroizeOnDrop } ;
1616
1717fn clone_big_num ( n : & BigNum ) -> Result < BigNum , ErrorStack > {
18- BigNum :: from_slice ( & n. to_vec ( ) )
18+ let mut bytes = n. to_vec ( ) ;
19+ let clone = BigNum :: from_slice ( & bytes) ;
20+ bytes. zeroize ( ) ;
21+ clone
1922}
2023
2124fn get_group_order ( ctxt : & mut BigNumContext ) -> Result < BigNum , ErrorStack > {
@@ -67,12 +70,20 @@ impl PartialEq for P256Scalar {
6770impl Eq for P256Scalar { }
6871
6972impl P256Scalar {
70- pub const SCALAR_LENGTH : usize = 32 ;
73+ pub const LENGTH : usize = 32 ;
7174}
7275
73- impl Seedable < { Self :: SCALAR_LENGTH } > for P256Scalar {
74- fn from_seed ( seed : & Secret < { Self :: SCALAR_LENGTH } > ) -> Self {
75- Self ( BigNum :: from_slice ( & * * seed) )
76+ impl Seedable < { Self :: LENGTH } > for P256Scalar {
77+ fn from_seed ( seed : & Secret < { Self :: LENGTH } > ) -> Self {
78+ let from_seed = |seed : & Secret < { Self :: LENGTH } > | {
79+ let n = BigNum :: from_slice ( & * * seed) ?;
80+ let mut ctxt = BigNumContext :: new ( ) ?;
81+ let mut res = BigNum :: new ( ) ?;
82+ let order = get_group_order ( & mut ctxt) ?;
83+ res. nnmod ( & n, & order, & mut ctxt) ?;
84+ Ok ( res)
85+ } ;
86+ Self ( from_seed ( seed) )
7687 }
7788}
7889
@@ -219,20 +230,32 @@ impl Serializable for P256Scalar {
219230 type Error = CryptoCoreError ;
220231
221232 fn length ( & self ) -> usize {
222- Self :: SCALAR_LENGTH
233+ Self :: LENGTH
223234 }
224235
225236 fn write ( & self , ser : & mut Serializer ) -> Result < usize , Self :: Error > {
226237 match & self . 0 {
227- Ok ( n) => ser. write_array ( & n. to_vec ( ) ) ,
238+ Ok ( n) => {
239+ let mut bytes = n. to_vec ( ) ;
240+ if Self :: LENGTH < bytes. len ( ) {
241+ return Err ( CryptoCoreError :: GenericSerializationError ( format ! (
242+ "scalar bytes is to big: {}" ,
243+ bytes. len( )
244+ ) ) ) ;
245+ }
246+ let mut fixed_length_bytes = Secret :: < { Self :: LENGTH } > :: new ( ) ;
247+ fixed_length_bytes[ ..bytes. len ( ) ] . copy_from_slice ( & bytes) ;
248+ bytes. zeroize ( ) ;
249+ ser. write_array ( & * fixed_length_bytes)
250+ }
228251 Err ( e) => Err ( CryptoCoreError :: GenericSerializationError ( format ! (
229252 "cannot serialize a scalar in error state: {e}"
230253 ) ) ) ,
231254 }
232255 }
233256
234257 fn read ( de : & mut Deserializer ) -> Result < Self , Self :: Error > {
235- let bytes = de. read_array :: < 32 > ( ) ?;
258+ let bytes = de. read_array :: < { Self :: LENGTH } > ( ) ?;
236259 BigNum :: from_slice ( & bytes) . map ( Ok ) . map ( Self ) . map_err ( |e| {
237260 CryptoCoreError :: GenericDeserializationError ( format ! ( "cannot deserialize scalar: {e}" ) )
238261 } )
@@ -255,5 +278,11 @@ mod tests {
255278 let mut rng = CsRng :: from_entropy ( ) ;
256279 let s = P256Scalar :: random ( & mut rng) ;
257280 test_serialization ( & s) . unwrap ( ) ;
281+
282+ // Test serialization from seed.
283+ let mut rng = CsRng :: from_entropy ( ) ;
284+ let seed = Secret :: < { P256Scalar :: LENGTH } > :: random ( & mut rng) ;
285+ let s = P256Scalar :: from_seed ( & seed) ;
286+ test_serialization ( & s) . unwrap ( ) ;
258287 }
259288}
0 commit comments