Botan 3.14.1
Crypto and TLS for C&
ed25519.h
Go to the documentation of this file.
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 <memory>
14#include <span>
15#include <vector>
16
17namespace Botan {
18
19class Ed25519_PublicKey_Data;
20class Ed25519_PrivateKey_Data;
21
22class BOTAN_PUBLIC_API(2, 2) Ed25519_PublicKey : public virtual Public_Key {
23 public:
24 std::string algo_name() const override { return "Ed25519"; }
25
26 size_t estimated_strength() const override { return 128; }
27
28 size_t key_length() const override { return 255; }
29
30 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
31
32 AlgorithmIdentifier algorithm_identifier() const override;
33
34 std::vector<uint8_t> raw_public_key_bits() const override;
35
36 std::vector<uint8_t> public_key_bits() const override;
37
38 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
39
40 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
41
42 BOTAN_DEPRECATED("Use raw_public_key_bits") const std::vector<uint8_t>& get_public_key() const;
43
44 /**
45 * Create a Ed25519 Public Key.
46 * @param alg_id the X.509 algorithm identifier
47 * @param key_bits DER encoded public key bits
48 *
49 * Throws Decoding_Error if the key is invalid; see the raw constructor
50 */
51 Ed25519_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
52
53 /**
54 * Create a Ed25519 Public Key from its 32 byte encoding.
55 *
56 * Throws Decoding_Error if the encoding is invalid, or if the point is
57 * the identity, has small order, or lies outside the prime order
58 * subgroup. No further checks are needed once the key is constructed.
59 */
60 BOTAN_FUTURE_EXPLICIT Ed25519_PublicKey(std::span<const uint8_t> pub) :
61 Ed25519_PublicKey(pub.data(), pub.size()) {}
62
63 Ed25519_PublicKey(const uint8_t pub_key[], size_t len);
64
65 std::unique_ptr<PK_Ops::Verification> _create_verification_op(
66 const PK_Signature_Options_Reader& options) const override;
67
68 std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm,
69 std::string_view provider) const override;
70
71 protected:
72 Ed25519_PublicKey() = default;
73 std::shared_ptr<const Ed25519_PublicKey_Data> m_public; // NOLINT(*non-private-member-variable*)
74
75 private:
76 friend class Ed25519_PrivateKey;
77
78 // Shares already validated key data, so the private key's public_key()
79 // need not repeat the checks performed when decoding
80 explicit Ed25519_PublicKey(std::shared_ptr<const Ed25519_PublicKey_Data> key) : m_public(std::move(key)) {}
81};
82
85
87 public virtual Private_Key {
88 public:
89 /**
90 * Construct a private key from the specified parameters.
91 * @param alg_id the X.509 algorithm identifier
92 * @param key_bits PKCS #8 structure
93 */
94 Ed25519_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
95
96 /**
97 * Generate a new random private key.
98 * @param rng the RNG to use
99 */
101
102 /**
103 * Construct a private key from the specified parameters.
104 *
105 * @param secret_key the private key
106 *
107 * The behavior of this function depends on the input length.
108 *
109 * If the input is 32 bytes long then it is treated as a seed, and a new
110 * keypair is generated.
111 *
112 * If the input is 64 bytes long then it is treated as a pair of 32 byte
113 * values, first the private key and then the public key.
114 *
115 * This constructor is deprecated since the above behavior is
116 * quite surprising. If you are relying on it, please comment in #4666.
117 */
118 BOTAN_DEPRECATED("Use from_seed or from_bytes") explicit Ed25519_PrivateKey(std::span<const uint8_t> secret_key);
119
120 /**
121 * Generate a new Ed25519_PrivateKey from the provided 32-byte seed
122 */
123 static Ed25519_PrivateKey from_seed(std::span<const uint8_t> seed);
124
125 /**
126 * Decode the Ed25519_PrivateKey from the provided 64-byte value
127 *
128 * The first 32 bytes are the private key and the last 32 bytes
129 * are the precomputed public key. Throws Decoding_Error if the
130 * public key does not match the one derived from the private key.
131 */
132 static Ed25519_PrivateKey from_bytes(std::span<const uint8_t> bytes);
133
134 BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_private_key() const;
135
137
139
140 std::unique_ptr<Public_Key> public_key() const override;
141
142 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
143
144 std::unique_ptr<PK_Ops::Signature> _create_signature_op(RandomNumberGenerator& rng,
145 const PK_Signature_Options_Reader& option) const override;
146
147 private:
148 std::shared_ptr<const Ed25519_PrivateKey_Data> m_private;
149};
150
152
153BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_seed_keypair")
154void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
155
156BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_detached")
157void ed25519_sign(uint8_t sig[64],
158 const uint8_t msg[],
159 size_t msg_len,
160 const uint8_t sk[64],
161 const uint8_t domain_sep[],
162 size_t domain_sep_len);
163
164BOTAN_DEPRECATED("Use Ed25519_PublicKey or Sodium::crypto_sign_ed25519_verify_detached")
165bool ed25519_verify(const uint8_t msg[],
166 size_t msg_len,
167 const uint8_t sig[64],
168 const uint8_t pk[32],
169 const uint8_t domain_sep[],
170 size_t domain_sep_len);
171
172} // namespace Botan
173
174#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:128
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:125
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:127
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
Ed25519_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
const secure_vector< uint8_t > & get_private_key() const
static Ed25519_PrivateKey from_seed(std::span< const uint8_t > seed)
std::unique_ptr< Public_Key > public_key() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
static Ed25519_PrivateKey from_bytes(std::span< const uint8_t > bytes)
secure_vector< uint8_t > raw_private_key_bits() const override
std::unique_ptr< PK_Ops::Signature > _create_signature_op(RandomNumberGenerator &rng, const PK_Signature_Options_Reader &option) const override
secure_vector< uint8_t > private_key_bits() const override
Ed25519_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
std::shared_ptr< const Ed25519_PublicKey_Data > m_public
Definition ed25519.h:73
std::string algo_name() const override
Definition ed25519.h:24
friend class Ed25519_PrivateKey
Definition ed25519.h:76
bool supports_operation(PublicKeyOperation op) const override
Definition ed25519.h:40
size_t key_length() const override
Definition ed25519.h:28
size_t estimated_strength() const override
Definition ed25519.h:26
void ed25519_sign(uint8_t sig[64], const uint8_t msg[], size_t msg_len, const uint8_t sk[64], const uint8_t domain_sep[], size_t domain_sep_len)
PublicKeyOperation
Definition pk_keys.h:50
void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32])
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
bool ed25519_verify(const uint8_t msg[], size_t msg_len, const uint8_t sig[64], const uint8_t pk[32], const uint8_t domain_sep[], size_t domain_sep_len)