11//! Bign256 secret key.
2- // TODO(tarcieri): replace with `elliptic_curve::SecretKey`
32
3+ use core:: fmt:: { self , Debug } ;
4+ #[ cfg( feature = "pem" ) ]
45use core:: str:: FromStr ;
6+ #[ cfg( feature = "pkcs8" ) ]
57use der:: { SecretDocument , asn1:: OctetStringRef } ;
68
79#[ cfg( feature = "pkcs8" ) ]
810use crate :: ALGORITHM_OID ;
9- use crate :: { PublicKey , ScalarValue } ;
11+ use crate :: { BignP256 , FieldBytes , NonZeroScalar , PublicKey , Result , ScalarValue } ;
12+ #[ cfg( feature = "pem" ) ]
13+ use elliptic_curve:: Error ;
1014#[ cfg( feature = "pkcs8" ) ]
1115use elliptic_curve:: pkcs8:: {
1216 self , AssociatedOid , DecodePrivateKey , EncodePrivateKey , ObjectIdentifier ,
1317 spki:: { AlgorithmIdentifier , AssociatedAlgorithmIdentifier } ,
1418} ;
15- use elliptic_curve:: { Error , Generate , array:: typenum:: Unsigned , zeroize:: Zeroizing } ;
16-
17- #[ cfg( feature = "arithmetic" ) ]
18- use crate :: { BignP256 , FieldBytes , NonZeroScalar , Result , elliptic_curve:: rand_core:: TryCryptoRng } ;
19+ use elliptic_curve:: { Generate , rand_core:: TryCryptoRng , zeroize:: ZeroizeOnDrop } ;
1920
20- /// Elliptic curve BignP256 Secret Key
21- #[ cfg( feature = "arithmetic" ) ]
22- #[ derive( Copy , Clone , Debug ) ]
23- pub struct SecretKey {
24- inner : ScalarValue ,
25- }
21+ /// Elliptic curve BignP256 Secret Key.
22+ ///
23+ /// A wrapper around [`elliptic_curve::SecretKey`] which uses the PKCS#8
24+ /// encoding defined in STB 34.101.45 (the raw secret scalar as an octet
25+ /// string with the bign algorithm identifier) instead of SEC1.
26+ #[ derive( Clone ) ]
27+ pub struct SecretKey ( elliptic_curve:: SecretKey < BignP256 > ) ;
2628
2729impl SecretKey {
28- const MIN_SIZE : usize = 24 ;
29-
3030 /// Borrow the inner secret [`elliptic_curve::ScalarValue`] value.
3131 ///
3232 /// # ⚠️ Warning
3333 ///
3434 /// This value is key material.
3535 ///
3636 /// Please treat it with the care it deserves!
37- pub fn as_scalar_primitive ( & self ) -> & ScalarValue {
38- & self . inner
37+ pub fn as_scalar_value ( & self ) -> & ScalarValue {
38+ self . 0 . as_scalar_value ( )
3939 }
4040
4141 /// Get the secret [`elliptic_curve::NonZeroScalar`] value for this key.
@@ -45,26 +45,18 @@ impl SecretKey {
4545 /// This value is key material.
4646 ///
4747 /// Please treat it with the care it deserves!
48- #[ cfg( feature = "arithmetic" ) ]
4948 pub fn to_nonzero_scalar ( & self ) -> NonZeroScalar {
50- ( * self ) . into ( )
49+ self . 0 . to_nonzero_scalar ( )
5150 }
5251
5352 /// Get the [`PublicKey`] which corresponds to this secret key
54- #[ cfg( feature = "arithmetic" ) ]
5553 pub fn public_key ( & self ) -> PublicKey {
56- PublicKey :: from_secret_scalar ( & self . to_nonzero_scalar ( ) )
54+ self . 0 . public_key ( ) . into ( )
5755 }
5856
5957 /// Deserialize secret key from an encoded secret scalar.
6058 pub fn from_bytes ( bytes : & FieldBytes ) -> Result < Self > {
61- let inner = ScalarValue :: from_bytes ( bytes) . into_option ( ) . ok_or ( Error ) ?;
62-
63- if inner. is_zero ( ) . into ( ) {
64- return Err ( Error ) ;
65- }
66-
67- Ok ( Self { inner } )
59+ elliptic_curve:: SecretKey :: from_bytes ( bytes) . map ( Self )
6860 }
6961
7062 /// Deserialize secret key from an encoded secret scalar passed as a byte slice.
@@ -77,68 +69,70 @@ impl SecretKey {
7769 /// NOTE: this function is variable-time with respect to the input length. To avoid a timing
7870 /// sidechannel, always ensure that the input has been pre-padded to `C::FieldBytesSize`.
7971 pub fn from_slice ( slice : & [ u8 ] ) -> Result < Self > {
80- if slice. len ( ) == <BignP256 as elliptic_curve:: Curve >:: FieldBytesSize :: USIZE {
81- Self :: from_bytes ( & FieldBytes :: try_from ( slice) . map_err ( |_| Error ) ?)
82- } else if ( Self :: MIN_SIZE ..<BignP256 as elliptic_curve:: Curve >:: FieldBytesSize :: USIZE )
83- . contains ( & slice. len ( ) )
84- {
85- let mut bytes = Zeroizing :: new ( FieldBytes :: default ( ) ) ;
86- let offset = <BignP256 as elliptic_curve:: Curve >:: FieldBytesSize :: USIZE
87- . saturating_sub ( slice. len ( ) ) ;
88- bytes[ offset..] . copy_from_slice ( slice) ;
89- Self :: from_bytes ( & bytes)
90- } else {
91- Err ( Error )
92- }
72+ elliptic_curve:: SecretKey :: from_slice ( slice) . map ( Self )
9373 }
9474
9575 /// Serialize raw secret scalar as a big endian integer.
9676 pub fn to_bytes ( & self ) -> FieldBytes {
97- self . inner . to_bytes ( )
77+ self . 0 . to_bytes ( )
9878 }
9979}
10080
101- #[ cfg( feature = "pkcs8" ) ]
102- impl AssociatedAlgorithmIdentifier for SecretKey {
103- type Params = ObjectIdentifier ;
104- const ALGORITHM_IDENTIFIER : AlgorithmIdentifier < Self :: Params > = AlgorithmIdentifier {
105- oid : ALGORITHM_OID ,
106- parameters : Some ( BignP256 :: OID ) ,
107- } ;
81+ impl Debug for SecretKey {
82+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
83+ self . 0 . fmt ( f)
84+ }
10885}
10986
87+ impl ZeroizeOnDrop for SecretKey { }
88+
11089impl From < SecretKey > for NonZeroScalar {
11190 fn from ( secret_key : SecretKey ) -> NonZeroScalar {
11291 secret_key. to_nonzero_scalar ( )
11392 }
11493}
11594
116- #[ cfg( feature = "arithmetic" ) ]
11795impl From < NonZeroScalar > for SecretKey {
11896 fn from ( scalar : NonZeroScalar ) -> SecretKey {
119- SecretKey :: from ( & scalar)
97+ Self ( scalar. into ( ) )
12098 }
12199}
122100
123- #[ cfg( feature = "arithmetic" ) ]
124101impl From < & NonZeroScalar > for SecretKey {
125102 fn from ( scalar : & NonZeroScalar ) -> SecretKey {
126- SecretKey {
127- inner : scalar. into ( ) ,
128- }
103+ Self ( scalar. into ( ) )
104+ }
105+ }
106+
107+ impl From < SecretKey > for elliptic_curve:: SecretKey < BignP256 > {
108+ fn from ( secret_key : SecretKey ) -> Self {
109+ secret_key. 0
110+ }
111+ }
112+
113+ impl From < elliptic_curve:: SecretKey < BignP256 > > for SecretKey {
114+ fn from ( secret_key : elliptic_curve:: SecretKey < BignP256 > ) -> Self {
115+ Self ( secret_key)
129116 }
130117}
131118
132119impl Generate for SecretKey {
133120 fn try_generate_from_rng < R : TryCryptoRng + ?Sized > (
134121 rng : & mut R ,
135122 ) -> core:: result:: Result < Self , R :: Error > {
136- Ok ( Self {
137- inner : ScalarValue :: try_generate_from_rng ( rng) ?,
138- } )
123+ elliptic_curve:: SecretKey :: try_generate_from_rng ( rng) . map ( Self )
139124 }
140125}
141126
127+ #[ cfg( feature = "pkcs8" ) ]
128+ impl AssociatedAlgorithmIdentifier for SecretKey {
129+ type Params = ObjectIdentifier ;
130+ const ALGORITHM_IDENTIFIER : AlgorithmIdentifier < Self :: Params > = AlgorithmIdentifier {
131+ oid : ALGORITHM_OID ,
132+ parameters : Some ( BignP256 :: OID ) ,
133+ } ;
134+ }
135+
142136#[ cfg( feature = "pkcs8" ) ]
143137impl TryFrom < pkcs8:: PrivateKeyInfoRef < ' _ > > for SecretKey {
144138 type Error = pkcs8:: Error ;
0 commit comments