ed25519_dalek_blake2_feeless/
keypair.rs

1// -*- mode: rust; -*-
2//
3// This file is part of ed25519-dalek.
4// Copyright (c) 2017-2019 isis lovecruft
5// See LICENSE for licensing information.
6//
7// Authors:
8// - isis agora lovecruft <isis@patternsinthevoid.net>
9
10//! ed25519 keypairs.
11
12#[cfg(feature = "rand")]
13use rand::{CryptoRng, RngCore};
14
15#[cfg(feature = "serde")]
16use serde::de::Error as SerdeError;
17#[cfg(feature = "serde")]
18use serde::{Deserialize, Deserializer, Serialize, Serializer};
19#[cfg(feature = "serde")]
20use serde_bytes::{Bytes as SerdeBytes, ByteBuf as SerdeByteBuf};
21
22pub use sha2::Sha512;
23
24use curve25519_dalek::digest::generic_array::typenum::U64;
25pub use curve25519_dalek::digest::Digest;
26
27use ed25519::signature::{Signer, Verifier};
28
29use crate::constants::*;
30use crate::errors::*;
31use crate::public::*;
32use crate::secret::*;
33
34/// An ed25519 keypair.
35#[derive(Debug)]
36pub struct Keypair {
37    /// The secret half of this keypair.
38    pub secret: SecretKey,
39    /// The public half of this keypair.
40    pub public: PublicKey,
41}
42
43impl Keypair {
44    /// Convert this keypair to bytes.
45    ///
46    /// # Returns
47    ///
48    /// An array of bytes, `[u8; KEYPAIR_LENGTH]`.  The first
49    /// `SECRET_KEY_LENGTH` of bytes is the `SecretKey`, and the next
50    /// `PUBLIC_KEY_LENGTH` bytes is the `PublicKey` (the same as other
51    /// libraries, such as [Adam Langley's ed25519 Golang
52    /// implementation](https://github.com/agl/ed25519/)).
53    pub fn to_bytes(&self) -> [u8; KEYPAIR_LENGTH] {
54        let mut bytes: [u8; KEYPAIR_LENGTH] = [0u8; KEYPAIR_LENGTH];
55
56        bytes[..SECRET_KEY_LENGTH].copy_from_slice(self.secret.as_bytes());
57        bytes[SECRET_KEY_LENGTH..].copy_from_slice(self.public.as_bytes());
58        bytes
59    }
60
61    /// Construct a `Keypair` from the bytes of a `PublicKey` and `SecretKey`.
62    ///
63    /// # Inputs
64    ///
65    /// * `bytes`: an `&[u8]` representing the scalar for the secret key, and a
66    ///   compressed Edwards-Y coordinate of a point on curve25519, both as bytes.
67    ///   (As obtained from `Keypair::to_bytes()`.)
68    ///
69    /// # Warning
70    ///
71    /// Absolutely no validation is done on the key.  If you give this function
72    /// bytes which do not represent a valid point, or which do not represent
73    /// corresponding parts of the key, then your `Keypair` will be broken and
74    /// it will be your fault.
75    ///
76    /// # Returns
77    ///
78    /// A `Result` whose okay value is an EdDSA `Keypair` or whose error value
79    /// is an `SignatureError` describing the error that occurred.
80    pub fn from_bytes<'a>(bytes: &'a [u8]) -> Result<Keypair, SignatureError> {
81        if bytes.len() != KEYPAIR_LENGTH {
82            return Err(InternalError::BytesLengthError {
83                name: "Keypair",
84                length: KEYPAIR_LENGTH,
85            }.into());
86        }
87        let secret = SecretKey::from_bytes(&bytes[..SECRET_KEY_LENGTH])?;
88        let public = PublicKey::from_bytes(&bytes[SECRET_KEY_LENGTH..])?;
89
90        Ok(Keypair{ secret: secret, public: public })
91    }
92
93    /// Generate an ed25519 keypair.
94    ///
95    /// # Example
96    ///
97    /// ```
98    /// extern crate rand;
99    /// extern crate ed25519_dalek;
100    ///
101    /// # #[cfg(feature = "std")]
102    /// # fn main() {
103    ///
104    /// use rand::rngs::OsRng;
105    /// use ed25519_dalek::Keypair;
106    /// use ed25519_dalek::Signature;
107    ///
108    /// let mut csprng = OsRng{};
109    /// let keypair: Keypair = Keypair::generate(&mut csprng);
110    ///
111    /// # }
112    /// #
113    /// # #[cfg(not(feature = "std"))]
114    /// # fn main() { }
115    /// ```
116    ///
117    /// # Input
118    ///
119    /// A CSPRNG with a `fill_bytes()` method, e.g. `rand_os::OsRng`.
120    ///
121    /// The caller must also supply a hash function which implements the
122    /// `Digest` and `Default` traits, and which returns 512 bits of output.
123    /// The standard hash function used for most ed25519 libraries is SHA-512,
124    /// which is available with `use sha2::Sha512` as in the example above.
125    /// Other suitable hash functions include Keccak-512 and Blake2b-512.
126    #[cfg(feature = "rand")]
127    pub fn generate<R>(csprng: &mut R) -> Keypair
128    where
129        R: CryptoRng + RngCore,
130    {
131        let sk: SecretKey = SecretKey::generate(csprng);
132        let pk: PublicKey = (&sk).into();
133
134        Keypair{ public: pk, secret: sk }
135    }
136
137    /// Sign a `prehashed_message` with this `Keypair` using the
138    /// Ed25519ph algorithm defined in [RFC8032 §5.1][rfc8032].
139    ///
140    /// # Inputs
141    ///
142    /// * `prehashed_message` is an instantiated hash digest with 512-bits of
143    ///   output which has had the message to be signed previously fed into its
144    ///   state.
145    /// * `context` is an optional context string, up to 255 bytes inclusive,
146    ///   which may be used to provide additional domain separation.  If not
147    ///   set, this will default to an empty string.
148    ///
149    /// # Returns
150    ///
151    /// An Ed25519ph [`Signature`] on the `prehashed_message`.
152    ///
153    /// # Examples
154    ///
155    /// ```
156    /// extern crate ed25519_dalek;
157    /// extern crate rand;
158    ///
159    /// use ed25519_dalek::Digest;
160    /// use ed25519_dalek::Keypair;
161    /// use ed25519_dalek::Sha512;
162    /// use ed25519_dalek::Signature;
163    /// use rand::rngs::OsRng;
164    ///
165    /// # #[cfg(feature = "std")]
166    /// # fn main() {
167    /// let mut csprng = OsRng{};
168    /// let keypair: Keypair = Keypair::generate(&mut csprng);
169    /// let message: &[u8] = b"All I want is to pet all of the dogs.";
170    ///
171    /// // Create a hash digest object which we'll feed the message into:
172    /// let mut prehashed: Sha512 = Sha512::new();
173    ///
174    /// prehashed.update(message);
175    /// # }
176    /// #
177    /// # #[cfg(not(feature = "std"))]
178    /// # fn main() { }
179    /// ```
180    ///
181    /// If you want, you can optionally pass a "context".  It is generally a
182    /// good idea to choose a context and try to make it unique to your project
183    /// and this specific usage of signatures.
184    ///
185    /// For example, without this, if you were to [convert your OpenPGP key
186    /// to a Bitcoin key][terrible_idea] (just as an example, and also Don't
187    /// Ever Do That) and someone tricked you into signing an "email" which was
188    /// actually a Bitcoin transaction moving all your magic internet money to
189    /// their address, it'd be a valid transaction.
190    ///
191    /// By adding a context, this trick becomes impossible, because the context
192    /// is concatenated into the hash, which is then signed.  So, going with the
193    /// previous example, if your bitcoin wallet used a context of
194    /// "BitcoinWalletAppTxnSigning" and OpenPGP used a context (this is likely
195    /// the least of their safety problems) of "GPGsCryptoIsntConstantTimeLol",
196    /// then the signatures produced by both could never match the other, even
197    /// if they signed the exact same message with the same key.
198    ///
199    /// Let's add a context for good measure (remember, you'll want to choose
200    /// your own!):
201    ///
202    /// ```
203    /// # extern crate ed25519_dalek;
204    /// # extern crate rand;
205    /// #
206    /// # use ed25519_dalek::Digest;
207    /// # use ed25519_dalek::Keypair;
208    /// # use ed25519_dalek::Signature;
209    /// # use ed25519_dalek::SignatureError;
210    /// # use ed25519_dalek::Sha512;
211    /// # use rand::rngs::OsRng;
212    /// #
213    /// # fn do_test() -> Result<Signature, SignatureError> {
214    /// # let mut csprng = OsRng{};
215    /// # let keypair: Keypair = Keypair::generate(&mut csprng);
216    /// # let message: &[u8] = b"All I want is to pet all of the dogs.";
217    /// # let mut prehashed: Sha512 = Sha512::new();
218    /// # prehashed.update(message);
219    /// #
220    /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
221    ///
222    /// let sig: Signature = keypair.sign_prehashed(prehashed, Some(context))?;
223    /// #
224    /// # Ok(sig)
225    /// # }
226    /// # #[cfg(feature = "std")]
227    /// # fn main() {
228    /// #     do_test();
229    /// # }
230    /// #
231    /// # #[cfg(not(feature = "std"))]
232    /// # fn main() { }
233    /// ```
234    ///
235    /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1
236    /// [terrible_idea]: https://github.com/isislovecruft/scripts/blob/master/gpgkey2bc.py
237    pub fn sign_prehashed<D>(
238        &self,
239        prehashed_message: D,
240        context: Option<&[u8]>,
241    ) -> Result<ed25519::Signature, SignatureError>
242    where
243        D: Digest<OutputSize = U64>,
244    {
245        let expanded: ExpandedSecretKey = (&self.secret).into(); // xxx thanks i hate this
246
247        expanded.sign_prehashed(prehashed_message, &self.public, context).into()
248    }
249
250    /// Verify a signature on a message with this keypair's public key.
251    pub fn verify(
252        &self,
253        message: &[u8],
254        signature: &ed25519::Signature
255    ) -> Result<(), SignatureError>
256    {
257        self.public.verify(message, signature)
258    }
259
260    /// Verify a `signature` on a `prehashed_message` using the Ed25519ph algorithm.
261    ///
262    /// # Inputs
263    ///
264    /// * `prehashed_message` is an instantiated hash digest with 512-bits of
265    ///   output which has had the message to be signed previously fed into its
266    ///   state.
267    /// * `context` is an optional context string, up to 255 bytes inclusive,
268    ///   which may be used to provide additional domain separation.  If not
269    ///   set, this will default to an empty string.
270    /// * `signature` is a purported Ed25519ph [`Signature`] on the `prehashed_message`.
271    ///
272    /// # Returns
273    ///
274    /// Returns `true` if the `signature` was a valid signature created by this
275    /// `Keypair` on the `prehashed_message`.
276    ///
277    /// # Examples
278    ///
279    /// ```
280    /// extern crate ed25519_dalek;
281    /// extern crate rand;
282    ///
283    /// use ed25519_dalek::Digest;
284    /// use ed25519_dalek::Keypair;
285    /// use ed25519_dalek::Signature;
286    /// use ed25519_dalek::SignatureError;
287    /// use ed25519_dalek::Sha512;
288    /// use rand::rngs::OsRng;
289    ///
290    /// # fn do_test() -> Result<(), SignatureError> {
291    /// let mut csprng = OsRng{};
292    /// let keypair: Keypair = Keypair::generate(&mut csprng);
293    /// let message: &[u8] = b"All I want is to pet all of the dogs.";
294    ///
295    /// let mut prehashed: Sha512 = Sha512::new();
296    /// prehashed.update(message);
297    ///
298    /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
299    ///
300    /// let sig: Signature = keypair.sign_prehashed(prehashed, Some(context))?;
301    ///
302    /// // The sha2::Sha512 struct doesn't implement Copy, so we'll have to create a new one:
303    /// let mut prehashed_again: Sha512 = Sha512::default();
304    /// prehashed_again.update(message);
305    ///
306    /// let verified = keypair.public.verify_prehashed(prehashed_again, Some(context), &sig);
307    ///
308    /// assert!(verified.is_ok());
309    ///
310    /// # verified
311    /// # }
312    /// #
313    /// # #[cfg(feature = "std")]
314    /// # fn main() {
315    /// #     do_test();
316    /// # }
317    /// #
318    /// # #[cfg(not(feature = "std"))]
319    /// # fn main() { }
320    /// ```
321    ///
322    /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1
323    pub fn verify_prehashed<D>(
324        &self,
325        prehashed_message: D,
326        context: Option<&[u8]>,
327        signature: &ed25519::Signature,
328    ) -> Result<(), SignatureError>
329    where
330        D: Digest<OutputSize = U64>,
331    {
332        self.public.verify_prehashed(prehashed_message, context, signature)
333    }
334
335    /// Strictly verify a signature on a message with this keypair's public key.
336    ///
337    /// # On The (Multiple) Sources of Malleability in Ed25519 Signatures
338    ///
339    /// This version of verification is technically non-RFC8032 compliant.  The
340    /// following explains why.
341    ///
342    /// 1. Scalar Malleability
343    ///
344    /// The authors of the RFC explicitly stated that verification of an ed25519
345    /// signature must fail if the scalar `s` is not properly reduced mod \ell:
346    ///
347    /// > To verify a signature on a message M using public key A, with F
348    /// > being 0 for Ed25519ctx, 1 for Ed25519ph, and if Ed25519ctx or
349    /// > Ed25519ph is being used, C being the context, first split the
350    /// > signature into two 32-octet halves.  Decode the first half as a
351    /// > point R, and the second half as an integer S, in the range
352    /// > 0 <= s < L.  Decode the public key A as point A'.  If any of the
353    /// > decodings fail (including S being out of range), the signature is
354    /// > invalid.)
355    ///
356    /// All `verify_*()` functions within ed25519-dalek perform this check.
357    ///
358    /// 2. Point malleability
359    ///
360    /// The authors of the RFC added in a malleability check to step #3 in
361    /// §5.1.7, for small torsion components in the `R` value of the signature,
362    /// *which is not strictly required*, as they state:
363    ///
364    /// > Check the group equation \[8\]\[S\]B = \[8\]R + \[8\]\[k\]A'.  It's
365    /// > sufficient, but not required, to instead check \[S\]B = R + \[k\]A'.
366    ///
367    /// # History of Malleability Checks
368    ///
369    /// As originally defined (cf. the "Malleability" section in the README of
370    /// this repo), ed25519 signatures didn't consider *any* form of
371    /// malleability to be an issue.  Later the scalar malleability was
372    /// considered important.  Still later, particularly with interests in
373    /// cryptocurrency design and in unique identities (e.g. for Signal users,
374    /// Tor onion services, etc.), the group element malleability became a
375    /// concern.
376    ///
377    /// However, libraries had already been created to conform to the original
378    /// definition.  One well-used library in particular even implemented the
379    /// group element malleability check, *but only for batch verification*!
380    /// Which meant that even using the same library, a single signature could
381    /// verify fine individually, but suddenly, when verifying it with a bunch
382    /// of other signatures, the whole batch would fail!
383    ///
384    /// # "Strict" Verification
385    ///
386    /// This method performs *both* of the above signature malleability checks.
387    ///
388    /// It must be done as a separate method because one doesn't simply get to
389    /// change the definition of a cryptographic primitive ten years
390    /// after-the-fact with zero consideration for backwards compatibility in
391    /// hardware and protocols which have it already have the older definition
392    /// baked in.
393    ///
394    /// # Return
395    ///
396    /// Returns `Ok(())` if the signature is valid, and `Err` otherwise.
397    #[allow(non_snake_case)]
398    pub fn verify_strict(
399        &self,
400        message: &[u8],
401        signature: &ed25519::Signature,
402    ) -> Result<(), SignatureError>
403    {
404        self.public.verify_strict(message, signature)
405    }
406}
407
408impl Signer<ed25519::Signature> for Keypair {
409    /// Sign a message with this keypair's secret key.
410    fn try_sign(&self, message: &[u8]) -> Result<ed25519::Signature, SignatureError> {
411        let expanded: ExpandedSecretKey = (&self.secret).into();
412        Ok(expanded.sign(&message, &self.public).into())
413    }
414}
415
416impl Verifier<ed25519::Signature> for Keypair {
417    /// Verify a signature on a message with this keypair's public key.
418    fn verify(&self, message: &[u8], signature: &ed25519::Signature) -> Result<(), SignatureError> {
419        self.public.verify(message, signature)
420    }
421}
422
423#[cfg(feature = "serde")]
424impl Serialize for Keypair {
425    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
426    where
427        S: Serializer,
428    {
429        let bytes = &self.to_bytes()[..];
430        SerdeBytes::new(bytes).serialize(serializer)
431    }
432}
433
434#[cfg(feature = "serde")]
435impl<'d> Deserialize<'d> for Keypair {
436    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
437    where
438        D: Deserializer<'d>,
439    {
440        let bytes = <SerdeByteBuf>::deserialize(deserializer)?;
441        Keypair::from_bytes(bytes.as_ref()).map_err(SerdeError::custom)
442    }
443}