Botan 3.6.1
Crypto and TLS for C&
x25519.h
Go to the documentation of this file.
1/*
2* (C) 2014,2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_X25519_H_
8#define BOTAN_X25519_H_
9
10#include <botan/pk_keys.h>
11
12namespace Botan {
13
14class BOTAN_PUBLIC_API(2, 0) X25519_PublicKey : public virtual Public_Key {
15 public:
16 std::string algo_name() const override { return "X25519"; }
17
18 size_t estimated_strength() const override { return 128; }
19
20 size_t key_length() const override { return 255; }
21
22 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
23
24 AlgorithmIdentifier algorithm_identifier() const override;
25
26 std::vector<uint8_t> raw_public_key_bits() const override;
27
28 std::vector<uint8_t> public_key_bits() const override;
29
30 std::vector<uint8_t> public_value() const { return m_public; }
31
32 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
33
34 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
35
36 /**
37 * Create a X25519 Public Key.
38 * @param alg_id the X.509 algorithm identifier
39 * @param key_bits DER encoded public key bits
40 */
41 X25519_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
42
43 /**
44 * Create a X25519 Public Key.
45 * @param pub 32-byte raw public key
46 */
47 explicit X25519_PublicKey(std::span<const uint8_t> pub);
48
49 protected:
50 X25519_PublicKey() = default;
51 std::vector<uint8_t> m_public;
52};
53
56
58 public virtual Private_Key,
59 public virtual PK_Key_Agreement_Key {
60 public:
61 /**
62 * Construct a private key from the specified parameters.
63 * @param alg_id the X.509 algorithm identifier
64 * @param key_bits PKCS #8 structure
65 */
66 X25519_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
67
68 /**
69 * Generate a private key.
70 * @param rng the RNG to use
71 */
73
74 /**
75 * Construct a private key from the specified parameters.
76 * @param secret_key the private key
77 */
78 explicit X25519_PrivateKey(const secure_vector<uint8_t>& secret_key);
79
80 std::vector<uint8_t> public_value() const override { return X25519_PublicKey::public_value(); }
81
82 secure_vector<uint8_t> agree(const uint8_t w[], size_t w_len) const;
83
84 secure_vector<uint8_t> raw_private_key_bits() const override { return m_private; }
85
86 BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_x() const { return m_private; }
87
88 secure_vector<uint8_t> private_key_bits() const override;
89
90 std::unique_ptr<Public_Key> public_key() const override;
91
92 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
93
94 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
95 std::string_view params,
96 std::string_view provider) const override;
97
98 private:
99 secure_vector<uint8_t> m_private;
100};
101
103
104/*
105* The types above are just wrappers for curve25519_donna, plus defining
106* encodings for public and private keys.
107*/
108BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519")
109void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32]);
110
111/**
112* Exponentiate by the x25519 base point
113* @param mypublic output value
114* @param secret random scalar
115*/
116BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519_base")
117void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32]);
118
119} // namespace Botan
120
121#endif
std::vector< uint8_t > public_value() const override
Definition x25519.h:80
secure_vector< uint8_t > raw_private_key_bits() const override
Definition x25519.h:84
std::string algo_name() const override
Definition x25519.h:16
size_t estimated_strength() const override
Definition x25519.h:18
std::vector< uint8_t > m_public
Definition x25519.h:51
std::vector< uint8_t > public_value() const
Definition x25519.h:30
bool supports_operation(PublicKeyOperation op) const override
Definition x25519.h:32
size_t key_length() const override
Definition x25519.h:20
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition compiler.h:191
#define BOTAN_DIAGNOSTIC_PUSH
Definition compiler.h:188
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition compiler.h:190
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
#define BOTAN_DEPRECATED_API(msg)
Definition compiler.h:37
PublicKeyOperation
Definition pk_keys.h:45
void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32])
Definition donna.cpp:454
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32])
Definition x25519.cpp:19