@@ -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,33 @@ impl Serializable for P256Scalar {
219230 type Error = CryptoCoreError ;
220231
221232 fn length ( & self ) -> usize {
222- Self :: SCALAR_LENGTH
233+ self . 0
234+ . as_ref ( )
235+ . map ( |n| {
236+ let mut bytes = n. to_vec ( ) ;
237+ let len = bytes. length ( ) ;
238+ bytes. zeroize ( ) ;
239+ len
240+ } )
241+ . unwrap_or_default ( )
223242 }
224243
225244 fn write ( & self , ser : & mut Serializer ) -> Result < usize , Self :: Error > {
226245 match & self . 0 {
227- Ok ( n) => ser. write_array ( & n. to_vec ( ) ) ,
246+ Ok ( n) => {
247+ let mut bytes = n. to_vec ( ) ;
248+ let n = ser. write_vec ( & bytes) ;
249+ bytes. zeroize ( ) ;
250+ n
251+ }
228252 Err ( e) => Err ( CryptoCoreError :: GenericSerializationError ( format ! (
229253 "cannot serialize a scalar in error state: {e}"
230254 ) ) ) ,
231255 }
232256 }
233257
234258 fn read ( de : & mut Deserializer ) -> Result < Self , Self :: Error > {
235- let bytes = de. read_array :: < 32 > ( ) ?;
259+ let bytes = de. read_vec ( ) ?;
236260 BigNum :: from_slice ( & bytes) . map ( Ok ) . map ( Self ) . map_err ( |e| {
237261 CryptoCoreError :: GenericDeserializationError ( format ! ( "cannot deserialize scalar: {e}" ) )
238262 } )
@@ -255,5 +279,11 @@ mod tests {
255279 let mut rng = CsRng :: from_entropy ( ) ;
256280 let s = P256Scalar :: random ( & mut rng) ;
257281 test_serialization ( & s) . unwrap ( ) ;
282+
283+ // Test serialization from seed.
284+ let mut rng = CsRng :: from_entropy ( ) ;
285+ let seed = Secret :: < { P256Scalar :: LENGTH } > :: random ( & mut rng) ;
286+ let s = P256Scalar :: from_seed ( & seed) ;
287+ test_serialization ( & s) . unwrap ( ) ;
258288 }
259289}
0 commit comments