sequoia-openpgp 2.4.0

OpenPGP data types and associated machinery
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
//! Asymmetric crypto operations.

use crate::packet::{self, key, Key};
use crate::crypto::SessionKey;
use crate::crypto::mpi;
use crate::types::{
    Curve,
    HashAlgorithm,
    PublicKeyAlgorithm,
    SymmetricAlgorithm,
};

use crate::{Error, Result};

/// Creates a signature.
///
/// Used in the streaming [`Signer`], the methods binding components
/// to certificates (e.g. [`UserID::bind`]), [`SignatureBuilder`]'s
/// signing functions (e.g. [`SignatureBuilder::sign_standalone`]),
/// and likely many more places.
///
///   [`Signer`]: crate::serialize::stream::Signer
///   [`UserID::bind`]: crate::packet::UserID::bind()
///   [`SignatureBuilder`]: crate::packet::signature::SignatureBuilder
///   [`SignatureBuilder::sign_standalone`]: crate::packet::signature::SignatureBuilder::sign_standalone()
///
/// This is a low-level mechanism to produce an arbitrary OpenPGP
/// signature.  Using this trait allows Sequoia to perform all
/// operations involving signing to use a variety of secret key
/// storage mechanisms (e.g. smart cards).
///
/// A signer consists of the public key and a way of creating a
/// signature.  This crate implements `Signer` for [`KeyPair`], which
/// is a tuple containing the public and unencrypted secret key in
/// memory.  Other crates may provide their own implementations of
/// `Signer` to utilize keys stored in various places.  Currently, the
/// following implementations exist:
///
///   - [`KeyPair`]: In-memory keys.
///   - [`sequoia_rpc::gnupg::KeyPair`]: Connects to the `gpg-agent`.
///
///   [`sequoia_rpc::gnupg::KeyPair`]: https://docs.sequoia-pgp.org/sequoia_ipc/gnupg/struct.KeyPair.html
pub trait Signer {
    /// Returns a reference to the public key.
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole>;

    /// Returns a list of hashes that this signer accepts.
    ///
    /// Some cryptographic libraries or hardware modules support signing digests
    /// produced with only a limited set of hashing algorithms. This function
    /// indicates to callers which algorithm digests are supported by this signer.
    ///
    /// The default implementation of this function allows all hash algorithms to
    /// be used. Provide an explicit implementation only when a smaller subset
    /// of hashing algorithms is valid for this `Signer` implementation.
    fn acceptable_hashes(&self) -> &[HashAlgorithm] {
        crate::crypto::hash::default_hashes_sorted()
    }

    /// Creates a signature over the `digest` produced by `hash_algo`.
    fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
            -> Result<mpi::Signature>;
}

impl Signer for Box<dyn Signer> {
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        self.as_ref().public()
    }

    fn acceptable_hashes(&self) -> &[HashAlgorithm] {
        self.as_ref().acceptable_hashes()
    }

    fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
            -> Result<mpi::Signature> {
        self.as_mut().sign(hash_algo, digest)
    }
}

impl Signer for Box<dyn Signer + Send + Sync> {
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        self.as_ref().public()
    }

    fn acceptable_hashes(&self) -> &[HashAlgorithm] {
        self.as_ref().acceptable_hashes()
    }

    fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
            -> Result<mpi::Signature> {
        self.as_mut().sign(hash_algo, digest)
    }
}

/// Decrypts a message.
///
/// Used by [`PKESK::decrypt`] to decrypt session keys.
///
///   [`PKESK::decrypt`]: crate::packet::PKESK#method.decrypt
///
/// This is a low-level mechanism to decrypt an arbitrary OpenPGP
/// ciphertext.  Using this trait allows Sequoia to perform all
/// operations involving decryption to use a variety of secret key
/// storage mechanisms (e.g. smart cards).
///
/// A decryptor consists of the public key and a way of decrypting a
/// session key.  This crate implements `Decryptor` for [`KeyPair`],
/// which is a tuple containing the public and unencrypted secret key
/// in memory.  Other crates may provide their own implementations of
/// `Decryptor` to utilize keys stored in various places.  Currently, the
/// following implementations exist:
///
///   - [`KeyPair`]: In-memory keys.
///   - [`sequoia_rpc::gnupg::KeyPair`]: Connects to the `gpg-agent`.
///
///   [`sequoia_rpc::gnupg::KeyPair`]: https://docs.sequoia-pgp.org/sequoia_ipc/gnupg/struct.KeyPair.html
pub trait Decryptor {
    /// Returns a reference to the public key.
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole>;

    /// Decrypts `ciphertext`, returning the plain session key.
    fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
               plaintext_len: Option<usize>)
               -> Result<SessionKey>;
}

impl Decryptor for Box<dyn Decryptor> {
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        self.as_ref().public()
    }

    fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
               plaintext_len: Option<usize>)
               -> Result<SessionKey> {
        self.as_mut().decrypt(ciphertext, plaintext_len)
    }
}

impl Decryptor for Box<dyn Decryptor + Send + Sync> {
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        self.as_ref().public()
    }

    fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
               plaintext_len: Option<usize>)
               -> Result<SessionKey> {
        self.as_mut().decrypt(ciphertext, plaintext_len)
    }
}

/// A cryptographic key pair.
///
/// A `KeyPair` is a combination of public and secret key.  If both
/// are available in memory, a `KeyPair` is a convenient
/// implementation of [`Signer`] and [`Decryptor`].
///
///
/// # Examples
///
/// ```
/// # fn main() -> sequoia_openpgp::Result<()> {
/// use sequoia_openpgp as openpgp;
/// use openpgp::types::Curve;
/// use openpgp::cert::prelude::*;
/// use openpgp::packet::prelude::*;
///
/// // Conveniently create a KeyPair from a bare key:
/// let keypair =
///     Key4::<_, key::UnspecifiedRole>::generate_ecc(false, Curve::Cv25519)?
///         .into_keypair()?;
///
/// // Or from a query over a certificate:
/// let (cert, _) =
///     CertBuilder::general_purpose(Some("alice@example.org"))
///         .generate()?;
/// let keypair =
///     cert.keys().unencrypted_secret().nth(0).unwrap().key().clone()
///         .into_keypair()?;
/// # Ok(()) }
/// ```
#[derive(Clone)]
pub struct KeyPair {
    public: Key<key::PublicParts, key::UnspecifiedRole>,
    secret: packet::key::Unencrypted,
}
assert_send_and_sync!(KeyPair);

impl KeyPair {
    /// Creates a new key pair.
    pub fn new(public: Key<key::PublicParts, key::UnspecifiedRole>,
               secret: packet::key::Unencrypted)
        -> Result<Self>
    {
        Ok(Self {
            public,
            secret,
        })
    }

    /// Returns a reference to the public key.
    pub fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        &self.public
    }

    /// Returns a reference to the secret key.
    pub fn secret(&self) -> &packet::key::Unencrypted {
        &self.secret
    }
}

impl From<KeyPair> for Key<key::SecretParts, key::UnspecifiedRole> {
    fn from(p: KeyPair) -> Self {
        let (key, secret) = (p.public, p.secret);
        key.add_secret(secret.into()).0
    }
}

impl Signer for KeyPair {
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        KeyPair::public(self)
    }

    fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
            -> Result<mpi::Signature>
    {
        use crate::crypto::backend::{Backend, interface::Asymmetric};

        self.secret().map(|secret| {
            #[allow(deprecated)]
            match (self.public().pk_algo(), self.public().mpis(), secret) {
                (PublicKeyAlgorithm::Ed25519,
                 mpi::PublicKey::Ed25519 { a },
                 mpi::SecretKeyMaterial::Ed25519 { x }) => {
                    Ok(mpi::Signature::Ed25519 {
                        s: Box::new(Backend::ed25519_sign(x, a, digest)?),
                    })
                },

                (PublicKeyAlgorithm::Ed448,
                 mpi::PublicKey::Ed448 { a },
                 mpi::SecretKeyMaterial::Ed448 { x }) => {
                    Ok(mpi::Signature::Ed448 {
                        s: Box::new(Backend::ed448_sign(x, a, digest)?),
                    })
                },

                (PublicKeyAlgorithm::MLDSA65_Ed25519,
                 mpi::PublicKey::MLDSA65_Ed25519 {
                     eddsa: eddsa_pub, ..
                 },
                 mpi::SecretKeyMaterial::MLDSA65_Ed25519 {
                     eddsa: eddsa_sec, mldsa: mldsa_sec,
                 }) => Ok(mpi::Signature::MLDSA65_Ed25519 {
                     eddsa: Box::new(Backend::ed25519_sign(
                         eddsa_sec, eddsa_pub, digest)?),
                     mldsa: Backend::mldsa65_sign(
                         mldsa_sec, digest)?,
                 }),

                (PublicKeyAlgorithm::MLDSA87_Ed448,
                 mpi::PublicKey::MLDSA87_Ed448 {
                     eddsa: eddsa_pub, ..
                 },
                 mpi::SecretKeyMaterial::MLDSA87_Ed448 {
                     eddsa: eddsa_sec, mldsa: mldsa_sec,
                 }) => Ok(mpi::Signature::MLDSA87_Ed448 {
                     eddsa: Box::new(Backend::ed448_sign(
                         eddsa_sec, eddsa_pub, digest)?),
                     mldsa: Backend::mldsa87_sign(
                         mldsa_sec, digest)?,
                 }),

                (PublicKeyAlgorithm::SLHDSA128s,
                 mpi::PublicKey::SLHDSA128s { .. },
                 mpi::SecretKeyMaterial::SLHDSA128s { secret }) =>
                    Ok(mpi::Signature::SLHDSA128s {
                        sig: Backend::slhdsa128s_sign(secret, digest)?,
                    }),

                (PublicKeyAlgorithm::SLHDSA128f,
                 mpi::PublicKey::SLHDSA128f { .. },
                 mpi::SecretKeyMaterial::SLHDSA128f { secret }) =>
                    Ok(mpi::Signature::SLHDSA128f {
                        sig: Backend::slhdsa128f_sign(secret, digest)?,
                    }),

                (PublicKeyAlgorithm::SLHDSA256s,
                 mpi::PublicKey::SLHDSA256s { .. },
                 mpi::SecretKeyMaterial::SLHDSA256s { secret }) =>
                    Ok(mpi::Signature::SLHDSA256s {
                        sig: Backend::slhdsa256s_sign(secret, digest)?,
                    }),

                (PublicKeyAlgorithm::EdDSA,
                 mpi::PublicKey::EdDSA { curve, q },
                 mpi::SecretKeyMaterial::EdDSA { scalar }) => match curve {
                    Curve::Ed25519 => {
                        let public = q.decode_point(&Curve::Ed25519)?.0
                            .try_into()?;
                        let secret = scalar.value_padded(32);
                        let sig =
                            Backend::ed25519_sign(&secret, &public, digest)?;
                        Ok(mpi::Signature::EdDSA {
                            r: mpi::MPI::new(&sig[..32]),
                            s: mpi::MPI::new(&sig[32..]),
                        })
                    },
                    _ => Err(
                        Error::UnsupportedEllipticCurve(curve.clone()).into()),
                },

                (PublicKeyAlgorithm::DSA,
                 mpi::PublicKey::DSA { p, q, g, y },
                 mpi::SecretKeyMaterial::DSA { x }) => {
                    let (r, s) = Backend::dsa_sign(x, p, q, g, y, digest)?;
                    Ok(mpi::Signature::DSA { r, s })
                },

                (_algo, _public, secret) =>
                    self.sign_backend(secret, hash_algo, digest),
            }
        })
    }
}

impl Decryptor for KeyPair {
    fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
        KeyPair::public(self)
    }

    fn decrypt(&mut self,
               ciphertext: &mpi::Ciphertext,
               plaintext_len: Option<usize>)
               -> Result<SessionKey>
    {
        use crate::crypto::ecdh::aes_key_unwrap;
        use crate::crypto::backend::{Backend, interface::{Asymmetric, Kdf}};

        self.secret().map(|secret| {
            #[allow(non_snake_case)]
            match (self.public().mpis(), secret, ciphertext) {
                (mpi::PublicKey::X25519 { u: U },
                 mpi::SecretKeyMaterial::X25519 { x },
                 mpi::Ciphertext::X25519 { e: E, key }) => {
                    // Compute the shared point S = xE;
                    let S = Backend::x25519_shared_point(x, E)?;

                    // Compute the wrap key.
                    let wrap_algo = SymmetricAlgorithm::AES128;
                    let mut ikm: SessionKey = vec![0; 32 + 32 + 32].into();

                    // Yes clippy, this operation will always return
                    // zero.  This is the intended outcome.  Chill.
                    #[allow(clippy::erasing_op)]
                    ikm[0 * 32..1 * 32].copy_from_slice(&E[..]);
                    ikm[1 * 32..2 * 32].copy_from_slice(&U[..]);
                    ikm[2 * 32..3 * 32].copy_from_slice(&S[..]);
                    let mut kek = vec![0; wrap_algo.key_size()?].into();
                    Backend::hkdf_sha256(&ikm, None, b"OpenPGP X25519",
                                         &mut kek)?;

                    Ok(aes_key_unwrap(wrap_algo, kek.as_protected(),
                                      key)?.into())
                },

                (mpi::PublicKey::X448 { u: U },
                 mpi::SecretKeyMaterial::X448 { x },
                 mpi::Ciphertext::X448 { e: E, key }) => {
                    // Compute the shared point S = xE;
                    let S = Backend::x448_shared_point(x, E)?;

                    // Compute the wrap key.
                    let wrap_algo = SymmetricAlgorithm::AES256;
                    let mut ikm: SessionKey = vec![0; 56 + 56 + 56].into();

                    // Yes clippy, this operation will always return
                    // zero.  This is the intended outcome.  Chill.
                    #[allow(clippy::erasing_op)]
                    ikm[0 * 56..1 * 56].copy_from_slice(&E[..]);
                    ikm[1 * 56..2 * 56].copy_from_slice(&U[..]);
                    ikm[2 * 56..3 * 56].copy_from_slice(&S[..]);
                    let mut kek = vec![0; wrap_algo.key_size()?].into();
                    Backend::hkdf_sha512(&ikm, None, b"OpenPGP X448",
                                         &mut kek)?;

                    Ok(aes_key_unwrap(wrap_algo, kek.as_protected(),
                                      key)?.into())
                },

                (mpi::PublicKey::ECDH { curve: Curve::Cv25519, .. },
                 mpi::SecretKeyMaterial::ECDH { scalar, },
                 mpi::Ciphertext::ECDH { e, .. }) =>
                {
                    // Get the public part V of the ephemeral key.
                    let V = e.decode_point(&Curve::Cv25519)?.0;

                    // X25519 expects the private key to be exactly 32
                    // bytes long but OpenPGP allows leading zeros to
                    // be stripped.  Padding has to be unconditional;
                    // otherwise we have a secret-dependent branch.
                    let mut r = scalar.value_padded(32);

                    // Reverse the scalar.  See
                    // https://lists.gnupg.org/pipermail/gnupg-devel/2018-February/033437.html
                    r.reverse();

                    // Compute the shared point S = rV = rvG, where
                    // (r, R) is the recipient's key pair.
                    let S = Backend::x25519_shared_point(&r, &V.try_into()?)?;

                    crate::crypto::ecdh::decrypt_unwrap(
                        self.public(), &S, ciphertext, plaintext_len)
                },

                (
                    mpi::PublicKey::MLKEM768_X25519 {
                        ecdh: ecdh_public, ..
                    },
                    mpi::SecretKeyMaterial::MLKEM768_X25519 {
                        ecdh: ecdh_secret, mlkem: mlkem_secret,
                    },
                    mpi::Ciphertext::MLKEM768_X25519 {
                        ecdh: ecdh_ciphertext, mlkem: mlkem_ciphertext, esk,
                    },
                ) => {
                    let ecdh_keyshare = Backend::x25519_shared_point(
                        ecdh_secret, ecdh_ciphertext)?;

                    let mlkem_keyshare = Backend::mlkem768_decapsulate(
                        mlkem_secret, mlkem_ciphertext)?;

                    let kek = multi_key_combine(
                        &mlkem_keyshare,
                        &ecdh_keyshare,
                        ecdh_ciphertext.as_ref(),
                        ecdh_public.as_ref(),
                        PublicKeyAlgorithm::MLKEM768_X25519)?;

                    Ok(aes_key_unwrap(SymmetricAlgorithm::AES256,
                                      kek.as_protected(),
                                      esk)?.into())
                },

                (
                    mpi::PublicKey::MLKEM1024_X448 {
                        ecdh: ecdh_public, ..
                    },
                    mpi::SecretKeyMaterial::MLKEM1024_X448 {
                        ecdh: ecdh_secret, mlkem: mlkem_secret,
                    },
                    mpi::Ciphertext::MLKEM1024_X448 {
                        ecdh: ecdh_ciphertext, mlkem: mlkem_ciphertext, esk,
                    },
                ) => {
                    let ecdh_keyshare = Backend::x448_shared_point(
                        ecdh_secret, ecdh_ciphertext)?;

                    let mlkem_keyshare = Backend::mlkem1024_decapsulate(
                        mlkem_secret, mlkem_ciphertext)?;

                    let kek = multi_key_combine(
                        &mlkem_keyshare,
                        &ecdh_keyshare,
                        ecdh_ciphertext.as_ref(),
                        ecdh_public.as_ref(),
                        PublicKeyAlgorithm::MLKEM1024_X448)?;

                    Ok(aes_key_unwrap(SymmetricAlgorithm::AES256,
                                      kek.as_protected(),
                                      esk)?.into())
                },

                (_public, secret, _ciphertext) =>
                    self.decrypt_backend(secret, ciphertext, plaintext_len),
            }
        })
    }
}

/// Combines PQC and classical algorithms.
///
/// See [Section 4.2.1 of draft-ietf-openpgp-pqc-08].
///
/// [Section 4.2.1 of draft-ietf-openpgp-pqc-08]: https://www.ietf.org/archive/id/draft-ietf-openpgp-pqc-08.html#kem-key-combiner
pub(crate) fn multi_key_combine(mlkem_key: &[u8],
                                ecdh_key: &[u8],
                                ecdh_ciphertext: &[u8],
                                ecdh_public: &[u8],
                                pk_algo: PublicKeyAlgorithm)
                                -> Result<SessionKey>
{
    //   multiKeyCombine(
    //       mlkemKeyShare, ecdhKeyShare,
    //       ecdhCipherText, ecdhPublicKey,
    //       algId
    //   )
    //
    //   Input:
    //   mlkemKeyShare   - the ML-KEM key share encoded as an octet string
    //   ecdhKeyShare    - the ECDH key share encoded as an octet string
    //   ecdhCipherText  - the ECDH ciphertext encoded as an octet string
    //   ecdhPublicKey   - the ECDH public key of the recipient as an octet string
    //   algId           - the OpenPGP algorithm ID of the public-key encryption algorithm
    //
    // KEK = SHA3-256(
    //           mlkemKeyShare || ecdhKeyShare ||
    //           ecdhCipherText || ecdhPublicKey ||
    //           algId || domSep || len(domSep)
    //       )

    let mut hash = HashAlgorithm::SHA3_256.context()?.for_digest();
    hash.update(mlkem_key);
    hash.update(ecdh_key);
    hash.update(ecdh_ciphertext);
    hash.update(ecdh_public);
    hash.update(&[pk_algo.into()]);
    // Domain separation and length octet.
    hash.update(b"OpenPGPCompositeKDFv1\x15");

    let mut kek = SessionKey::from(vec![0; 32]);
    hash.digest(&mut kek)?;
    Ok(kek)
}