Botan 3.8.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 <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 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 Ed25519_PublicKey() = default;
60 std::vector<uint8_t> m_public;
61};
62
65
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 */
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 BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_private_key() const {
114 return m_private;
115 }
116
117 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
134
135BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_seed_keypair")
136void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
137
138BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_detached")
139void 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
146BOTAN_DEPRECATED("Use Ed25519_PublicKey or Sodium::crypto_sign_ed25519_verify_detached")
147bool 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
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:108
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:105
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:107
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:19
#define BOTAN_DEPRECATED(msg)
Definition api.h:59
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:113
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:117
std::vector< uint8_t > m_public
Definition ed25519.h:60
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
Ed25519_PublicKey(std::span< const uint8_t > pub)
Definition ed25519.h:48
PublicKeyOperation
Definition pk_keys.h:46
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:37
void ed25519_gen_keypair(uint8_t *pk, uint8_t *sk, const uint8_t seed[32])
Definition ed25519.cpp:20
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:65
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:73