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