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

randombit / botan / 5454043422

04 Jul 2023 11:13AM UTC coverage: 91.659% (-0.07%) from 91.732%
5454043422

Pull #3609

github

web-flow
Merge 5741e183c into cf8d8a6ca
Pull Request #3609: [TLS 1.3] Hybrid PQ/T key establishment

78635 of 85791 relevant lines covered (91.66%)

12300857.61 hits per line

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

95.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) {
56✔
21
   m_public_key = std::make_shared<DL_PublicKey>(group, y);
56✔
22
}
56✔
23

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

28
size_t DH_PublicKey::estimated_strength() const {
2✔
29
   return m_public_key->estimated_strength();
2✔
30
}
31

32
size_t DH_PublicKey::key_length() const {
9✔
33
   return m_public_key->p_bits();
9✔
34
}
35

36
const BigInt& DH_PublicKey::get_int_field(std::string_view field) const {
6✔
37
   return m_public_key->get_int_field(algo_name(), field);
6✔
38
}
39

40
const DL_Group& DH_PublicKey::group() const {
41✔
41
   return m_public_key->group();
41✔
42
}
43

44
AlgorithmIdentifier DH_PublicKey::algorithm_identifier() const {
43✔
45
   return AlgorithmIdentifier(object_identifier(), m_public_key->group().DER_encode(DL_Group_Format::ANSI_X9_42));
86✔
46
}
47

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

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

56
DH_PrivateKey::DH_PrivateKey(RandomNumberGenerator& rng, const DL_Group& group) {
38✔
57
   m_private_key = std::make_shared<DL_PrivateKey>(group, rng);
38✔
58
   m_public_key = m_private_key->public_key();
38✔
59
}
38✔
60

61
DH_PrivateKey::DH_PrivateKey(const DL_Group& group, const BigInt& x) {
187✔
62
   m_private_key = std::make_shared<DL_PrivateKey>(group, x);
187✔
63
   m_public_key = m_private_key->public_key();
187✔
64
}
187✔
65

66
DH_PrivateKey::DH_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) {
44✔
67
   m_private_key = std::make_shared<DL_PrivateKey>(alg_id, key_bits, DL_Group_Format::ANSI_X9_42);
44✔
68
   m_public_key = m_private_key->public_key();
31✔
69
}
44✔
70

71
std::unique_ptr<Public_Key> DH_PrivateKey::public_key() const {
18✔
72
   return std::unique_ptr<DH_PublicKey>(new DH_PublicKey(m_public_key));
36✔
73
}
74

75
std::vector<uint8_t> DH_PrivateKey::public_value() const {
271✔
76
   return DH_PublicKey::public_value();
271✔
77
}
78

79
secure_vector<uint8_t> DH_PrivateKey::private_key_bits() const {
38✔
80
   return m_private_key->DER_encode();
38✔
81
}
82

83
secure_vector<uint8_t> DH_PrivateKey::raw_private_key_bits() const {
×
84
   return m_private_key->raw_private_key_bits();
×
85
}
86

87
const BigInt& DH_PrivateKey::get_int_field(std::string_view field) const {
9✔
88
   return m_private_key->get_int_field(algo_name(), field);
9✔
89
}
90

91
namespace {
92

93
/**
94
* DH operation
95
*/
96
class DH_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF {
×
97
   public:
98
      DH_KA_Operation(const std::shared_ptr<const DL_PrivateKey>& key,
231✔
99
                      std::string_view kdf,
100
                      RandomNumberGenerator& rng) :
231✔
101
            PK_Ops::Key_Agreement_with_KDF(kdf),
102
            m_key(key),
462✔
103
            m_key_bits(m_key->private_key().bits()),
231✔
104
            m_blinder(
462✔
105
               m_key->group().get_p(),
231✔
106
               rng,
107
               [](const BigInt& k) { return k; },
231✔
108
               [this](const BigInt& k) {
462✔
109
                  const BigInt inv_k = inverse_mod(k, group().get_p());
231✔
110
                  return powermod_x_p(inv_k);
231✔
111
               }) {}
693✔
112

113
      size_t agreed_value_size() const override { return group().p_bytes(); }
152✔
114

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

117
   private:
118
      const DL_Group& group() const { return m_key->group(); }
2,056✔
119

120
      BigInt powermod_x_p(const BigInt& v) const { return group().power_b_p(v, m_key->private_key(), m_key_bits); }
231✔
121

122
      std::shared_ptr<const DL_PrivateKey> m_key;
123
      std::shared_ptr<const Montgomery_Params> m_monty_p;
124
      const size_t m_key_bits;
125
      Blinder m_blinder;
126
};
127

128
secure_vector<uint8_t> DH_KA_Operation::raw_agree(const uint8_t w[], size_t w_len) {
1,750✔
129
   BigInt v = BigInt::decode(w, w_len);
1,750✔
130

131
   if(v <= 1 || v >= group().get_p()) {
1,750✔
132
      throw Invalid_Argument("DH agreement - invalid key provided");
2✔
133
   }
134

135
   v = m_blinder.blind(v);
1,748✔
136
   v = powermod_x_p(v);
1,748✔
137
   v = m_blinder.unblind(v);
1,748✔
138

139
   return BigInt::encode_1363(v, group().p_bytes());
1,748✔
140
}
1,748✔
141

142
}  // namespace
143

144
std::unique_ptr<PK_Ops::Key_Agreement> DH_PrivateKey::create_key_agreement_op(RandomNumberGenerator& rng,
351✔
145
                                                                              std::string_view params,
146
                                                                              std::string_view provider) const {
147
   if(provider == "base" || provider.empty()) {
391✔
148
      return std::make_unique<DH_KA_Operation>(this->m_private_key, params, rng);
231✔
149
   }
150
   throw Provider_Not_Found(algo_name(), provider);
240✔
151
}
152

153
}  // 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

© 2025 Coveralls, Inc