Botan 3.14.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#include <memory>
12#include <vector>
13
14namespace Botan {
15
16class X25519_PublicKey_Data;
17class X25519_PrivateKey_Data;
18
19class BOTAN_PUBLIC_API(2, 0) X25519_PublicKey : public virtual Public_Key {
20 public:
21 std::string algo_name() const override { return "X25519"; }
22
23 size_t estimated_strength() const override { return 128; }
24
25 size_t key_length() const override { return 255; }
26
27 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
28
29 AlgorithmIdentifier algorithm_identifier() const override;
30
31 std::vector<uint8_t> raw_public_key_bits() const override;
32
33 std::vector<uint8_t> public_key_bits() const override;
34
35 BOTAN_DEPRECATED("Use raw_public_key_bits") std::vector<uint8_t> public_value() const {
36 return raw_public_key_bits();
37 }
38
40
41 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
42
43 /**
44 * Create a X25519 Public Key.
45 * @param alg_id the X.509 algorithm identifier
46 * @param key_bits DER encoded public key bits
47 */
48 X25519_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
49
50 /**
51 * Create a X25519 Public Key.
52 * @param pub 32-byte raw public key
53 */
54 explicit X25519_PublicKey(std::span<const uint8_t> pub);
55
56 protected:
57 X25519_PublicKey() = default;
58 std::shared_ptr<const X25519_PublicKey_Data> m_public; // NOLINT(*non-private-member-variable*)
59};
60
63
65 public virtual Private_Key,
66 public virtual PK_Key_Agreement_Key {
67 public:
68 /**
69 * Construct a private key from the specified parameters.
70 * @param alg_id the X.509 algorithm identifier
71 * @param key_bits PKCS #8 structure
72 */
73 X25519_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
74
75 /**
76 * Generate a private key.
77 * @param rng the RNG to use
78 */
80
81 /**
82 * Construct a private key from the specified parameters.
83 * @param secret_key the private key
84 */
85 explicit X25519_PrivateKey(std::span<const uint8_t> secret_key);
86
87 std::vector<uint8_t> public_value() const override { return raw_public_key_bits(); }
88
89 /**
90 * Perform X25519 with the peer's 32-byte public value
91 *
92 * Throws Invalid_Argument if the peer's value is a point of low order
93 * (that is, if the result would be all zeros).
94 *
95 * Prefer PK_Key_Agreement, which also derives a key from the result.
96 */
97 BOTAN_DEPRECATED("Use PK_Key_Agreement") secure_vector<uint8_t> agree(const uint8_t w[], size_t w_len) const;
98
99 secure_vector<uint8_t> raw_private_key_bits() const override;
100
101 BOTAN_DEPRECATED("Use raw_private_key_bits") const secure_vector<uint8_t>& get_x() const;
102
103 secure_vector<uint8_t> private_key_bits() const override;
104
105 std::unique_ptr<Public_Key> public_key() const override;
106
107 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
108
109 std::unique_ptr<PK_Ops::Key_Agreement> _create_key_agreement_op(
110 RandomNumberGenerator& rng, const PK_Key_Agreement_Options_Reader& options) const override;
111
112 private:
113 std::shared_ptr<const X25519_PrivateKey_Data> m_private;
114};
115
117
118/*
119* The following functions are deprecated and no longer used within the
120* library itself; use X25519_PrivateKey instead.
121*/
122
123/**
124* Compute the X25519 scalar multiplication of secret and basepoint
125* @param mypublic output value
126* @param secret random scalar
127* @param basepoint the u-coordinate of the point to multiply
128*/
129BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519")
130void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32]);
131
132/**
133* Exponentiate by the x25519 base point
134* @param mypublic output value
135* @param secret random scalar
136*/
137BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519_base")
138void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32]);
139
140} // namespace Botan
141
142#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_DEPRECATED_API(msg)
Definition api.h:27
std::vector< uint8_t > public_value() const override
Definition x25519.h:87
X25519_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition x25519.cpp:156
std::shared_ptr< const X25519_PublicKey_Data > m_public
Definition x25519.h:58
std::string algo_name() const override
Definition x25519.h:21
size_t estimated_strength() const override
Definition x25519.h:23
std::vector< uint8_t > raw_public_key_bits() const override
Definition x25519.cpp:132
std::vector< uint8_t > public_value() const
Definition x25519.h:35
X25519_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition x25519.cpp:119
bool supports_operation(PublicKeyOperation op) const override
Definition x25519.h:39
size_t key_length() const override
Definition x25519.h:25
PublicKeyOperation
Definition pk_keys.h:50
void curve25519_donna(uint8_t mypublic[32], const uint8_t secret[32], const uint8_t basepoint[32])
Definition x25519.cpp:224
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32])
Definition x25519.cpp:230