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