Coverage Report

Created: 2026-03-11 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/build/include/public/botan/dh.h
Line
Count
Source
1
/*
2
* Diffie-Hellman
3
* (C) 1999-2007,2023 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_DIFFIE_HELLMAN_H_
9
#define BOTAN_DIFFIE_HELLMAN_H_
10
11
#include <botan/pk_keys.h>
12
#include <memory>
13
14
namespace Botan {
15
16
class BigInt;
17
class DL_Group;
18
class DL_PublicKey;
19
class DL_PrivateKey;
20
21
/**
22
* This class represents Diffie-Hellman public keys.
23
*/
24
class BOTAN_PUBLIC_API(2, 0) DH_PublicKey : public virtual Public_Key {
25
   public:
26
      /**
27
      * Create a public key.
28
      * @param alg_id the X.509 algorithm identifier
29
      * @param key_bits DER encoded public key bits
30
      */
31
      DH_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
32
33
      /**
34
      * Construct a public key with the specified parameters.
35
      * @param group the DL group to use in the key
36
      * @param y the public value y
37
      */
38
      DH_PublicKey(const DL_Group& group, const BigInt& y);
39
40
      AlgorithmIdentifier algorithm_identifier() const override;
41
42
      std::vector<uint8_t> raw_public_key_bits() const override;
43
44
      std::vector<uint8_t> public_key_bits() const override;
45
46
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
47
48
      size_t estimated_strength() const override;
49
      size_t key_length() const override;
50
51
0
      BOTAN_DEPRECATED("Use raw_public_key_bits") std::vector<uint8_t> public_value() const {
52
0
         return raw_public_key_bits();
53
0
      }
54
55
0
      std::string algo_name() const override { return "DH"; }
56
57
      const BigInt& get_int_field(std::string_view field) const override;
58
59
0
      bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
60
61
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
62
63
      const DL_Group& group() const;
64
65
   private:
66
      friend class DH_PrivateKey;
67
68
0
      DH_PublicKey() = default;
69
70
0
      explicit DH_PublicKey(std::shared_ptr<const DL_PublicKey> key) : m_public_key(std::move(key)) {}
71
72
      std::shared_ptr<const DL_PublicKey> m_public_key;
73
};
74
75
/**
76
* This class represents Diffie-Hellman private keys.
77
*/
78
79
BOTAN_DIAGNOSTIC_PUSH
80
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
81
82
class BOTAN_PUBLIC_API(2, 0) DH_PrivateKey final : public DH_PublicKey,
83
                                                   public virtual PK_Key_Agreement_Key,
84
                                                   public virtual Private_Key {
85
   public:
86
      /**
87
      * Load a private key from the ASN.1 encoding
88
      * @param alg_id the X.509 algorithm identifier
89
      * @param key_bits PKCS #8 structure
90
      */
91
      DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
92
93
      /**
94
      * Load a private key from the integer encoding
95
      * @param group the underlying DL group
96
      * @param private_key the private key
97
      */
98
      DH_PrivateKey(const DL_Group& group, const BigInt& private_key);
99
100
      /**
101
      * Create a new private key.
102
      * @param group the underlying DL group
103
      * @param rng the RNG to use
104
      */
105
      DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group);
106
107
      std::unique_ptr<Public_Key> public_key() const override;
108
109
      std::vector<uint8_t> public_value() const override;
110
111
      secure_vector<uint8_t> private_key_bits() const override;
112
113
      secure_vector<uint8_t> raw_private_key_bits() const override;
114
115
      const BigInt& get_int_field(std::string_view field) const override;
116
117
      std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
118
                                                                     std::string_view params,
119
                                                                     std::string_view provider) const override;
120
121
   private:
122
      std::shared_ptr<const DL_PrivateKey> m_private_key;
123
};
124
125
BOTAN_DIAGNOSTIC_POP
126
127
}  // namespace Botan
128
129
#endif