Coverage Report

Created: 2026-02-09 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/build/include/public/botan/ed25519.h
Line
Count
Source
1
/*
2
* Ed25519
3
* (C) 2017 Ribose Inc
4
*     2025 Jack Lloyd
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_ED25519_H_
10
#define BOTAN_ED25519_H_
11
12
#include <botan/pk_keys.h>
13
#include <span>
14
15
namespace Botan {
16
17
class BOTAN_PUBLIC_API(2, 2) Ed25519_PublicKey : public virtual Public_Key {
18
   public:
19
0
      std::string algo_name() const override { return "Ed25519"; }
20
21
0
      size_t estimated_strength() const override { return 128; }
22
23
0
      size_t key_length() const override { return 255; }
24
25
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
26
27
      AlgorithmIdentifier algorithm_identifier() const override;
28
29
      std::vector<uint8_t> raw_public_key_bits() const override;
30
31
      std::vector<uint8_t> public_key_bits() const override;
32
33
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
34
35
0
      bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
36
37
0
      BOTAN_DEPRECATED("Use raw_public_key_bits") const std::vector<uint8_t>& get_public_key() const {
38
0
         return m_public;
39
0
      }
40
41
      /**
42
      * Create a Ed25519 Public Key.
43
      * @param alg_id the X.509 algorithm identifier
44
      * @param key_bits DER encoded public key bits
45
      */
46
      Ed25519_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
47
48
      BOTAN_FUTURE_EXPLICIT Ed25519_PublicKey(std::span<const uint8_t> pub) :
49
0
            Ed25519_PublicKey(pub.data(), pub.size()) {}
50
51
      Ed25519_PublicKey(const uint8_t pub_key[], size_t len);
52
53
      std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
54
                                                                   std::string_view provider) const override;
55
56
      std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm,
57
                                                                        std::string_view provider) const override;
58
59
   protected:
60
0
      Ed25519_PublicKey() = default;
61
      std::vector<uint8_t> m_public;  // NOLINT(*non-private-member-variable*)
62
};
63
64
BOTAN_DIAGNOSTIC_PUSH
65
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
66
67
class BOTAN_PUBLIC_API(2, 2) Ed25519_PrivateKey final : public Ed25519_PublicKey,
68
                                                        public virtual Private_Key {
69
   public:
70
      /**
71
      * Construct a private key from the specified parameters.
72
      * @param alg_id the X.509 algorithm identifier
73
      * @param key_bits PKCS #8 structure
74
      */
75
      Ed25519_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
76
77
      /**
78
      * Generate a new random private key.
79
      * @param rng the RNG to use
80
      */
81
      explicit Ed25519_PrivateKey(RandomNumberGenerator& rng);
82
83
      /**
84
      * Construct a private key from the specified parameters.
85
      *
86
      * @param secret_key the private key
87
      *
88
      * The behavior of this function depends on the input length.
89
      *
90
      * If the input is 32 bytes long then it is treated as a seed, and a new
91
      * keypair is generated.
92
      *
93
      * If the input is 64 bytes long then it is treated as a pair of 32 byte
94
      * values, first the private key and then the public key.
95
      *
96
      * This constructor is deprecated since the above behavior is
97
      * quite surprising. If you are relying on it, please comment in #4666.
98
      */
99
      BOTAN_DEPRECATED("Use from_seed or from_bytes") explicit Ed25519_PrivateKey(std::span<const uint8_t> secret_key);
100
101
      /**
102
      * Generate a new Ed25519_PrivateKey from the provided 32-byte seed
103
      */
104
      static Ed25519_PrivateKey from_seed(std::span<const uint8_t> seed);
105
106
      /**
107
      * Decode the Ed25519_PrivateKey from the provided 64-byte value
108
      *
109
      * The first 32 bytes are the private key and the last 32 bytes
110
      * are the precomputed public key.
111
      */
112
      static Ed25519_PrivateKey from_bytes(std::span<const uint8_t> bytes);
113
114
0
      BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_private_key() const {
115
0
         return m_private;
116
0
      }
117
118
0
      secure_vector<uint8_t> raw_private_key_bits() const override { return m_private; }
119
120
      secure_vector<uint8_t> private_key_bits() const override;
121
122
      std::unique_ptr<Public_Key> public_key() const override;
123
124
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
125
126
      std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
127
                                                             std::string_view params,
128
                                                             std::string_view provider) const override;
129
130
   private:
131
      secure_vector<uint8_t> m_private;
132
};
133
134
BOTAN_DIAGNOSTIC_POP
135
136
BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_seed_keypair")
137
void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
138
139
BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_detached")
140
void ed25519_sign(uint8_t sig[64],
141
                  const uint8_t msg[],
142
                  size_t msg_len,
143
                  const uint8_t sk[64],
144
                  const uint8_t domain_sep[],
145
                  size_t domain_sep_len);
146
147
BOTAN_DEPRECATED("Use Ed25519_PublicKey or Sodium::crypto_sign_ed25519_verify_detached")
148
bool ed25519_verify(const uint8_t msg[],
149
                    size_t msg_len,
150
                    const uint8_t sig[64],
151
                    const uint8_t pk[32],
152
                    const uint8_t domain_sep[],
153
                    size_t domain_sep_len);
154
155
}  // namespace Botan
156
157
#endif