• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 24032704228

06 Apr 2026 12:58PM UTC coverage: 89.451% (-0.003%) from 89.454%
24032704228

Pull #5521

github

web-flow
Merge 17f437d7f into 417709dd7
Pull Request #5521: Rollup of small fixes

105882 of 118369 relevant lines covered (89.45%)

11473459.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.89
/src/lib/pubkey/dh/dh.cpp
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

14
namespace Botan {
15

16
DH_PublicKey::DH_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
2✔
17
   m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
2✔
18
}
2✔
19

20
DH_PublicKey::DH_PublicKey(const DL_Group& group, const BigInt& y) {
59✔
21
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
59✔
22
}
59✔
23

24
size_t DH_PublicKey::estimated_strength() const {
15✔
25
   return m_public_key->estimated_strength();
15✔
26
}
27

28
size_t DH_PublicKey::key_length() const {
20✔
29
   return m_public_key->p_bits();
20✔
30
}
31

32
const BigInt& DH_PublicKey::get_int_field(std::string_view field) const {
6✔
33
   return m_public_key->get_int_field(algo_name(), field);
6✔
34
}
35

36
const DL_Group& DH_PublicKey::group() const {
33✔
37
   return m_public_key->group();
33✔
38
}
39

40
AlgorithmIdentifier DH_PublicKey::algorithm_identifier() const {
45✔
41
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_42));
90✔
42
}
43

44
std::vector<uint8_t> DH_PublicKey::raw_public_key_bits() const {
349✔
45
   return m_public_key->public_key_as_bytes();
349✔
46
}
47

48
std::vector<uint8_t> DH_PublicKey::public_key_bits() const {
7✔
49
   return m_public_key->DER_encode();
7✔
50
}
51

52
bool DH_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
9✔
53
   return m_public_key->check_key(rng, strong);
9✔
54
}
55

56
std::unique_ptr<Private_Key> DH_PublicKey::generate_another(RandomNumberGenerator& rng) const {
5✔
57
   return std::make_unique<DH_PrivateKey>(rng, group());
10✔
58
}
59

60
DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
49✔
61
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
49✔
62
   m_public_key = m_private_key->public_key();
49✔
63
}
49✔
64

65
DH_PrivateKey::DH_PrivateKey(const DL_Group& group, const BigInt& x) {
187✔
66
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
187✔
67
   m_public_key = m_private_key->public_key();
187✔
68
}
187✔
69

70
DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
34✔
71
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
34✔
72
   m_public_key = m_private_key->public_key();
33✔
73
}
35✔
74

75
bool DH_PrivateKey::check_key(RandomNumberGenerator& rng, bool strong) const {
12✔
76
   return m_private_key->check_key(rng, strong);
12✔
77
}
78

79
std::unique_ptr<Public_Key> DH_PrivateKey::public_key() const {
17✔
80
   return std::unique_ptr<DH_PublicKey>(new DH_PublicKey(m_public_key));
17✔
81
}
82

83
std::vector<uint8_t> DH_PrivateKey::public_value() const {
272✔
84
   return raw_public_key_bits();
272✔
85
}
86

87
secure_vector<uint8_t> DH_PrivateKey::private_key_bits() const {
42✔
88
   return m_private_key->DER_encode();
42✔
89
}
90

91
secure_vector<uint8_t> DH_PrivateKey::raw_private_key_bits() const {
×
92
   return m_private_key->raw_private_key_bits();
×
93
}
94

95
const BigInt& DH_PrivateKey::get_int_field(std::string_view field) const {
11✔
96
   return m_private_key->get_int_field(algo_name(), field);
11✔
97
}
98

99
namespace {
100

101
/**
102
* DH operation
103
*/
104
class 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,
237✔
107
                      std::string_view kdf,
108
                      RandomNumberGenerator& rng) :
237✔
109
            PK_Ops::Key_Agreement_with_KDF(kdf),
110
            m_key(key),
474✔
111
            m_key_bits(m_key->private_key().bits()),
237✔
112
            m_blinder(
474✔
113
               m_key->group()._reducer_mod_p(),
237✔
114
               rng,
115
               [](const BigInt& k) { return k; },
237✔
116
               [this](const BigInt& k) { return powermod_x_p(group().inverse_mod_p(k)); }) {}
948✔
117

118
      size_t agreed_value_size() const override { return group().p_bytes(); }
134✔
119

120
      secure_vector<uint8_t> raw_agree(const uint8_t w[], size_t w_len) override;
121

122
   private:
123
      const DL_Group& group() const { return m_key->group(); }
2,134✔
124

125
      BigInt powermod_x_p(const BigInt& v) const { return group().power_b_p(v, m_key->private_key(), m_key_bits); }
237✔
126

127
      std::shared_ptr<const DL_PrivateKey> m_key;
128
      const size_t m_key_bits;
129
      Blinder m_blinder;
130
};
131

132
secure_vector<uint8_t> DH_KA_Operation::raw_agree(const uint8_t w[], size_t w_len) {
1,831✔
133
   BigInt v = BigInt::from_bytes(std::span{w, w_len});
1,831✔
134

135
   if(v <= 1 || v >= group().get_p()) {
1,831✔
136
      throw Invalid_Argument("DH agreement - invalid key provided");
2✔
137
   }
138

139
   v = m_blinder.blind(v);
1,829✔
140
   v = powermod_x_p(v);
1,829✔
141
   v = m_blinder.unblind(v);
1,829✔
142

143
   return v.serialize<secure_vector<uint8_t>>(group().p_bytes());
3,658✔
144
}
1,831✔
145

146
}  // namespace
147

148
std::unique_ptr<PK_Ops::Key_Agreement> DH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
357✔
149
                                                                              std::string_view params,
150
                                                                              std::string_view provider) const {
151
   if(provider == "base" || provider.empty()) {
397✔
152
      return std::make_unique<DH_KA_Operation>(this->m_private_key, params, rng);
237✔
153
   }
154
   throw Provider_Not_Found(algo_name(), provider);
240✔
155
}
156

157
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc