sequoia_openpgp/packet/key/
v4.rs

1//! OpenPGP v4 key packet.
2
3use std::fmt;
4use std::cmp::Ordering;
5use std::convert::TryInto;
6use std::hash::Hasher;
7use std::time;
8
9#[cfg(test)]
10use quickcheck::{Arbitrary, Gen};
11
12use crate::Error;
13use crate::crypto::{mem::Protected, mpi, hash::Hash, KeyPair};
14use crate::packet;
15use crate::packet::prelude::*;
16use crate::PublicKeyAlgorithm;
17use crate::SymmetricAlgorithm;
18use crate::HashAlgorithm;
19use crate::types::{
20    Curve,
21    Timestamp,
22};
23use crate::Result;
24use crate::crypto::Password;
25use crate::KeyID;
26use crate::Fingerprint;
27use crate::KeyHandle;
28use crate::packet::key::{
29    self,
30    KeyParts,
31    KeyRole,
32    KeyRoleRT,
33    PublicParts,
34    SecretParts,
35    UnspecifiedParts,
36};
37use crate::policy::HashAlgoSecurity;
38
39
40/// Holds a public key, public subkey, private key or private subkey
41/// packet.
42///
43/// Use [`Key4::generate_rsa`] or [`Key4::generate_ecc`] to create a
44/// new key.
45///
46/// Existing key material can be turned into an OpenPGP key using
47/// [`Key4::new`], [`Key4::with_secret`], [`Key4::import_public_cv25519`],
48/// [`Key4::import_public_ed25519`], [`Key4::import_public_rsa`],
49/// [`Key4::import_secret_cv25519`], [`Key4::import_secret_ed25519`],
50/// and [`Key4::import_secret_rsa`].
51///
52/// Whether you create a new key or import existing key material, you
53/// still need to create a binding signature, and, for signing keys, a
54/// back signature before integrating the key into a certificate.
55///
56/// Normally, you won't directly use `Key4`, but [`Key`], which is a
57/// relatively thin wrapper around `Key4`.
58///
59/// See [Section 5.5 of RFC 9580] and [the documentation for `Key`]
60/// for more details.
61///
62/// [Section 5.5 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.5
63/// [the documentation for `Key`]: super::Key
64/// [`Key`]: super::Key
65pub struct Key4<P, R>
66    where P: KeyParts, R: KeyRole
67{
68    /// CTB packet header fields.
69    pub(crate) common: packet::Common,
70    /// When the key was created.
71    pub(crate) creation_time: Timestamp,
72    /// Public key algorithm of this signature.
73    pk_algo: PublicKeyAlgorithm,
74    /// Public key MPIs.
75    mpis: mpi::PublicKey,
76    /// Optional secret part of the key.
77    pub(crate) secret: Option<SecretKeyMaterial>,
78
79    pub(crate) fingerprint: std::sync::OnceLock<Fingerprint>,
80
81    /// The key role tracked at run time.
82    role: KeyRoleRT,
83
84    p: std::marker::PhantomData<P>,
85    r: std::marker::PhantomData<R>,
86}
87
88// derive(Clone) doesn't work as expected with generic type parameters
89// that don't implement clone: it adds a trait bound on Clone to P and
90// R in the Clone implementation.  Happily, we don't need P or R to
91// implement Clone: they are just marker traits, which we can clone
92// manually.
93//
94// See: https://github.com/rust-lang/rust/issues/26925
95impl<P, R> Clone for Key4<P, R>
96    where P: KeyParts, R: KeyRole
97{
98    fn clone(&self) -> Self {
99        Key4 {
100            common: self.common.clone(),
101            creation_time: self.creation_time.clone(),
102            pk_algo: self.pk_algo.clone(),
103            mpis: self.mpis.clone(),
104            secret: self.secret.clone(),
105            fingerprint: self.fingerprint.get()
106                .map(|fp| fp.clone().into())
107                .unwrap_or_default(),
108            role: self.role,
109            p: std::marker::PhantomData,
110            r: std::marker::PhantomData,
111        }
112    }
113}
114
115assert_send_and_sync!(Key4<P, R> where P: KeyParts, R: KeyRole);
116
117impl<P: KeyParts, R: KeyRole> PartialEq for Key4<P, R> {
118    fn eq(&self, other: &Key4<P, R>) -> bool {
119        self.creation_time == other.creation_time
120            && self.pk_algo == other.pk_algo
121            && self.mpis == other.mpis
122            && (! P::significant_secrets() || self.secret == other.secret)
123    }
124}
125
126impl<P: KeyParts, R: KeyRole> Eq for Key4<P, R> {}
127
128impl<P: KeyParts, R: KeyRole> std::hash::Hash for Key4<P, R> {
129    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
130        std::hash::Hash::hash(&self.creation_time, state);
131        std::hash::Hash::hash(&self.pk_algo, state);
132        std::hash::Hash::hash(&self.mpis, state);
133        if P::significant_secrets() {
134            std::hash::Hash::hash(&self.secret, state);
135        }
136    }
137}
138
139impl<P, R> fmt::Debug for Key4<P, R>
140    where P: key::KeyParts,
141          R: key::KeyRole,
142{
143    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
144        f.debug_struct("Key4")
145            .field("fingerprint", &self.fingerprint())
146            .field("creation_time", &self.creation_time)
147            .field("pk_algo", &self.pk_algo)
148            .field("mpis", &self.mpis)
149            .field("secret", &self.secret)
150            .finish()
151    }
152}
153
154impl<P, R> fmt::Display for Key4<P, R>
155    where P: key::KeyParts,
156          R: key::KeyRole,
157{
158    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
159        write!(f, "{}", self.fingerprint())
160    }
161}
162
163impl<P, R> Key4<P, R>
164    where P: key::KeyParts,
165          R: key::KeyRole,
166{
167    /// The security requirements of the hash algorithm for
168    /// self-signatures.
169    ///
170    /// A cryptographic hash algorithm usually has [three security
171    /// properties]: pre-image resistance, second pre-image
172    /// resistance, and collision resistance.  If an attacker can
173    /// influence the signed data, then the hash algorithm needs to
174    /// have both second pre-image resistance, and collision
175    /// resistance.  If not, second pre-image resistance is
176    /// sufficient.
177    ///
178    ///   [three security properties]: https://en.wikipedia.org/wiki/Cryptographic_hash_function#Properties
179    ///
180    /// In general, an attacker may be able to influence third-party
181    /// signatures.  But direct key signatures, and binding signatures
182    /// are only over data fully determined by signer.  And, an
183    /// attacker's control over self signatures over User IDs is
184    /// limited due to their structure.
185    ///
186    /// These observations can be used to extend the life of a hash
187    /// algorithm after its collision resistance has been partially
188    /// compromised, but not completely broken.  For more details,
189    /// please refer to the documentation for [HashAlgoSecurity].
190    ///
191    ///   [HashAlgoSecurity]: crate::policy::HashAlgoSecurity
192    pub fn hash_algo_security(&self) -> HashAlgoSecurity {
193        HashAlgoSecurity::SecondPreImageResistance
194    }
195
196    /// Compares the public bits of two keys.
197    ///
198    /// This returns `Ordering::Equal` if the public MPIs, creation
199    /// time, and algorithm of the two `Key4`s match.  This does not
200    /// consider the packets' encodings, packets' tags or their secret
201    /// key material.
202    pub fn public_cmp<PB, RB>(&self, b: &Key4<PB, RB>) -> Ordering
203        where PB: key::KeyParts,
204              RB: key::KeyRole,
205    {
206        self.mpis.cmp(&b.mpis)
207            .then_with(|| self.creation_time.cmp(&b.creation_time))
208            .then_with(|| self.pk_algo.cmp(&b.pk_algo))
209    }
210
211    /// Tests whether two keys are equal modulo their secret key
212    /// material.
213    ///
214    /// This returns true if the public MPIs, creation time and
215    /// algorithm of the two `Key4`s match.  This does not consider
216    /// the packets' encodings, packets' tags or their secret key
217    /// material.
218    pub fn public_eq<PB, RB>(&self, b: &Key4<PB, RB>) -> bool
219        where PB: key::KeyParts,
220              RB: key::KeyRole,
221    {
222        self.public_cmp(b) == Ordering::Equal
223    }
224
225    /// Hashes everything but any secret key material into state.
226    ///
227    /// This is an alternate implementation of [`Hash`], which never
228    /// hashes the secret key material.
229    ///
230    ///   [`Hash`]: std::hash::Hash
231    pub fn public_hash<H>(&self, state: &mut H)
232        where H: Hasher
233    {
234        use std::hash::Hash;
235
236        self.common.hash(state);
237        self.creation_time.hash(state);
238        self.pk_algo.hash(state);
239        Hash::hash(&self.mpis(), state);
240    }
241}
242
243impl<P, R> Key4<P, R>
244     where P: key::KeyParts,
245           R: key::KeyRole,
246{
247    /// Gets the `Key`'s creation time.
248    pub fn creation_time(&self) -> time::SystemTime {
249        self.creation_time.into()
250    }
251
252    /// Gets the `Key`'s creation time without converting it to a
253    /// system time.
254    ///
255    /// This conversion may truncate the time to signed 32-bit time_t.
256    pub(crate) fn creation_time_raw(&self) -> Timestamp {
257        self.creation_time
258    }
259
260    /// Sets the `Key`'s creation time.
261    ///
262    /// `timestamp` is converted to OpenPGP's internal format,
263    /// [`Timestamp`]: a 32-bit quantity containing the number of
264    /// seconds since the Unix epoch.
265    ///
266    /// `timestamp` is silently rounded to match the internal
267    /// resolution.  An error is returned if `timestamp` is out of
268    /// range.
269    ///
270    /// [`Timestamp`]: crate::types::Timestamp
271    pub fn set_creation_time<T>(&mut self, timestamp: T)
272                                -> Result<time::SystemTime>
273        where T: Into<time::SystemTime>
274    {
275        // Clear the cache.
276        self.fingerprint = Default::default();
277
278        Ok(std::mem::replace(&mut self.creation_time,
279                             timestamp.into().try_into()?)
280           .into())
281    }
282
283    /// Gets the public key algorithm.
284    pub fn pk_algo(&self) -> PublicKeyAlgorithm {
285        self.pk_algo
286    }
287
288    /// Sets the public key algorithm.
289    ///
290    /// Returns the old public key algorithm.
291    pub fn set_pk_algo(&mut self, pk_algo: PublicKeyAlgorithm)
292        -> PublicKeyAlgorithm
293    {
294        // Clear the cache.
295        self.fingerprint = Default::default();
296
297        ::std::mem::replace(&mut self.pk_algo, pk_algo)
298    }
299
300    /// Returns a reference to the `Key`'s MPIs.
301    pub fn mpis(&self) -> &mpi::PublicKey {
302        &self.mpis
303    }
304
305    /// Returns a mutable reference to the `Key`'s MPIs.
306    pub fn mpis_mut(&mut self) -> &mut mpi::PublicKey {
307        // Clear the cache.
308        self.fingerprint = Default::default();
309
310        &mut self.mpis
311    }
312
313    /// Sets the `Key`'s MPIs.
314    ///
315    /// This function returns the old MPIs, if any.
316    pub fn set_mpis(&mut self, mpis: mpi::PublicKey) -> mpi::PublicKey {
317        // Clear the cache.
318        self.fingerprint = Default::default();
319
320        ::std::mem::replace(&mut self.mpis, mpis)
321    }
322
323    /// Returns whether the `Key` contains secret key material.
324    pub fn has_secret(&self) -> bool {
325        self.secret.is_some()
326    }
327
328    /// Returns whether the `Key` contains unencrypted secret key
329    /// material.
330    ///
331    /// This returns false if the `Key` doesn't contain any secret key
332    /// material.
333    pub fn has_unencrypted_secret(&self) -> bool {
334        matches!(self.secret, Some(SecretKeyMaterial::Unencrypted { .. }))
335    }
336
337    /// Returns `Key`'s secret key material, if any.
338    pub fn optional_secret(&self) -> Option<&SecretKeyMaterial> {
339        self.secret.as_ref()
340    }
341
342    /// Computes and returns the `Key`'s `Fingerprint` and returns it as
343    /// a `KeyHandle`.
344    ///
345    /// See [Section 5.5.4 of RFC 9580].
346    ///
347    /// [Section 5.5.4 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.5.4
348    pub fn key_handle(&self) -> KeyHandle {
349        self.fingerprint().into()
350    }
351
352    /// Computes and returns the `Key`'s `Fingerprint`.
353    ///
354    /// See [Key IDs and Fingerprints].
355    ///
356    /// [Key IDs and Fingerprints]: https://www.rfc-editor.org/rfc/rfc9580.html#key-ids-fingerprints
357    pub fn fingerprint(&self) -> Fingerprint {
358        self.fingerprint.get_or_init(|| {
359            let mut h = HashAlgorithm::SHA1.context()
360                .expect("SHA1 is MTI for RFC4880")
361            // v4 fingerprints are computed the same way a key is
362            // hashed for v4 signatures.
363                .for_signature(4);
364
365            self.hash(&mut h).expect("v4 key hashing is infallible");
366
367            let mut digest = [0u8; 20];
368            let _ = h.digest(&mut digest);
369            Fingerprint::V4(digest)
370        }).clone()
371    }
372
373    /// Computes and returns the `Key`'s `Key ID`.
374    ///
375    /// See [Section 5.5.4 of RFC 9580].
376    ///
377    /// [Section 5.5.4 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.5.4
378    pub fn keyid(&self) -> KeyID {
379        self.fingerprint().into()
380    }
381
382    /// Creates an OpenPGP public key from the specified key material.
383    ///
384    /// This is an internal version for parse.rs that avoids going
385    /// through SystemTime.
386    pub(crate) fn make(creation_time: Timestamp,
387                       pk_algo: PublicKeyAlgorithm,
388                       mpis: mpi::PublicKey,
389                       secret: Option<SecretKeyMaterial>)
390                       -> Result<Self>
391    {
392        Ok(Key4 {
393            common: Default::default(),
394            creation_time,
395            pk_algo,
396            mpis,
397            secret,
398            fingerprint: Default::default(),
399            role: R::role(),
400            p: std::marker::PhantomData,
401            r: std::marker::PhantomData,
402        })
403    }
404
405    pub(crate) fn role(&self) -> KeyRoleRT {
406        self.role
407    }
408
409    pub(crate) fn set_role(&mut self, role: KeyRoleRT) {
410        self.role = role;
411    }
412}
413
414impl<R> Key4<key::PublicParts, R>
415    where R: key::KeyRole,
416{
417    /// Creates an OpenPGP public key from the specified key material.
418    pub fn new<T>(creation_time: T, pk_algo: PublicKeyAlgorithm,
419                  mpis: mpi::PublicKey)
420                  -> Result<Self>
421        where T: Into<time::SystemTime>
422    {
423        Ok(Key4 {
424            common: Default::default(),
425            creation_time: creation_time.into().try_into()?,
426            pk_algo,
427            mpis,
428            secret: None,
429            fingerprint: Default::default(),
430            role: R::role(),
431            p: std::marker::PhantomData,
432            r: std::marker::PhantomData,
433        })
434    }
435
436    /// Creates an OpenPGP public key packet from existing X25519 key
437    /// material.
438    ///
439    /// The ECDH key will use hash algorithm `hash` and symmetric
440    /// algorithm `sym`.  If one or both are `None` secure defaults
441    /// will be used.  The key will have its creation date set to
442    /// `ctime` or the current time if `None` is given.
443    pub fn import_public_cv25519<H, S, T>(public_key: &[u8],
444                                          hash: H, sym: S, ctime: T)
445        -> Result<Self> where H: Into<Option<HashAlgorithm>>,
446                              S: Into<Option<SymmetricAlgorithm>>,
447                              T: Into<Option<time::SystemTime>>
448    {
449        let mut point = Vec::from(public_key);
450        point.insert(0, 0x40);
451
452        use crate::crypto::ecdh;
453        Self::new(
454            ctime.into().unwrap_or_else(crate::now),
455            PublicKeyAlgorithm::ECDH,
456            mpi::PublicKey::ECDH {
457                curve: Curve::Cv25519,
458                hash: hash.into().unwrap_or_else(
459                    || ecdh::default_ecdh_kdf_hash(&Curve::Cv25519)),
460                sym: sym.into().unwrap_or_else(
461                    || ecdh::default_ecdh_kek_cipher(&Curve::Cv25519)),
462                q: mpi::MPI::new(&point),
463            })
464    }
465
466    /// Creates an OpenPGP public key packet from existing Ed25519 key
467    /// material.
468    ///
469    /// The key will have its creation date set to `ctime` or the
470    /// current time if `None` is given.
471    pub fn import_public_ed25519<T>(public_key: &[u8], ctime: T) -> Result<Self>
472        where  T: Into<Option<time::SystemTime>>
473    {
474        let mut point = Vec::from(public_key);
475        point.insert(0, 0x40);
476
477        Self::new(
478            ctime.into().unwrap_or_else(crate::now),
479            PublicKeyAlgorithm::EdDSA,
480            mpi::PublicKey::EdDSA {
481                curve: Curve::Ed25519,
482                q: mpi::MPI::new(&point),
483            })
484    }
485
486    /// Creates an OpenPGP public key packet from existing RSA key
487    /// material.
488    ///
489    /// The RSA key will use the public exponent `e` and the modulo
490    /// `n`. The key will have its creation date set to `ctime` or the
491    /// current time if `None` is given.
492    pub fn import_public_rsa<T>(e: &[u8], n: &[u8], ctime: T)
493        -> Result<Self> where T: Into<Option<time::SystemTime>>
494    {
495        Self::new(
496            ctime.into().unwrap_or_else(crate::now),
497            PublicKeyAlgorithm::RSAEncryptSign,
498            mpi::PublicKey::RSA {
499                e: mpi::MPI::new(e),
500                n: mpi::MPI::new(n),
501            })
502    }
503}
504
505impl<R> Key4<SecretParts, R>
506    where R: key::KeyRole,
507{
508    /// Creates an OpenPGP key packet from the specified secret key
509    /// material.
510    pub fn with_secret<T>(creation_time: T, pk_algo: PublicKeyAlgorithm,
511                          mpis: mpi::PublicKey,
512                          secret: SecretKeyMaterial)
513                          -> Result<Self>
514        where T: Into<time::SystemTime>
515    {
516        Ok(Key4 {
517            common: Default::default(),
518            creation_time: creation_time.into().try_into()?,
519            pk_algo,
520            mpis,
521            secret: Some(secret),
522            fingerprint: Default::default(),
523            role: R::role(),
524            p: std::marker::PhantomData,
525            r: std::marker::PhantomData,
526        })
527    }
528
529    /// Creates a new OpenPGP secret key packet for an existing X25519 key.
530    ///
531    /// The ECDH key will use hash algorithm `hash` and symmetric
532    /// algorithm `sym`.  If one or both are `None` secure defaults
533    /// will be used.  The key will have its creation date set to
534    /// `ctime` or the current time if `None` is given.
535    ///
536    /// The given `private_key` is expected to be in the native X25519
537    /// representation, i.e. as opaque byte string of length 32.  It
538    /// is transformed into OpenPGP's representation during import.
539    pub fn import_secret_cv25519<H, S, T>(private_key: &[u8],
540                                          hash: H, sym: S, ctime: T)
541        -> Result<Self> where H: Into<Option<HashAlgorithm>>,
542                              S: Into<Option<SymmetricAlgorithm>>,
543                              T: Into<Option<std::time::SystemTime>>
544    {
545        use crate::crypto::backend::{Backend, interface::Asymmetric};
546
547        let mut private_key = Protected::from(private_key);
548        let public_key = Backend::x25519_derive_public(&private_key)?;
549
550        // Clamp the X25519 secret key scalar.
551        //
552        // X25519 does the clamping implicitly, but OpenPGP's ECDH
553        // over Curve25519 requires the secret to be clamped.  To
554        // increase compatibility with OpenPGP implementations that do
555        // not implicitly clamp the secrets before use, we do that
556        // before we store the secrets in OpenPGP data structures.
557        Backend::x25519_clamp_secret(&mut private_key);
558
559        // Reverse the scalar.
560        //
561        // X25519 stores the secret as opaque byte string representing
562        // a little-endian scalar.  OpenPGP's ECDH over Curve25519 on
563        // the other hand stores it as big-endian scalar, as was
564        // customary in OpenPGP.  See
565        // https://lists.gnupg.org/pipermail/gnupg-devel/2018-February/033437.html.
566        private_key.reverse();
567
568        use crate::crypto::ecdh;
569        Self::with_secret(
570            ctime.into().unwrap_or_else(crate::now),
571            PublicKeyAlgorithm::ECDH,
572            mpi::PublicKey::ECDH {
573                curve: Curve::Cv25519,
574                hash: hash.into().unwrap_or_else(
575                    || ecdh::default_ecdh_kdf_hash(&Curve::Cv25519)),
576                sym: sym.into().unwrap_or_else(
577                    || ecdh::default_ecdh_kek_cipher(&Curve::Cv25519)),
578                q: mpi::MPI::new_compressed_point(&public_key),
579            },
580            mpi::SecretKeyMaterial::ECDH {
581                scalar: private_key.into(),
582            }.into())
583    }
584
585    /// Creates a new OpenPGP secret key packet for an existing Ed25519 key.
586    ///
587    /// The key will have its creation date set to `ctime` or the current
588    /// time if `None` is given.
589    pub fn import_secret_ed25519<T>(private_key: &[u8], ctime: T)
590        -> Result<Self> where T: Into<Option<time::SystemTime>>
591    {
592        use crate::crypto::backend::{Backend, interface::Asymmetric};
593
594        let private_key = Protected::from(private_key);
595        let public_key = Backend::ed25519_derive_public(&private_key)?;
596
597        Self::with_secret(
598            ctime.into().unwrap_or_else(crate::now),
599            PublicKeyAlgorithm::EdDSA,
600            mpi::PublicKey::EdDSA {
601                curve: Curve::Ed25519,
602                q: mpi::MPI::new_compressed_point(&public_key),
603            },
604            mpi::SecretKeyMaterial::EdDSA {
605                scalar: private_key.into(),
606            }.into())
607    }
608
609    /// Generates a new ECC key over `curve`.
610    ///
611    /// If `for_signing` is false a ECDH key, if it's true either a
612    /// EdDSA or ECDSA key is generated.  Giving `for_signing == true` and
613    /// `curve == Cv25519` will produce an error. Likewise
614    /// `for_signing == false` and `curve == Ed25519` will produce an error.
615    pub fn generate_ecc(for_signing: bool, curve: Curve) -> Result<Self> {
616        use crate::crypto::backend::{Backend, interface::Asymmetric};
617
618        let (pk_algo, public, secret) = match (curve, for_signing) {
619            (Curve::Ed25519, true) => {
620                let (secret, public) = Backend::ed25519_generate_key()?;
621
622                (
623                    PublicKeyAlgorithm::EdDSA,
624                    mpi::PublicKey::EdDSA {
625                        curve: Curve::Ed25519,
626                        q: mpi::MPI::new_compressed_point(&public),
627                    },
628                    mpi::SecretKeyMaterial::EdDSA {
629                        scalar: secret.into(),
630                    },
631                )
632            },
633
634            (Curve::Cv25519, false) => {
635                let (mut secret, public) = Backend::x25519_generate_key()?;
636
637                // Clamp the X25519 secret key scalar.
638                //
639                // X25519 does the clamping implicitly, but OpenPGP's ECDH over
640                // Curve25519 requires the secret to be clamped.  To increase
641                // compatibility with OpenPGP implementations that do not
642                // implicitly clamp the secrets before use, we do that before we
643                // store the secrets in OpenPGP data structures.
644                Backend::x25519_clamp_secret(&mut secret);
645
646                // Reverse the scalar.
647                //
648                // X25519 stores the secret as opaque byte string
649                // representing a little-endian scalar.  OpenPGP's
650                // ECDH over Curve25519 on the other hand stores it as
651                // big-endian scalar, as was customary in OpenPGP.
652                // See
653                // https://lists.gnupg.org/pipermail/gnupg-devel/2018-February/033437.html.
654                secret.reverse();
655
656                (
657                    PublicKeyAlgorithm::ECDH,
658                    mpi::PublicKey::ECDH {
659                        curve: Curve::Cv25519,
660                        q: mpi::MPI::new_compressed_point(&public),
661                        hash: crate::crypto::ecdh::default_ecdh_kdf_hash(
662                            &Curve::Cv25519),
663                        sym: crate::crypto::ecdh::default_ecdh_kek_cipher(
664                            &Curve::Cv25519),
665                    },
666                    mpi::SecretKeyMaterial::ECDH {
667                        scalar: secret.into(),
668                    },
669                )
670            },
671
672            (curve, for_signing) =>
673                Self::generate_ecc_backend(for_signing, curve)?,
674        };
675
676        Self::with_secret(crate::now(), pk_algo, public, secret.into())
677    }
678
679    /// Generates a new DSA key with a public modulus of size `p_bits`.
680    ///
681    /// Note: In order to comply with FIPS 186-4, and to increase
682    /// compatibility with implementations, you SHOULD only generate
683    /// keys with moduli of size `2048` or `3072` bits.
684    pub fn generate_dsa(p_bits: usize) -> Result<Self> {
685        use crate::crypto::backend::{Backend, interface::Asymmetric};
686
687        let (p, q, g, y, x) = Backend::dsa_generate_key(p_bits)?;
688        let public_mpis = mpi::PublicKey::DSA { p, q, g, y };
689        let private_mpis = mpi::SecretKeyMaterial::DSA { x };
690
691        Self::with_secret(
692            crate::now(),
693            #[allow(deprecated)]
694            PublicKeyAlgorithm::DSA,
695            public_mpis,
696            private_mpis.into())
697    }
698
699    /// Generates a new ElGamal key with a public modulus of size `p_bits`.
700    ///
701    /// Note: ElGamal is no longer well-supported in cryptographic
702    /// libraries and should be avoided.
703    pub fn generate_elgamal(p_bits: usize) -> Result<Self> {
704        use crate::crypto::backend::{Backend, interface::Asymmetric};
705
706        let (p, g, y, x) = Backend::elgamal_generate_key(p_bits)?;
707        let public_mpis = mpi::PublicKey::ElGamal { p, g, y };
708        let private_mpis = mpi::SecretKeyMaterial::ElGamal { x };
709
710        Self::with_secret(
711            crate::now(),
712            #[allow(deprecated)]
713            PublicKeyAlgorithm::ElGamalEncrypt,
714            public_mpis,
715            private_mpis.into())
716    }
717
718    /// Generates a new X25519 key.
719    pub fn generate_x25519() -> Result<Self> {
720        use crate::crypto::backend::{Backend, interface::Asymmetric};
721
722        let (private, public) = Backend::x25519_generate_key()?;
723
724        Self::with_secret(
725            crate::now(),
726            PublicKeyAlgorithm::X25519,
727            mpi::PublicKey::X25519 {
728                u: public,
729            },
730            mpi::SecretKeyMaterial::X25519 {
731                x: private,
732            }.into())
733    }
734
735    /// Generates a new X448 key.
736    pub fn generate_x448() -> Result<Self> {
737        use crate::crypto::backend::{Backend, interface::Asymmetric};
738
739        let (private, public) = Backend::x448_generate_key()?;
740
741        Self::with_secret(
742            crate::now(),
743            PublicKeyAlgorithm::X448,
744            mpi::PublicKey::X448 {
745                u: Box::new(public),
746            },
747            mpi::SecretKeyMaterial::X448 {
748                x: private,
749            }.into())
750    }
751
752    /// Generates a new Ed25519 key.
753    pub fn generate_ed25519() -> Result<Self> {
754        use crate::crypto::backend::{Backend, interface::Asymmetric};
755
756        let (private, public) = Backend::ed25519_generate_key()?;
757
758        Self::with_secret(
759            crate::now(),
760            PublicKeyAlgorithm::Ed25519,
761            mpi::PublicKey::Ed25519 {
762                a: public,
763            },
764            mpi::SecretKeyMaterial::Ed25519 {
765                x: private,
766            }.into())
767    }
768
769    /// Generates a new Ed448 key.
770    pub fn generate_ed448() -> Result<Self> {
771        use crate::crypto::backend::{Backend, interface::Asymmetric};
772
773        let (private, public) = Backend::ed448_generate_key()?;
774
775        Self::with_secret(
776            crate::now(),
777            PublicKeyAlgorithm::Ed448,
778            mpi::PublicKey::Ed448 {
779                a: Box::new(public),
780            },
781            mpi::SecretKeyMaterial::Ed448 {
782                x: private,
783            }.into())
784    }
785
786    /// Creates a new key pair from a secret `Key` with an unencrypted
787    /// secret key.
788    ///
789    /// # Errors
790    ///
791    /// Fails if the secret key is encrypted.  You can use
792    /// [`Key::decrypt_secret`] to decrypt a key.
793    pub fn into_keypair(self) -> Result<KeyPair> {
794        let (key, secret) = self.take_secret();
795        let secret = match secret {
796            SecretKeyMaterial::Unencrypted(secret) => secret,
797            SecretKeyMaterial::Encrypted(_) =>
798                return Err(Error::InvalidArgument(
799                    "secret key material is encrypted".into()).into()),
800        };
801
802        KeyPair::new(key.role_into_unspecified().into(), secret)
803    }
804}
805
806macro_rules! impl_common_secret_functions {
807    ($t: ident) => {
808        /// Secret key material handling.
809        impl<R> Key4<$t, R>
810            where R: key::KeyRole,
811        {
812            /// Takes the `Key`'s `SecretKeyMaterial`, if any.
813            pub fn take_secret(mut self)
814                               -> (Key4<PublicParts, R>, Option<SecretKeyMaterial>)
815            {
816                let old = std::mem::replace(&mut self.secret, None);
817                (self.parts_into_public(), old)
818            }
819
820            /// Adds the secret key material to the `Key`, returning
821            /// the old secret key material, if any.
822            pub fn add_secret(mut self, secret: SecretKeyMaterial)
823                              -> (Key4<SecretParts, R>, Option<SecretKeyMaterial>)
824            {
825                let old = std::mem::replace(&mut self.secret, Some(secret));
826                (self.parts_into_secret().expect("secret just set"), old)
827            }
828
829            /// Takes the `Key`'s `SecretKeyMaterial`, if any.
830            pub fn steal_secret(&mut self) -> Option<SecretKeyMaterial>
831            {
832                std::mem::replace(&mut self.secret, None)
833            }
834        }
835    }
836}
837impl_common_secret_functions!(PublicParts);
838impl_common_secret_functions!(UnspecifiedParts);
839
840/// Secret key handling.
841impl<R> Key4<SecretParts, R>
842    where R: key::KeyRole,
843{
844    /// Gets the `Key`'s `SecretKeyMaterial`.
845    pub fn secret(&self) -> &SecretKeyMaterial {
846        self.secret.as_ref().expect("has secret")
847    }
848
849    /// Gets a mutable reference to the `Key`'s `SecretKeyMaterial`.
850    pub fn secret_mut(&mut self) -> &mut SecretKeyMaterial {
851        self.secret.as_mut().expect("has secret")
852    }
853
854    /// Takes the `Key`'s `SecretKeyMaterial`.
855    pub fn take_secret(mut self)
856                       -> (Key4<PublicParts, R>, SecretKeyMaterial)
857    {
858        let old = std::mem::replace(&mut self.secret, None);
859        (self.parts_into_public(),
860         old.expect("Key<SecretParts, _> has a secret key material"))
861    }
862
863    /// Adds `SecretKeyMaterial` to the `Key`.
864    ///
865    /// This function returns the old secret key material, if any.
866    pub fn add_secret(mut self, secret: SecretKeyMaterial)
867                      -> (Key4<SecretParts, R>, SecretKeyMaterial)
868    {
869        let old = std::mem::replace(&mut self.secret, Some(secret));
870        (self.parts_into_secret().expect("secret just set"),
871         old.expect("Key<SecretParts, _> has a secret key material"))
872    }
873
874    /// Decrypts the secret key material using `password`.
875    ///
876    /// In OpenPGP, secret key material can be [protected with a
877    /// password].  The password is usually hardened using a [KDF].
878    ///
879    /// Refer to the documentation of [`Key::decrypt_secret`] for
880    /// details.
881    ///
882    /// This function returns an error if the secret key material is
883    /// not encrypted or the password is incorrect.
884    ///
885    /// [protected with a password]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.5.3
886    /// [KDF]: https://www.rfc-editor.org/rfc/rfc9580.html#section-3.7
887    /// [`Key::decrypt_secret`]: super::Key::decrypt_secret()
888    pub fn decrypt_secret(self, password: &Password) -> Result<Self> {
889        let (key, mut secret) = self.take_secret();
890        let key = Key::V4(key);
891        secret.decrypt_in_place(&key, password)?;
892        let key = if let Key::V4(k) = key { k } else { unreachable!() };
893        Ok(key.add_secret(secret).0)
894    }
895
896    /// Encrypts the secret key material using `password`.
897    ///
898    /// In OpenPGP, secret key material can be [protected with a
899    /// password].  The password is usually hardened using a [KDF].
900    ///
901    /// Refer to the documentation of [`Key::encrypt_secret`] for
902    /// details.
903    ///
904    /// This returns an error if the secret key material is already
905    /// encrypted.
906    ///
907    /// [protected with a password]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.5.3
908    /// [KDF]: https://www.rfc-editor.org/rfc/rfc9580.html#section-3.7
909    /// [`Key::encrypt_secret`]: super::Key::encrypt_secret()
910    pub fn encrypt_secret(self, password: &Password)
911        -> Result<Key4<SecretParts, R>>
912    {
913        let (key, mut secret) = self.take_secret();
914        let key = Key::V4(key);
915        secret.encrypt_in_place(&key, password)?;
916        let key = if let Key::V4(k) = key { k } else { unreachable!() };
917        Ok(key.add_secret(secret).0)
918    }
919}
920
921impl<P, R> From<Key4<P, R>> for super::Key<P, R>
922    where P: key::KeyParts,
923          R: key::KeyRole,
924{
925    fn from(p: Key4<P, R>) -> Self {
926        super::Key::V4(p)
927    }
928}
929
930#[cfg(test)]
931use crate::packet::key::{
932    PrimaryRole,
933    SubordinateRole,
934    UnspecifiedRole,
935};
936
937#[cfg(test)]
938impl Arbitrary for Key4<PublicParts, PrimaryRole> {
939    fn arbitrary(g: &mut Gen) -> Self {
940        Key4::<PublicParts, UnspecifiedRole>::arbitrary(g).into()
941    }
942}
943
944#[cfg(test)]
945impl Arbitrary for Key4<PublicParts, SubordinateRole> {
946    fn arbitrary(g: &mut Gen) -> Self {
947        Key4::<PublicParts, UnspecifiedRole>::arbitrary(g).into()
948    }
949}
950
951#[cfg(test)]
952impl Arbitrary for Key4<PublicParts, UnspecifiedRole> {
953    fn arbitrary(g: &mut Gen) -> Self {
954        let mpis = mpi::PublicKey::arbitrary(g);
955        Key4 {
956            common: Arbitrary::arbitrary(g),
957            creation_time: Arbitrary::arbitrary(g),
958            pk_algo: mpis.algo()
959                .expect("mpi::PublicKey::arbitrary only uses known algos"),
960            mpis,
961            secret: None,
962            fingerprint: Default::default(),
963            role: UnspecifiedRole::role(),
964            p: std::marker::PhantomData,
965            r: std::marker::PhantomData,
966        }
967    }
968}
969
970#[cfg(test)]
971impl Arbitrary for Key4<SecretParts, PrimaryRole> {
972    fn arbitrary(g: &mut Gen) -> Self {
973        Key4::<SecretParts, PrimaryRole>::arbitrary_secret_key(g)
974    }
975}
976
977#[cfg(test)]
978impl Arbitrary for Key4<SecretParts, SubordinateRole> {
979    fn arbitrary(g: &mut Gen) -> Self {
980        Key4::<SecretParts, SubordinateRole>::arbitrary_secret_key(g)
981    }
982}
983
984#[cfg(test)]
985impl<R> Key4<SecretParts, R>
986where
987    R: KeyRole,
988    Key4::<PublicParts, R>: Arbitrary,
989{
990    fn arbitrary_secret_key(g: &mut Gen) -> Self {
991        let key = Key::V4(Key4::<PublicParts, R>::arbitrary(g));
992        let mut secret: SecretKeyMaterial =
993            mpi::SecretKeyMaterial::arbitrary_for(g, key.pk_algo())
994            .expect("only known algos used")
995            .into();
996
997        if <bool>::arbitrary(g) {
998            secret.encrypt_in_place(&key, &Password::from(Vec::arbitrary(g)))
999                .unwrap();
1000        }
1001
1002        let key = if let Key::V4(k) = key { k } else { unreachable!() };
1003        Key4::<PublicParts, R>::add_secret(key, secret).0
1004    }
1005}
1006
1007#[cfg(test)]
1008mod tests {
1009    use std::time::Duration;
1010    use std::time::UNIX_EPOCH;
1011
1012    use crate::crypto::S2K;
1013    use crate::packet::Key;
1014    use crate::Cert;
1015    use crate::packet::pkesk::PKESK3;
1016    use crate::packet::key;
1017    use crate::packet::key::SecretKeyMaterial;
1018    use crate::packet::Packet;
1019    use super::*;
1020    use crate::PacketPile;
1021    use crate::serialize::Serialize;
1022    use crate::parse::Parse;
1023
1024    #[test]
1025    fn encrypted_rsa_key() {
1026        let cert = Cert::from_bytes(
1027            crate::tests::key("testy-new-encrypted-with-123.pgp")).unwrap();
1028        let key = cert.primary_key().key().clone();
1029        let (key, secret) = key.take_secret();
1030        let mut secret = secret.unwrap();
1031
1032        assert!(secret.is_encrypted());
1033        secret.decrypt_in_place(&key, &"123".into()).unwrap();
1034        assert!(!secret.is_encrypted());
1035        let (pair, _) = key.add_secret(secret);
1036        assert!(pair.has_unencrypted_secret());
1037
1038        match pair.secret() {
1039            SecretKeyMaterial::Unencrypted(ref u) => u.map(|mpis| match mpis {
1040                mpi::SecretKeyMaterial::RSA { .. } => (),
1041                _ => panic!(),
1042            }),
1043            _ => panic!(),
1044        }
1045    }
1046
1047    #[test]
1048    fn primary_key_encrypt_decrypt() -> Result<()> {
1049        key_encrypt_decrypt::<PrimaryRole>()
1050    }
1051
1052    #[test]
1053    fn subkey_encrypt_decrypt() -> Result<()> {
1054        key_encrypt_decrypt::<SubordinateRole>()
1055    }
1056
1057    fn key_encrypt_decrypt<R>() -> Result<()>
1058    where
1059        R: KeyRole + PartialEq,
1060    {
1061        let mut g = quickcheck::Gen::new(256);
1062        let p: Password = Vec::<u8>::arbitrary(&mut g).into();
1063
1064        let check = |key: Key4<SecretParts, R>| -> Result<()> {
1065            let key: Key<_, _> = key.into();
1066            let encrypted = key.clone().encrypt_secret(&p)?;
1067            let decrypted = encrypted.decrypt_secret(&p)?;
1068            assert_eq!(key, decrypted);
1069            Ok(())
1070        };
1071
1072        use crate::types::Curve::*;
1073        for curve in vec![NistP256, NistP384, NistP521, Ed25519] {
1074            if ! curve.is_supported() {
1075                eprintln!("Skipping unsupported {}", curve);
1076                continue;
1077            }
1078
1079            let key: Key4<_, R>
1080                = Key4::generate_ecc(true, curve.clone())?;
1081            check(key)?;
1082        }
1083
1084        for bits in vec![2048, 3072] {
1085            if ! PublicKeyAlgorithm::RSAEncryptSign.is_supported() {
1086                eprintln!("Skipping unsupported RSA");
1087                continue;
1088            }
1089
1090            let key: Key4<_, R>
1091                = Key4::generate_rsa(bits)?;
1092            check(key)?;
1093        }
1094
1095        Ok(())
1096    }
1097
1098    #[test]
1099    fn eq() {
1100        use crate::types::Curve::*;
1101
1102        for curve in vec![NistP256, NistP384, NistP521] {
1103            if ! curve.is_supported() {
1104                eprintln!("Skipping unsupported {}", curve);
1105                continue;
1106            }
1107
1108            let sign_key : Key4<_, key::UnspecifiedRole>
1109                = Key4::generate_ecc(true, curve.clone()).unwrap();
1110            let enc_key : Key4<_, key::UnspecifiedRole>
1111                = Key4::generate_ecc(false, curve).unwrap();
1112            let sign_clone = sign_key.clone();
1113            let enc_clone = enc_key.clone();
1114
1115            assert_eq!(sign_key, sign_clone);
1116            assert_eq!(enc_key, enc_clone);
1117        }
1118
1119        for bits in vec![1024, 2048, 3072, 4096] {
1120            if ! PublicKeyAlgorithm::RSAEncryptSign.is_supported() {
1121                eprintln!("Skipping unsupported RSA");
1122                continue;
1123            }
1124
1125            let key : Key4<_, key::UnspecifiedRole>
1126                = Key4::generate_rsa(bits).unwrap();
1127            let clone = key.clone();
1128            assert_eq!(key, clone);
1129        }
1130    }
1131
1132    #[test]
1133    fn generate_roundtrip() {
1134        use crate::types::Curve::*;
1135
1136        let keys = vec![NistP256, NistP384, NistP521].into_iter().flat_map(|cv|
1137        {
1138            if ! cv.is_supported() {
1139                eprintln!("Skipping unsupported {}", cv);
1140                return Vec::new();
1141            }
1142
1143            let sign_key : Key4<key::SecretParts, key::PrimaryRole>
1144                = Key4::generate_ecc(true, cv.clone()).unwrap();
1145            let enc_key = Key4::generate_ecc(false, cv).unwrap();
1146
1147            vec![sign_key, enc_key]
1148        }).chain(vec![1024, 2048, 3072, 4096].into_iter().filter_map(|b| {
1149            Key4::generate_rsa(b).ok()
1150        }));
1151
1152        for key in keys {
1153            let mut b = Vec::new();
1154            Packet::SecretKey(key.clone().into()).serialize(&mut b).unwrap();
1155
1156            let pp = PacketPile::from_bytes(&b).unwrap();
1157            if let Some(Packet::SecretKey(Key::V4(ref parsed_key))) =
1158                pp.path_ref(&[0])
1159            {
1160                assert_eq!(key.creation_time(), parsed_key.creation_time());
1161                assert_eq!(key.pk_algo(), parsed_key.pk_algo());
1162                assert_eq!(key.mpis(), parsed_key.mpis());
1163                assert_eq!(key.secret(), parsed_key.secret());
1164
1165                assert_eq!(&key, parsed_key);
1166            } else {
1167                panic!("bad packet: {:?}", pp.path_ref(&[0]));
1168            }
1169
1170            let mut b = Vec::new();
1171            let pk4 : Key4<PublicParts, PrimaryRole> = key.clone().into();
1172            Packet::PublicKey(pk4.into()).serialize(&mut b).unwrap();
1173
1174            let pp = PacketPile::from_bytes(&b).unwrap();
1175            if let Some(Packet::PublicKey(Key::V4(ref parsed_key))) =
1176                pp.path_ref(&[0])
1177            {
1178                assert!(! parsed_key.has_secret());
1179
1180                let key = key.take_secret().0;
1181                assert_eq!(&key, parsed_key);
1182            } else {
1183                panic!("bad packet: {:?}", pp.path_ref(&[0]));
1184            }
1185        }
1186    }
1187
1188    #[test]
1189    fn encryption_roundtrip() {
1190        use crate::crypto::SessionKey;
1191        use crate::types::Curve::*;
1192
1193        let keys = vec![NistP256, NistP384, NistP521].into_iter()
1194            .filter_map(|cv| {
1195                Key4::generate_ecc(false, cv).ok()
1196            }).chain(vec![1024, 2048, 3072, 4096].into_iter().filter_map(|b| {
1197                Key4::generate_rsa(b).ok()
1198            })).chain(vec![1024, 2048, 3072, 4096].into_iter().filter_map(|b| {
1199                Key4::generate_elgamal(b).ok()
1200            }));
1201
1202        for key in keys.into_iter() {
1203            let key: Key<key::SecretParts, key::UnspecifiedRole> = key.into();
1204            let mut keypair = key.clone().into_keypair().unwrap();
1205            let cipher = SymmetricAlgorithm::AES256;
1206            let sk = SessionKey::new(cipher.key_size().unwrap()).unwrap();
1207
1208            let pkesk = PKESK3::for_recipient(cipher, &sk, &key).unwrap();
1209            let (cipher_, sk_) = pkesk.decrypt(&mut keypair, None)
1210                .expect("keypair should be able to decrypt PKESK");
1211
1212            assert_eq!(cipher, cipher_);
1213            assert_eq!(sk, sk_);
1214
1215            let (cipher_, sk_) =
1216                pkesk.decrypt(&mut keypair, Some(cipher)).unwrap();
1217
1218            assert_eq!(cipher, cipher_);
1219            assert_eq!(sk, sk_);
1220        }
1221    }
1222
1223    #[test]
1224    fn signature_roundtrip() {
1225        use crate::types::{Curve::*, SignatureType};
1226
1227        let keys = vec![NistP256, NistP384, NistP521].into_iter()
1228            .filter_map(|cv| {
1229                Key4::generate_ecc(true, cv).ok()
1230            }).chain(vec![1024, 2048, 3072, 4096].into_iter().filter_map(|b| {
1231                Key4::generate_rsa(b).ok()
1232            })).chain(vec![1024, 2048, 3072].into_iter().filter_map(|b| {
1233                Key4::generate_dsa(b).ok()
1234            }));
1235
1236        for key in keys.into_iter() {
1237            let key: Key<key::SecretParts, key::UnspecifiedRole> = key.into();
1238            let mut keypair = key.clone().into_keypair().unwrap();
1239            let hash = HashAlgorithm::default();
1240
1241            // Sign.
1242            let ctx = hash.context().unwrap().for_signature(key.version());
1243            let sig = SignatureBuilder::new(SignatureType::Binary)
1244                .sign_hash(&mut keypair, ctx).unwrap();
1245
1246            // Verify.
1247            let ctx = hash.context().unwrap().for_signature(key.version());
1248            sig.verify_hash(&key, ctx).unwrap();
1249        }
1250    }
1251
1252    #[test]
1253    fn secret_encryption_roundtrip() {
1254        use crate::types::Curve::*;
1255        use crate::types::SymmetricAlgorithm::*;
1256        use crate::types::AEADAlgorithm::*;
1257
1258        let keys = vec![NistP256, NistP384, NistP521].into_iter()
1259            .filter_map(|cv| -> Option<Key<key::SecretParts, key::PrimaryRole>> {
1260                Key4::generate_ecc(false, cv).map(Into::into).ok()
1261            }).chain(vec![1024, 2048, 3072, 4096].into_iter().filter_map(|b| {
1262                Key4::generate_rsa(b).map(Into::into).ok()
1263            }));
1264
1265        for key in keys {
1266          for (symm, aead) in [(AES128, None),
1267                               (AES128, Some(OCB)),
1268                               (AES256, Some(EAX))] {
1269            if ! aead.map(|a| a.is_supported()).unwrap_or(true) {
1270                continue;
1271            }
1272            assert!(! key.secret().is_encrypted());
1273
1274            let password = Password::from("foobarbaz");
1275            let mut encrypted_key = key.clone();
1276
1277            encrypted_key.secret_mut()
1278                .encrypt_in_place_with(&key, S2K::default(), symm, aead,
1279                                       &password).unwrap();
1280            assert!(encrypted_key.secret().is_encrypted());
1281
1282            encrypted_key.secret_mut()
1283                .decrypt_in_place(&key, &password).unwrap();
1284            assert!(! key.secret().is_encrypted());
1285            assert_eq!(key, encrypted_key);
1286            assert_eq!(key.secret(), encrypted_key.secret());
1287          }
1288        }
1289    }
1290
1291    #[test]
1292    fn import_cv25519() {
1293        use crate::crypto::{ecdh, mem, SessionKey};
1294        use self::mpi::{MPI, Ciphertext};
1295
1296        // X25519 key
1297        let ctime =
1298            time::UNIX_EPOCH + time::Duration::new(0x5c487129, 0);
1299        let public = b"\xed\x59\x0a\x15\x08\x95\xe9\x92\xd2\x2c\x14\x01\xb3\xe9\x3b\x7f\xff\xe6\x6f\x22\x65\xec\x69\xd9\xb8\xda\x24\x2c\x64\x84\x44\x11";
1300        let key : Key<_, key::UnspecifiedRole>
1301            = Key4::import_public_cv25519(&public[..],
1302                                          HashAlgorithm::SHA256,
1303                                          SymmetricAlgorithm::AES128,
1304                                          ctime).unwrap().into();
1305
1306        // PKESK
1307        let eph_pubkey = MPI::new(&b"\x40\xda\x1c\x69\xc4\xe3\xb6\x9c\x6e\xd4\xc6\x69\x6c\x89\xc7\x09\xe9\xf8\x6a\xf1\xe3\x8d\xb6\xaa\xb5\xf7\x29\xae\xa6\xe7\xdd\xfe\x38"[..]);
1308        let ciphertext = Ciphertext::ECDH{
1309            e: eph_pubkey.clone(),
1310            key: Vec::from(&b"\x45\x8b\xd8\x4d\x88\xb3\xd2\x16\xb6\xc2\x3b\x99\x33\xd1\x23\x4b\x10\x15\x8e\x04\x16\xc5\x7c\x94\x88\xf6\x63\xf2\x68\x37\x08\x66\xfd\x5a\x7b\x40\x58\x21\x6b\x2c\xc0\xf4\xdc\x91\xd3\x48\xed\xc1"[..]).into_boxed_slice()
1311        };
1312        let shared_sec: mem::Protected = b"\x44\x0C\x99\x27\xF7\xD6\x1E\xAD\xD1\x1E\x9E\xC8\x22\x2C\x5D\x43\xCE\xB0\xE5\x45\x94\xEC\xAF\x67\xD9\x35\x1D\xA1\xA3\xA8\x10\x0B"[..].into();
1313
1314        // Session key
1315        let dek = b"\x09\x0D\xDC\x40\xC5\x71\x51\x88\xAC\xBD\x45\x56\xD4\x2A\xDF\x77\xCD\xF4\x82\xA2\x1B\x8F\x2E\x48\x3B\xCA\xBF\xD3\xE8\x6D\x0A\x7C\xDF\x10\xe6";
1316        let sk = SessionKey::from(Vec::from(&dek[..]));
1317
1318        // Expected
1319        let got_enc = ecdh::encrypt_wrap(&key.parts_into_public().role_into_subordinate(),
1320                                           &sk, eph_pubkey, &shared_sec)
1321            .unwrap();
1322
1323        assert_eq!(ciphertext, got_enc);
1324    }
1325
1326    #[test]
1327    fn import_cv25519_sec() -> Result<()> {
1328        use self::mpi::{MPI, Ciphertext};
1329
1330        // X25519 key
1331        let ctime =
1332            time::UNIX_EPOCH + time::Duration::new(0x5c487129, 0);
1333        let public = b"\xed\x59\x0a\x15\x08\x95\xe9\x92\xd2\x2c\x14\x01\xb3\xe9\x3b\x7f\xff\xe6\x6f\x22\x65\xec\x69\xd9\xb8\xda\x24\x2c\x64\x84\x44\x11";
1334        let secret = b"\xa0\x27\x13\x99\xc9\xe3\x2e\xd2\x47\xf6\xd6\x63\x9d\xe6\xec\xcb\x57\x0b\x92\xbb\x17\xfe\xb8\xf1\xc4\x1f\x06\x7c\x55\xfc\xdd\x58";
1335        let key: Key<_, UnspecifiedRole>
1336            = Key4::import_secret_cv25519(&secret[..],
1337                                          HashAlgorithm::SHA256,
1338                                          SymmetricAlgorithm::AES128,
1339                                          ctime).unwrap().into();
1340        match key.mpis() {
1341            self::mpi::PublicKey::ECDH{ ref q,.. } =>
1342                assert_eq!(&q.value()[1..], &public[..]),
1343            _ => unreachable!(),
1344        }
1345
1346        // PKESK
1347        let eph_pubkey: &[u8; 33] = b"\x40\xda\x1c\x69\xc4\xe3\xb6\x9c\x6e\xd4\xc6\x69\x6c\x89\xc7\x09\xe9\xf8\x6a\xf1\xe3\x8d\xb6\xaa\xb5\xf7\x29\xae\xa6\xe7\xdd\xfe\x38";
1348        let ciphertext = Ciphertext::ECDH{
1349            e: MPI::new(&eph_pubkey[..]),
1350            key: Vec::from(&b"\x45\x8b\xd8\x4d\x88\xb3\xd2\x16\xb6\xc2\x3b\x99\x33\xd1\x23\x4b\x10\x15\x8e\x04\x16\xc5\x7c\x94\x88\xf6\x63\xf2\x68\x37\x08\x66\xfd\x5a\x7b\x40\x58\x21\x6b\x2c\xc0\xf4\xdc\x91\xd3\x48\xed\xc1"[..]).into_boxed_slice()
1351        };
1352        let pkesk =
1353            PKESK3::new(None, PublicKeyAlgorithm::ECDH, ciphertext)?;
1354
1355        // Session key
1356        let dek = b"\x0D\xDC\x40\xC5\x71\x51\x88\xAC\xBD\x45\x56\xD4\x2A\xDF\x77\xCD\xF4\x82\xA2\x1B\x8F\x2E\x48\x3B\xCA\xBF\xD3\xE8\x6D\x0A\x7C\xDF";
1357
1358        let key = key.parts_into_secret().unwrap();
1359        let mut keypair = key.into_keypair()?;
1360        let (sym, got_dek) = pkesk.decrypt(&mut keypair, None).unwrap();
1361
1362        assert_eq!(sym, SymmetricAlgorithm::AES256);
1363        assert_eq!(&dek[..], &got_dek[..]);
1364        Ok(())
1365    }
1366
1367    #[test]
1368    fn import_rsa() -> Result<()> {
1369        use crate::crypto::SessionKey;
1370        use self::mpi::{MPI, Ciphertext};
1371
1372        // RSA key
1373        let ctime =
1374            time::UNIX_EPOCH + time::Duration::new(1548950502, 0);
1375        let d = b"\x14\xC4\x3A\x0C\x3A\x79\xA4\xF7\x63\x0D\x89\x93\x63\x8B\x56\x9C\x29\x2E\xCD\xCF\xBF\xB0\xEC\x66\x52\xC3\x70\x1B\x19\x21\x73\xDE\x8B\xAC\x0E\xF2\xE1\x28\x42\x66\x56\x55\x00\x3B\xFD\x50\xC4\x7C\xBC\x9D\xEB\x7D\xF4\x81\xFC\xC3\xBF\xF7\xFF\xD0\x41\x3E\x50\x3B\x5F\x5D\x5F\x56\x67\x5E\x00\xCE\xA4\x53\xB8\x59\xA0\x40\xC8\x96\x6D\x12\x09\x27\xBE\x1D\xF1\xC2\x68\xFC\xF0\x14\xD6\x52\x77\x07\xC8\x12\x36\x9C\x9A\x5C\xAF\x43\xCC\x95\x20\xBB\x0A\x44\x94\xDD\xB4\x4F\x45\x4E\x3A\x1A\x30\x0D\x66\x40\xAC\x68\xE8\xB0\xFD\xCD\x6C\x6B\x6C\xB5\xF7\xE4\x36\x95\xC2\x96\x98\xFD\xCA\x39\x6C\x1A\x2E\x55\xAD\xB6\xE0\xF8\x2C\xFF\xBC\xD3\x32\x15\x52\x39\xB3\x92\x35\xDB\x8B\x68\xAF\x2D\x4A\x6E\x64\xB8\x28\x63\xC4\x24\x94\x2D\xA9\xDB\x93\x56\xE3\xBC\xD0\xB6\x38\x84\x04\xA4\xC6\x18\x48\xFE\xB2\xF8\xE1\x60\x37\x52\x96\x41\xA5\x79\xF6\x3D\xB7\x2A\x71\x5B\x7A\x75\xBF\x7F\xA2\x5A\xC8\xA1\x38\xF2\x5A\xBD\x14\xFC\xAF\xB4\x54\x83\xA4\xBD\x49\xA2\x8B\x91\xB0\xE0\x4A\x1B\x21\x54\x07\x19\x70\x64\x7C\x3E\x9F\x8D\x8B\xE4\x70\xD1\xE7\xBE\x4E\x5C\xCE\xF1";
1376        let p = b"\xC8\x32\xD1\x17\x41\x4D\x8F\x37\x09\x18\x32\x4C\x4C\xF4\xA2\x15\x27\x43\x3D\xBB\xB5\xF6\x1F\xCF\xD2\xE4\x43\x61\x07\x0E\x9E\x35\x1F\x0A\x5D\xFB\x3A\x45\x74\x61\x73\x73\x7B\x5F\x1F\x87\xFB\x54\x8D\xA8\x85\x3E\xB0\xB7\xC7\xF5\xC9\x13\x99\x8D\x40\xE6\xA6\xD0\x71\x3A\xE3\x2D\x4A\xC3\xA3\xFF\xF7\x72\x82\x14\x52\xA4\xBA\x63\x0E\x17\xCA\xCA\x18\xC4\x3A\x40\x79\xF1\x86\xB3\x10\x4B\x9F\xB2\xAE\x2E\x13\x38\x8D\x2C\xF9\x88\x4C\x25\x53\xEF\xF9\xD1\x8B\x1A\x7C\xE7\xF6\x4B\x73\x51\x31\xFA\x44\x1D\x36\x65\x71\xDA\xFC\x6F";
1377        let q = b"\xCC\x30\xE9\xCC\xCB\x31\x28\xB5\x90\xFF\x06\x62\x42\x5B\x24\x0E\x00\xFE\xE2\x37\xC4\xAC\xBB\x3B\x8F\xF2\x0E\x3F\x78\xCF\x6B\x7C\xE8\x75\x57\x7C\x15\x9D\x1A\x66\xF2\x0A\xE5\xD3\x0B\xE7\x40\xF7\xE7\x00\xB6\x86\xB5\xD9\x20\x67\xE0\x4A\xC0\x90\xA4\x13\x4D\xC9\xB0\x12\xC5\xCD\x4C\xEB\xA1\x91\x2D\x43\x58\x6E\xB6\x75\xA0\x93\xF0\x5B\xC5\x31\xCA\xB7\xC6\x22\x0C\xD3\xEC\x84\xC5\x91\xA1\x5F\x2C\x8E\x07\x5D\xA1\x98\x67\xC5\x7A\x58\x16\x71\x3D\xED\x91\x03\x0D\xD4\x25\x07\x89\x9B\x33\x98\xA3\x70\xD9\xE7\xC8\x17\xA3\xD9";
1378
1379        let reference_key = Packet::from_bytes(b"\
1380-----BEGIN PGP PRIVATE KEY BLOCK-----
1381
1382xcLYBFxTG+YBCACfrr78JBmS/7rxsQg7y1IialuUqqbXmpMXz8mmd/tYKB1kymnK
1383kciCvYJ3CKq/3c3AlTlV7x4qKcXIL5XSuONdq9xHHpFyxjMJLAYMNn+PR6BgyLJG
1384J9MThBxELQGw7MEL+/7iFT6N92eu8PTyUnQwdDXA6JV5M49fbYCiG/2sCXSyVtJJ
1385DcQWkWQSZasC82PmFX4C/5QqunZ6nXRLkx79ErHwCzqO9GqY7rgPErmV0Hd2LXUt
1386AesCmSBFiR3Ole1MwNwp67hzQmFILqoBRKmJoEOfhjOiTCMET4SP7IE2pcpGKJyP
1387yJHwlfsG8SKTXBOsu9NUizX4HvSZ4ohXU6cXABEBAAEAB/0UxDoMOnmk92MNiZNj
1388i1acKS7Nz7+w7GZSw3AbGSFz3ousDvLhKEJmVlUAO/1QxHy8net99IH8w7/3/9BB
1389PlA7X11fVmdeAM6kU7hZoEDIlm0SCSe+HfHCaPzwFNZSdwfIEjacmlyvQ8yVILsK
1390RJTdtE9FTjoaMA1mQKxo6LD9zWxrbLX35DaVwpaY/co5bBouVa224Pgs/7zTMhVS
1391ObOSNduLaK8tSm5kuChjxCSULanbk1bjvNC2OIQEpMYYSP6y+OFgN1KWQaV59j23
1392KnFbenW/f6JayKE48lq9FPyvtFSDpL1JoouRsOBKGyFUBxlwZHw+n42L5HDR575O
1393XM7xBADIMtEXQU2PNwkYMkxM9KIVJ0M9u7X2H8/S5ENhBw6eNR8KXfs6RXRhc3N7
1394Xx+H+1SNqIU+sLfH9ckTmY1A5qbQcTrjLUrDo//3coIUUqS6Yw4XysoYxDpAefGG
1395sxBLn7KuLhM4jSz5iEwlU+/50YsafOf2S3NRMfpEHTZlcdr8bwQAzDDpzMsxKLWQ
1396/wZiQlskDgD+4jfErLs7j/IOP3jPa3zodVd8FZ0aZvIK5dML50D35wC2hrXZIGfg
1397SsCQpBNNybASxc1M66GRLUNYbrZ1oJPwW8UxyrfGIgzT7ITFkaFfLI4HXaGYZ8V6
1398WBZxPe2RAw3UJQeJmzOYo3DZ58gXo9kD/jLU2VpxpU/3BKrRO5AyvfMUKWlKXleT
1399pogdwcu0hHYntKr4mce7tBlRQRhtUv4dzRQ8OJ75oyvtl9mNfWaIOB/Jv5UOxeXh
1400jh20P23NClrtoM+5lYiaLEx0ZfD8xq85AKqrhMwcw4jH0lhJebTYOEeKrfD6SPbL
1401bzBv85X5WCw7RQ8=
1402=hd62
1403-----END PGP PRIVATE KEY BLOCK-----
1404")?;
1405
1406        // PKESK
1407        let c = b"\x8A\x1A\xD4\x82\x91\x6B\xBF\xA1\x65\xD3\x82\x8C\x97\xAB\xD0\x91\xE4\xB4\xC4\x9D\x08\xD8\x8B\xB7\xE6\x13\x3F\x6F\x52\x14\xED\xC4\x77\xB7\x31\x00\xC1\x43\xF9\x62\x53\xBF\x21\x21\x52\x74\x35\xD8\xC7\xA2\x11\x89\xA5\xD5\x21\x98\x6D\x3C\x9F\xF0\xED\xDB\xD7\x0F\xAC\x3C\x15\x25\x34\x52\xC7\x7C\x82\x07\x5A\x99\xC1\xC6\xF6\xF2\x6D\x46\xC8\x56\x59\xE7\xC6\x34\x0C\xCA\x37\x70\xB4\x97\xDA\x18\x14\xC4\x03\x0A\xCB\xE5\x0C\x41\x43\x61\xBA\x32\xB6\x9A\xF3\xDF\x0C\xB0\xCE\xBD\xFE\x72\x6C\xCC\xC1\xE8\xF0\x05\x97\x61\xEA\x30\x10\xB9\x43\xC4\x9A\x41\xED\x72\x27\xA4\xD5\xE7\x08\x41\x6C\x57\x80\xF3\x64\xF0\x45\x70\x27\x36\xBD\x64\x59\x74\xCF\xCD\x39\xE6\xEB\x7C\x62\xC8\x38\x23\xF8\x4C\xB7\x30\x9F\xF1\x40\x4A\xE9\x72\x66\x99\xF7\x2A\x47\x1C\xE7\x12\x20\x58\xBA\x87\x00\xB8\xFC\x54\xBC\xA5\x1D\x7D\x8B\x50\xA4\x4B\xB3\xD7\x44\xC7\x68\x5E\x2D\xBB\xE9\x6E\xC4\xD0\x31\xB0\xD0\xB6\x02\xD1\x74\x6B\xC9\x3D\x19\x32\x3B\xF1\x0E\x74\xF6\x12\x13\xE6\x40\x8F\xA6\x97\xAD\x83\xB0\x84\xD6\xD9\xE5\x25\x8E\x57\x0B\x7A\x7B\xD0\x5C\x29\x96\xED\x29\xED";
1408        let ciphertext = Ciphertext::RSA{
1409            c: MPI::new(&c[..]),
1410        };
1411        let pkesk = PKESK3::new(None,
1412                                PublicKeyAlgorithm::RSAEncryptSign,
1413                                ciphertext).unwrap();
1414
1415        // Session key
1416        let dek = b"\xA5\x58\x3A\x04\x35\x8B\xC7\x3F\x4A\xEF\x0C\x5A\xEB\xED\x59\xCA\xFD\x96\xB5\x32\x23\x26\x0C\x91\x78\xD1\x31\x12\xF0\x41\x42\x9D";
1417        let sk = SessionKey::from(Vec::from(&dek[..]));
1418
1419        let test = |d: &[u8], p: &[u8], q: &[u8]| -> Result<()> {
1420            let key: key::SecretKey
1421                = Key4::import_secret_rsa(d, p, q, ctime)?.into();
1422            assert_eq!(Packet::from(key.clone()), reference_key);
1423            let mut decryptor = key.into_keypair()?;
1424            let got_sk = pkesk.decrypt(&mut decryptor, None).unwrap();
1425            assert_eq!(got_sk.1, sk);
1426            Ok(())
1427        };
1428
1429        test(d, p, q)?;
1430        test(d, q, p)?;
1431        Ok(())
1432    }
1433
1434    #[test]
1435    fn import_ed25519() {
1436        use crate::types::SignatureType;
1437        use crate::packet::signature::Signature4;
1438        use crate::packet::signature::subpacket::{
1439            Subpacket, SubpacketValue, SubpacketArea};
1440
1441        // Ed25519 key
1442        let ctime =
1443            time::UNIX_EPOCH + time::Duration::new(1548249630, 0);
1444        let q = b"\x57\x15\x45\x1B\x68\xA5\x13\xA2\x20\x0F\x71\x9D\xE3\x05\x3B\xED\xA2\x21\xDE\x61\x5A\xF5\x67\x45\xBB\x97\x99\x43\x53\x59\x7C\x3F";
1445        let key: key::PublicKey
1446            = Key4::import_public_ed25519(q, ctime).unwrap().into();
1447
1448        let mut hashed = SubpacketArea::default();
1449        let mut unhashed = SubpacketArea::default();
1450        let fpr = "D81A 5DC0 DEBF EE5F 9AC8  20EB 6769 5DB9 920D 4FAC"
1451            .parse().unwrap();
1452        let kid = "6769 5DB9 920D 4FAC".parse().unwrap();
1453        let ctime = 1549460479.into();
1454        let r = b"\x5A\xF9\xC7\x42\x70\x24\x73\xFF\x7F\x27\xF9\x20\x9D\x20\x0F\xE3\x8F\x71\x3C\x5F\x97\xFD\x60\x80\x39\x29\xC2\x14\xFD\xC2\x4D\x70";
1455        let s = b"\x6E\x68\x74\x11\x72\xF4\x9C\xE1\x99\x99\x1F\x67\xFC\x3A\x68\x33\xF9\x3F\x3A\xB9\x1A\xA5\x72\x4E\x78\xD4\x81\xCB\x7B\xA5\xE5\x0A";
1456
1457        hashed.add(Subpacket::new(SubpacketValue::IssuerFingerprint(fpr), false).unwrap()).unwrap();
1458        hashed.add(Subpacket::new(SubpacketValue::SignatureCreationTime(ctime), false).unwrap()).unwrap();
1459        unhashed.add(Subpacket::new(SubpacketValue::Issuer(kid), false).unwrap()).unwrap();
1460
1461        eprintln!("fpr: {}", key.fingerprint());
1462        let sig = Signature4::new(SignatureType::Binary, PublicKeyAlgorithm::EdDSA,
1463                                  HashAlgorithm::SHA256, hashed, unhashed,
1464                                  [0xa7,0x19],
1465                                  mpi::Signature::EdDSA{
1466                                      r: mpi::MPI::new(r), s: mpi::MPI::new(s)
1467                                  });
1468        let sig: Signature = sig.into();
1469        sig.verify_message(&key, b"Hello, World\n").unwrap();
1470    }
1471
1472    #[test]
1473    fn fingerprint_test() {
1474        let pile =
1475            PacketPile::from_bytes(crate::tests::key("public-key.pgp")).unwrap();
1476
1477        // The blob contains a public key and three subkeys.
1478        let mut pki = 0;
1479        let mut ski = 0;
1480
1481        let pks = [ "8F17777118A33DDA9BA48E62AACB3243630052D9" ];
1482        let sks = [ "C03FA6411B03AE12576461187223B56678E02528",
1483                    "50E6D924308DBF223CFB510AC2B819056C652598",
1484                    "2DC50AB55BE2F3B04C2D2CF8A3506AFB820ABD08"];
1485
1486        for p in pile.descendants() {
1487            if let &Packet::PublicKey(ref p) = p {
1488                let fp = p.fingerprint().to_hex();
1489                // eprintln!("PK: {:?}", fp);
1490
1491                assert!(pki < pks.len());
1492                assert_eq!(fp, pks[pki]);
1493                pki += 1;
1494            }
1495
1496            if let &Packet::PublicSubkey(ref p) = p {
1497                let fp = p.fingerprint().to_hex();
1498                // eprintln!("SK: {:?}", fp);
1499
1500                assert!(ski < sks.len());
1501                assert_eq!(fp, sks[ski]);
1502                ski += 1;
1503            }
1504        }
1505        assert!(pki == pks.len() && ski == sks.len());
1506    }
1507
1508    #[test]
1509    fn issue_617() -> Result<()> {
1510        use crate::serialize::MarshalInto;
1511        let p = Packet::from_bytes(&b"-----BEGIN PGP ARMORED FILE-----
1512
1513xcClBAAAAMUWBSuBBAAjAPDbS+Z6Ti+PouOV6c5Ypr3jn1w1Ih5GqikN5E29PGz+
1514CQMIoYc7R4YRiLr/ZJB/MW5M0kuuWyUirUKRkYCotB5omVE8fGtqW5wGCGf79Tzb
1515rKVmPl25CJdEabIfAOl0WwciipDx1tqNOOYEci/JWSbTEymEyCH9oQPObt2sdDxh
1516wLcBgsd/CVl3kuqiXFHNYDvWVBmUHeltS/J22Kfy/n1qD3CCBFooHGdc13KwtMLk
1517UPb5LTTqCk2ihQ7e+5u7EmueLUp1431HJiYa+olaPZ7caRNfQfggtHcfQOJdnWRJ
1518FN2nTDgLHX0cEOiMboZrS4S9xtjyVRLcRZcCIyeQF0Q889rq0lmxHG38XUeIj/3y
1519SJJNnZxmJtHNo+SZQ/gXhO9TzeeA6yQm2myQlRkXBtdQEz6mtznphWeWMkWApZpa
1520FwPoSAbbsLkNS/iNN2MDGAVYvezYn2QZ
1521=0cxs
1522-----END PGP ARMORED FILE-----"[..])?;
1523        let i: usize = 360;
1524        let mut buf = p.to_vec().unwrap();
1525        // Avoid first two bytes so that we don't change the
1526        // type and reduce the chance of changing the length.
1527        let bit = i.saturating_add(2 * 8) % (buf.len() * 8);
1528        buf[bit / 8] ^= 1 << (bit % 8);
1529        match Packet::from_bytes(&buf) {
1530            Ok(q) => {
1531                eprintln!("{:?}", p);
1532                eprintln!("{:?}", q);
1533                assert!(p != q);
1534            },
1535            Err(_) => unreachable!(),
1536        };
1537        Ok(())
1538    }
1539
1540    #[test]
1541    fn encrypt_huge_plaintext() -> Result<()> {
1542        let sk = crate::crypto::SessionKey::new(256).unwrap();
1543
1544        if PublicKeyAlgorithm::RSAEncryptSign.is_supported() {
1545            let rsa2k: Key<SecretParts, UnspecifiedRole> =
1546                Key4::generate_rsa(2048)?.into();
1547            assert!(matches!(
1548                rsa2k.encrypt(&sk).unwrap_err().downcast().unwrap(),
1549                crate::Error::InvalidArgument(_)
1550            ));
1551        }
1552
1553        if PublicKeyAlgorithm::ECDH.is_supported()
1554            && Curve::Cv25519.is_supported()
1555        {
1556            let cv25519: Key<SecretParts, UnspecifiedRole> =
1557                Key4::generate_ecc(false, Curve::Cv25519)?.into();
1558            assert!(matches!(
1559                cv25519.encrypt(&sk).unwrap_err().downcast().unwrap(),
1560                crate::Error::InvalidArgument(_)
1561            ));
1562        }
1563
1564        Ok(())
1565    }
1566
1567    #[test]
1568    fn cv25519_secret_is_reversed() -> Result<()> {
1569        use crate::crypto::backend::{Backend, interface::Asymmetric};
1570
1571        let (mut private_key, _) = Backend::x25519_generate_key()?;
1572        Backend::x25519_clamp_secret(&mut private_key);
1573
1574        let key: Key4<_, UnspecifiedRole> =
1575            Key4::import_secret_cv25519(&private_key, None, None, None)?;
1576        if let crate::packet::key::SecretKeyMaterial::Unencrypted(key) = key.secret() {
1577            key.map(|secret| {
1578                if let mpi::SecretKeyMaterial::ECDH { scalar } = secret {
1579                    let scalar_reversed = private_key.iter().copied().rev().collect::<Vec<u8>>();
1580                    let scalar_actual = &*scalar.value_padded(32);
1581                    assert_eq!(scalar_actual, scalar_reversed);
1582                } else {
1583                    unreachable!();
1584                }
1585            })
1586        } else {
1587            unreachable!();
1588        }
1589
1590        Ok(())
1591    }
1592
1593    #[test]
1594    fn ed25519_secret_is_not_reversed() {
1595        let private_key: &[u8] =
1596            &crate::crypto::SessionKey::new(32).unwrap();
1597        let key: Key4<_, UnspecifiedRole> = Key4::import_secret_ed25519(private_key, None).unwrap();
1598        if let crate::packet::key::SecretKeyMaterial::Unencrypted(key) = key.secret() {
1599            key.map(|secret| {
1600                if let mpi::SecretKeyMaterial::EdDSA { scalar } = secret {
1601                    assert_eq!(&*scalar.value_padded(32), private_key);
1602                } else {
1603                    unreachable!();
1604                }
1605            })
1606        } else {
1607            unreachable!();
1608        }
1609    }
1610
1611    #[test]
1612    fn issue_1016() {
1613        // The fingerprint is a function of the creation time,
1614        // algorithm, and public MPIs.  When we change them make sure
1615        // the fingerprint also changes.
1616
1617        let mut g = quickcheck::Gen::new(256);
1618
1619        let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
1620        let fpr1 = key.fingerprint();
1621        if key.creation_time() == UNIX_EPOCH {
1622            key.set_creation_time(UNIX_EPOCH + Duration::new(1, 0)).expect("ok");
1623        } else {
1624            key.set_creation_time(UNIX_EPOCH).expect("ok");
1625        }
1626        assert_ne!(fpr1, key.fingerprint());
1627
1628        let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
1629        let fpr1 = key.fingerprint();
1630        key.set_pk_algo(PublicKeyAlgorithm::from(u8::from(key.pk_algo()) + 1));
1631        assert_ne!(fpr1, key.fingerprint());
1632
1633        let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
1634        let fpr1 = key.fingerprint();
1635        loop {
1636            let mpis2 = mpi::PublicKey::arbitrary(&mut g);
1637            if key.mpis() != &mpis2 {
1638                *key.mpis_mut() = mpis2;
1639                break;
1640            }
1641        }
1642        assert_ne!(fpr1, key.fingerprint());
1643
1644        let mut key = Key4::<PublicParts, UnspecifiedRole>::arbitrary(&mut g);
1645        let fpr1 = key.fingerprint();
1646        loop {
1647            let mpis2 = mpi::PublicKey::arbitrary(&mut g);
1648            if key.mpis() != &mpis2 {
1649                key.set_mpis(mpis2);
1650                break;
1651            }
1652        }
1653        assert_ne!(fpr1, key.fingerprint());
1654    }
1655
1656    /// Smoke test for ECC key creation, signing and verification, and
1657    /// encryption and decryption.
1658    #[test]
1659    fn ecc_support() -> Result<()> {
1660        for for_signing in [true, false] {
1661            for curve in Curve::variants()
1662                .filter(Curve::is_supported)
1663            {
1664                match curve {
1665                    Curve::Cv25519 if for_signing => continue,
1666                    Curve::Ed25519 if ! for_signing => continue,
1667                    _ => (),
1668                }
1669
1670                eprintln!("curve {}, for signing {:?}", curve, for_signing);
1671                let key: Key<SecretParts, UnspecifiedRole> =
1672                    Key4::generate_ecc(for_signing, curve.clone())?.into();
1673                let mut pair = key.into_keypair()?;
1674
1675                if for_signing {
1676                    use crate::crypto::Signer;
1677                    let hash = HashAlgorithm::default();
1678                    let digest = hash.context()?
1679                        .for_signature(pair.public().version())
1680                        .into_digest()?;
1681                    let sig = pair.sign(hash, &digest)?;
1682                    pair.public().verify(&sig, hash, &digest)?;
1683                } else {
1684                    use crate::crypto::{SessionKey, Decryptor};
1685                    let sk = SessionKey::new(32).unwrap();
1686                    let ciphertext = pair.public().encrypt(&sk)?;
1687                    assert_eq!(pair.decrypt(&ciphertext, Some(sk.len()))?, sk);
1688                }
1689            }
1690        }
1691        Ok(())
1692    }
1693
1694    #[test]
1695    fn ecc_encoding() -> Result<()> {
1696        for for_signing in [true, false] {
1697            for curve in Curve::variants()
1698                .filter(Curve::is_supported)
1699            {
1700                match curve {
1701                    Curve::Cv25519 if for_signing => continue,
1702                    Curve::Ed25519 if ! for_signing => continue,
1703                    _ => (),
1704                }
1705
1706                use crate::crypto::mpi::{Ciphertext, MPI, PublicKey};
1707                eprintln!("curve {}, for signing {:?}", curve, for_signing);
1708
1709                let key: Key<SecretParts, UnspecifiedRole> =
1710                    Key4::generate_ecc(for_signing, curve.clone())?.into();
1711
1712                let compressed = |mpi: &MPI| mpi.value()[0] == 0x40;
1713                let uncompressed = |mpi: &MPI| mpi.value()[0] == 0x04;
1714
1715                match key.mpis() {
1716                    PublicKey::ECDSA { curve: c, q } if for_signing => {
1717                        assert!(c == &curve);
1718                        assert!(uncompressed(q));
1719                    },
1720                    PublicKey::EdDSA { curve: c, q } if for_signing => {
1721                        assert!(c == &curve);
1722                        assert!(compressed(q));
1723                    },
1724                    PublicKey::ECDH { curve: c, q, .. } if ! for_signing => {
1725                        assert!(c == &curve);
1726                        if curve == Curve::Cv25519 {
1727                            assert!(compressed(q));
1728                        } else {
1729                            assert!(uncompressed(q));
1730                        }
1731
1732                        use crate::crypto::SessionKey;
1733                        let sk = SessionKey::new(32).unwrap();
1734                        let ciphertext = key.encrypt(&sk)?;
1735                        if let Ciphertext::ECDH { e, .. } = &ciphertext {
1736                            if curve == Curve::Cv25519 {
1737                                assert!(compressed(e));
1738                            } else {
1739                                assert!(uncompressed(e));
1740                            }
1741                        } else {
1742                            panic!("unexpected ciphertext: {:?}", ciphertext);
1743                        }
1744                    },
1745                    mpi => unreachable!(
1746                        "curve {}, mpi {:?}, for signing {:?}",
1747                        curve, mpi, for_signing),
1748                }
1749            }
1750        }
1751        Ok(())
1752    }
1753}