Coverage Report

Created: 2025-11-09 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rnp/src/lib/key_material.hpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2024 [Ribose Inc](https://www.ribose.com).
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without modification,
6
 * are permitted provided that the following conditions are met:
7
 *
8
 * 1.  Redistributions of source code must retain the above copyright notice,
9
 *     this list of conditions and the following disclaimer.
10
 *
11
 * 2.  Redistributions in binary form must reproduce the above copyright notice,
12
 *     this list of conditions and the following disclaimer in the documentation
13
 *     and/or other materials provided with the distribution.
14
 *
15
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
19
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#ifndef RNP_KEY_MATERIAL_HPP_
28
#define RNP_KEY_MATERIAL_HPP_
29
30
#include "types.h"
31
#include "defaults.h"
32
#include "enc_material.hpp"
33
#include "sig_material.hpp"
34
#include "fingerprint.hpp"
35
36
typedef struct pgp_packet_body_t pgp_packet_body_t;
37
38
namespace pgp {
39
40
class KeyParams {
41
  public:
42
    virtual ~KeyParams();
43
    virtual size_t bits() const noexcept = 0;
44
    virtual pgp_hash_alg_t
45
    min_hash() const noexcept
46
0
    {
47
0
        return PGP_HASH_UNKNOWN;
48
0
    }
49
50
    virtual void
51
    check_defaults() noexcept
52
0
    {
53
0
    }
54
55
    static std::unique_ptr<KeyParams> create(pgp_pubkey_alg_t alg);
56
};
57
58
class BitsKeyParams : public KeyParams {
59
  private:
60
    size_t bits_;
61
62
  public:
63
0
    BitsKeyParams(size_t bits) : bits_(bits){};
64
65
    size_t
66
    bits() const noexcept override
67
0
    {
68
0
        return bits_;
69
0
    };
70
71
    void
72
    set_bits(size_t value) noexcept
73
0
    {
74
0
        bits_ = value;
75
0
    };
76
};
77
78
class RSAKeyParams : public BitsKeyParams {
79
  public:
80
0
    RSAKeyParams() : BitsKeyParams(DEFAULT_RSA_NUMBITS){};
81
};
82
83
class DSAKeyParams : public BitsKeyParams {
84
  private:
85
    size_t qbits_;
86
87
  public:
88
0
    DSAKeyParams() : BitsKeyParams(DSA_DEFAULT_P_BITLEN), qbits_(0){};
89
90
    size_t
91
    qbits() const noexcept
92
0
    {
93
0
        return qbits_;
94
0
    };
95
96
    void
97
    set_qbits(size_t value) noexcept
98
0
    {
99
0
        qbits_ = value;
100
0
    };
101
102
    void check_defaults() noexcept override;
103
104
    pgp_hash_alg_t min_hash() const noexcept override;
105
};
106
107
class EGKeyParams : public BitsKeyParams {
108
  public:
109
0
    EGKeyParams() : BitsKeyParams(DEFAULT_ELGAMAL_NUMBITS){};
110
};
111
112
class ECCKeyParams : public KeyParams {
113
  private:
114
    pgp_curve_t curve_;
115
116
  public:
117
0
    ECCKeyParams(pgp_curve_t curve = PGP_CURVE_UNKNOWN) : curve_(curve){};
118
119
    pgp_curve_t
120
    curve() const noexcept
121
0
    {
122
0
        return curve_;
123
0
    }
124
125
    void
126
    set_curve(const pgp_curve_t value) noexcept
127
0
    {
128
0
        curve_ = value;
129
0
    }
130
131
    size_t bits() const noexcept override;
132
};
133
134
class ECDSAKeyParams : public ECCKeyParams {
135
  public:
136
    pgp_hash_alg_t min_hash() const noexcept override;
137
};
138
139
#if defined(ENABLE_PQC)
140
class MlkemEcdhKeyParams : public KeyParams {
141
  private:
142
    pgp_pubkey_alg_t alg_;
143
144
  public:
145
0
    MlkemEcdhKeyParams(pgp_pubkey_alg_t alg) : alg_(alg){};
146
    size_t bits() const noexcept override;
147
};
148
149
class DilithiumEccKeyParams : public KeyParams {
150
  private:
151
    pgp_pubkey_alg_t alg_;
152
153
  public:
154
0
    DilithiumEccKeyParams(pgp_pubkey_alg_t alg) : alg_(alg){};
155
    size_t bits() const noexcept override;
156
};
157
158
class SlhdsaKeyParams : public KeyParams {
159
  private:
160
    sphincsplus_parameter_t param_;
161
162
  public:
163
0
    SlhdsaKeyParams() : param_(sphincsplus_simple_128f){};
164
165
    sphincsplus_parameter_t
166
    param() const noexcept
167
0
    {
168
0
        return param_;
169
0
    }
170
171
    void
172
    set_param(sphincsplus_parameter_t value) noexcept
173
0
    {
174
0
        param_ = value;
175
0
    }
176
177
    size_t bits() const noexcept override;
178
};
179
#endif
180
181
class KeyMaterial {
182
    pgp_validity_t validity_; /* key material validation status */
183
  protected:
184
    pgp_pubkey_alg_t alg_;    /* algorithm of the key */
185
    bool             secret_; /* secret part of the key material is populated */
186
187
    virtual void grip_update(rnp::Hash &hash) const = 0;
188
    virtual bool validate_material(rnp::SecurityContext &ctx, bool reset = true) = 0;
189
    bool         finish_generate();
190
191
  public:
192
    KeyMaterial(pgp_pubkey_alg_t kalg = PGP_PKA_NOTHING, bool secret = false)
193
694k
        : validity_({}), alg_(kalg), secret_(secret){};
194
    virtual ~KeyMaterial();
195
    virtual std::unique_ptr<KeyMaterial> clone() = 0;
196
197
    pgp_pubkey_alg_t      alg() const noexcept;
198
    bool                  secret() const noexcept;
199
    void                  validate(rnp::SecurityContext &ctx, bool reset = true);
200
    const pgp_validity_t &validity() const noexcept;
201
    void                  set_validity(const pgp_validity_t &val);
202
    void                  reset_validity();
203
    bool                  valid() const;
204
    virtual void          clear_secret() noexcept;
205
    virtual bool          parse(pgp_packet_body_t &pkt) noexcept = 0;
206
    virtual bool          parse_secret(pgp_packet_body_t &pkt) noexcept = 0;
207
    virtual void          write(pgp_packet_body_t &pkt) const = 0;
208
    virtual void          write_secret(pgp_packet_body_t &pkt) const = 0;
209
    virtual bool          generate(rnp::SecurityContext &ctx, const KeyParams &params);
210
    virtual rnp_result_t  encrypt(rnp::SecurityContext &   ctx,
211
                                  EncMaterial &            out,
212
                                  const rnp::secure_bytes &data) const;
213
    virtual rnp_result_t  decrypt(rnp::SecurityContext &ctx,
214
                                  rnp::secure_bytes &   out,
215
                                  const EncMaterial &   in) const;
216
    virtual rnp_result_t  verify(const rnp::SecurityContext &ctx,
217
                                 const SigMaterial &         sig,
218
                                 const rnp::secure_bytes &   hash) const;
219
    virtual rnp_result_t  sign(rnp::SecurityContext &   ctx,
220
                               SigMaterial &            sig,
221
                               const rnp::secure_bytes &hash) const;
222
223
    /* Pick up hash algorithm, used for signing, to be compatible with key material. */
224
    virtual pgp_hash_alg_t adjust_hash(pgp_hash_alg_t hash) const;
225
    virtual bool           sig_hash_allowed(pgp_hash_alg_t hash) const;
226
    virtual size_t         bits() const noexcept = 0;
227
    virtual pgp_curve_t    curve() const noexcept;
228
    KeyGrip                grip() const;
229
230
    static std::unique_ptr<KeyMaterial> create(pgp_pubkey_alg_t alg);
231
    static std::unique_ptr<KeyMaterial> create(pgp_pubkey_alg_t alg, const rsa::Key &key);
232
    static std::unique_ptr<KeyMaterial> create(const dsa::Key &key);
233
    static std::unique_ptr<KeyMaterial> create(pgp_pubkey_alg_t alg, const eg::Key &key);
234
    static std::unique_ptr<KeyMaterial> create(pgp_pubkey_alg_t alg, const ec::Key &key);
235
};
236
237
class RSAKeyMaterial : public KeyMaterial {
238
  protected:
239
    rsa::Key key_;
240
241
    void grip_update(rnp::Hash &hash) const override;
242
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
243
244
  public:
245
340k
    RSAKeyMaterial(pgp_pubkey_alg_t kalg) : KeyMaterial(kalg), key_{} {};
246
    RSAKeyMaterial(pgp_pubkey_alg_t kalg, const rsa::Key &key, bool secret = false)
247
776
        : KeyMaterial(kalg, secret), key_(key){};
248
    std::unique_ptr<KeyMaterial> clone() override;
249
250
    void         clear_secret() noexcept override;
251
    bool         parse(pgp_packet_body_t &pkt) noexcept override;
252
    bool         parse_secret(pgp_packet_body_t &pkt) noexcept override;
253
    void         write(pgp_packet_body_t &pkt) const override;
254
    void         write_secret(pgp_packet_body_t &pkt) const override;
255
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
256
    rnp_result_t encrypt(rnp::SecurityContext &   ctx,
257
                         EncMaterial &            out,
258
                         const rnp::secure_bytes &data) const override;
259
    rnp_result_t decrypt(rnp::SecurityContext &ctx,
260
                         rnp::secure_bytes &   out,
261
                         const EncMaterial &   in) const override;
262
    rnp_result_t verify(const rnp::SecurityContext &ctx,
263
                        const SigMaterial &         sig,
264
                        const rnp::secure_bytes &   hash) const override;
265
    rnp_result_t sign(rnp::SecurityContext &   ctx,
266
                      SigMaterial &            sig,
267
                      const rnp::secure_bytes &hash) const override;
268
269
    void   set_secret(const mpi &d, const mpi &p, const mpi &q, const mpi &u);
270
    size_t bits() const noexcept override;
271
272
    const mpi &n() const noexcept;
273
    const mpi &e() const noexcept;
274
    const mpi &d() const noexcept;
275
    const mpi &p() const noexcept;
276
    const mpi &q() const noexcept;
277
    const mpi &u() const noexcept;
278
};
279
280
class DSAKeyMaterial : public KeyMaterial {
281
  protected:
282
    dsa::Key key_;
283
284
    void grip_update(rnp::Hash &hash) const override;
285
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
286
287
  public:
288
16.9k
    DSAKeyMaterial() : KeyMaterial(PGP_PKA_DSA), key_{} {};
289
    DSAKeyMaterial(const dsa::Key &key, bool secret = false)
290
15
        : KeyMaterial(PGP_PKA_DSA, secret), key_(key){};
291
    std::unique_ptr<KeyMaterial> clone() override;
292
293
    void           clear_secret() noexcept override;
294
    bool           parse(pgp_packet_body_t &pkt) noexcept override;
295
    bool           parse_secret(pgp_packet_body_t &pkt) noexcept override;
296
    void           write(pgp_packet_body_t &pkt) const override;
297
    void           write_secret(pgp_packet_body_t &pkt) const override;
298
    bool           generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
299
    rnp_result_t   verify(const rnp::SecurityContext &ctx,
300
                          const SigMaterial &         sig,
301
                          const rnp::secure_bytes &   hash) const override;
302
    rnp_result_t   sign(rnp::SecurityContext &   ctx,
303
                        SigMaterial &            sig,
304
                        const rnp::secure_bytes &hash) const override;
305
    pgp_hash_alg_t adjust_hash(pgp_hash_alg_t hash) const override;
306
    void           set_secret(const mpi &x);
307
    size_t         bits() const noexcept override;
308
    size_t         qbits() const noexcept;
309
310
    const mpi &p() const noexcept;
311
    const mpi &q() const noexcept;
312
    const mpi &g() const noexcept;
313
    const mpi &y() const noexcept;
314
    const mpi &x() const noexcept;
315
};
316
317
class EGKeyMaterial : public KeyMaterial {
318
  protected:
319
    eg::Key key_;
320
321
    void grip_update(rnp::Hash &hash) const override;
322
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
323
324
  public:
325
19.7k
    EGKeyMaterial(pgp_pubkey_alg_t kalg) : KeyMaterial(kalg), key_{} {};
326
    EGKeyMaterial(pgp_pubkey_alg_t kalg, const eg::Key &key, bool secret = false)
327
40
        : KeyMaterial(kalg, secret), key_(key){};
328
    std::unique_ptr<KeyMaterial> clone() override;
329
330
    void         clear_secret() noexcept override;
331
    bool         parse(pgp_packet_body_t &pkt) noexcept override;
332
    bool         parse_secret(pgp_packet_body_t &pkt) noexcept override;
333
    void         write(pgp_packet_body_t &pkt) const override;
334
    void         write_secret(pgp_packet_body_t &pkt) const override;
335
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
336
    rnp_result_t encrypt(rnp::SecurityContext &   ctx,
337
                         EncMaterial &            out,
338
                         const rnp::secure_bytes &data) const override;
339
    rnp_result_t decrypt(rnp::SecurityContext &ctx,
340
                         rnp::secure_bytes &   out,
341
                         const EncMaterial &   in) const override;
342
    rnp_result_t verify(const rnp::SecurityContext &ctx,
343
                        const SigMaterial &         sig,
344
                        const rnp::secure_bytes &   hash) const override;
345
346
    void   set_secret(const mpi &x);
347
    size_t bits() const noexcept override;
348
349
    const mpi &p() const noexcept;
350
    const mpi &g() const noexcept;
351
    const mpi &y() const noexcept;
352
    const mpi &x() const noexcept;
353
};
354
355
class ECKeyMaterial : public KeyMaterial {
356
  protected:
357
    ec::Key key_;
358
359
    void         grip_update(rnp::Hash &hash) const override;
360
    rnp_result_t check_curve(size_t hash_len) const;
361
362
  public:
363
265k
    ECKeyMaterial(pgp_pubkey_alg_t kalg) : KeyMaterial(kalg), key_{} {};
364
    ECKeyMaterial(pgp_pubkey_alg_t kalg, const ec::Key &key, bool secret = false)
365
31
        : KeyMaterial(kalg, secret), key_(key){};
366
367
    void        clear_secret() noexcept override;
368
    bool        parse(pgp_packet_body_t &pkt) noexcept override;
369
    bool        parse_secret(pgp_packet_body_t &pkt) noexcept override;
370
    void        write(pgp_packet_body_t &pkt) const override;
371
    void        write_secret(pgp_packet_body_t &pkt) const override;
372
    bool        generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
373
    void        set_secret(const mpi &x);
374
    size_t      bits() const noexcept override;
375
    pgp_curve_t curve() const noexcept override;
376
377
    const mpi &p() const noexcept;
378
    const mpi &x() const noexcept;
379
};
380
381
class ECDSAKeyMaterial : public ECKeyMaterial {
382
  protected:
383
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
384
385
  public:
386
73.0k
    ECDSAKeyMaterial() : ECKeyMaterial(PGP_PKA_ECDSA){};
387
    ECDSAKeyMaterial(const ec::Key &key, bool secret = false)
388
7
        : ECKeyMaterial(PGP_PKA_ECDSA, key, secret){};
389
    std::unique_ptr<KeyMaterial> clone() override;
390
391
    rnp_result_t   verify(const rnp::SecurityContext &ctx,
392
                          const SigMaterial &         sig,
393
                          const rnp::secure_bytes &   hash) const override;
394
    rnp_result_t   sign(rnp::SecurityContext &   ctx,
395
                        SigMaterial &            sig,
396
                        const rnp::secure_bytes &hash) const override;
397
    pgp_hash_alg_t adjust_hash(pgp_hash_alg_t hash) const override;
398
};
399
400
class ECDHKeyMaterial : public ECKeyMaterial {
401
  protected:
402
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
403
404
  public:
405
64.2k
    ECDHKeyMaterial() : ECKeyMaterial(PGP_PKA_ECDH){};
406
    ECDHKeyMaterial(const ec::Key &key, bool secret = false)
407
15
        : ECKeyMaterial(PGP_PKA_ECDH, key, secret){};
408
    std::unique_ptr<KeyMaterial> clone() override;
409
410
    bool         parse(pgp_packet_body_t &pkt) noexcept override;
411
    void         write(pgp_packet_body_t &pkt) const override;
412
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
413
    rnp_result_t encrypt(rnp::SecurityContext &   ctx,
414
                         EncMaterial &            out,
415
                         const rnp::secure_bytes &data) const override;
416
    rnp_result_t decrypt(rnp::SecurityContext &ctx,
417
                         rnp::secure_bytes &   out,
418
                         const EncMaterial &   in) const override;
419
420
    pgp_hash_alg_t kdf_hash_alg() const noexcept;
421
    pgp_symm_alg_t key_wrap_alg() const noexcept;
422
    bool           x25519_bits_tweaked() const noexcept;
423
    bool           x25519_tweak_bits() noexcept;
424
};
425
426
class EDDSAKeyMaterial : public ECKeyMaterial {
427
  protected:
428
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
429
430
  public:
431
107k
    EDDSAKeyMaterial() : ECKeyMaterial(PGP_PKA_EDDSA){};
432
    EDDSAKeyMaterial(const ec::Key &key, bool secret = false)
433
9
        : ECKeyMaterial(PGP_PKA_EDDSA, key, secret){};
434
    std::unique_ptr<KeyMaterial> clone() override;
435
436
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
437
    rnp_result_t verify(const rnp::SecurityContext &ctx,
438
                        const SigMaterial &         sig,
439
                        const rnp::secure_bytes &   hash) const override;
440
    rnp_result_t sign(rnp::SecurityContext &   ctx,
441
                      SigMaterial &            sig,
442
                      const rnp::secure_bytes &hash) const override;
443
};
444
445
class SM2KeyMaterial : public ECKeyMaterial {
446
  protected:
447
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
448
449
  public:
450
20.4k
    SM2KeyMaterial() : ECKeyMaterial(PGP_PKA_SM2){};
451
    SM2KeyMaterial(const ec::Key &key, bool secret = false)
452
0
        : ECKeyMaterial(PGP_PKA_SM2, key, secret){};
453
    std::unique_ptr<KeyMaterial> clone() override;
454
455
    rnp_result_t encrypt(rnp::SecurityContext &   ctx,
456
                         EncMaterial &            out,
457
                         const rnp::secure_bytes &data) const override;
458
    rnp_result_t decrypt(rnp::SecurityContext &ctx,
459
                         rnp::secure_bytes &   out,
460
                         const EncMaterial &   in) const override;
461
    rnp_result_t verify(const rnp::SecurityContext &ctx,
462
                        const SigMaterial &         sig,
463
                        const rnp::secure_bytes &   hash) const override;
464
    rnp_result_t sign(rnp::SecurityContext &   ctx,
465
                      SigMaterial &            sig,
466
                      const rnp::secure_bytes &hash) const override;
467
    void         compute_za(rnp::Hash &hash) const;
468
};
469
470
#if defined(ENABLE_CRYPTO_REFRESH)
471
class Ed25519KeyMaterial : public KeyMaterial {
472
    pgp_ed25519_key_t key_;
473
474
  protected:
475
    void grip_update(rnp::Hash &hash) const override;
476
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
477
478
  public:
479
15.1k
    Ed25519KeyMaterial() : KeyMaterial(PGP_PKA_ED25519), key_{} {};
480
    std::unique_ptr<KeyMaterial> clone() override;
481
482
    void         clear_secret() noexcept override;
483
    bool         parse(pgp_packet_body_t &pkt) noexcept override;
484
    bool         parse_secret(pgp_packet_body_t &pkt) noexcept override;
485
    void         write(pgp_packet_body_t &pkt) const override;
486
    void         write_secret(pgp_packet_body_t &pkt) const override;
487
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
488
    rnp_result_t verify(const rnp::SecurityContext &ctx,
489
                        const SigMaterial &         sig,
490
                        const rnp::secure_bytes &   hash) const override;
491
    rnp_result_t sign(rnp::SecurityContext &   ctx,
492
                      SigMaterial &            sig,
493
                      const rnp::secure_bytes &hash) const override;
494
    size_t       bits() const noexcept override;
495
    pgp_curve_t  curve() const noexcept override;
496
497
    const std::vector<uint8_t> &pub() const noexcept;
498
    const std::vector<uint8_t> &priv() const noexcept;
499
};
500
501
class X25519KeyMaterial : public KeyMaterial {
502
    pgp_x25519_key_t key_;
503
504
  protected:
505
    void grip_update(rnp::Hash &hash) const override;
506
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
507
508
  public:
509
6.74k
    X25519KeyMaterial() : KeyMaterial(PGP_PKA_X25519), key_{} {};
510
    std::unique_ptr<KeyMaterial> clone() override;
511
512
    void         clear_secret() noexcept override;
513
    bool         parse(pgp_packet_body_t &pkt) noexcept override;
514
    bool         parse_secret(pgp_packet_body_t &pkt) noexcept override;
515
    void         write(pgp_packet_body_t &pkt) const override;
516
    void         write_secret(pgp_packet_body_t &pkt) const override;
517
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
518
    rnp_result_t encrypt(rnp::SecurityContext &   ctx,
519
                         EncMaterial &            out,
520
                         const rnp::secure_bytes &data) const override;
521
    rnp_result_t decrypt(rnp::SecurityContext &ctx,
522
                         rnp::secure_bytes &   out,
523
                         const EncMaterial &   in) const override;
524
    size_t       bits() const noexcept override;
525
    pgp_curve_t  curve() const noexcept override;
526
527
    const std::vector<uint8_t> &pub() const noexcept;
528
    const std::vector<uint8_t> &priv() const noexcept;
529
};
530
#endif
531
532
#if defined(ENABLE_PQC)
533
class MlkemEcdhKeyMaterial : public KeyMaterial {
534
    pgp_kyber_ecdh_key_t key_;
535
536
  protected:
537
    void grip_update(rnp::Hash &hash) const override;
538
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
539
540
  public:
541
16.8k
    MlkemEcdhKeyMaterial(pgp_pubkey_alg_t kalg) : KeyMaterial(kalg), key_{} {};
542
    std::unique_ptr<KeyMaterial> clone() override;
543
544
    void         clear_secret() noexcept override;
545
    bool         parse(pgp_packet_body_t &pkt) noexcept override;
546
    bool         parse_secret(pgp_packet_body_t &pkt) noexcept override;
547
    void         write(pgp_packet_body_t &pkt) const override;
548
    void         write_secret(pgp_packet_body_t &pkt) const override;
549
    bool         generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
550
    rnp_result_t encrypt(rnp::SecurityContext &   ctx,
551
                         EncMaterial &            out,
552
                         const rnp::secure_bytes &data) const override;
553
    rnp_result_t decrypt(rnp::SecurityContext &ctx,
554
                         rnp::secure_bytes &   out,
555
                         const EncMaterial &   in) const override;
556
    size_t       bits() const noexcept override;
557
558
    const pgp_kyber_ecdh_composite_public_key_t & pub() const noexcept;
559
    const pgp_kyber_ecdh_composite_private_key_t &priv() const noexcept;
560
};
561
562
class DilithiumEccKeyMaterial : public KeyMaterial {
563
    pgp_dilithium_exdsa_key_t key_;
564
565
  protected:
566
    void grip_update(rnp::Hash &hash) const override;
567
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
568
569
  public:
570
6.75k
    DilithiumEccKeyMaterial(pgp_pubkey_alg_t kalg) : KeyMaterial(kalg), key_{} {};
571
    std::unique_ptr<KeyMaterial> clone() override;
572
573
    /** @brief Check two key material for equality. Only public part is checked, so this may be
574
     * called on public/secret key material */
575
    void           clear_secret() noexcept override;
576
    bool           parse(pgp_packet_body_t &pkt) noexcept override;
577
    bool           parse_secret(pgp_packet_body_t &pkt) noexcept override;
578
    void           write(pgp_packet_body_t &pkt) const override;
579
    void           write_secret(pgp_packet_body_t &pkt) const override;
580
    bool           generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
581
    rnp_result_t   verify(const rnp::SecurityContext &ctx,
582
                          const SigMaterial &         sig,
583
                          const rnp::secure_bytes &   hash) const override;
584
    rnp_result_t   sign(rnp::SecurityContext &   ctx,
585
                        SigMaterial &            sig,
586
                        const rnp::secure_bytes &hash) const override;
587
    pgp_hash_alg_t adjust_hash(pgp_hash_alg_t hash) const override;
588
    size_t         bits() const noexcept override;
589
590
    const pgp_dilithium_exdsa_composite_public_key_t & pub() const noexcept;
591
    const pgp_dilithium_exdsa_composite_private_key_t &priv() const noexcept;
592
};
593
594
class SlhdsaKeyMaterial : public KeyMaterial {
595
    pgp_sphincsplus_key_t key_;
596
597
  protected:
598
    void grip_update(rnp::Hash &hash) const override;
599
    bool validate_material(rnp::SecurityContext &ctx, bool reset) override;
600
601
  public:
602
5.18k
    SlhdsaKeyMaterial(pgp_pubkey_alg_t kalg) : KeyMaterial(kalg), key_{} {};
603
    std::unique_ptr<KeyMaterial> clone() override;
604
605
    void           clear_secret() noexcept override;
606
    bool           parse(pgp_packet_body_t &pkt) noexcept override;
607
    bool           parse_secret(pgp_packet_body_t &pkt) noexcept override;
608
    void           write(pgp_packet_body_t &pkt) const override;
609
    void           write_secret(pgp_packet_body_t &pkt) const override;
610
    bool           generate(rnp::SecurityContext &ctx, const KeyParams &params) override;
611
    rnp_result_t   verify(const rnp::SecurityContext &ctx,
612
                          const SigMaterial &         sig,
613
                          const rnp::secure_bytes &   hash) const override;
614
    rnp_result_t   sign(rnp::SecurityContext &   ctx,
615
                        SigMaterial &            sig,
616
                        const rnp::secure_bytes &hash) const override;
617
    pgp_hash_alg_t adjust_hash(pgp_hash_alg_t hash) const override;
618
    bool           sig_hash_allowed(pgp_hash_alg_t hash) const override;
619
    size_t         bits() const noexcept override;
620
621
    const pgp_sphincsplus_public_key_t & pub() const noexcept;
622
    const pgp_sphincsplus_private_key_t &priv() const noexcept;
623
};
624
#endif
625
} // namespace pgp
626
627
#endif // RNP_KEY_MATERIAL_HPP_