Botan 3.9.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 <span>
14
15namespace Botan {
16
17class BOTAN_PUBLIC_API(2, 2) Ed25519_PublicKey : public virtual Public_Key {
18 public:
19 std::string algo_name() const override { return "Ed25519"; }
20
21 size_t estimated_strength() const override { return 128; }
22
23 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 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
36
37 BOTAN_DEPRECATED("Use raw_public_key_bits") const std::vector<uint8_t>& get_public_key() const {
38 return m_public;
39 }
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 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 Ed25519_PublicKey() = default;
61 std::vector<uint8_t> m_public; // NOLINT(*non-private-member-variable*)
62};
63
66
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 */
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 BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_private_key() const {
115 return m_private;
116 }
117
118 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
135
136BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_seed_keypair")
137void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
138
139BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_detached")
140void 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
147BOTAN_DEPRECATED("Use Ed25519_PublicKey or Sodium::crypto_sign_ed25519_verify_detached")
148bool 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
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:122
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:119
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:121
#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
Definition ed25519.h:114
static Ed25519_PrivateKey from_seed(std::span< const uint8_t > seed)
static Ed25519_PrivateKey from_bytes(std::span< const uint8_t > bytes)
secure_vector< uint8_t > raw_private_key_bits() const override
Definition ed25519.h:118
BOTAN_FUTURE_EXPLICIT Ed25519_PublicKey(std::span< const uint8_t > pub)
Definition ed25519.h:48
std::vector< uint8_t > m_public
Definition ed25519.h:61
Ed25519_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
const std::vector< uint8_t > & get_public_key() const
Definition ed25519.h:37
std::string algo_name() const override
Definition ed25519.h:19
bool supports_operation(PublicKeyOperation op) const override
Definition ed25519.h:35
size_t key_length() const override
Definition ed25519.h:23
size_t estimated_strength() const override
Definition ed25519.h:21
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:36
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:20
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69
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:72