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

randombit / botan / 16311760011

16 Jul 2025 06:06AM UTC coverage: 90.63% (+0.007%) from 90.623%
16311760011

Pull #4991

github

web-flow
Merge 8289e33b3 into 6eea3cee4
Pull Request #4991: Scrub pcurves ECC private key values on destruction

99650 of 109952 relevant lines covered (90.63%)

12352068.79 hits per line

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

95.52
/src/lib/pubkey/ec_group/ec_inner_pc.cpp
1
/*
2
* (C) 2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/internal/ec_inner_pc.h>
8

9
#include <botan/mem_ops.h>
10

11
namespace Botan {
12

13
const EC_Scalar_Data_PC& EC_Scalar_Data_PC::checked_ref(const EC_Scalar_Data& data) {
266,043✔
14
   const auto* p = dynamic_cast<const EC_Scalar_Data_PC*>(&data);
266,043✔
15
   if(p == nullptr) {
266,043✔
16
      throw Invalid_State("Failed conversion to EC_Scalar_Data_PC");
×
17
   }
18
   return *p;
266,043✔
19
}
20

21
const std::shared_ptr<const EC_Group_Data>& EC_Scalar_Data_PC::group() const {
161,136✔
22
   return m_group;
61,717✔
23
}
24

25
size_t EC_Scalar_Data_PC::bytes() const {
99,396✔
26
   return this->group()->order_bytes();
99,396✔
27
}
28

29
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::clone() const {
61,717✔
30
   return std::make_unique<EC_Scalar_Data_PC>(this->group(), this->value());
61,717✔
31
}
32

33
bool EC_Scalar_Data_PC::is_zero() const {
67,724✔
34
   const auto& pcurve = this->group()->pcurve();
67,724✔
35
   return pcurve.scalar_is_zero(m_v);
67,724✔
36
}
37

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
3,142✔
39
   const auto& pcurve = group()->pcurve();
3,142✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
3,142✔
41
}
42

43
void EC_Scalar_Data_PC::assign(const EC_Scalar_Data& other) {
×
44
   m_v = checked_ref(other).value();
×
45
}
×
46

47
void EC_Scalar_Data_PC::zeroize() {
8,103✔
48
   m_v._zeroize();
8,103✔
49
}
8,103✔
50

51
void EC_Scalar_Data_PC::square_self() {
11,122✔
52
   // TODO square in place
53
   m_v = m_group->pcurve().scalar_square(m_v);
11,122✔
54
}
11,122✔
55

56
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::negate() const {
12,721✔
57
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_negate(m_v));
12,721✔
58
}
59

60
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert() const {
17,155✔
61
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert(m_v));
17,155✔
62
}
63

64
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::invert_vartime() const {
26,380✔
65
   return std::make_unique<EC_Scalar_Data_PC>(m_group, m_group->pcurve().scalar_invert_vartime(m_v));
26,380✔
66
}
67

68
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
19,007✔
69
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_add(m_v, checked_ref(other).m_v));
19,007✔
70
}
71

72
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::sub(const EC_Scalar_Data& other) const {
12,792✔
73
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_sub(m_v, checked_ref(other).m_v));
12,792✔
74
}
75

76
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::mul(const EC_Scalar_Data& other) const {
113,296✔
77
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_mul(m_v, checked_ref(other).m_v));
113,296✔
78
}
79

80
void EC_Scalar_Data_PC::serialize_to(std::span<uint8_t> bytes) const {
102,065✔
81
   BOTAN_ARG_CHECK(bytes.size() == m_group->order_bytes(), "Invalid output length");
102,065✔
82
   m_group->pcurve().serialize_scalar(bytes, m_v);
102,065✔
83
}
102,065✔
84

85
EC_AffinePoint_Data_PC::EC_AffinePoint_Data_PC(std::shared_ptr<const EC_Group_Data> group,
103,712✔
86
                                               PCurve::PrimeOrderCurve::AffinePoint pt) :
103,712✔
87
      m_group(std::move(group)), m_pt(std::move(pt)) {
103,712✔
88
   const auto& pcurve = m_group->pcurve();
103,712✔
89

90
   if(!pcurve.affine_point_is_identity(m_pt)) {
103,712✔
91
      m_xy.resize(1 + 2 * field_element_bytes());
103,184✔
92
      pcurve.serialize_point(m_xy, m_pt);
103,184✔
93
   }
94
}
103,712✔
95

96
const EC_AffinePoint_Data_PC& EC_AffinePoint_Data_PC::checked_ref(const EC_AffinePoint_Data& data) {
43,533✔
97
   const auto* p = dynamic_cast<const EC_AffinePoint_Data_PC*>(&data);
43,533✔
98
   if(p == nullptr) {
43,533✔
99
      throw Invalid_State("Failed conversion to EC_AffinePoint_Data_PC");
×
100
   }
101
   return *p;
43,533✔
102
}
103

104
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::clone() const {
17,984✔
105
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, m_pt);
17,984✔
106
}
107

108
const std::shared_ptr<const EC_Group_Data>& EC_AffinePoint_Data_PC::group() const {
64,730✔
109
   return m_group;
64,730✔
110
}
111

112
std::unique_ptr<EC_AffinePoint_Data> EC_AffinePoint_Data_PC::mul(const EC_Scalar_Data& scalar,
13,115✔
113
                                                                 RandomNumberGenerator& rng) const {
114
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
13,115✔
115
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
13,115✔
116
   const auto& pcurve = m_group->pcurve();
13,115✔
117
   auto pt = pcurve.point_to_affine(pcurve.mul(m_pt, k, rng));
13,115✔
118
   return std::make_unique<EC_AffinePoint_Data_PC>(m_group, std::move(pt));
13,115✔
119
}
13,115✔
120

121
secure_vector<uint8_t> EC_AffinePoint_Data_PC::mul_x_only(const EC_Scalar_Data& scalar,
5,073✔
122
                                                          RandomNumberGenerator& rng) const {
123
   BOTAN_ARG_CHECK(scalar.group() == m_group, "Curve mismatch");
5,073✔
124
   const auto& k = EC_Scalar_Data_PC::checked_ref(scalar).value();
5,073✔
125
   return m_group->pcurve().mul_x_only(m_pt, k, rng);
5,073✔
126
}
127

128
size_t EC_AffinePoint_Data_PC::field_element_bytes() const {
222,263✔
129
   return m_group->pcurve().field_element_bytes();
222,263✔
130
}
131

132
bool EC_AffinePoint_Data_PC::is_identity() const {
154,099✔
133
   return m_xy.empty();
154,099✔
134
}
135

136
void EC_AffinePoint_Data_PC::serialize_x_to(std::span<uint8_t> bytes) const {
11,274✔
137
   BOTAN_STATE_CHECK(!this->is_identity());
11,274✔
138
   const size_t fe_bytes = this->field_element_bytes();
11,274✔
139
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
11,274✔
140
   copy_mem(bytes, std::span{m_xy}.subspan(1, fe_bytes));
11,274✔
141
}
11,274✔
142

143
void EC_AffinePoint_Data_PC::serialize_y_to(std::span<uint8_t> bytes) const {
71✔
144
   BOTAN_STATE_CHECK(!this->is_identity());
71✔
145
   const size_t fe_bytes = this->field_element_bytes();
71✔
146
   BOTAN_ARG_CHECK(bytes.size() == fe_bytes, "Invalid output size");
71✔
147
   copy_mem(bytes, std::span{m_xy}.subspan(1 + fe_bytes, fe_bytes));
71✔
148
}
71✔
149

150
void EC_AffinePoint_Data_PC::serialize_xy_to(std::span<uint8_t> bytes) const {
230✔
151
   BOTAN_STATE_CHECK(!this->is_identity());
230✔
152
   const size_t fe_bytes = this->field_element_bytes();
230✔
153
   BOTAN_ARG_CHECK(bytes.size() == 2 * fe_bytes, "Invalid output size");
230✔
154
   copy_mem(bytes, std::span{m_xy}.last(2 * fe_bytes));
230✔
155
}
230✔
156

157
void EC_AffinePoint_Data_PC::serialize_compressed_to(std::span<uint8_t> bytes) const {
10,678✔
158
   BOTAN_STATE_CHECK(!this->is_identity());
10,678✔
159
   const size_t fe_bytes = this->field_element_bytes();
10,678✔
160
   BOTAN_ARG_CHECK(bytes.size() == 1 + fe_bytes, "Invalid output size");
10,678✔
161
   const bool y_is_odd = (m_xy.back() & 0x01) == 0x01;
10,678✔
162

163
   BufferStuffer stuffer(bytes);
10,678✔
164
   stuffer.append(y_is_odd ? 0x03 : 0x02);
16,112✔
165
   this->serialize_x_to(stuffer.next(fe_bytes));
10,678✔
166
}
10,678✔
167

168
void EC_AffinePoint_Data_PC::serialize_uncompressed_to(std::span<uint8_t> bytes) const {
27,995✔
169
   BOTAN_STATE_CHECK(!this->is_identity());
27,995✔
170
   const size_t fe_bytes = this->field_element_bytes();
27,995✔
171
   BOTAN_ARG_CHECK(bytes.size() == 1 + 2 * fe_bytes, "Invalid output size");
27,995✔
172
   copy_mem(bytes, m_xy);
27,995✔
173
}
27,995✔
174

175
#if defined(BOTAN_HAS_LEGACY_EC_POINT)
176
EC_Point EC_AffinePoint_Data_PC::to_legacy_point() const {
29,491✔
177
   if(this->is_identity()) {
29,491✔
178
      return EC_Point(m_group->curve());
216✔
179
   } else {
180
      const size_t fe_bytes = this->field_element_bytes();
29,275✔
181
      return EC_Point(m_group->curve(),
29,275✔
182
                      BigInt::from_bytes(std::span{m_xy}.subspan(1, fe_bytes)),
58,550✔
183
                      BigInt::from_bytes(std::span{m_xy}.last(fe_bytes)));
87,825✔
184
   }
185
}
186
#endif
187

188
EC_Mul2Table_Data_PC::EC_Mul2Table_Data_PC(const EC_AffinePoint_Data& q) : m_group(q.group()) {
14,653✔
189
   BOTAN_ARG_CHECK(q.group() == m_group, "Curve mismatch");
14,653✔
190

191
   const auto& pt_q = EC_AffinePoint_Data_PC::checked_ref(q);
14,653✔
192

193
   m_tbl = m_group->pcurve().mul2_setup_g(pt_q.value());
14,653✔
194
}
14,653✔
195

196
std::unique_ptr<EC_AffinePoint_Data> EC_Mul2Table_Data_PC::mul2_vartime(const EC_Scalar_Data& xd,
1,865✔
197
                                                                        const EC_Scalar_Data& yd) const {
198
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
1,865✔
199

200
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
1,865✔
201
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
1,865✔
202

203
   const auto& pcurve = m_group->pcurve();
1,865✔
204

205
   if(auto pt = pcurve.mul2_vartime(*m_tbl, x.value(), y.value())) {
1,865✔
206
      return std::make_unique<EC_AffinePoint_Data_PC>(m_group, pcurve.point_to_affine(*pt));
1,865✔
207
   } else {
208
      return nullptr;
×
209
   }
1,865✔
210
}
211

212
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
23,343✔
213
                                                       const EC_Scalar_Data& xd,
214
                                                       const EC_Scalar_Data& yd) const {
215
   BOTAN_ARG_CHECK(xd.group() == m_group && yd.group() == m_group, "Curve mismatch");
23,343✔
216

217
   const auto& v = EC_Scalar_Data_PC::checked_ref(vd);
23,343✔
218
   const auto& x = EC_Scalar_Data_PC::checked_ref(xd);
23,343✔
219
   const auto& y = EC_Scalar_Data_PC::checked_ref(yd);
23,343✔
220

221
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
23,343✔
222
}
223

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