Botan 3.14.1
Crypto and TLS for C&
dh.cpp
Go to the documentation of this file.
1/*
2* Diffie-Hellman
3* (C) 1999-2007,2016,2019,2023 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/dh.h>
9
10#include <botan/internal/blinding.h>
11#include <botan/internal/ct_utils.h>
12#include <botan/internal/dl_scheme.h>
13#include <botan/internal/pk_ops_impl.h>
14#include <botan/internal/pk_options_impl.h>
15
16namespace Botan {
17
18DH_PublicKey::DH_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
19 m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
20}
21
23 m_public_key = std::make_shared<DL_PublicKey>(group, y);
24}
25
27 return m_public_key->estimated_strength();
28}
29
31 return m_public_key->p_bits();
32}
33
34const BigInt& DH_PublicKey::get_int_field(std::string_view field) const {
35 return m_public_key->get_int_field(algo_name(), field);
36}
37
39 return m_public_key->group();
40}
41
45
46std::vector<uint8_t> DH_PublicKey::raw_public_key_bits() const {
47 return m_public_key->public_key_as_bytes();
48}
49
50std::vector<uint8_t> DH_PublicKey::public_key_bits() const {
51 return m_public_key->DER_encode();
52}
53
54bool DH_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
55 return m_public_key->check_key(rng, strong);
56}
57
58std::unique_ptr<Private_Key> DH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
59 return std::make_unique<DH_PrivateKey>(rng, group());
60}
61
63 m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
64 m_public_key = m_private_key->public_key();
65}
66
68 m_private_key = std::make_shared<DL_PrivateKey>(group, x);
69 m_public_key = m_private_key->public_key();
70}
71
72DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
73 m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
74 m_public_key = m_private_key->public_key();
75}
76
77bool DH_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
78 return m_private_key->check_key(rng, strong);
79}
80
81std::unique_ptr<Public_Key> DH_PrivateKey::public_key() const {
82 return std::unique_ptr<DH_PublicKey>(new DH_PublicKey(m_public_key));
83}
84
85std::vector<uint8_t> DH_PrivateKey::public_value() const {
86 return raw_public_key_bits();
87}
88
90 return m_private_key->DER_encode();
91}
92
94 return m_private_key->raw_private_key_bits();
95}
96
97const BigInt& DH_PrivateKey::get_int_field(std::string_view field) const {
98 return m_private_key->get_int_field(algo_name(), field);
99}
100
101namespace {
102
103/**
104* DH operation
105*/
106class DH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
107 public:
108 DH_KA_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
109 const PK_Key_Agreement_Options_Reader& options,
111 PK_Ops::Key_Agreement_with_KDF(options),
112 m_key(key),
113 m_key_bits(m_key->max_private_key_bits()),
114 m_blinder(
115 m_key->group()._reducer_mod_p(),
116 rng,
117 [](const BigInt& k) { return k; },
118 [this](const BigInt& k) { return powermod_x_p(group().inverse_mod_p(k)); }) {}
119
120 size_t agreed_value_size() const override { return group().p_bytes(); }
121
122 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override;
123
124 private:
125 const DL_Group& group() const { return m_key->group(); }
126
127 BigInt powermod_x_p(const BigInt& v) const { return group().power_b_p(v, m_key->private_key(), m_key_bits); }
128
129 std::shared_ptr<const DL_PrivateKey> m_key;
130 const size_t m_key_bits;
131 Blinder m_blinder;
132};
133
134secure_vector<uint8_t> DH_KA_Operation::raw_agree(const uint8_t w[], size_t w_len) {
135 BigInt v = BigInt::from_bytes(std::span{w, w_len});
136
137 if(v <= 1 || v >= group().get_p() - 1) {
138 throw Invalid_Argument("DH agreement - invalid key provided");
139 }
140
141 const auto scope = CT::scoped_poison(m_key->private_key(), m_blinder);
142
143 v = m_blinder.blind(v);
144 v = powermod_x_p(v);
145 v = m_blinder.unblind(v);
146
147 // Any further secret handling of the output is up to the caller
148 CT::unpoison(v);
149
150 return v.serialize<secure_vector<uint8_t>>(group().p_bytes());
151}
152
153} // namespace
154
155std::unique_ptr<PK_Ops::Key_Agreement> DH_PrivateKey::_create_key_agreement_op(
156 RandomNumberGenerator& rng, const PK_Key_Agreement_Options_Reader& options) const {
158
159 return std::make_unique<DH_KA_Operation>(this->m_private_key, options, rng);
160}
161
162} // namespace Botan
virtual OID object_identifier() const
Definition pk_keys.cpp:24
BigInt blind(const BigInt &x) const
Definition blinding.cpp:34
BigInt unblind(const BigInt &x) const
Definition blinding.cpp:50
std::vector< uint8_t > public_value() const override
Definition dh.cpp:85
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition dh.cpp:77
std::unique_ptr< PK_Ops::Key_Agreement > _create_key_agreement_op(RandomNumberGenerator &rng, const PK_Key_Agreement_Options_Reader &options) const override
Definition dh.cpp:155
secure_vector< uint8_t > raw_private_key_bits() const override
Definition dh.cpp:93
const BigInt & get_int_field(std::string_view field) const override
Definition dh.cpp:97
secure_vector< uint8_t > private_key_bits() const override
Definition dh.cpp:89
std::unique_ptr< Public_Key > public_key() const override
Definition dh.cpp:81
size_t key_length() const override
Definition dh.cpp:30
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition dh.cpp:54
DH_PublicKey(const AlgorithmIdentifier &alg_id, std::span< const uint8_t > key_bits)
Definition dh.cpp:18
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition dh.cpp:58
std::vector< uint8_t > public_key_bits() const override
Definition dh.cpp:50
friend class DH_PrivateKey
Definition dh.h:66
AlgorithmIdentifier algorithm_identifier() const override
Definition dh.cpp:42
const BigInt & get_int_field(std::string_view field) const override
Definition dh.cpp:34
std::string algo_name() const override
Definition dh.h:55
const DL_Group & group() const
Definition dh.cpp:38
size_t estimated_strength() const override
Definition dh.cpp:26
std::vector< uint8_t > raw_public_key_bits() const override
Definition dh.cpp:46
void require_software_provider(const OptionsT &options, std::string_view algo_name)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128