@@ -16,7 +16,7 @@ use signature::Error;
1616use subtle:: { Choice , ConstantTimeEq } ;
1717
1818#[ cfg( feature = "pkcs8" ) ]
19- use crate :: { PUBLIC_KEY_LENGTH , edwards :: affine :: PointBytes } ;
19+ use :: ed448 :: pkcs8 :: { KeypairBytes , PublicKeyBytes } ;
2020
2121/// Ed448 secret key as defined in [RFC8032 § 5.2.5]
2222///
@@ -329,70 +329,6 @@ impl pkcs8::spki::DynSignatureAlgorithmIdentifier for SigningKey {
329329 }
330330}
331331
332- #[ cfg( feature = "pkcs8" ) ]
333- /// Keypair bytes for Ed448
334- #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
335- pub struct KeypairBytes {
336- /// The secret key bytes
337- pub secret_key : PointBytes ,
338- /// The public key bytes if included
339- pub verifying_key : Option < PointBytes > ,
340- }
341-
342- #[ cfg( all( feature = "alloc" , feature = "pkcs8" ) ) ]
343- impl pkcs8:: EncodePrivateKey for KeypairBytes {
344- fn to_pkcs8_der ( & self ) -> pkcs8:: Result < pkcs8:: SecretDocument > {
345- let verifying_key = self . verifying_key . as_ref ( ) ;
346- let public_key = verifying_key
347- . map ( |k| pkcs8:: der:: asn1:: BitStringRef :: from_bytes ( k) )
348- . transpose ( ) ?;
349- let private_key = pkcs8:: der:: asn1:: OctetStringRef :: new ( self . secret_key . as_ref ( ) ) ?;
350-
351- let private_key_info = pkcs8:: PrivateKeyInfoRef {
352- algorithm : super :: ALGORITHM_ID ,
353- private_key,
354- public_key,
355- } ;
356- let result = pkcs8:: SecretDocument :: encode_msg ( & private_key_info) ?;
357-
358- Ok ( result)
359- }
360- }
361-
362- #[ cfg( feature = "pkcs8" ) ]
363- impl TryFrom < pkcs8:: PrivateKeyInfoRef < ' _ > > for KeypairBytes {
364- type Error = pkcs8:: Error ;
365-
366- fn try_from ( value : pkcs8:: PrivateKeyInfoRef < ' _ > ) -> Result < Self , Self :: Error > {
367- if value. algorithm . oid != super :: ALGORITHM_OID {
368- return Err ( pkcs8:: Error :: KeyMalformed ) ;
369- }
370- if value. private_key . as_bytes ( ) . len ( ) != SECRET_KEY_LENGTH {
371- return Err ( pkcs8:: Error :: KeyMalformed ) ;
372- }
373- let mut secret_key = [ 0u8 ; SECRET_KEY_LENGTH ] ;
374- secret_key. copy_from_slice ( value. private_key . as_bytes ( ) ) ;
375- let verifying_key = if let Some ( public_key) = value. public_key {
376- if public_key. has_unused_bits ( ) {
377- return Err ( pkcs8:: Error :: KeyMalformed ) ;
378- }
379- let public_key = public_key. raw_bytes ( ) ;
380- if public_key. len ( ) != PUBLIC_KEY_LENGTH {
381- return Err ( pkcs8:: Error :: KeyMalformed ) ;
382- }
383- let mut bytes = [ 0u8 ; PUBLIC_KEY_LENGTH ] ;
384- bytes. copy_from_slice ( public_key) ;
385- Some ( bytes)
386- } else {
387- None
388- } ;
389- Ok ( KeypairBytes {
390- secret_key,
391- verifying_key,
392- } )
393- }
394- }
395-
396332#[ cfg( feature = "pkcs8" ) ]
397333impl TryFrom < KeypairBytes > for SigningKey {
398334 type Error = pkcs8:: Error ;
@@ -410,11 +346,11 @@ impl TryFrom<&KeypairBytes> for SigningKey {
410346 let signing_key =
411347 SigningKey :: from ( SecretKey :: try_from ( & value. secret_key [ ..] ) . expect ( "invalid length" ) ) ;
412348
413- if let Some ( public_bytes) = value. verifying_key {
414- let verifying_key =
415- VerifyingKey :: from_bytes ( & public_bytes ) . map_err ( |_| pkcs8:: Error :: KeyMalformed ) ?;
349+ if let Some ( public_bytes) = & value. public_key {
350+ let verifying_key = VerifyingKey :: from_bytes ( public_bytes . as_ref ( ) )
351+ . map_err ( |_| pkcs8:: KeyError :: Invalid ) ?;
416352 if signing_key. verifying_key ( ) != verifying_key {
417- return Err ( pkcs8:: Error :: KeyMalformed ) ;
353+ return Err ( pkcs8:: KeyError :: Invalid . into ( ) ) ;
418354 }
419355 }
420356 Ok ( signing_key)
@@ -425,8 +361,8 @@ impl TryFrom<&KeypairBytes> for SigningKey {
425361impl From < & SigningKey > for KeypairBytes {
426362 fn from ( signing_key : & SigningKey ) -> Self {
427363 KeypairBytes {
428- secret_key : PointBytes :: from ( signing_key. to_bytes ( ) ) ,
429- verifying_key : Some ( PointBytes :: from ( signing_key. verifying_key ( ) . to_bytes ( ) ) ) ,
364+ secret_key : signing_key. to_bytes ( ) . into ( ) ,
365+ public_key : Some ( PublicKeyBytes ( signing_key. verifying_key ( ) . to_bytes ( ) ) ) ,
430366 }
431367 }
432368}
0 commit comments