/src/botan/build/include/public/botan/x25519.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
12 | | namespace Botan { |
13 | | |
14 | | class BOTAN_PUBLIC_API(2, 0) X25519_PublicKey : public virtual Public_Key { |
15 | | public: |
16 | 0 | std::string algo_name() const override { return "X25519"; } |
17 | | |
18 | 0 | size_t estimated_strength() const override { return 128; } |
19 | | |
20 | 0 | 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 | 207 | std::vector<uint8_t> public_value() const { return m_public; } |
31 | | |
32 | 0 | 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 | 207 | X25519_PublicKey() = default; |
51 | | std::vector<uint8_t> m_public; |
52 | | }; |
53 | | |
54 | | BOTAN_DIAGNOSTIC_PUSH |
55 | | BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE |
56 | | |
57 | | class BOTAN_PUBLIC_API(2, 0) X25519_PrivateKey final : public X25519_PublicKey, |
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 | | */ |
72 | | explicit X25519_PrivateKey(RandomNumberGenerator& rng); |
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 | 207 | 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 | 0 | secure_vector<uint8_t> raw_private_key_bits() const override { return m_private; } |
85 | | |
86 | 0 | 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 | | |
102 | | BOTAN_DIAGNOSTIC_POP |
103 | | |
104 | | /* |
105 | | * The types above are just wrappers for curve25519_donna, plus defining |
106 | | * encodings for public and private keys. |
107 | | */ |
108 | | BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519") |
109 | | void 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 | | */ |
116 | | BOTAN_DEPRECATED_API("Use X25519_PrivateKey or Sodium::crypto_scalarmult_curve25519_base") |
117 | | void curve25519_basepoint(uint8_t mypublic[32], const uint8_t secret[32]); |
118 | | |
119 | | } // namespace Botan |
120 | | |
121 | | #endif |