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