Coverage Report

Created: 2026-06-07 07:04

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
0
      Ed25519_PublicKey(std::span<const uint8_t> pub) : Ed25519_PublicKey(pub.data(), pub.size()) {}
49
50
      Ed25519_PublicKey(const uint8_t pub_key[], size_t len);
51
52
      std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
53
                                                                   std::string_view provider) const override;
54
55
      std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm,
56
                                                                        std::string_view provider) const override;
57
58
   protected:
59
0
      Ed25519_PublicKey() = default;
60
      std::vector<uint8_t> m_public;
61
};
62
63
BOTAN_DIAGNOSTIC_PUSH
64
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
65
66
class BOTAN_PUBLIC_API(2, 2) Ed25519_PrivateKey final : public Ed25519_PublicKey,
67
                                                        public virtual Private_Key {
68
   public:
69
      /**
70
      * Construct a private key from the specified parameters.
71
      * @param alg_id the X.509 algorithm identifier
72
      * @param key_bits PKCS #8 structure
73
      */
74
      Ed25519_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
75
76
      /**
77
      * Generate a new random private key.
78
      * @param rng the RNG to use
79
      */
80
      explicit Ed25519_PrivateKey(RandomNumberGenerator& rng);
81
82
      /**
83
      * Construct a private key from the specified parameters.
84
      *
85
      * @param secret_key the private key
86
      *
87
      * The behavior of this function depends on the input length.
88
      *
89
      * If the input is 32 bytes long then it is treated as a seed, and a new
90
      * keypair is generated.
91
      *
92
      * If the input is 64 bytes long then it is treated as a pair of 32 byte
93
      * values, first the private key and then the public key.
94
      *
95
      * This constructor is deprecated since the above behavior is
96
      * quite surprising. If you are relying on it, please comment in #4666.
97
      */
98
      BOTAN_DEPRECATED("Use from_seed or from_bytes") explicit Ed25519_PrivateKey(std::span<const uint8_t> secret_key);
99
100
      /**
101
      * Generate a new Ed25519_PrivateKey from the provided 32-byte seed
102
      */
103
      static Ed25519_PrivateKey from_seed(std::span<const uint8_t> seed);
104
105
      /**
106
      * Decode the Ed25519_PrivateKey from the provided 64-byte value
107
      *
108
      * The first 32 bytes are the private key and the last 32 bytes
109
      * are the precomputed public key.
110
      */
111
      static Ed25519_PrivateKey from_bytes(std::span<const uint8_t> bytes);
112
113
0
      BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_private_key() const {
114
0
         return m_private;
115
0
      }
116
117
0
      secure_vector<uint8_t> raw_private_key_bits() const override { return m_private; }
118
119
      secure_vector<uint8_t> private_key_bits() const override;
120
121
      std::unique_ptr<Public_Key> public_key() const override;
122
123
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
124
125
      std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
126
                                                             std::string_view params,
127
                                                             std::string_view provider) const override;
128
129
   private:
130
      secure_vector<uint8_t> m_private;
131
};
132
133
BOTAN_DIAGNOSTIC_POP
134
135
BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_seed_keypair")
136
void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
137
138
BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_detached")
139
void ed25519_sign(uint8_t sig[64],
140
                  const uint8_t msg[],
141
                  size_t msg_len,
142
                  const uint8_t sk[64],
143
                  const uint8_t domain_sep[],
144
                  size_t domain_sep_len);
145
146
BOTAN_DEPRECATED("Use Ed25519_PublicKey or Sodium::crypto_sign_ed25519_verify_detached")
147
bool ed25519_verify(const uint8_t msg[],
148
                    size_t msg_len,
149
                    const uint8_t sig[64],
150
                    const uint8_t pk[32],
151
                    const uint8_t domain_sep[],
152
                    size_t domain_sep_len);
153
154
}  // namespace Botan
155
156
#endif