/src/Botan-3.13.0/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 <memory> |
14 | | #include <span> |
15 | | #include <vector> |
16 | | |
17 | | namespace Botan { |
18 | | |
19 | | class Ed25519_PublicKey_Data; |
20 | | class Ed25519_PrivateKey_Data; |
21 | | |
22 | | class BOTAN_PUBLIC_API(2, 2) Ed25519_PublicKey : public virtual Public_Key { |
23 | | public: |
24 | 0 | std::string algo_name() const override { return "Ed25519"; } |
25 | | |
26 | 0 | size_t estimated_strength() const override { return 128; } |
27 | | |
28 | 0 | 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 | 0 | 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 | 83.3k | 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 | 0 | Ed25519_PublicKey() = default; |
64 | | std::shared_ptr<const Ed25519_PublicKey_Data> m_public; // NOLINT(*non-private-member-variable*) |
65 | | }; |
66 | | |
67 | | BOTAN_DIAGNOSTIC_PUSH |
68 | | BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE |
69 | | |
70 | | class BOTAN_PUBLIC_API(2, 2) Ed25519_PrivateKey final : public Ed25519_PublicKey, |
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 | | */ |
84 | | explicit Ed25519_PrivateKey(RandomNumberGenerator& rng); |
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 | | |
119 | | secure_vector<uint8_t> raw_private_key_bits() const override; |
120 | | |
121 | | secure_vector<uint8_t> private_key_bits() const override; |
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 | | |
135 | | BOTAN_DIAGNOSTIC_POP |
136 | | |
137 | | BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_seed_keypair") |
138 | | void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]); |
139 | | |
140 | | BOTAN_DEPRECATED("Use Ed25519_PrivateKey or Sodium::crypto_sign_ed25519_detached") |
141 | | void 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 | | |
148 | | BOTAN_DEPRECATED("Use Ed25519_PublicKey or Sodium::crypto_sign_ed25519_verify_detached") |
149 | | bool 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 |