Botan 3.6.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/dl_scheme.h>
12#include <botan/internal/pk_ops_impl.h>
13
14namespace Botan {
15
16DH_PublicKey::DH_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
17 m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
18}
19
20DH_PublicKey::DH_PublicKey(const DL_Group& group, const BigInt& y) {
21 m_public_key = std::make_shared<DL_PublicKey>(group, y);
22}
23
24std::vector<uint8_t> DH_PublicKey::public_value() const {
25 return m_public_key->public_key_as_bytes();
26}
27
29 return m_public_key->estimated_strength();
30}
31
33 return m_public_key->p_bits();
34}
35
36const BigInt& DH_PublicKey::get_int_field(std::string_view field) const {
37 return m_public_key->get_int_field(algo_name(), field);
38}
39
41 return m_public_key->group();
42}
43
47
48std::vector<uint8_t> DH_PublicKey::raw_public_key_bits() const {
49 return public_value();
50}
51
52std::vector<uint8_t> DH_PublicKey::public_key_bits() const {
53 return m_public_key->DER_encode();
54}
55
56bool DH_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
57 return m_public_key->check_key(rng, strong);
58}
59
60std::unique_ptr<Private_Key> DH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
61 return std::make_unique<DH_PrivateKey>(rng, group());
62}
63
65 m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
66 m_public_key = m_private_key->public_key();
67}
68
70 m_private_key = std::make_shared<DL_PrivateKey>(group, x);
71 m_public_key = m_private_key->public_key();
72}
73
74DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
75 m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
76 m_public_key = m_private_key->public_key();
77}
78
79std::unique_ptr<Public_Key> DH_PrivateKey::public_key() const {
80 return std::unique_ptr<DH_PublicKey>(new DH_PublicKey(m_public_key));
81}
82
83std::vector<uint8_t> DH_PrivateKey::public_value() const {
85}
86
88 return m_private_key->DER_encode();
89}
90
92 return m_private_key->raw_private_key_bits();
93}
94
95const BigInt& DH_PrivateKey::get_int_field(std::string_view field) const {
96 return m_private_key->get_int_field(algo_name(), field);
97}
98
99namespace {
100
101/**
102* DH operation
103*/
104class DH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
105 public:
106 DH_KA_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
107 std::string_view kdf,
109 PK_Ops::Key_Agreement_with_KDF(kdf),
110 m_key(key),
111 m_key_bits(m_key->private_key().bits()),
112 m_blinder(
113 m_key->group().get_p(),
114 rng,
115 [](const BigInt& k) { return k; },
116 [this](const BigInt& k) {
117 const BigInt inv_k = inverse_mod(k, group().get_p());
118 return powermod_x_p(inv_k);
119 }) {}
120
121 size_t agreed_value_size() const override { return group().p_bytes(); }
122
123 secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override;
124
125 private:
126 const DL_Group& group() const { return m_key->group(); }
127
128 BigInt powermod_x_p(const BigInt& v) const { return group().power_b_p(v, m_key->private_key(), m_key_bits); }
129
130 std::shared_ptr<const DL_PrivateKey> m_key;
131 std::shared_ptr<const Montgomery_Params> m_monty_p;
132 const size_t m_key_bits;
133 Blinder m_blinder;
134};
135
136secure_vector<uint8_t> DH_KA_Operation::raw_agree(const uint8_t w[], size_t w_len) {
137 BigInt v = BigInt::from_bytes(std::span{w, w_len});
138
139 if(v <= 1 || v >= group().get_p()) {
140 throw Invalid_Argument("DH agreement - invalid key provided");
141 }
142
143 v = m_blinder.blind(v);
144 v = powermod_x_p(v);
145 v = m_blinder.unblind(v);
146
147 return v.serialize<secure_vector<uint8_t>>(group().p_bytes());
148}
149
150} // namespace
151
152std::unique_ptr<PK_Ops::Key_Agreement> DH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
153 std::string_view params,
154 std::string_view provider) const {
155 if(provider == "base" || provider.empty()) {
156 return std::make_unique<DH_KA_Operation>(this->m_private_key, params, rng);
157 }
158 throw Provider_Not_Found(algo_name(), provider);
159}
160
161} // namespace Botan
virtual OID object_identifier() const
Definition pk_keys.cpp:22
T serialize(size_t len) const
Definition bigint.h:712
BigInt blind(const BigInt &x) const
Definition blinding.cpp:33
BigInt unblind(const BigInt &x) const
Definition blinding.cpp:53
std::vector< uint8_t > public_value() const override
Definition dh.cpp:83
secure_vector< uint8_t > raw_private_key_bits() const override
Definition dh.cpp:91
const BigInt & get_int_field(std::string_view field) const override
Definition dh.cpp:95
secure_vector< uint8_t > private_key_bits() const override
Definition dh.cpp:87
std::unique_ptr< Public_Key > public_key() const override
Definition dh.cpp:79
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:152
size_t key_length() const override
Definition dh.cpp:32
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition dh.cpp:56
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition dh.cpp:60
std::vector< uint8_t > public_value() const
Definition dh.cpp:24
std::vector< uint8_t > public_key_bits() const override
Definition dh.cpp:52
friend class DH_PrivateKey
Definition dh.h:64
AlgorithmIdentifier algorithm_identifier() const override
Definition dh.cpp:44
const BigInt & get_int_field(std::string_view field) const override
Definition dh.cpp:36
std::string algo_name() const override
Definition dh.h:53
const DL_Group & group() const
Definition dh.cpp:40
size_t estimated_strength() const override
Definition dh.cpp:28
std::vector< uint8_t > raw_public_key_bits() const override
Definition dh.cpp:48
int(* final)(unsigned char *, CTX *)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition mod_inv.cpp:179