33use std:: {
44 borrow:: Borrow ,
55 cmp:: { Ord , PartialOrd } ,
6- fmt:: { Debug , Display } ,
6+ fmt:: { self , Debug , Display } ,
77 hash:: Hash ,
88 ops:: Deref ,
99 str:: FromStr ,
1010} ;
1111
1212use curve25519_dalek:: edwards:: CompressedEdwardsY ;
13- pub use ed25519_dalek:: { Signature , SignatureError } ;
1413use ed25519_dalek:: { SigningKey , VerifyingKey } ;
1514use nested_enum_utils:: common_fields;
1615use rand_core:: CryptoRng ;
@@ -100,24 +99,22 @@ impl<'de> Deserialize<'de> for PublicKey {
10099}
101100
102101impl PublicKey {
102+ /// The length of an ed25519 `PublicKey`, in bytes.
103+ pub const LENGTH : usize = ed25519_dalek:: PUBLIC_KEY_LENGTH ;
104+
103105 /// Get this public key as a byte array.
104106 pub fn as_bytes ( & self ) -> & [ u8 ; 32 ] {
105107 self . 0 . as_bytes ( )
106108 }
107109
108- /// Returns the [`VerifyingKey`] for this `PublicKey`.
109- pub fn public ( & self ) -> VerifyingKey {
110- VerifyingKey :: from_bytes ( self . 0 . as_bytes ( ) ) . expect ( "already verified" )
111- }
112-
113110 /// Construct a `PublicKey` from a slice of bytes.
114111 ///
115112 /// # Warning
116113 ///
117114 /// This will return a [`SignatureError`] if the bytes passed into this method do not represent
118115 /// a valid `ed25519_dalek` curve point. Will never fail for bytes return from [`Self::as_bytes`].
119116 /// See [`VerifyingKey::from_bytes`] for details.
120- pub fn from_bytes ( bytes : & [ u8 ; 32 ] ) -> Result < Self , SignatureError > {
117+ pub fn from_bytes ( bytes : & [ u8 ; 32 ] ) -> Result < Self , KeyParsingError > {
121118 let key = VerifyingKey :: from_bytes ( bytes) ?;
122119 let y = CompressedEdwardsY ( key. to_bytes ( ) ) ;
123120 Ok ( Self ( y) )
@@ -129,7 +126,9 @@ impl PublicKey {
129126 ///
130127 /// Returns `Ok(())` if the signature is valid, and `Err` otherwise.
131128 pub fn verify ( & self , message : & [ u8 ] , signature : & Signature ) -> Result < ( ) , SignatureError > {
132- self . public ( ) . verify_strict ( message, signature)
129+ self . as_verifying_key ( )
130+ . verify_strict ( message, & signature. 0 )
131+ . map_err ( |_| SignatureSnafu . build ( ) )
133132 }
134133
135134 /// Convert to a hex string limited to the first 5 bytes for a friendly string
@@ -142,8 +141,17 @@ impl PublicKey {
142141 )
143142 }
144143
145- /// The length of an ed25519 `PublicKey`, in bytes.
146- pub const LENGTH : usize = ed25519_dalek:: PUBLIC_KEY_LENGTH ;
144+ /// Needed for internal conversions, not part of the stable API.
145+ #[ doc( hidden) ]
146+ pub fn as_verifying_key ( & self ) -> VerifyingKey {
147+ VerifyingKey :: from_bytes ( self . 0 . as_bytes ( ) ) . expect ( "already verified" )
148+ }
149+
150+ /// Needed for internal conversions, not part of the stable API.
151+ #[ doc( hidden) ]
152+ pub fn from_verifying_key ( key : VerifyingKey ) -> Self {
153+ Self ( CompressedEdwardsY ( key. to_bytes ( ) ) )
154+ }
147155}
148156
149157struct PublicKeyShort ( [ u8 ; 5 ] ) ;
@@ -155,7 +163,7 @@ impl Display for PublicKeyShort {
155163}
156164
157165impl TryFrom < & [ u8 ] > for PublicKey {
158- type Error = SignatureError ;
166+ type Error = KeyParsingError ;
159167
160168 #[ inline]
161169 fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
@@ -165,7 +173,7 @@ impl TryFrom<&[u8]> for PublicKey {
165173}
166174
167175impl TryFrom < & [ u8 ; 32 ] > for PublicKey {
168- type Error = SignatureError ;
176+ type Error = KeyParsingError ;
169177
170178 #[ inline]
171179 fn try_from ( bytes : & [ u8 ; 32 ] ) -> Result < Self , Self :: Error > {
@@ -179,13 +187,6 @@ impl AsRef<[u8]> for PublicKey {
179187 }
180188}
181189
182- impl From < VerifyingKey > for PublicKey {
183- fn from ( verifying_key : VerifyingKey ) -> Self {
184- let key = verifying_key. to_bytes ( ) ;
185- PublicKey ( CompressedEdwardsY ( key) )
186- }
187- }
188-
189190impl Debug for PublicKey {
190191 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
191192 write ! (
@@ -234,15 +235,13 @@ impl FromStr for PublicKey {
234235 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
235236 let bytes = decode_base32_hex ( s) ?;
236237
237- Ok ( Self :: from_bytes ( & bytes) ? )
238+ Self :: from_bytes ( & bytes)
238239 }
239240}
240241
241242/// A secret key.
242- #[ derive( Clone ) ]
243- pub struct SecretKey {
244- secret : SigningKey ,
245- }
243+ #[ derive( Clone , zeroize:: ZeroizeOnDrop ) ]
244+ pub struct SecretKey ( SigningKey ) ;
246245
247246impl Debug for SecretKey {
248247 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
@@ -264,7 +263,7 @@ impl Serialize for SecretKey {
264263 where
265264 S : serde:: Serializer ,
266265 {
267- self . secret . serialize ( serializer)
266+ self . 0 . serialize ( serializer)
268267 }
269268}
270269
@@ -274,14 +273,15 @@ impl<'de> Deserialize<'de> for SecretKey {
274273 D : serde:: Deserializer < ' de > ,
275274 {
276275 let secret = SigningKey :: deserialize ( deserializer) ?;
277- Ok ( secret . into ( ) )
276+ Ok ( Self ( secret ) )
278277 }
279278}
280279
281280impl SecretKey {
282281 /// The public key of this [`SecretKey`].
283282 pub fn public ( & self ) -> PublicKey {
284- self . secret . verifying_key ( ) . into ( )
283+ let key = self . 0 . verifying_key ( ) . to_bytes ( ) ;
284+ PublicKey ( CompressedEdwardsY ( key) )
285285 }
286286
287287 /// Generate a new [`SecretKey`] with a randomness generator.
@@ -292,38 +292,33 @@ impl SecretKey {
292292 /// ```
293293 pub fn generate < R : CryptoRng + ?Sized > ( csprng : & mut R ) -> Self {
294294 let secret = SigningKey :: generate ( csprng) ;
295-
296- Self { secret }
295+ Self ( secret)
297296 }
298297
299298 /// Sign the given message and return a digital signature
300299 pub fn sign ( & self , msg : & [ u8 ] ) -> Signature {
301300 use ed25519_dalek:: Signer ;
302301
303- self . secret . sign ( msg)
302+ let sig = self . 0 . sign ( msg) ;
303+ Signature ( sig)
304304 }
305305
306306 /// Convert this to the bytes representing the secret part.
307307 /// The public part can always be recovered.
308308 pub fn to_bytes ( & self ) -> [ u8 ; 32 ] {
309- self . secret . to_bytes ( )
309+ self . 0 . to_bytes ( )
310310 }
311311
312312 /// Create a secret key from its byte representation.
313313 pub fn from_bytes ( bytes : & [ u8 ; 32 ] ) -> Self {
314314 let secret = SigningKey :: from_bytes ( bytes) ;
315- secret . into ( )
315+ Self ( secret )
316316 }
317317
318- /// Returns the [`SigningKey`] for this `SecretKey`.
319- pub fn secret ( & self ) -> & SigningKey {
320- & self . secret
321- }
322- }
323-
324- impl From < SigningKey > for SecretKey {
325- fn from ( secret : SigningKey ) -> Self {
326- SecretKey { secret }
318+ /// Needed for internal conversions, not part of the stable API.
319+ #[ doc( hidden) ]
320+ pub fn as_signing_key ( & self ) -> & SigningKey {
321+ & self . 0
327322 }
328323}
329324
@@ -334,15 +329,51 @@ impl From<[u8; 32]> for SecretKey {
334329}
335330
336331impl TryFrom < & [ u8 ] > for SecretKey {
337- type Error = SignatureError ;
332+ type Error = KeyParsingError ;
338333
339334 #[ inline]
340335 fn try_from ( bytes : & [ u8 ] ) -> Result < Self , Self :: Error > {
341336 let secret = SigningKey :: try_from ( bytes) ?;
342- Ok ( secret. into ( ) )
337+ Ok ( Self ( secret) )
338+ }
339+ }
340+
341+ /// Ed25519 signature.
342+ #[ derive( Copy , Clone , Eq , PartialEq ) ]
343+ pub struct Signature ( ed25519_dalek:: Signature ) ;
344+
345+ impl Debug for Signature {
346+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
347+ write ! ( f, "{:?}" , self . 0 )
348+ }
349+ }
350+
351+ impl Display for Signature {
352+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
353+ write ! ( f, "{}" , self . 0 )
343354 }
344355}
345356
357+ impl Signature {
358+ /// The length of an ed25519 `Signature`, in bytes.
359+ pub const LENGTH : usize = ed25519_dalek:: Signature :: BYTE_SIZE ;
360+
361+ /// Return the inner byte array.
362+ pub fn to_bytes ( & self ) -> [ u8 ; Self :: LENGTH ] {
363+ self . 0 . to_bytes ( )
364+ }
365+
366+ /// Parse an Ed25519 signature from a byte slice.
367+ pub fn from_bytes ( bytes : & [ u8 ; Self :: LENGTH ] ) -> Self {
368+ Self ( ed25519_dalek:: Signature :: from_bytes ( bytes) )
369+ }
370+ }
371+
372+ /// Verification of a signature failed.
373+ #[ derive( Debug , Snafu ) ]
374+ #[ snafu( display( "Invalid signature" ) ) ]
375+ pub struct SignatureError ;
376+
346377fn decode_base32_hex ( s : & str ) -> Result < [ u8 ; 32 ] , KeyParsingError > {
347378 let mut bytes = [ 0u8 ; 32 ] ;
348379
0 commit comments