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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

96.83
/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) {
13✔
17
   m_public_key = std::make_shared<DL_PublicKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
13✔
18
}
13✔
19

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

24
std::vector<uint8_t> DH_PublicKey::public_value() const { return m_public_key->public_key_as_bytes(); }
310✔
25

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

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

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

34
AlgorithmIdentifier DH_PublicKey::algorithm_identifier() const {
18✔
35
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_42));
36✔
36
}
37

38
std::vector<uint8_t> DH_PublicKey::public_key_bits() const { return m_public_key->DER_encode(); }
5✔
39

40
bool DH_PublicKey::check_key(RandomNumberGenerator& rng, bool strong) const {
11✔
41
   return m_public_key->check_key(rng, strong);
11✔
42
}
43

44
DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
34✔
45
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
34✔
46
   m_public_key = m_private_key->public_key();
34✔
47
}
34✔
48

49
DH_PrivateKey::DH_PrivateKey(const DL_Group& group, const BigInt& x) {
187✔
50
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
187✔
51
   m_public_key = m_private_key->public_key();
187✔
52
}
187✔
53

54
DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
18✔
55
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
18✔
56
   m_public_key = m_private_key->public_key();
6✔
57
}
18✔
58

59
std::unique_ptr<Public_Key> DH_PrivateKey::public_key() const {
3✔
60
   return std::unique_ptr<DH_PublicKey>(new DH_PublicKey(m_public_key));
6✔
61
}
62

63
std::vector<uint8_t> DH_PrivateKey::public_value() const { return DH_PublicKey::public_value(); }
260✔
64

65
secure_vector<uint8_t> DH_PrivateKey::private_key_bits() const { return m_private_key->DER_encode(); }
13✔
66

67
secure_vector<uint8_t> DH_PrivateKey::raw_private_key_bits() const { return m_private_key->raw_private_key_bits(); }
×
68

69
const BigInt& DH_PrivateKey::get_int_field(std::string_view field) const {
11✔
70
   return m_private_key->get_int_field(algo_name(), field);
11✔
71
}
72

73
namespace {
74

75
/**
76
* DH operation
77
*/
78
class DH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
×
79
   public:
80
      DH_KA_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
219✔
81
                      std::string_view kdf,
82
                      RandomNumberGenerator& rng) :
219✔
83
            PK_Ops::Key_Agreement_with_KDF(kdf),
84
            m_key(key),
438✔
85
            m_key_bits(m_key->private_key().bits()),
219✔
86
            m_blinder(
438✔
87
               m_key->group().get_p(),
219✔
88
               rng,
89
               [](const BigInt& k) { return k; },
219✔
90
               [this](const BigInt& k) {
438✔
91
                  const BigInt inv_k = inverse_mod(k, group().get_p());
219✔
92
                  return powermod_x_p(inv_k);
219✔
93
               }) {}
657✔
94

95
      size_t agreed_value_size() const override { return group().p_bytes(); }
72✔
96

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

99
   private:
100
      const DL_Group& group() const { return m_key->group(); }
1,996✔
101

102
      BigInt powermod_x_p(const BigInt& v) const { return group().power_b_p(v, m_key->private_key(), m_key_bits); }
219✔
103

104
      std::shared_ptr<const DL_PrivateKey> m_key;
105
      std::shared_ptr<const Montgomery_Params> m_monty_p;
106
      const size_t m_key_bits;
107
      Blinder m_blinder;
108
};
109

110
secure_vector<uint8_t> DH_KA_Operation::raw_agree(const uint8_t w[], size_t w_len) {
1,742✔
111
   BigInt v = BigInt::decode(w, w_len);
1,742✔
112

113
   if(v <= 1 || v >= group().get_p())
1,742✔
114
      throw Invalid_Argument("DH agreement - invalid key provided");
2✔
115

116
   v = m_blinder.blind(v);
1,740✔
117
   v = powermod_x_p(v);
1,740✔
118
   v = m_blinder.unblind(v);
1,740✔
119

120
   return BigInt::encode_1363(v, group().p_bytes());
1,740✔
121
}
1,740✔
122

123
}
124

125
std::unique_ptr<PK_Ops::Key_Agreement> DH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
339✔
126
                                                                              std::string_view params,
127
                                                                              std::string_view provider) const {
128
   if(provider == "base" || provider.empty())
379✔
129
      return std::make_unique<DH_KA_Operation>(this->m_private_key, params, rng);
219✔
130
   throw Provider_Not_Found(algo_name(), provider);
240✔
131
}
132

133
}
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