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

randombit / botan / 16286629150

15 Jul 2025 07:13AM UTC coverage: 90.623% (-0.001%) from 90.624%
16286629150

Pull #4991

github

web-flow
Merge 0595e308a into e18675f45
Pull Request #4991: FIX: Cleanup of pcurves ECC private keys

99637 of 109947 relevant lines covered (90.62%)

12340770.64 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,091✔
14
   const auto* p = dynamic_cast<const EC_Scalar_Data_PC*>(&data);
266,091✔
15
   if(p == nullptr) {
266,091✔
16
      throw Invalid_State("Failed conversion to EC_Scalar_Data_PC");
×
17
   }
18
   return *p;
266,091✔
19
}
20

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

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

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

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

38
bool EC_Scalar_Data_PC::is_eq(const EC_Scalar_Data& other) const {
3,144✔
39
   const auto& pcurve = group()->pcurve();
3,144✔
40
   return pcurve.scalar_equal(m_v, checked_ref(other).m_v);
3,144✔
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::zap() {
8,079✔
48
   m_v._zap();
8,079✔
49
}
8,079✔
50

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

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

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

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

68
std::unique_ptr<EC_Scalar_Data> EC_Scalar_Data_PC::add(const EC_Scalar_Data& other) const {
19,009✔
69
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_add(m_v, checked_ref(other).m_v));
19,009✔
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,312✔
77
   return std::make_unique<EC_Scalar_Data_PC>(m_group, group()->pcurve().scalar_mul(m_v, checked_ref(other).m_v));
113,312✔
78
}
79

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

212
bool EC_Mul2Table_Data_PC::mul2_vartime_x_mod_order_eq(const EC_Scalar_Data& vd,
23,345✔
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,345✔
216

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

221
   return m_group->pcurve().mul2_vartime_x_mod_order_eq(*m_tbl, v.value(), x.value(), y.value());
23,345✔
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