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::CryptoRng;
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::{Digest, array::typenum::U64},
28    edwards::{CompressedEdwardsY, EdwardsPoint},
29    scalar::Scalar,
30};
31
32use ed25519::signature::{KeypairRef, MultipartSigner, MultipartVerifier, 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    Signature,
46    constants::{KEYPAIR_LENGTH, SECRET_KEY_LENGTH},
47    errors::{InternalError, SignatureError},
48    hazmat::ExpandedSecretKey,
49    signature::InternalSignature,
50    verifying::VerifyingKey,
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 rand_core::TryRngCore;
192    /// use ed25519_dalek::{Signature, SigningKey};
193    ///
194    /// let mut csprng = OsRng.unwrap_err();
195    /// let signing_key: SigningKey = SigningKey::generate(&mut csprng);
196    /// # }
197    /// ```
198    ///
199    /// # Input
200    ///
201    /// A CSPRNG with a `fill_bytes()` method, e.g. `rand_os::OsRng`.
202    #[cfg(any(test, feature = "rand_core"))]
203    pub fn generate<R: CryptoRng + ?Sized>(csprng: &mut R) -> SigningKey {
204        let mut secret = SecretKey::default();
205        csprng.fill_bytes(&mut secret);
206        Self::from_bytes(&secret)
207    }
208
209    /// Sign a `prehashed_message` with this [`SigningKey`] using the
210    /// Ed25519ph algorithm defined in [RFC8032 §5.1][rfc8032].
211    ///
212    /// # Inputs
213    ///
214    /// * `prehashed_message` is an instantiated hash digest with 512-bits of
215    ///   output which has had the message to be signed previously fed into its
216    ///   state.
217    /// * `context` is an optional context string, up to 255 bytes inclusive,
218    ///   which may be used to provide additional domain separation.  If not
219    ///   set, this will default to an empty string.
220    ///
221    /// # Returns
222    ///
223    /// An Ed25519ph [`Signature`] on the `prehashed_message`.
224    ///
225    /// # Note
226    ///
227    /// The RFC only permits SHA-512 to be used for prehashing, i.e., `MsgDigest = Sha512`. This
228    /// function technically works, and is probably safe to use, with any secure hash function with
229    /// 512-bit digests, but anything outside of SHA-512 is NOT specification-compliant. We expose
230    /// [`crate::Sha512`] for user convenience.
231    ///
232    /// # Examples
233    ///
234    #[cfg_attr(all(feature = "rand_core", feature = "digest"), doc = "```")]
235    #[cfg_attr(
236        any(not(feature = "rand_core"), not(feature = "digest")),
237        doc = "```ignore"
238    )]
239    /// use ed25519_dalek::Digest;
240    /// use ed25519_dalek::SigningKey;
241    /// use ed25519_dalek::Signature;
242    /// use sha2::Sha512;
243    /// use rand::rngs::OsRng;
244    /// use rand_core::TryRngCore;
245    ///
246    /// # fn main() {
247    /// let mut csprng = OsRng.unwrap_err();
248    /// let signing_key: SigningKey = SigningKey::generate(&mut csprng);
249    /// let message: &[u8] = b"All I want is to pet all of the dogs.";
250    ///
251    /// // Create a hash digest object which we'll feed the message into:
252    /// let mut prehashed: Sha512 = Sha512::new();
253    ///
254    /// prehashed.update(message);
255    /// # }
256    /// ```
257    ///
258    /// If you want, you can optionally pass a "context".  It is generally a
259    /// good idea to choose a context and try to make it unique to your project
260    /// and this specific usage of signatures.
261    ///
262    /// For example, without this, if you were to [convert your OpenPGP key
263    /// to a Bitcoin key][terrible_idea] (just as an example, and also Don't
264    /// Ever Do That) and someone tricked you into signing an "email" which was
265    /// actually a Bitcoin transaction moving all your magic internet money to
266    /// their address, it'd be a valid transaction.
267    ///
268    /// By adding a context, this trick becomes impossible, because the context
269    /// is concatenated into the hash, which is then signed.  So, going with the
270    /// previous example, if your bitcoin wallet used a context of
271    /// "BitcoinWalletAppTxnSigning" and OpenPGP used a context (this is likely
272    /// the least of their safety problems) of "GPGsCryptoIsntConstantTimeLol",
273    /// then the signatures produced by both could never match the other, even
274    /// if they signed the exact same message with the same key.
275    ///
276    /// Let's add a context for good measure (remember, you'll want to choose
277    /// your own!):
278    ///
279    #[cfg_attr(all(feature = "rand_core", feature = "digest"), doc = "```")]
280    #[cfg_attr(
281        any(not(feature = "rand_core"), not(feature = "digest")),
282        doc = "```ignore"
283    )]
284    /// # use ed25519_dalek::Digest;
285    /// # use ed25519_dalek::SigningKey;
286    /// # use ed25519_dalek::Signature;
287    /// # use ed25519_dalek::SignatureError;
288    /// # use sha2::Sha512;
289    /// # use rand::rngs::OsRng;
290    /// # use rand_core::TryRngCore;
291    /// #
292    /// # fn do_test() -> Result<Signature, SignatureError> {
293    /// # let mut csprng = OsRng.unwrap_err();
294    /// # let signing_key: SigningKey = SigningKey::generate(&mut csprng);
295    /// # let message: &[u8] = b"All I want is to pet all of the dogs.";
296    /// # let mut prehashed: Sha512 = Sha512::new();
297    /// # prehashed.update(message);
298    /// #
299    /// let context: &[u8] = b"Ed25519DalekSignPrehashedDoctest";
300    ///
301    /// let sig: Signature = signing_key.sign_prehashed(prehashed, Some(context))?;
302    /// #
303    /// # Ok(sig)
304    /// # }
305    /// # fn main() {
306    /// #     do_test();
307    /// # }
308    /// ```
309    ///
310    /// [rfc8032]: https://tools.ietf.org/html/rfc8032#section-5.1
311    /// [terrible_idea]: https://github.com/isislovecruft/scripts/blob/master/gpgkey2bc.py
312    #[cfg(feature = "digest")]
313    pub fn sign_prehashed<MsgDigest>(
314        &self,
315        prehashed_message: MsgDigest,
316        context: Option<&[u8]>,
317    ) -> Result<Signature, SignatureError>
318    where
319        MsgDigest: Digest<OutputSize = U64>,
320    {
321        ExpandedSecretKey::from(&self.secret_key).raw_sign_prehashed::<Sha512, MsgDigest>(
322            prehashed_message,
323            &self.verifying_key,
324            context,
325        )
326    }
327
328    /// Verify a signature on a message with this signing key's public key.
329    pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
330        self.verifying_key.verify(message, signature)
331    }
332
333    /// Verify a `signature` on a `prehashed_message` using the Ed25519ph algorithm.
334    ///
335    /// # Inputs
336    ///
337    /// * `prehashed_message` is an instantiated hash digest with 512-bits of
338    ///   output which has had the message to be signed previously fed into its
339    ///   state.
340    /// * `context` is an optional context string, up to 255 bytes inclusive,
341    ///   which may be used to provide additional domain separation.  If not
342    ///   set, this will default to an empty string.
343    /// * `signature` is a purported Ed25519ph [`Signature`] on the `prehashed_message`.
344    ///
345    /// # Returns
346    ///
347    /// Returns `true` if the `signature` was a valid signature created by this
348    /// [`SigningKey`] on the `prehashed_message`.
349    ///
350    /// # Note
351    ///
352    /// The RFC only permits SHA-512 to be used for prehashing, i.e., `MsgDigest = Sha512`. This
353    /// function technically works, and is probably safe to use, with any secure hash function with
354    /// 512-bit digests, but anything outside of SHA-512 is NOT specification-compliant. We expose
355    /// [`crate::Sha512`] for user convenience.
356    ///
357    /// # Examples
358    ///
359    #[cfg_attr(all(feature = "rand_core", feature = "digest"), doc = "```")]
360    #[cfg_attr(
361        any(not(feature = "rand_core"), not(feature = "digest")),
362        doc = "```ignore"
363    )]
364    /// use ed25519_dalek::Digest;
365    /// use ed25519_dalek::SigningKey;
366    /// use ed25519_dalek::Signature;
367    /// use ed25519_dalek::SignatureError;
368    /// use sha2::Sha512;
369    /// use rand::rngs::OsRng;
370    /// use rand_core::TryRngCore;
371    ///
372    /// # fn do_test() -> Result<(), SignatureError> {
373    /// let mut csprng = OsRng.unwrap_err();
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    /// Constructs stream verifier with candidate `signature`.
487    ///
488    /// See [`VerifyingKey::verify_stream()`] for more details.
489    #[cfg(feature = "hazmat")]
490    pub fn verify_stream(
491        &self,
492        signature: &ed25519::Signature,
493    ) -> Result<StreamVerifier, SignatureError> {
494        self.verifying_key.verify_stream(signature)
495    }
496
497    /// Convert this signing key into a byte representation of an unreduced, unclamped Curve25519
498    /// scalar. This is NOT the same thing as `self.to_scalar().to_bytes()`, since `to_scalar()`
499    /// performs a clamping step, which changes the value of the resulting scalar.
500    ///
501    /// This can be used for performing X25519 Diffie-Hellman using Ed25519 keys. The bytes output
502    /// 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)
503    /// for the X25519 public key given by `self.verifying_key().to_montgomery()`.
504    ///
505    /// # Note
506    ///
507    /// We do NOT recommend using a signing/verifying key for encryption. Signing keys are usually
508    /// long-term keys, while keys used for key exchange should rather be ephemeral. If you can
509    /// help it, use a separate key for encryption.
510    ///
511    /// For more information on the security of systems which use the same keys for both signing
512    /// and Diffie-Hellman, see the paper
513    /// [On using the same key pair for Ed25519 and an X25519 based KEM](https://eprint.iacr.org/2021/509).
514    pub fn to_scalar_bytes(&self) -> [u8; 32] {
515        // Per the spec, the ed25519 secret key sk is expanded to
516        //     (scalar_bytes, hash_prefix) = SHA-512(sk)
517        // where the two outputs are both 32 bytes. scalar_bytes is what we return. Its clamped and
518        // reduced form is what we use for signing (see impl ExpandedSecretKey)
519        let mut buf = [0u8; 32];
520        let scalar_and_hash_prefix = Sha512::default().chain_update(self.secret_key).finalize();
521        buf.copy_from_slice(&scalar_and_hash_prefix[..32]);
522        buf
523    }
524
525    /// Convert this signing key into a Curve25519 scalar. This is computed by clamping and
526    /// reducing the output of [`Self::to_scalar_bytes`].
527    ///
528    /// This can be used anywhere where a Curve25519 scalar is used as a private key, e.g., in
529    /// [`crypto_box`](https://docs.rs/crypto_box/0.9.1/crypto_box/struct.SecretKey.html#impl-From%3CScalar%3E-for-SecretKey).
530    ///
531    /// # Note
532    ///
533    /// We do NOT recommend using a signing/verifying key for encryption. Signing keys are usually
534    /// long-term keys, while keys used for key exchange should rather be ephemeral. If you can
535    /// help it, use a separate key for encryption.
536    ///
537    /// For more information on the security of systems which use the same keys for both signing
538    /// and Diffie-Hellman, see the paper
539    /// [On using the same key pair for Ed25519 and an X25519 based KEM](https://eprint.iacr.org/2021/509).
540    pub fn to_scalar(&self) -> Scalar {
541        // Per the spec, the ed25519 secret key sk is expanded to
542        //     (scalar_bytes, hash_prefix) = SHA-512(sk)
543        // where the two outputs are both 32 bytes. To use for signing, scalar_bytes must be
544        // clamped and reduced (see ExpandedSecretKey::from_bytes). We return the clamped and
545        // reduced form.
546        ExpandedSecretKey::from(&self.secret_key).scalar
547    }
548}
549
550impl AsRef<VerifyingKey> for SigningKey {
551    fn as_ref(&self) -> &VerifyingKey {
552        &self.verifying_key
553    }
554}
555
556impl Debug for SigningKey {
557    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
558        f.debug_struct("SigningKey")
559            .field("verifying_key", &self.verifying_key)
560            .finish_non_exhaustive() // avoids printing `secret_key`
561    }
562}
563
564impl KeypairRef for SigningKey {
565    type VerifyingKey = VerifyingKey;
566}
567
568impl Signer<Signature> for SigningKey {
569    /// Sign a message with this signing key's secret key.
570    fn try_sign(&self, message: &[u8]) -> Result<Signature, SignatureError> {
571        self.try_multipart_sign(&[message])
572    }
573}
574
575impl MultipartSigner<Signature> for SigningKey {
576    fn try_multipart_sign(&self, message: &[&[u8]]) -> Result<Signature, SignatureError> {
577        let expanded: ExpandedSecretKey = (&self.secret_key).into();
578        Ok(expanded.raw_sign::<Sha512>(message, &self.verifying_key))
579    }
580}
581
582/// Equivalent to [`SigningKey::sign_prehashed`] with `context` set to [`None`].
583///
584/// # Note
585///
586/// The RFC only permits SHA-512 to be used for prehashing. This function technically works, and is
587/// probably safe to use, with any secure hash function with 512-bit digests, but anything outside
588/// of SHA-512 is NOT specification-compliant. We expose [`crate::Sha512`] for user convenience.
589#[cfg(feature = "digest")]
590impl<D> DigestSigner<D, Signature> for SigningKey
591where
592    D: Digest<OutputSize = U64>,
593{
594    fn try_sign_digest(&self, msg_digest: D) -> Result<Signature, SignatureError> {
595        self.sign_prehashed(msg_digest, None)
596    }
597}
598
599/// Equivalent to [`SigningKey::sign_prehashed`] with `context` set to [`Some`]
600/// containing `self.value()`.
601///
602/// # Note
603///
604/// The RFC only permits SHA-512 to be used for prehashing. This function technically works, and is
605/// probably safe to use, with any secure hash function with 512-bit digests, but anything outside
606/// of SHA-512 is NOT specification-compliant. We expose [`crate::Sha512`] for user convenience.
607#[cfg(feature = "digest")]
608impl<D> DigestSigner<D, Signature> for Context<'_, '_, SigningKey>
609where
610    D: Digest<OutputSize = U64>,
611{
612    fn try_sign_digest(&self, msg_digest: D) -> Result<Signature, SignatureError> {
613        self.key().sign_prehashed(msg_digest, Some(self.value()))
614    }
615}
616
617impl Verifier<Signature> for SigningKey {
618    /// Verify a signature on a message with this signing key's public key.
619    fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
620        self.verifying_key.verify(message, signature)
621    }
622}
623
624impl MultipartVerifier<Signature> for SigningKey {
625    fn multipart_verify(
626        &self,
627        message: &[&[u8]],
628        signature: &Signature,
629    ) -> Result<(), SignatureError> {
630        self.verifying_key.multipart_verify(message, signature)
631    }
632}
633
634impl From<SecretKey> for SigningKey {
635    #[inline]
636    fn from(secret: SecretKey) -> Self {
637        Self::from_bytes(&secret)
638    }
639}
640
641impl From<&SecretKey> for SigningKey {
642    #[inline]
643    fn from(secret: &SecretKey) -> Self {
644        Self::from_bytes(secret)
645    }
646}
647
648impl TryFrom<&[u8]> for SigningKey {
649    type Error = SignatureError;
650
651    fn try_from(bytes: &[u8]) -> Result<SigningKey, SignatureError> {
652        SecretKey::try_from(bytes)
653            .map(|bytes| Self::from_bytes(&bytes))
654            .map_err(|_| {
655                InternalError::BytesLength {
656                    name: "SecretKey",
657                    length: SECRET_KEY_LENGTH,
658                }
659                .into()
660            })
661    }
662}
663
664impl ConstantTimeEq for SigningKey {
665    fn ct_eq(&self, other: &Self) -> Choice {
666        self.secret_key.ct_eq(&other.secret_key)
667    }
668}
669
670impl PartialEq for SigningKey {
671    fn eq(&self, other: &Self) -> bool {
672        self.ct_eq(other).into()
673    }
674}
675
676impl Eq for SigningKey {}
677
678#[cfg(feature = "zeroize")]
679impl Drop for SigningKey {
680    fn drop(&mut self) {
681        self.secret_key.zeroize();
682    }
683}
684
685#[cfg(feature = "zeroize")]
686impl ZeroizeOnDrop for SigningKey {}
687
688#[cfg(all(feature = "alloc", feature = "pkcs8"))]
689impl pkcs8::EncodePrivateKey for SigningKey {
690    fn to_pkcs8_der(&self) -> pkcs8::Result<pkcs8::SecretDocument> {
691        pkcs8::KeypairBytes::from(self).to_pkcs8_der()
692    }
693}
694
695#[cfg(feature = "pkcs8")]
696impl TryFrom<pkcs8::KeypairBytes> for SigningKey {
697    type Error = pkcs8::Error;
698
699    fn try_from(pkcs8_key: pkcs8::KeypairBytes) -> pkcs8::Result<Self> {
700        SigningKey::try_from(&pkcs8_key)
701    }
702}
703
704#[cfg(feature = "pkcs8")]
705impl TryFrom<&pkcs8::KeypairBytes> for SigningKey {
706    type Error = pkcs8::Error;
707
708    fn try_from(pkcs8_key: &pkcs8::KeypairBytes) -> pkcs8::Result<Self> {
709        let signing_key = SigningKey::from_bytes(&pkcs8_key.secret_key);
710
711        // Validate the public key in the PKCS#8 document if present
712        if let Some(public_bytes) = &pkcs8_key.public_key {
713            let expected_verifying_key = VerifyingKey::from_bytes(public_bytes.as_ref())
714                .map_err(|_| pkcs8::Error::KeyMalformed)?;
715
716            if signing_key.verifying_key() != expected_verifying_key {
717                return Err(pkcs8::Error::KeyMalformed);
718            }
719        }
720
721        Ok(signing_key)
722    }
723}
724
725#[cfg(feature = "pkcs8")]
726impl pkcs8::spki::SignatureAlgorithmIdentifier for SigningKey {
727    type Params = pkcs8::spki::der::AnyRef<'static>;
728
729    const SIGNATURE_ALGORITHM_IDENTIFIER: pkcs8::spki::AlgorithmIdentifier<Self::Params> =
730        <Signature as pkcs8::spki::AssociatedAlgorithmIdentifier>::ALGORITHM_IDENTIFIER;
731}
732
733#[cfg(feature = "pkcs8")]
734impl From<SigningKey> for pkcs8::KeypairBytes {
735    fn from(signing_key: SigningKey) -> pkcs8::KeypairBytes {
736        pkcs8::KeypairBytes::from(&signing_key)
737    }
738}
739
740#[cfg(feature = "pkcs8")]
741impl From<&SigningKey> for pkcs8::KeypairBytes {
742    fn from(signing_key: &SigningKey) -> pkcs8::KeypairBytes {
743        pkcs8::KeypairBytes {
744            secret_key: signing_key.to_bytes(),
745            public_key: Some(pkcs8::PublicKeyBytes(signing_key.verifying_key.to_bytes())),
746        }
747    }
748}
749
750#[cfg(feature = "pkcs8")]
751impl TryFrom<pkcs8::PrivateKeyInfoRef<'_>> for SigningKey {
752    type Error = pkcs8::Error;
753
754    fn try_from(private_key: pkcs8::PrivateKeyInfoRef<'_>) -> pkcs8::Result<Self> {
755        pkcs8::KeypairBytes::try_from(private_key)?.try_into()
756    }
757}
758
759#[cfg(feature = "serde")]
760impl Serialize for SigningKey {
761    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
762    where
763        S: Serializer,
764    {
765        serializer.serialize_bytes(&self.secret_key)
766    }
767}
768
769#[cfg(feature = "serde")]
770impl<'d> Deserialize<'d> for SigningKey {
771    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
772    where
773        D: Deserializer<'d>,
774    {
775        struct SigningKeyVisitor;
776
777        impl<'de> serde::de::Visitor<'de> for SigningKeyVisitor {
778            type Value = SigningKey;
779
780            fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
781                write!(formatter, concat!("An ed25519 signing (private) key"))
782            }
783
784            fn visit_bytes<E: serde::de::Error>(self, bytes: &[u8]) -> Result<Self::Value, E> {
785                SigningKey::try_from(bytes).map_err(E::custom)
786            }
787
788            fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
789            where
790                A: serde::de::SeqAccess<'de>,
791            {
792                let mut bytes = [0u8; 32];
793                #[allow(clippy::needless_range_loop)]
794                for i in 0..32 {
795                    bytes[i] = seq
796                        .next_element()?
797                        .ok_or_else(|| serde::de::Error::invalid_length(i, &"expected 32 bytes"))?;
798                }
799
800                let remaining = (0..)
801                    .map(|_| seq.next_element::<u8>())
802                    .take_while(|el| matches!(el, Ok(Some(_))))
803                    .count();
804
805                if remaining > 0 {
806                    return Err(serde::de::Error::invalid_length(
807                        32 + remaining,
808                        &"expected 32 bytes",
809                    ));
810                }
811
812                Ok(SigningKey::from(bytes))
813            }
814        }
815
816        deserializer.deserialize_bytes(SigningKeyVisitor)
817    }
818}
819
820/// The spec-compliant way to define an expanded secret key. This computes `SHA512(sk)`, clamps the
821/// first 32 bytes and uses it as a scalar, and uses the second 32 bytes as a domain separator for
822/// hashing.
823impl From<&SecretKey> for ExpandedSecretKey {
824    #[allow(clippy::unwrap_used)]
825    fn from(secret_key: &SecretKey) -> ExpandedSecretKey {
826        let hash = Sha512::default().chain_update(secret_key).finalize();
827        ExpandedSecretKey::from_bytes(hash.as_ref())
828    }
829}
830
831//
832// Signing functions. These are pub(crate) so that the `hazmat` module can use them
833//
834
835impl ExpandedSecretKey {
836    /// The plain, non-prehashed, signing function for Ed25519. `CtxDigest` is the digest used to
837    /// calculate the pseudorandomness needed for signing. According to the spec, `CtxDigest =
838    /// Sha512`, and `self` is derived via the method defined in `impl From<&SigningKey> for
839    /// ExpandedSecretKey`.
840    ///
841    /// This definition is loose in its parameters so that end-users of the `hazmat` module can
842    /// change how the `ExpandedSecretKey` is calculated and which hash function to use.
843    #[allow(non_snake_case)]
844    #[allow(clippy::unwrap_used)]
845    #[inline(always)]
846    pub(crate) fn raw_sign<CtxDigest>(
847        &self,
848        message: &[&[u8]],
849        verifying_key: &VerifyingKey,
850    ) -> Signature
851    where
852        CtxDigest: Digest<OutputSize = U64>,
853    {
854        // OK unwrap, update can't fail.
855        self.raw_sign_byupdate(
856            |h: &mut CtxDigest| {
857                message.iter().for_each(|slice| h.update(slice));
858                Ok(())
859            },
860            verifying_key,
861        )
862        .unwrap()
863    }
864
865    /// Sign a message provided in parts. The `msg_update` closure will be called twice to hash the
866    /// message parts. This closure MUST leave its hasher in the same state (i.e., must hash the
867    /// same values) after both calls. Otherwise it will produce an invalid signature.
868    #[allow(non_snake_case)]
869    #[inline(always)]
870    pub(crate) fn raw_sign_byupdate<CtxDigest, F>(
871        &self,
872        msg_update: F,
873        verifying_key: &VerifyingKey,
874    ) -> Result<Signature, SignatureError>
875    where
876        CtxDigest: Digest<OutputSize = U64>,
877        F: Fn(&mut CtxDigest) -> Result<(), SignatureError>,
878    {
879        let mut h = CtxDigest::new();
880
881        h.update(self.hash_prefix);
882        msg_update(&mut h)?;
883
884        let r = Scalar::from_hash(h);
885        let R: CompressedEdwardsY = EdwardsPoint::mul_base(&r).compress();
886
887        h = CtxDigest::new();
888        h.update(R.as_bytes());
889        h.update(verifying_key.as_bytes());
890        msg_update(&mut h)?;
891
892        let k = Scalar::from_hash(h);
893        let s: Scalar = (k * self.scalar) + r;
894
895        Ok(InternalSignature { R, s }.into())
896    }
897
898    /// The prehashed signing function for Ed25519 (i.e., Ed25519ph). `CtxDigest` is the digest
899    /// function used to calculate the pseudorandomness needed for signing. `MsgDigest` is the
900    /// digest function used to hash the signed message. According to the spec, `MsgDigest =
901    /// CtxDigest = Sha512`, and `self` is derived via the method defined in `impl
902    /// From<&SigningKey> for ExpandedSecretKey`.
903    ///
904    /// This definition is loose in its parameters so that end-users of the `hazmat` module can
905    /// change how the `ExpandedSecretKey` is calculated and which `CtxDigest` function to use.
906    #[cfg(feature = "digest")]
907    #[allow(non_snake_case)]
908    #[inline(always)]
909    pub(crate) fn raw_sign_prehashed<CtxDigest, MsgDigest>(
910        &self,
911        prehashed_message: MsgDigest,
912        verifying_key: &VerifyingKey,
913        context: Option<&[u8]>,
914    ) -> Result<Signature, SignatureError>
915    where
916        CtxDigest: Digest<OutputSize = U64>,
917        MsgDigest: Digest<OutputSize = U64>,
918    {
919        let mut prehash: [u8; 64] = [0u8; 64];
920
921        let ctx: &[u8] = context.unwrap_or(b""); // By default, the context is an empty string.
922
923        if ctx.len() > 255 {
924            return Err(SignatureError::from(InternalError::PrehashedContextLength));
925        }
926
927        let ctx_len: u8 = ctx.len() as u8;
928
929        // Get the result of the pre-hashed message.
930        prehash.copy_from_slice(prehashed_message.finalize().as_slice());
931
932        // This is the dumbest, ten-years-late, non-admission of fucking up the
933        // domain separation I have ever seen.  Why am I still required to put
934        // the upper half "prefix" of the hashed "secret key" in here?  Why
935        // can't the user just supply their own nonce and decide for themselves
936        // whether or not they want a deterministic signature scheme?  Why does
937        // the message go into what's ostensibly the signature domain separation
938        // hash?  Why wasn't there always a way to provide a context string?
939        //
940        // ...
941        //
942        // This is a really fucking stupid bandaid, and the damned scheme is
943        // still bleeding from malleability, for fuck's sake.
944        let mut h = CtxDigest::new()
945            .chain_update(b"SigEd25519 no Ed25519 collisions")
946            .chain_update([1]) // Ed25519ph
947            .chain_update([ctx_len])
948            .chain_update(ctx)
949            .chain_update(self.hash_prefix)
950            .chain_update(&prehash[..]);
951
952        let r = Scalar::from_hash(h);
953        let R: CompressedEdwardsY = EdwardsPoint::mul_base(&r).compress();
954
955        h = CtxDigest::new()
956            .chain_update(b"SigEd25519 no Ed25519 collisions")
957            .chain_update([1]) // Ed25519ph
958            .chain_update([ctx_len])
959            .chain_update(ctx)
960            .chain_update(R.as_bytes())
961            .chain_update(verifying_key.as_bytes())
962            .chain_update(&prehash[..]);
963
964        let k = Scalar::from_hash(h);
965        let s: Scalar = (k * self.scalar) + r;
966
967        Ok(InternalSignature { R, s }.into())
968    }
969}