Botan 3.8.1
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
41
42 std::vector<uint8_t> raw_public_key_bits() const override;
43
44 std::vector<uint8_t> public_key_bits() const override;
45
46 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
47
48 size_t estimated_strength() const override;
49 size_t key_length() const override;
50
51 BOTAN_DEPRECATED("Use raw_public_key_bits") std::vector<uint8_t> public_value() const {
52 return raw_public_key_bits();
53 }
54
55 std::string algo_name() const override { return "DH"; }
56
57 const BigInt& get_int_field(std::string_view field) const override;
58
60
61 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
62
63 const DL_Group& group() const;
64
65 private:
66 friend class DH_PrivateKey;
67
68 DH_PublicKey() = default;
69
70 DH_PublicKey(std::shared_ptr<const DL_PublicKey> key) : m_public_key(std::move(key)) {}
71
72 std::shared_ptr<const DL_PublicKey> m_public_key;
73};
74
75/**
76* This class represents Diffie-Hellman private keys.
77*/
78
81
82class BOTAN_PUBLIC_API(2, 0) DH_PrivateKey final : public DH_PublicKey,
84 public virtual Private_Key {
85 public:
86 /**
87 * Load a private key from the ASN.1 encoding
88 * @param alg_id the X.509 algorithm identifier
89 * @param key_bits PKCS #8 structure
90 */
91 DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
92
93 /**
94 * Load a private key from the integer encoding
95 * @param group the underlying DL group
96 * @param private_key the private key
97 */
98 DH_PrivateKey(const DL_Group& group, const BigInt& private_key);
99
100 /**
101 * Create a new private key.
102 * @param group the underlying DL group
103 * @param rng the RNG to use
104 */
106
107 std::unique_ptr<Public_Key> public_key() const override;
108
109 std::vector<uint8_t> public_value() const override;
110
112
114
115 const BigInt& get_int_field(std::string_view field) const override;
116
117 std::unique_ptr<PK_Ops::Key_Agreement> create_key_agreement_op(RandomNumberGenerator& rng,
118 std::string_view params,
119 std::string_view provider) const override;
120
121 private:
122 std::shared_ptr<const DL_PrivateKey> m_private_key;
123};
124
126
127} // namespace Botan
128
129#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:108
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:105
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:107
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:19
#define BOTAN_DEPRECATED(msg)
Definition api.h:59
DH_PrivateKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition dh.cpp:70
std::vector< uint8_t > public_value() const override
Definition dh.cpp:79
secure_vector< uint8_t > raw_private_key_bits() const override
Definition dh.cpp:87
const BigInt & get_int_field(std::string_view field) const override
Definition dh.cpp:91
secure_vector< uint8_t > private_key_bits() const override
Definition dh.cpp:83
std::unique_ptr< Public_Key > public_key() const override
Definition dh.cpp:75
std::unique_ptr< PK_Ops::Key_Agreement > create_key_agreement_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition dh.cpp:144
size_t key_length() const override
Definition dh.cpp:28
bool supports_operation(PublicKeyOperation op) const override
Definition dh.h:59
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition dh.cpp:52
DH_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition dh.cpp:16
std::vector< uint8_t > public_value() const
Definition dh.h:51
std::vector< uint8_t > public_key_bits() const override
Definition dh.cpp:48
friend class DH_PrivateKey
Definition dh.h:66
AlgorithmIdentifier algorithm_identifier() const override
Definition dh.cpp:40
std::string algo_name() const override
Definition dh.h:55
const DL_Group & group() const
Definition dh.cpp:36
size_t estimated_strength() const override
Definition dh.cpp:24
std::vector< uint8_t > raw_public_key_bits() const override
Definition dh.cpp:44
PublicKeyOperation
Definition pk_keys.h:46
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:65