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

randombit / botan / 11856050109

15 Nov 2024 12:13PM UTC coverage: 91.073% (-0.002%) from 91.075%
11856050109

push

github

web-flow
Merge pull request #4433 from randombit/jack/mul-px-qy

Add a constant time p*x + q*y ECC operation

90625 of 99508 relevant lines covered (91.07%)

9482119.51 hits per line

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

93.33
/src/lib/pubkey/ec_group/ec_inner_data.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_data.h>
8

9
#include <botan/der_enc.h>
10
#include <botan/internal/ec_inner_bn.h>
11
#include <botan/internal/ec_inner_pc.h>
12
#include <botan/internal/pcurves.h>
13
#include <botan/internal/point_mul.h>
14

15
namespace Botan {
16

17
EC_Group_Data::~EC_Group_Data() = default;
9,346✔
18

19
EC_Group_Data::EC_Group_Data(const BigInt& p,
1,170✔
20
                             const BigInt& a,
21
                             const BigInt& b,
22
                             const BigInt& g_x,
23
                             const BigInt& g_y,
24
                             const BigInt& order,
25
                             const BigInt& cofactor,
26
                             const OID& oid,
27
                             EC_Group_Source source) :
1,170✔
28
      m_curve(p, a, b),
1,170✔
29
      m_base_point(m_curve, g_x, g_y),
2,340✔
30
      m_g_x(g_x),
1,169✔
31
      m_g_y(g_y),
1,169✔
32
      m_order(order),
1,169✔
33
      m_cofactor(cofactor),
1,169✔
34
      m_mod_order(order),
1,169✔
35
      m_oid(oid),
1,169✔
36
      m_p_bits(p.bits()),
1,169✔
37
      m_order_bits(order.bits()),
1,169✔
38
      m_order_bytes((m_order_bits + 7) / 8),
1,169✔
39
      m_a_is_minus_3(a == p - 3),
2,338✔
40
      m_a_is_zero(a.is_zero()),
1,169✔
41
      m_has_cofactor(m_cofactor != 1),
1,169✔
42
      m_order_is_less_than_p(m_order < p),
1,169✔
43
      m_source(source) {
2,339✔
44
   if(!m_oid.empty()) {
1,169✔
45
      DER_Encoder der(m_der_named_curve);
1,157✔
46
      der.encode(m_oid);
1,157✔
47

48
      if(const auto id = PCurve::PrimeOrderCurveId::from_oid(m_oid)) {
1,157✔
49
         m_pcurve = PCurve::PrimeOrderCurve::from_id(*id);
917✔
50
         // still possibly null, if the curve is supported in general but not
51
         // available in the build
52
      }
53
   }
1,157✔
54

55
   if(!m_pcurve) {
1,169✔
56
      m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(m_base_point, m_mod_order);
252✔
57
   }
58
}
1,171✔
59

60
bool EC_Group_Data::params_match(const BigInt& p,
934✔
61
                                 const BigInt& a,
62
                                 const BigInt& b,
63
                                 const BigInt& g_x,
64
                                 const BigInt& g_y,
65
                                 const BigInt& order,
66
                                 const BigInt& cofactor) const {
67
   return (this->p() == p && this->a() == a && this->b() == b && this->order() == order &&
1,128✔
68
           this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
1,022✔
69
}
70

71
bool EC_Group_Data::params_match(const EC_Group_Data& other) const {
21✔
72
   return params_match(other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
21✔
73
}
74

75
void EC_Group_Data::set_oid(const OID& oid) {
6✔
76
   BOTAN_ARG_CHECK(!oid.empty(), "OID should be set");
6✔
77
   BOTAN_STATE_CHECK(m_oid.empty() && m_der_named_curve.empty());
6✔
78
   m_oid = oid;
6✔
79

80
   DER_Encoder der(m_der_named_curve);
6✔
81
   der.encode(m_oid);
6✔
82
}
6✔
83

84
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_with_trunc(std::span<const uint8_t> bytes) const {
26,056✔
85
   const size_t bit_length = 8 * bytes.size();
26,056✔
86

87
   if(bit_length < order_bits()) {
26,056✔
88
      // No shifting required, but might still need to reduce by modulus
89
      return this->scalar_from_bytes_mod_order(bytes);
5,256✔
90
   } else {
91
      const size_t shift = bit_length - order_bits();
20,800✔
92

93
      const size_t new_length = bytes.size() - (shift / 8);
20,800✔
94
      const size_t bit_shift = shift % 8;
20,800✔
95

96
      if(bit_shift == 0) {
20,800✔
97
         // Easy case just read different bytes
98
         return this->scalar_from_bytes_mod_order(bytes.first(new_length));
18,175✔
99
      } else {
100
         std::vector<uint8_t> sbytes(new_length);
2,625✔
101

102
         uint8_t carry = 0;
2,625✔
103
         for(size_t i = 0; i != new_length; ++i) {
71,758✔
104
            const uint8_t w = bytes[i];
69,133✔
105
            sbytes[i] = (w >> bit_shift) | carry;
69,133✔
106
            carry = w << (8 - bit_shift);
69,133✔
107
         }
108

109
         return this->scalar_from_bytes_mod_order(sbytes);
2,625✔
110
      }
2,625✔
111
   }
112
}
113

114
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bytes_mod_order(std::span<const uint8_t> bytes) const {
26,780✔
115
   if(bytes.size() >= 2 * order_bytes()) {
26,780✔
116
      return {};
×
117
   }
118

119
   if(m_pcurve) {
26,780✔
120
      if(auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
19,793✔
121
         return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
19,793✔
122
      } else {
123
         return {};
×
124
      }
19,793✔
125
   } else {
126
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), mod_order(BigInt(bytes)));
20,961✔
127
   }
128
}
129

130
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_random(RandomNumberGenerator& rng) const {
6,868✔
131
   if(m_pcurve) {
6,868✔
132
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->random_scalar(rng));
3,422✔
133
   } else {
134
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
6,892✔
135
                                                 BigInt::random_integer(rng, BigInt::one(), m_order));
13,784✔
136
   }
137
}
138

139
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_zero() const {
×
140
   if(m_pcurve) {
×
141
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_zero());
×
142
   } else {
143
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::zero());
×
144
   }
145
}
146

147
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_one() const {
45✔
148
   if(m_pcurve) {
45✔
149
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_one());
37✔
150
   } else {
151
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::one());
8✔
152
   }
153
}
154

155
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_from_bigint(const BigInt& bn) const {
8,081✔
156
   if(bn <= 0 || bn >= m_order) {
8,081✔
157
      return {};
×
158
   }
159

160
   if(m_pcurve) {
8,081✔
161
      return this->scalar_deserialize(bn.serialize(m_order_bytes));
13,546✔
162
   } else {
163
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
1,308✔
164
   }
165
}
166

167
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::gk_x_mod_order(const EC_Scalar_Data& scalar,
3,306✔
168
                                                              RandomNumberGenerator& rng,
169
                                                              std::vector<BigInt>& ws) const {
170
   if(m_pcurve) {
3,306✔
171
      const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
1,685✔
172
      auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
1,685✔
173
      return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), gk_x_mod_order);
1,685✔
174
   } else {
1,685✔
175
      const auto& k = EC_Scalar_Data_BN::checked_ref(scalar);
1,621✔
176
      BOTAN_STATE_CHECK(m_base_mult != nullptr);
1,621✔
177
      const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
1,621✔
178

179
      if(pt.is_zero()) {
3,242✔
180
         return scalar_zero();
×
181
      } else {
182
         return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), mod_order(pt.get_affine_x()));
4,863✔
183
      }
184
   }
1,621✔
185
}
186

187
std::unique_ptr<EC_Scalar_Data> EC_Group_Data::scalar_deserialize(std::span<const uint8_t> bytes) const {
66,621✔
188
   if(bytes.size() != m_order_bytes) {
66,621✔
189
      return nullptr;
6,558✔
190
   }
191

192
   if(m_pcurve) {
60,063✔
193
      if(auto s = m_pcurve->deserialize_scalar(bytes)) {
46,888✔
194
         return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
44,708✔
195
      } else {
196
         return nullptr;
2,180✔
197
      }
46,888✔
198
   } else {
199
      BigInt r(bytes);
13,175✔
200

201
      if(r.is_zero() || r >= m_order) {
26,350✔
202
         return nullptr;
673✔
203
      }
204

205
      return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
12,502✔
206
   }
13,175✔
207
}
208

209
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_deserialize(std::span<const uint8_t> bytes) const {
49,013✔
210
   try {
49,013✔
211
      if(m_pcurve) {
49,013✔
212
         if(auto pt = m_pcurve->deserialize_point(bytes)) {
41,479✔
213
            return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
35,500✔
214
         } else {
215
            return nullptr;
5,979✔
216
         }
41,479✔
217
      } else {
218
         auto pt = Botan::OS2ECP(bytes.data(), bytes.size(), curve());
7,534✔
219
         return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
7,440✔
220
      }
7,440✔
221
   } catch(...) {
94✔
222
      return nullptr;
94✔
223
   }
94✔
224
}
225

226
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_ro(std::string_view hash_fn,
24✔
227
                                                                           std::span<const uint8_t> input,
228
                                                                           std::span<const uint8_t> domain_sep) const {
229
   if(m_pcurve) {
24✔
230
      auto pt = m_pcurve->hash_to_curve_ro(hash_fn, input, domain_sep);
24✔
231
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
24✔
232
   } else {
24✔
233
      throw Not_Implemented("Hash to curve is not implemented for this curve");
×
234
   }
235
}
236

237
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_hash_to_curve_nu(std::string_view hash_fn,
33✔
238
                                                                           std::span<const uint8_t> input,
239
                                                                           std::span<const uint8_t> domain_sep) const {
240
   if(m_pcurve) {
33✔
241
      auto pt = m_pcurve->hash_to_curve_nu(hash_fn, input, domain_sep);
33✔
242
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
30✔
243
   } else {
30✔
244
      throw Not_Implemented("Hash to curve is not implemented for this curve");
×
245
   }
246
}
247

248
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::point_g_mul(const EC_Scalar_Data& scalar,
8,629✔
249
                                                                RandomNumberGenerator& rng,
250
                                                                std::vector<BigInt>& ws) const {
251
   if(m_pcurve) {
8,629✔
252
      const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
5,309✔
253
      auto pt = m_pcurve->mul_by_g(k.value(), rng).to_affine();
10,618✔
254
      return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
5,309✔
255
   } else {
5,309✔
256
      const auto& group = scalar.group();
3,320✔
257
      const auto& bn = EC_Scalar_Data_BN::checked_ref(scalar);
3,320✔
258

259
      BOTAN_STATE_CHECK(group->m_base_mult != nullptr);
3,320✔
260
      auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
3,320✔
261
      return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
3,320✔
262
   }
3,320✔
263
}
264

265
std::unique_ptr<EC_AffinePoint_Data> EC_Group_Data::mul_px_qy(const EC_AffinePoint_Data& p,
819✔
266
                                                              const EC_Scalar_Data& x,
267
                                                              const EC_AffinePoint_Data& q,
268
                                                              const EC_Scalar_Data& y,
269
                                                              RandomNumberGenerator& rng) const {
270
   if(m_pcurve) {
819✔
271
      auto pt = m_pcurve->mul_px_qy(EC_AffinePoint_Data_PC::checked_ref(p).value(),
1,260✔
272
                                    EC_Scalar_Data_PC::checked_ref(x).value(),
630✔
273
                                    EC_AffinePoint_Data_PC::checked_ref(q).value(),
630✔
274
                                    EC_Scalar_Data_PC::checked_ref(y).value(),
630✔
275
                                    rng);
630✔
276

277
      if(pt) {
630✔
278
         return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt->to_affine());
630✔
279
      } else {
280
         return nullptr;
×
281
      }
282
   } else {
630✔
283
      std::vector<BigInt> ws;
189✔
284
      const auto& group = p.group();
189✔
285

286
      // TODO this could be better!
287
      EC_Point_Var_Point_Precompute p_mul(p.to_legacy_point(), rng, ws);
189✔
288
      EC_Point_Var_Point_Precompute q_mul(q.to_legacy_point(), rng, ws);
189✔
289

290
      const auto order = group->order() * group->cofactor();  // See #3800
189✔
291

292
      auto px = p_mul.mul(EC_Scalar_Data_BN::checked_ref(x).value(), rng, order, ws);
189✔
293
      auto qy = q_mul.mul(EC_Scalar_Data_BN::checked_ref(y).value(), rng, order, ws);
189✔
294

295
      auto px_qy = px + qy;
189✔
296
      px_qy.force_affine();
189✔
297

298
      if(!px_qy.is_zero()) {
189✔
299
         return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
189✔
300
      } else {
301
         return nullptr;
×
302
      }
303
   }
378✔
304
}
305

306
std::unique_ptr<EC_Mul2Table_Data> EC_Group_Data::make_mul2_table(const EC_AffinePoint_Data& h) const {
11,579✔
307
   if(m_pcurve) {
11,579✔
308
      EC_AffinePoint_Data_PC g(shared_from_this(), m_pcurve->generator());
20,804✔
309
      return std::make_unique<EC_Mul2Table_Data_PC>(g, h);
10,402✔
310
   } else {
10,402✔
311
      EC_AffinePoint_Data_BN g(shared_from_this(), this->base_point());
2,354✔
312
      return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
1,177✔
313
   }
1,177✔
314
}
315

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