Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/x25519.h
Line
Count
Source
1
/*
2
* (C) 2014,2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_X25519_H_
8
#define BOTAN_X25519_H_
9
10
#include <botan/pk_keys.h>
11
#include <memory>
12
#include <vector>
13
14
namespace Botan {
15
16
class X25519_PublicKey_Data;
17
class X25519_PrivateKey_Data;
18
19
class BOTAN_PUBLIC_API(2, 0) X25519_PublicKey : public virtual Public_Key {
20
   public:
21
0
      std::string algo_name() const override { return "X25519"; }
22
23
0
      size_t estimated_strength() const override { return 128; }
24
25
0
      size_t key_length() const override { return 255; }
26
27
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
28
29
      AlgorithmIdentifier algorithm_identifier() const override;
30
31
      std::vector<uint8_t> raw_public_key_bits() const override;
32
33
      std::vector<uint8_t> public_key_bits() const override;
34
35
0
      BOTAN_DEPRECATED("Use raw_public_key_bits") std::vector<uint8_t> public_value() const {
36
0
         return raw_public_key_bits();
37
0
      }
38
39
0
      bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
40
41
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
42
43
      /**
44
      * Create a X25519 Public Key.
45
      * @param alg_id the X.509 algorithm identifier
46
      * @param key_bits DER encoded public key bits
47
      */
48
      X25519_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
49
50
      /**
51
      * Create a X25519 Public Key.
52
      * @param pub 32-byte raw public key
53
      */
54
      explicit X25519_PublicKey(std::span<const uint8_t> pub);
55
56
   protected:
57
0
      X25519_PublicKey() = default;
58
      std::shared_ptr<const X25519_PublicKey_Data> m_public;  // NOLINT(*non-private-member-variable*)
59
};
60
61
BOTAN_DIAGNOSTIC_PUSH
62
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
63
64
class BOTAN_PUBLIC_API(2, 0) X25519_PrivateKey final : public X25519_PublicKey,
65
                                                       public virtual Private_Key,
66
                                                       public virtual PK_Key_Agreement_Key {
67
   public:
68
      /**
69
      * Construct a private key from the specified parameters.
70
      * @param alg_id the X.509 algorithm identifier
71
      * @param key_bits PKCS #8 structure
72
      */
73
      X25519_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
74
75
      /**
76
      * Generate a private key.
77
      * @param rng the RNG to use
78
      */
79
      explicit X25519_PrivateKey(RandomNumberGenerator& rng);
80
81
      /**
82
      * Construct a private key from the specified parameters.
83
      * @param secret_key the private key
84
      */
85
      explicit X25519_PrivateKey(std::span<const uint8_t> secret_key);
86
87
0
      std::vector<uint8_t> public_value() const override { return raw_public_key_bits(); }
88
89
      secure_vector<uint8_t> agree(const uint8_t w[], size_t w_len) const;
90
91
      secure_vector<uint8_t> raw_private_key_bits() const override;
92
93
      BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_x() const;
94
95
      secure_vector<uint8_t> private_key_bits() const override;
96
97
      std::unique_ptr<Public_Key> public_key() const override;
98
99
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
100
101
      std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
102
                                                                     std::string_view params,
103
                                                                     std::string_view provider) const override;
104
105
   private:
106
      std::shared_ptr<const X25519_PrivateKey_Data> m_private;
107
};
108
109
BOTAN_DIAGNOSTIC_POP
110
111
/*
112
* The types above are just wrappers for curve25519_donna, plus defining
113
* encodings for public and private keys.
114
*/
115
BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519")
116
void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32]);
117
118
/**
119
* Exponentiate by the x25519 base point
120
* @param mypublic output value
121
* @param secret random scalar
122
*/
123
BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519_base")
124
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32]);
125
126
}  // namespace Botan
127
128
#endif