Botan 3.3.0
Crypto and TLS for C&
dh.h
Go to the documentation of this file.
1/*
2* Diffie-Hellman
3* (C) 1999-2007,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_DIFFIE_HELLMAN_H_
9#define BOTAN_DIFFIE_HELLMAN_H_
10
11#include <botan/pk_keys.h>
12#include <memory>
13
14namespace Botan {
15
16class BigInt;
17class DL_Group;
18class DL_PublicKey;
19class DL_PrivateKey;
20
21/**
22* This class represents Diffie-Hellman public keys.
23*/
24class BOTAN_PUBLIC_API(2, 0) DH_PublicKey : public virtual Public_Key {
25 public:
26 /**
27 * Create a public key.
28 * @param alg_id the X.509 algorithm identifier
29 * @param key_bits DER encoded public key bits
30 */
31 DH_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
32
33 /**
34 * Construct a public key with the specified parameters.
35 * @param group the DL group to use in the key
36 * @param y the public value y
37 */
38 DH_PublicKey(const DL_Group& group, const BigInt& y);
39
40 AlgorithmIdentifier algorithm_identifier() const override;
41 std::vector<uint8_t> public_key_bits() const override;
42
43 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
44
45 size_t estimated_strength() const override;
46 size_t key_length() const override;
47
48 std::vector<uint8_t> public_value() const;
49
50 std::string algo_name() const override { return "DH"; }
51
52 const BigInt& get_int_field(std::string_view field) const override;
53
54 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::KeyAgreement); }
55
56 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
57
58 const DL_Group& group() const;
59
60 private:
61 friend class DH_PrivateKey;
62
63 DH_PublicKey() = default;
64
65 DH_PublicKey(std::shared_ptr<const DL_PublicKey> key) : m_public_key(std::move(key)) {}
66
67 std::shared_ptr<const DL_PublicKey> m_public_key;
68};
69
70/**
71* This class represents Diffie-Hellman private keys.
72*/
73
76
79 public virtual Private_Key {
80 public:
81 /**
82 * Load a private key from the ASN.1 encoding
83 * @param alg_id the X.509 algorithm identifier
84 * @param key_bits PKCS #8 structure
85 */
86 DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
87
88 /**
89 * Load a private key from the integer encoding
90 * @param group the underlying DL group
91 * @param private_key the private key
92 */
93 DH_PrivateKey(const DL_Group& group, const BigInt& private_key);
94
95 /**
96 * Create a new private key.
97 * @param group the underlying DL group
98 * @param rng the RNG to use
99 */
100 DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group);
101
102 std::unique_ptr<Public_Key> public_key() const override;
103
104 std::vector<uint8_t> public_value() const override;
105
106 secure_vector<uint8_t> private_key_bits() const override;
107
108 secure_vector<uint8_t> raw_private_key_bits() const override;
109
110 const BigInt& get_int_field(std::string_view field) const override;
111
112 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
113 std::string_view params,
114 std::string_view provider) const override;
115
116 private:
117 std::shared_ptr<const DL_PrivateKey> m_private_key;
118};
119
121
122} // namespace Botan
123
124#endif
bool supports_operation(PublicKeyOperation op) const override
Definition dh.h:54
std::string algo_name() const override
Definition dh.h:50
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
PublicKeyOperation
Definition pk_keys.h:45
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61