Coverage Report

Created: 2025-04-11 06:34

/src/botan/build/include/public/botan/ecdh.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* ECDH
3
* (C) 2007 Falko Strenzke, FlexSecure GmbH
4
*          Manuel Hartl, FlexSecure GmbH
5
* (C) 2008-2010 Jack Lloyd
6
*
7
* Botan is released under the Simplified BSD License (see license.txt)
8
*/
9
10
#ifndef BOTAN_ECDH_KEY_H_
11
#define BOTAN_ECDH_KEY_H_
12
13
#include <botan/ecc_key.h>
14
15
namespace Botan {
16
17
/**
18
* This class represents ECDH Public Keys.
19
*/
20
class BOTAN_PUBLIC_API(2, 0) ECDH_PublicKey : public virtual EC_PublicKey {
21
   public:
22
      /**
23
      * Create an ECDH public key.
24
      * @param alg_id algorithm identifier
25
      * @param key_bits DER encoded public key bits
26
      */
27
      ECDH_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
28
1
            EC_PublicKey(alg_id, key_bits) {}
29
30
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
31
      /**
32
      * Construct a public key from a given public point.
33
      * @param group the domain parameters associated with this key
34
      * @param public_point the public point defining this key
35
      */
36
0
      ECDH_PublicKey(const EC_Group& group, const EC_Point& public_point) : EC_PublicKey(group, public_point) {}
37
#endif
38
39
      /**
40
      * Construct a public key from a given public point.
41
      * @param group the domain parameters associated with this key
42
      * @param public_key the public point defining this key
43
      */
44
1.73k
      ECDH_PublicKey(const EC_Group& group, const EC_AffinePoint& public_key) : EC_PublicKey(group, public_key) {}
45
46
      /**
47
      * Get this keys algorithm name.
48
      * @return this keys algorithm name
49
      */
50
5.34k
      std::string algo_name() const override { return "ECDH"; }
51
52
      /**
53
      * @return public point value
54
      */
55
0
      std::vector<uint8_t> public_value() const { return this->public_value(EC_Point_Format::Uncompressed); }
56
57
      /**
58
      * @return public point value
59
      */
60
      std::vector<uint8_t> public_value(EC_Point_Format format) const;
61
62
0
      bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
63
64
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
65
66
   protected:
67
14.1k
      ECDH_PublicKey() = default;
68
};
69
70
/**
71
* This class represents ECDH Private Keys.
72
*/
73
74
BOTAN_DIAGNOSTIC_PUSH
75
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
76
77
class BOTAN_PUBLIC_API(2, 0) ECDH_PrivateKey final : public ECDH_PublicKey,
78
                                                     public EC_PrivateKey,
79
                                                     public PK_Key_Agreement_Key {
80
   public:
81
      /**
82
      * Load a private key.
83
      * @param alg_id the X.509 algorithm identifier
84
      * @param key_bits ECPrivateKey bits
85
      */
86
      ECDH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
87
1.30k
            EC_PrivateKey(alg_id, key_bits) {}
88
89
      /**
90
      * Create a private key from a given secret @p x
91
      * @param group curve parameters to bu used for this key
92
      * @param x      the private key
93
      */
94
0
      ECDH_PrivateKey(EC_Group group, EC_Scalar x) : EC_PrivateKey(std::move(group), std::move(x)) {}
95
96
      /**
97
      * Create a new private key
98
      * @param rng a random number generator
99
      * @param group parameters to used for this key
100
      */
101
12.8k
      ECDH_PrivateKey(RandomNumberGenerator& rng, EC_Group group) : EC_PrivateKey(rng, std::move(group)) {}
102
103
      /**
104
      * Generate a new private key
105
      * @param rng a random number generator
106
      * @param group parameters to used for this key
107
      * @param x the private key; if zero, a new random key is generated
108
      */
109
      BOTAN_DEPRECATED("Use one of the other constructors")
110
      ECDH_PrivateKey(RandomNumberGenerator& rng, const EC_Group& group, const BigInt& x) :
111
0
            EC_PrivateKey(rng, group, x) {}
112
113
      std::unique_ptr<Public_Key> public_key() const override;
114
115
0
      std::vector<uint8_t> public_value() const override {
116
0
         return ECDH_PublicKey::public_value(EC_Point_Format::Uncompressed);
117
0
      }
118
119
12.8k
      std::vector<uint8_t> public_value(EC_Point_Format type) const { return ECDH_PublicKey::public_value(type); }
120
121
      std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
122
                                                                     std::string_view params,
123
                                                                     std::string_view provider) const override;
124
};
125
126
BOTAN_DIAGNOSTIC_POP
127
128
}  // namespace Botan
129
130
#endif