ed25519_dalek/
signing.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 signing keys.
11
12use core::fmt::Debug;
13
14#[cfg(feature = "pkcs8")]
15use ed25519::pkcs8;
16
17#[cfg(any(test, feature = "rand_core"))]
18use rand_core::CryptoRngCore;
19
20#[cfg(feature = "serde")]
21use serde::{Deserialize, Deserializer, Serialize, Serializer};
22
23use sha2::Sha512;
24use subtle::{Choice, ConstantTimeEq};
25
26use curve25519_dalek::{
27    digest::{generic_array::typenum::U64, Digest},
28    edwards::{CompressedEdwardsY, EdwardsPoint},
29    scalar::Scalar,
30};
31
32use ed25519::signature::{KeypairRef, Signer, Verifier};
33
34#[cfg(feature = "digest")]
35use crate::context::Context;
36#[cfg(feature = "digest")]
37use signature::DigestSigner;
38
39#[cfg(feature = "zeroize")]
40use zeroize::{Zeroize, ZeroizeOnDrop};
41
42use crate::{
43    constants::{KEYPAIR_LENGTH, SECRET_KEY_LENGTH},
44    errors::{InternalError, SignatureError},
45    hazmat::ExpandedSecretKey,
46    signature::InternalSignature,
47    verifying::VerifyingKey,
48    Signature,
49};
50
51/// ed25519 secret key as defined in [RFC8032 § 5.1.5]:
52///
53/// > The private key is 32 octets (256 bits, corresponding to b) of
54/// > cryptographically secure random data.
55///
56/// [RFC8032 § 5.1.5]: https://www.rfc-editor.org/rfc/rfc8032#section-5.1.5
57pub type SecretKey = [u8; SECRET_KEY_LENGTH];
58
59/// ed25519 signing key which can be used to produce signatures.
60// Invariant: `verifying_key` is always the public key of
61// `secret_key`. This prevents the signing function oracle attack
62// described in https://github.com/MystenLabs/ed25519-unsafe-libs
63#[derive(Clone)]
64pub struct SigningKey {
65    /// The secret half of this signing key.
66    pub(crate) secret_key: SecretKey,
67    /// The public half of this signing key.
68    pub(crate) verifying_key: VerifyingKey,
69}
70
71/// # Example
72///
73/// ```
74/// # extern crate ed25519_dalek;
75/// #
76/// use ed25519_dalek::SigningKey;
77/// use ed25519_dalek::SECRET_KEY_LENGTH;
78/// use ed25519_dalek::SignatureError;
79///
80/// # fn doctest() -> Result<SigningKey, SignatureError> {
81/// let secret_key_bytes: [u8; SECRET_KEY_LENGTH] = [
82///    157, 097, 177, 157, 239, 253, 090, 096,
83///    186, 132, 074, 244, 146, 236, 044, 196,
84///    068, 073, 197, 105, 123, 050, 105, 025,
85///    112, 059, 172, 003, 028, 174, 127, 096, ];
86///
87/// let signing_key: SigningKey = SigningKey::from_bytes(&secret_key_bytes);
88/// assert_eq!(signing_key.to_bytes(), secret_key_bytes);
89///
90/// # Ok(signing_key)
91/// # }
92/// #
93/// # fn main() {
94/// #     let result = doctest();
95/// #     assert!(result.is_ok());
96/// # }
97/// ```
98impl SigningKey {
99    /// Construct a [`SigningKey`] from a [`SecretKey`]
100    ///
101    #[inline]
102    pub fn from_bytes(secret_key: &SecretKey) -> Self {
103        let verifying_key = VerifyingKey::from(&ExpandedSecretKey::from(secret_key));
104        Self {
105            secret_key: *secret_key,
106            verifying_key,
107        }
108    }
109
110    /// Convert this [`SigningKey`] into a [`SecretKey`]
111    #[inline]
112    pub fn to_bytes(&self) -> SecretKey {
113        self.secret_key
114    }
115
116    /// Convert this [`SigningKey`] into a [`SecretKey`] reference
117    #[inline]
118    pub fn as_bytes(&self) -> &SecretKey {
119        &self.secret_key
120    }
121
122    /// Construct a [`SigningKey`] from the bytes of a `VerifyingKey` and `SecretKey`.
123    ///
124    /// # Inputs
125    ///
126    /// * `bytes`: an `&[u8]` of length [`KEYPAIR_LENGTH`], representing the
127    ///   scalar for the secret key, and a compressed Edwards-Y coordinate of a
128    ///   point on curve25519, both as bytes. (As obtained from
129    ///   [`SigningKey::to_bytes`].)
130    ///
131    /// # Returns
132    ///
133    /// A `Result` whose okay value is an EdDSA [`SigningKey`] or whose error value
134    /// is an `SignatureError` describing the error that occurred.
135    #[inline]
136    pub fn from_keypair_bytes(bytes: &[u8; 64]) -> Result<SigningKey, SignatureError> {
137        let (secret_key, verifying_key) = bytes.split_at(SECRET_KEY_LENGTH);
138        let signing_key = SigningKey::try_from(secret_key)?;
139        let verifying_key = VerifyingKey::try_from(verifying_key)?;
140
141        if signing_key.verifying_key() != verifying_key {
142            return Err(InternalError::MismatchedKeypair.into());
143        }
144
145        Ok(signing_key)
146    }
147
148    /// Convert this signing key to a 64-byte keypair.
149    ///
150    /// # Returns
151    ///
152    /// An array of bytes, `[u8; KEYPAIR_LENGTH]`.  The first
153    /// `SECRET_KEY_LENGTH` of bytes is the `SecretKey`, and the next
154    /// `PUBLIC_KEY_LENGTH` bytes is the `VerifyingKey` (the same as other
155    /// libraries, such as [Adam Langley's ed25519 Golang
156    /// implementation](https://github.com/agl/ed25519/)). It is guaranteed that
157    /// the encoded public key is the one derived from the encoded secret key.
158    pub fn to_keypair_bytes(&self) -> [u8; KEYPAIR_LENGTH] {
159        let mut bytes: [u8; KEYPAIR_LENGTH] = [0u8; KEYPAIR_LENGTH];
160
161        bytes[..SECRET_KEY_LENGTH].copy_from_slice(&self.secret_key);
162        bytes[SECRET_KEY_LENGTH..].copy_from_slice(self.verifying_key.as_bytes());
163        bytes
164    }
165
166    /// Get the [`VerifyingKey`] for this [`SigningKey`].
167    pub fn verifying_key(&self) -> VerifyingKey {
168        self.verifying_key
169    }
170
171    /// Create a signing context that can be used for Ed25519ph with
172    /// [`DigestSigner`].
173    #[cfg(feature = "digest")]
174    pub fn with_context<'k, 'v>(
175        &'k self,
176        context_value: &'v [u8],
177    ) -> Result<Context<'k, 'v, Self>, SignatureError> {
178        Context::new(self, context_value)
179    }
180
181    /// Generate an ed25519 signing key.
182    ///
183    /// # Example
184    ///
185    #[cfg_attr(feature = "rand_core", doc = "```")]
186    #[cfg_attr(not(feature = "rand_core"), doc = "```ignore")]
187    /// # fn main() {
188    /// use rand::rngs::OsRng;
189    /// use ed25519_dalek::{Signature, SigningKey};
190    ///
191    /// let mut csprng = OsRng;
192    /// let signing_key: SigningKey = SigningKey::generate(&mut csprng);
193    /// # }
194    /// ```
195    ///
196    /// # Input
197    ///
198    /// A CSPRNG with a `fill_bytes()` method, e.g. `rand_os::OsRng`.
199    ///
200    /// The caller must also supply a hash function which implements the
201    /// `Digest` and `Default` traits, and which returns 512 bits of output.
202    /// The standard hash function used for most ed25519 libraries is SHA-512,
203    /// which is available with `use sha2::Sha512` as in the example above.
204    /// Other suitable hash functions include Keccak-512 and Blake2b-512.
205    #[cfg(any(test, feature = "rand_core"))]
206    pub fn generate<R: CryptoRngCore + ?Sized>(csprng: &mut R) -> SigningKey {
207        let mut secret = SecretKey::default();
208        csprng.fill_bytes(&mut secret);
209        Self::from_bytes(&secret)
210    }
211
212    /// Sign a `prehashed_message` with this [`SigningKey`] using the
213    /// Ed25519ph algorithm defined in [RFC8032 §5.1][rfc8032].
214    ///
215    /// # Inputs
216    ///
217    /// * `prehashed_message` is an instantiated hash digest with 512-bits of
218    ///   output which has had the message to be signed previously fed into its
219    ///   state.
220    /// * `context` is an optional context string, up to 255 bytes inclusive,
221    ///   which may be used to provide additional domain separation.  If not
222    ///   set, this will default to an empty string.
223    ///
224    /// # Returns
225    ///
226    /// An Ed25519ph [`Signature`] on the `prehashed_message`.
227    ///
228    /// # Note
229    ///
230    /// The RFC only permits SHA-512 to be used for prehashing, i.e., `MsgDigest = Sha512`. This
231    /// function technically works, and is probably safe to use, with any secure hash function with
232    /// 512-bit digests, but anything outside of SHA-512 is NOT specification-compliant. We expose
233    /// [`crate::Sha512`] for user convenience.
234    ///
235    /// # Examples
236    ///
237    #[cfg_attr(all(feature = "rand_core", feature = "digest"), doc = "```")]
238    #[cfg_attr(
239        any(not(feature = "rand_core"), not(feature = "digest")),
240        doc = "```ignore"
241    )]
242    /// use ed25519_dalek::Digest;
243    /// use ed25519_dalek::SigningKey;
244    /// use ed25519_dalek::Signature;
245    /// use sha2::Sha512;
246    /// use rand::rngs::OsRng;
247    ///
248    /// # fn main() {
249    /// let mut csprng = OsRng;
250    /// let signing_key: SigningKey = SigningKey::generate(&mut csprng);
251    /// let message: &[u8] = b"All I want is to pet all of the dogs.";
252    ///
253    /// // Create a hash digest object which we'll feed the message into:
254    /// let mut prehashed: Sha512 = Sha512::new();
255    ///
256    /// prehashed.update(message);
257    /// # }
258    /// ```
259    ///
260    /// If you want, you can optionally pass a "context".  It is generally a
261    /// good idea to choose a context and try to make it unique to your project
262    /// and this specific usage of signatures.
263    ///
264    /// For example, without this, if you were to [convert your OpenPGP key
265    /// to a Bitcoin key][terrible_idea] (just as an example, and also Don't
266    /// Ever Do That) and someone tricked you into signing an "email" which was
267    /// actually a Bitcoin transaction moving all your magic internet money to
268    /// their address, it'd be a valid transaction.
269    ///
270    /// By adding a context, this trick becomes impossible, because the context
271    /// is concatenated into the hash, which is then signed.  So, going with the
272    /// previous example, if your bitcoin wallet used a context of
273    /// "BitcoinWalletAppTxnSigning" and OpenPGP used a context (this is likely
274    /// the least of their safety problems) of "GPGsCryptoIsntConstantTimeLol",
275    /// then the signatures produced by both could never match the other, even
276    /// if they signed the exact same message with the same key.
277    ///
278    /// Let's add a context for good measure (remember, you'll want to choose
279    /// your own!):
280    ///
281    #[cfg_attr(all(feature = "rand_core", feature = "digest"), doc = "```")]
282    #[cfg_attr(
283        any(not(feature = "rand_core"), not(feature = "digest")),
284        doc = "```ignore"
285    )]
286    /// # use ed25519_dalek::Digest;
287    /// # use ed25519_dalek::SigningKey;
288    /// # use ed25519_dalek::Signature;
289    /// # use ed25519_dalek::SignatureError;
290    /// # use sha2::Sha512;
291    /// # use rand::rngs::OsRng;
292    /// #
293    /// # fn do_test() -> Result<Signature, SignatureError> {
294    /// # let mut csprng = OsRng;
295    /// # let signing_key: SigningKey = SigningKey::generate(&mut csprng);
296    /// # let message: &[u8] = b"All I want is to pet all of the dogs.";
297    /// # let mut prehashed: Sha512 = Sha512::new();
298    /// # prehashed.update(message);
299    /// #
300    /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
301    ///
302    /// let sig: Signature = signing_key.sign_prehashed(prehashed, Some(context))?;
303    /// #
304    /// # Ok(sig)
305    /// # }
306    /// # fn main() {
307    /// #     do_test();
308    /// # }
309    /// ```
310    ///
311    /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1
312    /// [terrible_idea]: https://github.com/isislovecruft/scripts/blob/master/gpgkey2bc.py
313    #[cfg(feature = "digest")]
314    pub fn sign_prehashed<MsgDigest>(
315        &self,
316        prehashed_message: MsgDigest,
317        context: Option<&[u8]>,
318    ) -> Result<Signature, SignatureError>
319    where
320        MsgDigest: Digest<OutputSize = U64>,
321    {
322        ExpandedSecretKey::from(&self.secret_key).raw_sign_prehashed::<Sha512, MsgDigest>(
323            prehashed_message,
324            &self.verifying_key,
325            context,
326        )
327    }
328
329    /// Verify a signature on a message with this signing key's public key.
330    pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
331        self.verifying_key.verify(message, signature)
332    }
333
334    /// Verify a `signature` on a `prehashed_message` using the Ed25519ph algorithm.
335    ///
336    /// # Inputs
337    ///
338    /// * `prehashed_message` is an instantiated hash digest with 512-bits of
339    ///   output which has had the message to be signed previously fed into its
340    ///   state.
341    /// * `context` is an optional context string, up to 255 bytes inclusive,
342    ///   which may be used to provide additional domain separation.  If not
343    ///   set, this will default to an empty string.
344    /// * `signature` is a purported Ed25519ph [`Signature`] on the `prehashed_message`.
345    ///
346    /// # Returns
347    ///
348    /// Returns `true` if the `signature` was a valid signature created by this
349    /// [`SigningKey`] on the `prehashed_message`.
350    ///
351    /// # Note
352    ///
353    /// The RFC only permits SHA-512 to be used for prehashing, i.e., `MsgDigest = Sha512`. This
354    /// function technically works, and is probably safe to use, with any secure hash function with
355    /// 512-bit digests, but anything outside of SHA-512 is NOT specification-compliant. We expose
356    /// [`crate::Sha512`] for user convenience.
357    ///
358    /// # Examples
359    ///
360    #[cfg_attr(all(feature = "rand_core", feature = "digest"), doc = "```")]
361    #[cfg_attr(
362        any(not(feature = "rand_core"), not(feature = "digest")),
363        doc = "```ignore"
364    )]
365    /// use ed25519_dalek::Digest;
366    /// use ed25519_dalek::SigningKey;
367    /// use ed25519_dalek::Signature;
368    /// use ed25519_dalek::SignatureError;
369    /// use sha2::Sha512;
370    /// use rand::rngs::OsRng;
371    ///
372    /// # fn do_test() -> Result<(), SignatureError> {
373    /// let mut csprng = OsRng;
374    /// let signing_key: SigningKey = SigningKey::generate(&mut csprng);
375    /// let message: &[u8] = b"All I want is to pet all of the dogs.";
376    ///
377    /// let mut prehashed: Sha512 = Sha512::new();
378    /// prehashed.update(message);
379    ///
380    /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
381    ///
382    /// let sig: Signature = signing_key.sign_prehashed(prehashed, Some(context))?;
383    ///
384    /// // The sha2::Sha512 struct doesn't implement Copy, so we'll have to create a new one:
385    /// let mut prehashed_again: Sha512 = Sha512::default();
386    /// prehashed_again.update(message);
387    ///
388    /// let verified = signing_key.verifying_key().verify_prehashed(prehashed_again, Some(context), &sig);
389    ///
390    /// assert!(verified.is_ok());
391    ///
392    /// # verified
393    /// # }
394    /// #
395    /// # fn main() {
396    /// #     do_test();
397    /// # }
398    /// ```
399    ///
400    /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1
401    #[cfg(feature = "digest")]
402    pub fn verify_prehashed<MsgDigest>(
403        &self,
404        prehashed_message: MsgDigest,
405        context: Option<&[u8]>,
406        signature: &Signature,
407    ) -> Result<(), SignatureError>
408    where
409        MsgDigest: Digest<OutputSize = U64>,
410    {
411        self.verifying_key
412            .verify_prehashed(prehashed_message, context, signature)
413    }
414
415    /// Strictly verify a signature on a message with this signing key's public key.
416    ///
417    /// # On The (Multiple) Sources of Malleability in Ed25519 Signatures
418    ///
419    /// This version of verification is technically non-RFC8032 compliant.  The
420    /// following explains why.
421    ///
422    /// 1. Scalar Malleability
423    ///
424    /// The authors of the RFC explicitly stated that verification of an ed25519
425    /// signature must fail if the scalar `s` is not properly reduced mod \ell:
426    ///
427    /// > To verify a signature on a message M using public key A, with F
428    /// > being 0 for Ed25519ctx, 1 for Ed25519ph, and if Ed25519ctx or
429    /// > Ed25519ph is being used, C being the context, first split the
430    /// > signature into two 32-octet halves.  Decode the first half as a
431    /// > point R, and the second half as an integer S, in the range
432    /// > 0 <= s < L.  Decode the public key A as point A'.  If any of the
433    /// > decodings fail (including S being out of range), the signature is
434    /// > invalid.)
435    ///
436    /// All `verify_*()` functions within ed25519-dalek perform this check.
437    ///
438    /// 2. Point malleability
439    ///
440    /// The authors of the RFC added in a malleability check to step #3 in
441    /// §5.1.7, for small torsion components in the `R` value of the signature,
442    /// *which is not strictly required*, as they state:
443    ///
444    /// > Check the group equation \[8\]\[S\]B = \[8\]R + \[8\]\[k\]A'.  It's
445    /// > sufficient, but not required, to instead check \[S\]B = R + \[k\]A'.
446    ///
447    /// # History of Malleability Checks
448    ///
449    /// As originally defined (cf. the "Malleability" section in the README of
450    /// this repo), ed25519 signatures didn't consider *any* form of
451    /// malleability to be an issue.  Later the scalar malleability was
452    /// considered important.  Still later, particularly with interests in
453    /// cryptocurrency design and in unique identities (e.g. for Signal users,
454    /// Tor onion services, etc.), the group element malleability became a
455    /// concern.
456    ///
457    /// However, libraries had already been created to conform to the original
458    /// definition.  One well-used library in particular even implemented the
459    /// group element malleability check, *but only for batch verification*!
460    /// Which meant that even using the same library, a single signature could
461    /// verify fine individually, but suddenly, when verifying it with a bunch
462    /// of other signatures, the whole batch would fail!
463    ///
464    /// # "Strict" Verification
465    ///
466    /// This method performs *both* of the above signature malleability checks.
467    ///
468    /// It must be done as a separate method because one doesn't simply get to
469    /// change the definition of a cryptographic primitive ten years
470    /// after-the-fact with zero consideration for backwards compatibility in
471    /// hardware and protocols which have it already have the older definition
472    /// baked in.
473    ///
474    /// # Return
475    ///
476    /// Returns `Ok(())` if the signature is valid, and `Err` otherwise.
477    #[allow(non_snake_case)]
478    pub fn verify_strict(
479        &self,
480        message: &[u8],
481        signature: &Signature,
482    ) -> Result<(), SignatureError> {
483        self.verifying_key.verify_strict(message, signature)
484    }
485
486    /// Convert this signing key into a byte representation of an unreduced, unclamped Curve25519
487    /// scalar. This is NOT the same thing as `self.to_scalar().to_bytes()`, since `to_scalar()`
488    /// performs a clamping step, which changes the value of the resulting scalar.
489    ///
490    /// This can be used for performing X25519 Diffie-Hellman using Ed25519 keys. The bytes output
491    /// by this function are a valid corresponding [`StaticSecret`](https://docs.rs/x25519-dalek/2.0.0/x25519_dalek/struct.StaticSecret.html#impl-From%3C%5Bu8;+32%5D%3E-for-StaticSecret)
492    /// for the X25519 public key given by `self.verifying_key().to_montgomery()`.
493    ///
494    /// # Note
495    ///
496    /// We do NOT recommend using a signing/verifying key for encryption. Signing keys are usually
497    /// long-term keys, while keys used for key exchange should rather be ephemeral. If you can
498    /// help it, use a separate key for encryption.
499    ///
500    /// For more information on the security of systems which use the same keys for both signing
501    /// and Diffie-Hellman, see the paper
502    /// [On using the same key pair for Ed25519 and an X25519 based KEM](https://eprint.iacr.org/2021/509).
503    pub fn to_scalar_bytes(&self) -> [u8; 32] {
504        // Per the spec, the ed25519 secret key sk is expanded to
505        //     (scalar_bytes, hash_prefix) = SHA-512(sk)
506        // where the two outputs are both 32 bytes. scalar_bytes is what we return. Its clamped and
507        // reduced form is what we use for signing (see impl ExpandedSecretKey)
508        let mut buf = [0u8; 32];
509        let scalar_and_hash_prefix = Sha512::default().chain_update(self.secret_key).finalize();
510        buf.copy_from_slice(&scalar_and_hash_prefix[..32]);
511        buf
512    }
513
514    /// Convert this signing key into a Curve25519 scalar. This is computed by clamping and
515    /// reducing the output of [`Self::to_scalar_bytes`].
516    ///
517    /// This can be used anywhere where a Curve25519 scalar is used as a private key, e.g., in
518    /// [`crypto_box`](https://docs.rs/crypto_box/0.9.1/crypto_box/struct.SecretKey.html#impl-From%3CScalar%3E-for-SecretKey).
519    ///
520    /// # Note
521    ///
522    /// We do NOT recommend using a signing/verifying key for encryption. Signing keys are usually
523    /// long-term keys, while keys used for key exchange should rather be ephemeral. If you can
524    /// help it, use a separate key for encryption.
525    ///
526    /// For more information on the security of systems which use the same keys for both signing
527    /// and Diffie-Hellman, see the paper
528    /// [On using the same key pair for Ed25519 and an X25519 based KEM](https://eprint.iacr.org/2021/509).
529    pub fn to_scalar(&self) -> Scalar {
530        // Per the spec, the ed25519 secret key sk is expanded to
531        //     (scalar_bytes, hash_prefix) = SHA-512(sk)
532        // where the two outputs are both 32 bytes. To use for signing, scalar_bytes must be
533        // clamped and reduced (see ExpandedSecretKey::from_bytes). We return the clamped and
534        // reduced form.
535        ExpandedSecretKey::from(&self.secret_key).scalar
536    }
537}
538
539impl AsRef<VerifyingKey> for SigningKey {
540    fn as_ref(&self) -> &VerifyingKey {
541        &self.verifying_key
542    }
543}
544
545impl Debug for SigningKey {
546    fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
547        f.debug_struct("SigningKey")
548            .field("verifying_key", &self.verifying_key)
549            .finish_non_exhaustive() // avoids printing `secret_key`
550    }
551}
552
553impl KeypairRef for SigningKey {
554    type VerifyingKey = VerifyingKey;
555}
556
557impl Signer<Signature> for SigningKey {
558    /// Sign a message with this signing key's secret key.
559    fn try_sign(&self, message: &[u8]) -> Result<Signature, SignatureError> {
560        let expanded: ExpandedSecretKey = (&self.secret_key).into();
561        Ok(expanded.raw_sign::<Sha512>(message, &self.verifying_key))
562    }
563}
564
565/// Equivalent to [`SigningKey::sign_prehashed`] with `context` set to [`None`].
566///
567/// # Note
568///
569/// The RFC only permits SHA-512 to be used for prehashing. This function technically works, and is
570/// probably safe to use, with any secure hash function with 512-bit digests, but anything outside
571/// of SHA-512 is NOT specification-compliant. We expose [`crate::Sha512`] for user convenience.
572#[cfg(feature = "digest")]
573impl<D> DigestSigner<D, Signature> for SigningKey
574where
575    D: Digest<OutputSize = U64>,
576{
577    fn try_sign_digest(&self, msg_digest: D) -> Result<Signature, SignatureError> {
578        self.sign_prehashed(msg_digest, None)
579    }
580}
581
582/// Equivalent to [`SigningKey::sign_prehashed`] with `context` set to [`Some`]
583/// containing `self.value()`.
584///
585/// # Note
586///
587/// The RFC only permits SHA-512 to be used for prehashing. This function technically works, and is
588/// probably safe to use, with any secure hash function with 512-bit digests, but anything outside
589/// of SHA-512 is NOT specification-compliant. We expose [`crate::Sha512`] for user convenience.
590#[cfg(feature = "digest")]
591impl<D> DigestSigner<D, Signature> for Context<'_, '_, SigningKey>
592where
593    D: Digest<OutputSize = U64>,
594{
595    fn try_sign_digest(&self, msg_digest: D) -> Result<Signature, SignatureError> {
596        self.key().sign_prehashed(msg_digest, Some(self.value()))
597    }
598}
599
600impl Verifier<Signature> for SigningKey {
601    /// Verify a signature on a message with this signing key's public key.
602    fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
603        self.verifying_key.verify(message, signature)
604    }
605}
606
607impl From<SecretKey> for SigningKey {
608    #[inline]
609    fn from(secret: SecretKey) -> Self {
610        Self::from_bytes(&secret)
611    }
612}
613
614impl From<&SecretKey> for SigningKey {
615    #[inline]
616    fn from(secret: &SecretKey) -> Self {
617        Self::from_bytes(secret)
618    }
619}
620
621impl TryFrom<&[u8]> for SigningKey {
622    type Error = SignatureError;
623
624    fn try_from(bytes: &[u8]) -> Result<SigningKey, SignatureError> {
625        SecretKey::try_from(bytes)
626            .map(|bytes| Self::from_bytes(&bytes))
627            .map_err(|_| {
628                InternalError::BytesLength {
629                    name: "SecretKey",
630                    length: SECRET_KEY_LENGTH,
631                }
632                .into()
633            })
634    }
635}
636
637impl ConstantTimeEq for SigningKey {
638    fn ct_eq(&self, other: &Self) -> Choice {
639        self.secret_key.ct_eq(&other.secret_key)
640    }
641}
642
643impl PartialEq for SigningKey {
644    fn eq(&self, other: &Self) -> bool {
645        self.ct_eq(other).into()
646    }
647}
648
649impl Eq for SigningKey {}
650
651#[cfg(feature = "zeroize")]
652impl Drop for SigningKey {
653    fn drop(&mut self) {
654        self.secret_key.zeroize();
655    }
656}
657
658#[cfg(feature = "zeroize")]
659impl ZeroizeOnDrop for SigningKey {}
660
661#[cfg(all(feature = "alloc", feature = "pkcs8"))]
662impl pkcs8::EncodePrivateKey for SigningKey {
663    fn to_pkcs8_der(&self) -> pkcs8::Result<pkcs8::SecretDocument> {
664        pkcs8::KeypairBytes::from(self).to_pkcs8_der()
665    }
666}
667
668#[cfg(feature = "pkcs8")]
669impl TryFrom<pkcs8::KeypairBytes> for SigningKey {
670    type Error = pkcs8::Error;
671
672    fn try_from(pkcs8_key: pkcs8::KeypairBytes) -> pkcs8::Result<Self> {
673        SigningKey::try_from(&pkcs8_key)
674    }
675}
676
677#[cfg(feature = "pkcs8")]
678impl TryFrom<&pkcs8::KeypairBytes> for SigningKey {
679    type Error = pkcs8::Error;
680
681    fn try_from(pkcs8_key: &pkcs8::KeypairBytes) -> pkcs8::Result<Self> {
682        let signing_key = SigningKey::from_bytes(&pkcs8_key.secret_key);
683
684        // Validate the public key in the PKCS#8 document if present
685        if let Some(public_bytes) = &pkcs8_key.public_key {
686            let expected_verifying_key = VerifyingKey::from_bytes(public_bytes.as_ref())
687                .map_err(|_| pkcs8::Error::KeyMalformed)?;
688
689            if signing_key.verifying_key() != expected_verifying_key {
690                return Err(pkcs8::Error::KeyMalformed);
691            }
692        }
693
694        Ok(signing_key)
695    }
696}
697
698#[cfg(feature = "pkcs8")]
699impl From<SigningKey> for pkcs8::KeypairBytes {
700    fn from(signing_key: SigningKey) -> pkcs8::KeypairBytes {
701        pkcs8::KeypairBytes::from(&signing_key)
702    }
703}
704
705#[cfg(feature = "pkcs8")]
706impl From<&SigningKey> for pkcs8::KeypairBytes {
707    fn from(signing_key: &SigningKey) -> pkcs8::KeypairBytes {
708        pkcs8::KeypairBytes {
709            secret_key: signing_key.to_bytes(),
710            public_key: Some(pkcs8::PublicKeyBytes(signing_key.verifying_key.to_bytes())),
711        }
712    }
713}
714
715#[cfg(feature = "pkcs8")]
716impl TryFrom<pkcs8::PrivateKeyInfo<'_>> for SigningKey {
717    type Error = pkcs8::Error;
718
719    fn try_from(private_key: pkcs8::PrivateKeyInfo<'_>) -> pkcs8::Result<Self> {
720        pkcs8::KeypairBytes::try_from(private_key)?.try_into()
721    }
722}
723
724#[cfg(feature = "serde")]
725impl Serialize for SigningKey {
726    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
727    where
728        S: Serializer,
729    {
730        serializer.serialize_bytes(&self.secret_key)
731    }
732}
733
734#[cfg(feature = "serde")]
735impl<'d> Deserialize<'d> for SigningKey {
736    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
737    where
738        D: Deserializer<'d>,
739    {
740        struct SigningKeyVisitor;
741
742        impl<'de> serde::de::Visitor<'de> for SigningKeyVisitor {
743            type Value = SigningKey;
744
745            fn expecting(&self, formatter: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
746                write!(formatter, concat!("An ed25519 signing (private) key"))
747            }
748
749            fn visit_bytes<E: serde::de::Error>(self, bytes: &[u8]) -> Result<Self::Value, E> {
750                SigningKey::try_from(bytes).map_err(E::custom)
751            }
752
753            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
754            where
755                A: serde::de::SeqAccess<'de>,
756            {
757                let mut bytes = [0u8; 32];
758                #[allow(clippy::needless_range_loop)]
759                for i in 0..32 {
760                    bytes[i] = seq
761                        .next_element()?
762                        .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
763                }
764
765                let remaining = (0..)
766                    .map(|_| seq.next_element::<u8>())
767                    .take_while(|el| matches!(el, Ok(Some(_))))
768                    .count();
769
770                if remaining > 0 {
771                    return Err(serde::de::Error::invalid_length(
772                        32 + remaining,
773                        &"expected 32 bytes",
774                    ));
775                }
776
777                SigningKey::try_from(bytes).map_err(serde::de::Error::custom)
778            }
779        }
780
781        deserializer.deserialize_bytes(SigningKeyVisitor)
782    }
783}
784
785/// The spec-compliant way to define an expanded secret key. This computes `SHA512(sk)`, clamps the
786/// first 32 bytes and uses it as a scalar, and uses the second 32 bytes as a domain separator for
787/// hashing.
788impl From<&SecretKey> for ExpandedSecretKey {
789    #[allow(clippy::unwrap_used)]
790    fn from(secret_key: &SecretKey) -> ExpandedSecretKey {
791        let hash = Sha512::default().chain_update(secret_key).finalize();
792        ExpandedSecretKey::from_bytes(hash.as_ref())
793    }
794}
795
796//
797// Signing functions. These are pub(crate) so that the `hazmat` module can use them
798//
799
800impl ExpandedSecretKey {
801    /// The plain, non-prehashed, signing function for Ed25519. `CtxDigest` is the digest used to
802    /// calculate the pseudorandomness needed for signing. According to the spec, `CtxDigest =
803    /// Sha512`, and `self` is derived via the method defined in `impl From<&SigningKey> for
804    /// ExpandedSecretKey`.
805    ///
806    /// This definition is loose in its parameters so that end-users of the `hazmat` module can
807    /// change how the `ExpandedSecretKey` is calculated and which hash function to use.
808    #[allow(non_snake_case)]
809    #[inline(always)]
810    pub(crate) fn raw_sign<CtxDigest>(
811        &self,
812        message: &[u8],
813        verifying_key: &VerifyingKey,
814    ) -> Signature
815    where
816        CtxDigest: Digest<OutputSize = U64>,
817    {
818        let mut h = CtxDigest::new();
819
820        h.update(self.hash_prefix);
821        h.update(message);
822
823        let r = Scalar::from_hash(h);
824        let R: CompressedEdwardsY = EdwardsPoint::mul_base(&r).compress();
825
826        h = CtxDigest::new();
827        h.update(R.as_bytes());
828        h.update(verifying_key.as_bytes());
829        h.update(message);
830
831        let k = Scalar::from_hash(h);
832        let s: Scalar = (k * self.scalar) + r;
833
834        InternalSignature { R, s }.into()
835    }
836
837    /// The prehashed signing function for Ed25519 (i.e., Ed25519ph). `CtxDigest` is the digest
838    /// function used to calculate the pseudorandomness needed for signing. `MsgDigest` is the
839    /// digest function used to hash the signed message. According to the spec, `MsgDigest =
840    /// CtxDigest = Sha512`, and `self` is derived via the method defined in `impl
841    /// From<&SigningKey> for ExpandedSecretKey`.
842    ///
843    /// This definition is loose in its parameters so that end-users of the `hazmat` module can
844    /// change how the `ExpandedSecretKey` is calculated and which `CtxDigest` function to use.
845    #[cfg(feature = "digest")]
846    #[allow(non_snake_case)]
847    #[inline(always)]
848    pub(crate) fn raw_sign_prehashed<CtxDigest, MsgDigest>(
849        &self,
850        prehashed_message: MsgDigest,
851        verifying_key: &VerifyingKey,
852        context: Option<&[u8]>,
853    ) -> Result<Signature, SignatureError>
854    where
855        CtxDigest: Digest<OutputSize = U64>,
856        MsgDigest: Digest<OutputSize = U64>,
857    {
858        let mut prehash: [u8; 64] = [0u8; 64];
859
860        let ctx: &[u8] = context.unwrap_or(b""); // By default, the context is an empty string.
861
862        if ctx.len() > 255 {
863            return Err(SignatureError::from(InternalError::PrehashedContextLength));
864        }
865
866        let ctx_len: u8 = ctx.len() as u8;
867
868        // Get the result of the pre-hashed message.
869        prehash.copy_from_slice(prehashed_message.finalize().as_slice());
870
871        // This is the dumbest, ten-years-late, non-admission of fucking up the
872        // domain separation I have ever seen.  Why am I still required to put
873        // the upper half "prefix" of the hashed "secret key" in here?  Why
874        // can't the user just supply their own nonce and decide for themselves
875        // whether or not they want a deterministic signature scheme?  Why does
876        // the message go into what's ostensibly the signature domain separation
877        // hash?  Why wasn't there always a way to provide a context string?
878        //
879        // ...
880        //
881        // This is a really fucking stupid bandaid, and the damned scheme is
882        // still bleeding from malleability, for fuck's sake.
883        let mut h = CtxDigest::new()
884            .chain_update(b"SigEd25519 no Ed25519 collisions")
885            .chain_update([1]) // Ed25519ph
886            .chain_update([ctx_len])
887            .chain_update(ctx)
888            .chain_update(self.hash_prefix)
889            .chain_update(&prehash[..]);
890
891        let r = Scalar::from_hash(h);
892        let R: CompressedEdwardsY = EdwardsPoint::mul_base(&r).compress();
893
894        h = CtxDigest::new()
895            .chain_update(b"SigEd25519 no Ed25519 collisions")
896            .chain_update([1]) // Ed25519ph
897            .chain_update([ctx_len])
898            .chain_update(ctx)
899            .chain_update(R.as_bytes())
900            .chain_update(verifying_key.as_bytes())
901            .chain_update(&prehash[..]);
902
903        let k = Scalar::from_hash(h);
904        let s: Scalar = (k * self.scalar) + r;
905
906        Ok(InternalSignature { R, s }.into())
907    }
908}