Botan 3.7.1
Crypto and TLS for C&
Botan::EC_Group_Data Class Referencefinal

#include <ec_inner_data.h>

Inheritance diagram for Botan::EC_Group_Data:

Public Member Functions

const BigInta () const
 
bool a_is_minus_3 () const
 
bool a_is_zero () const
 
std::unique_ptr< EC_AffinePoint_Dataaffine_add (const EC_AffinePoint_Data &p, const EC_AffinePoint_Data &q) const
 
std::unique_ptr< EC_AffinePoint_Dataaffine_neg (const EC_AffinePoint_Data &p) const
 
const BigIntb () const
 
const BigIntcofactor () const
 
const std::vector< uint8_t > & der_named_curve () const
 
 EC_Group_Data (const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor, const OID &oid, EC_Group_Source source)
 
EC_Group_Engine engine () const
 
const BigIntg_x () const
 
const BigIntg_y () const
 
std::unique_ptr< EC_Scalar_Datagk_x_mod_order (const EC_Scalar_Data &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
 
bool has_cofactor () const
 
std::unique_ptr< EC_Mul2Table_Datamake_mul2_table (const EC_AffinePoint_Data &pt) const
 
std::unique_ptr< EC_AffinePoint_Datamul_px_qy (const EC_AffinePoint_Data &p, const EC_Scalar_Data &x, const EC_AffinePoint_Data &q, const EC_Scalar_Data &y, RandomNumberGenerator &rng) const
 
const OIDoid () const
 
const BigIntorder () const
 
size_t order_bits () const
 
size_t order_bytes () const
 
bool order_is_less_than_p () const
 
const BigIntp () const
 
size_t p_bits () const
 
size_t p_bytes () const
 
size_t p_words () const
 
bool params_match (const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor) const
 
bool params_match (const EC_Group_Data &other) const
 
const PCurve::PrimeOrderCurvepcurve () const
 
std::unique_ptr< EC_AffinePoint_Datapoint_deserialize (std::span< const uint8_t > bytes) const
 
std::unique_ptr< EC_AffinePoint_Datapoint_g_mul (const EC_Scalar_Data &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
 
std::unique_ptr< EC_AffinePoint_Datapoint_hash_to_curve_nu (std::string_view hash_fn, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const
 
std::unique_ptr< EC_AffinePoint_Datapoint_hash_to_curve_ro (std::string_view hash_fn, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep) const
 
std::unique_ptr< EC_Scalar_Datascalar_deserialize (std::span< const uint8_t > bytes) const
 
std::unique_ptr< EC_Scalar_Datascalar_from_bigint (const BigInt &bn) const
 
std::unique_ptr< EC_Scalar_Datascalar_from_bytes_mod_order (std::span< const uint8_t > bytes) const
 
std::unique_ptr< EC_Scalar_Datascalar_from_bytes_with_trunc (std::span< const uint8_t > bytes) const
 
std::unique_ptr< EC_Scalar_Datascalar_one () const
 
std::unique_ptr< EC_Scalar_Datascalar_random (RandomNumberGenerator &rng) const
 
std::unique_ptr< EC_Scalar_Datascalar_zero () const
 
void set_oid (const OID &oid)
 
EC_Group_Source source () const
 
 ~EC_Group_Data ()
 

Static Public Member Functions

static std::shared_ptr< EC_Group_Datacreate (const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor, const OID &oid, EC_Group_Source source)
 

Detailed Description

Definition at line 128 of file ec_inner_data.h.

Constructor & Destructor Documentation

◆ ~EC_Group_Data()

Botan::EC_Group_Data::~EC_Group_Data ( )
default

◆ EC_Group_Data()

Botan::EC_Group_Data::EC_Group_Data ( const BigInt & p,
const BigInt & a,
const BigInt & b,
const BigInt & g_x,
const BigInt & g_y,
const BigInt & order,
const BigInt & cofactor,
const OID & oid,
EC_Group_Source source )

Note this constructor should only be called by EC_Group_Data::create.

It is only public to allow use of std::make_shared

Definition at line 24 of file ec_inner_data.cpp.

32 :
33 m_p(p),
34 m_a(a),
35 m_b(b),
36 m_g_x(g_x),
37 m_g_y(g_y),
38 m_order(order),
39 m_cofactor(cofactor),
40#if defined(BOTAN_HAS_LEGACY_EC_POINT)
43 m_monty(m_p, m_mod_field),
44#endif
45 m_oid(oid),
46 m_p_words(p.sig_words()),
47 m_p_bits(p.bits()),
48 m_order_bits(order.bits()),
49 m_order_bytes((m_order_bits + 7) / 8),
50 m_a_is_minus_3(a == p - 3),
51 m_a_is_zero(a.is_zero()),
52 m_has_cofactor(m_cofactor != 1),
53 m_order_is_less_than_p(m_order < p),
54 m_source(source) {
55 if(!m_oid.empty()) {
56 DER_Encoder der(m_der_named_curve);
57 der.encode(m_oid);
58
59 if(const auto id = PCurve::PrimeOrderCurveId::from_oid(m_oid)) {
61 if(m_pcurve) {
63 }
64 // still possibly null, if the curve is supported in general but not
65 // available in the build
66 }
67 }
68
69#if defined(BOTAN_HAS_LEGACY_EC_POINT)
71 m_a_r = m_monty.mul(a, m_monty.R2(), ws);
72 m_b_r = m_monty.mul(b, m_monty.R2(), ws);
73 if(!m_pcurve) {
74 m_engine = EC_Group_Engine::Legacy;
75 }
76#else
77 if(!m_pcurve) {
78 if(m_oid.empty()) {
79 throw Not_Implemented("EC_Group this group is not supported in this build configuration");
80 } else {
81 throw Not_Implemented(
82 fmt("EC_Group the group {} is not supported in this build configuration", oid.to_string()));
83 }
84 }
85#endif
86}
size_t sig_words() const
Definition bigint.h:616
size_t bits() const
Definition bigint.cpp:295
bool is_zero() const
Definition bigint.h:458
const BigInt & p() const
const BigInt & g_x() const
const BigInt & a() const
const OID & oid() const
const BigInt & cofactor() const
EC_Group_Source source() const
const BigInt & g_y() const
const BigInt & b() const
const BigInt & order() const
static Modular_Reducer for_public_modulus(const BigInt &m)
Definition reducer.cpp:43
bool empty() const
Definition asn1_obj.h:266
std::string to_string() const
Definition asn1_oid.cpp:125
static std::shared_ptr< const PrimeOrderCurve > from_id(PrimeOrderCurveId id)
Definition pcurves.cpp:101
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
@ Optimized
Using per curve implementation; fastest available.
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

References Botan::OID::empty().

Member Function Documentation

◆ a()

const BigInt & Botan::EC_Group_Data::a ( ) const
inline

Definition at line 160 of file ec_inner_data.h.

160{ return m_a; }

Referenced by params_match().

◆ a_is_minus_3()

bool Botan::EC_Group_Data::a_is_minus_3 ( ) const
inline

Definition at line 200 of file ec_inner_data.h.

200{ return m_a_is_minus_3; }

◆ a_is_zero()

bool Botan::EC_Group_Data::a_is_zero ( ) const
inline

Definition at line 202 of file ec_inner_data.h.

202{ return m_a_is_zero; }

◆ affine_add()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::affine_add ( const EC_AffinePoint_Data & p,
const EC_AffinePoint_Data & q ) const

Definition at line 397 of file ec_inner_data.cpp.

398 {
399 if(m_pcurve) {
400 auto pt = m_pcurve->point_add_mixed(
403
404 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
405 } else {
406#if defined(BOTAN_HAS_LEGACY_EC_POINT)
407 auto pt = p.to_legacy_point() + q.to_legacy_point();
408 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
409#else
410 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
411#endif
412 }
413}
static const EC_AffinePoint_Data_PC & checked_ref(const EC_AffinePoint_Data &data)
static ProjectivePoint from_affine(const AffinePoint &pt)
Definition pcurves.h:246

◆ affine_neg()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::affine_neg ( const EC_AffinePoint_Data & p) const

Definition at line 415 of file ec_inner_data.cpp.

415 {
416 if(m_pcurve) {
417 auto pt = m_pcurve->point_negate(EC_AffinePoint_Data_PC::checked_ref(p).value());
418 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt);
419 } else {
420#if defined(BOTAN_HAS_LEGACY_EC_POINT)
421 auto pt = p.to_legacy_point();
422 pt.negate(); // negates in place
423 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
424#else
425 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
426#endif
427 }
428}

◆ b()

const BigInt & Botan::EC_Group_Data::b ( ) const
inline

Definition at line 162 of file ec_inner_data.h.

162{ return m_b; }

Referenced by params_match().

◆ cofactor()

const BigInt & Botan::EC_Group_Data::cofactor ( ) const
inline

Definition at line 166 of file ec_inner_data.h.

166{ return m_cofactor; }

Referenced by params_match().

◆ create()

std::shared_ptr< EC_Group_Data > Botan::EC_Group_Data::create ( const BigInt & p,
const BigInt & a,
const BigInt & b,
const BigInt & g_x,
const BigInt & g_y,
const BigInt & order,
const BigInt & cofactor,
const OID & oid,
EC_Group_Source source )
static

Definition at line 88 of file ec_inner_data.cpp.

96 {
97 auto group = std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, source);
98
99#if defined(BOTAN_HAS_LEGACY_EC_POINT)
100 group->m_curve = CurveGFp(group.get());
101 group->m_base_point = EC_Point(group->m_curve, g_x, g_y);
102 if(!group->m_pcurve) {
103 group->m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(group->m_base_point, group->m_mod_order);
104 }
105#endif
106
107 return group;
108}

References Botan::b.

◆ der_named_curve()

const std::vector< uint8_t > & Botan::EC_Group_Data::der_named_curve ( ) const
inline

Definition at line 156 of file ec_inner_data.h.

156{ return m_der_named_curve; }

◆ engine()

EC_Group_Engine Botan::EC_Group_Data::engine ( ) const
inline

Definition at line 206 of file ec_inner_data.h.

206{ return m_engine; }

◆ g_x()

const BigInt & Botan::EC_Group_Data::g_x ( ) const
inline

Definition at line 186 of file ec_inner_data.h.

186{ return m_g_x; }

Referenced by params_match().

◆ g_y()

const BigInt & Botan::EC_Group_Data::g_y ( ) const
inline

Definition at line 188 of file ec_inner_data.h.

188{ return m_g_y; }

Referenced by params_match().

◆ gk_x_mod_order()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::gk_x_mod_order ( const EC_Scalar_Data & scalar,
RandomNumberGenerator & rng,
std::vector< BigInt > & ws ) const

Definition at line 237 of file ec_inner_data.cpp.

239 {
240 if(m_pcurve) {
241 const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
242 auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
243 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), gk_x_mod_order);
244 } else {
245#if defined(BOTAN_HAS_LEGACY_EC_POINT)
246 const auto& k = EC_Scalar_Data_BN::checked_ref(scalar);
247 BOTAN_STATE_CHECK(m_base_mult != nullptr);
248 const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
249
250 if(pt.is_zero()) {
251 return scalar_zero();
252 } else {
253 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(pt.get_affine_x()));
254 }
255#else
256 BOTAN_UNUSED(ws);
257 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
258#endif
259 }
260}
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
std::unique_ptr< EC_Scalar_Data > gk_x_mod_order(const EC_Scalar_Data &scalar, RandomNumberGenerator &rng, std::vector< BigInt > &ws) const
std::unique_ptr< EC_Scalar_Data > scalar_zero() const
static const EC_Scalar_Data_BN & checked_ref(const EC_Scalar_Data &data)
static const EC_Scalar_Data_PC & checked_ref(const EC_Scalar_Data &data)

References BOTAN_STATE_CHECK, and BOTAN_UNUSED.

◆ has_cofactor()

bool Botan::EC_Group_Data::has_cofactor ( ) const
inline

Definition at line 184 of file ec_inner_data.h.

184{ return m_has_cofactor; }

◆ make_mul2_table()

std::unique_ptr< EC_Mul2Table_Data > Botan::EC_Group_Data::make_mul2_table ( const EC_AffinePoint_Data & pt) const

Definition at line 430 of file ec_inner_data.cpp.

430 {
431 if(m_pcurve) {
432 return std::make_unique<EC_Mul2Table_Data_PC>(h);
433 } else {
434#if defined(BOTAN_HAS_LEGACY_EC_POINT)
435 EC_AffinePoint_Data_BN g(shared_from_this(), this->base_point());
436 return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
437#else
438 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
439#endif
440 }
441}

◆ mul_px_qy()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::mul_px_qy ( const EC_AffinePoint_Data & p,
const EC_Scalar_Data & x,
const EC_AffinePoint_Data & q,
const EC_Scalar_Data & y,
RandomNumberGenerator & rng ) const

Definition at line 352 of file ec_inner_data.cpp.

356 {
357 if(m_pcurve) {
358 auto pt = m_pcurve->mul_px_qy(EC_AffinePoint_Data_PC::checked_ref(p).value(),
362 rng);
363
364 if(pt) {
365 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt->to_affine());
366 } else {
367 return nullptr;
368 }
369 } else {
370#if defined(BOTAN_HAS_LEGACY_EC_POINT)
371 std::vector<BigInt> ws;
372 const auto& group = p.group();
373
374 // TODO this could be better!
375 EC_Point_Var_Point_Precompute p_mul(p.to_legacy_point(), rng, ws);
376 EC_Point_Var_Point_Precompute q_mul(q.to_legacy_point(), rng, ws);
377
378 const auto order = group->order() * group->cofactor(); // See #3800
379
380 auto px = p_mul.mul(EC_Scalar_Data_BN::checked_ref(x).value(), rng, order, ws);
381 auto qy = q_mul.mul(EC_Scalar_Data_BN::checked_ref(y).value(), rng, order, ws);
382
383 auto px_qy = px + qy;
384
385 if(!px_qy.is_zero()) {
386 px_qy.force_affine();
387 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
388 } else {
389 return nullptr;
390 }
391#else
392 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
393#endif
394 }
395}
BigInt & mul(const BigInt &y, secure_vector< word > &ws)
Definition big_ops2.cpp:150

References Botan::EC_AffinePoint_Data::group(), and Botan::EC_Point_Var_Point_Precompute::mul().

◆ oid()

const OID & Botan::EC_Group_Data::oid ( ) const
inline

Definition at line 154 of file ec_inner_data.h.

154{ return m_oid; }

◆ order()

const BigInt & Botan::EC_Group_Data::order ( ) const
inline

Definition at line 164 of file ec_inner_data.h.

164{ return m_order; }

Referenced by params_match().

◆ order_bits()

size_t Botan::EC_Group_Data::order_bits ( ) const
inline

Definition at line 196 of file ec_inner_data.h.

196{ return m_order_bits; }

◆ order_bytes()

size_t Botan::EC_Group_Data::order_bytes ( ) const
inline

Definition at line 198 of file ec_inner_data.h.

198{ return m_order_bytes; }

◆ order_is_less_than_p()

bool Botan::EC_Group_Data::order_is_less_than_p ( ) const
inline

Definition at line 182 of file ec_inner_data.h.

182{ return m_order_is_less_than_p; }

◆ p()

const BigInt & Botan::EC_Group_Data::p ( ) const
inline

Definition at line 158 of file ec_inner_data.h.

158{ return m_p; }

Referenced by params_match().

◆ p_bits()

size_t Botan::EC_Group_Data::p_bits ( ) const
inline

Definition at line 192 of file ec_inner_data.h.

192{ return m_p_bits; }

◆ p_bytes()

size_t Botan::EC_Group_Data::p_bytes ( ) const
inline

Definition at line 194 of file ec_inner_data.h.

194{ return (m_p_bits + 7) / 8; }

Referenced by Botan::EC_Point::encode(), Botan::EC_Point::x_bytes(), Botan::EC_Point::xy_bytes(), and Botan::EC_Point::y_bytes().

◆ p_words()

size_t Botan::EC_Group_Data::p_words ( ) const
inline

Definition at line 190 of file ec_inner_data.h.

190{ return m_p_words; }

◆ params_match() [1/2]

bool Botan::EC_Group_Data::params_match ( const BigInt & p,
const BigInt & a,
const BigInt & b,
const BigInt & g_x,
const BigInt & g_y,
const BigInt & order,
const BigInt & cofactor ) const

Definition at line 110 of file ec_inner_data.cpp.

116 {
117 return (this->p() == p && this->a() == a && this->b() == b && this->order() == order &&
118 this->cofactor() == cofactor && this->g_x() == g_x && this->g_y() == g_y);
119}

References Botan::b.

◆ params_match() [2/2]

bool Botan::EC_Group_Data::params_match ( const EC_Group_Data & other) const

Definition at line 121 of file ec_inner_data.cpp.

121 {
122 return params_match(other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
123}
bool params_match(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &g_x, const BigInt &g_y, const BigInt &order, const BigInt &cofactor) const

References a(), b(), cofactor(), g_x(), g_y(), order(), and p().

◆ pcurve()

const PCurve::PrimeOrderCurve & Botan::EC_Group_Data::pcurve ( ) const
inline

Definition at line 277 of file ec_inner_data.h.

277 {
278 BOTAN_ASSERT_NONNULL(m_pcurve);
279 return *m_pcurve;
280 }
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86

References BOTAN_ASSERT_NONNULL.

◆ point_deserialize()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::point_deserialize ( std::span< const uint8_t > bytes) const

Deserialize a point

Returns nullptr if the point encoding was invalid or not on the curve

Definition at line 288 of file ec_inner_data.cpp.

288 {
289 try {
290 if(m_pcurve) {
291 if(auto pt = m_pcurve->deserialize_point(bytes)) {
292 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
293 } else {
294 return nullptr;
295 }
296 } else {
297#if defined(BOTAN_HAS_LEGACY_EC_POINT)
298 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), bytes);
299#else
300 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
301#endif
302 }
303 } catch(...) {
304 return nullptr;
305 }
306}

◆ point_g_mul()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::point_g_mul ( const EC_Scalar_Data & scalar,
RandomNumberGenerator & rng,
std::vector< BigInt > & ws ) const

Definition at line 330 of file ec_inner_data.cpp.

332 {
333 if(m_pcurve) {
334 const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
335 auto pt = m_pcurve->mul_by_g(k.value(), rng).to_affine();
336 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
337 } else {
338#if defined(BOTAN_HAS_LEGACY_EC_POINT)
339 const auto& group = scalar.group();
340 const auto& bn = EC_Scalar_Data_BN::checked_ref(scalar);
341
342 BOTAN_STATE_CHECK(group->m_base_mult != nullptr);
343 auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
344 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
345#else
346 BOTAN_UNUSED(ws);
347 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
348#endif
349 }
350}

References BOTAN_STATE_CHECK, BOTAN_UNUSED, and Botan::EC_Scalar_Data::group().

◆ point_hash_to_curve_nu()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::point_hash_to_curve_nu ( std::string_view hash_fn,
std::span< const uint8_t > input,
std::span< const uint8_t > domain_sep ) const

Definition at line 319 of file ec_inner_data.cpp.

321 {
322 if(m_pcurve) {
323 auto pt = m_pcurve->hash_to_curve_nu(hash_fn, input, domain_sep);
324 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
325 } else {
326 throw Not_Implemented("Hash to curve is not implemented for this curve");
327 }
328}

◆ point_hash_to_curve_ro()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::point_hash_to_curve_ro ( std::string_view hash_fn,
std::span< const uint8_t > input,
std::span< const uint8_t > domain_sep ) const

Definition at line 308 of file ec_inner_data.cpp.

310 {
311 if(m_pcurve) {
312 auto pt = m_pcurve->hash_to_curve_ro(hash_fn, input, domain_sep);
313 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt.to_affine());
314 } else {
315 throw Not_Implemented("Hash to curve is not implemented for this curve");
316 }
317}

◆ scalar_deserialize()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_deserialize ( std::span< const uint8_t > bytes) const

Scalar from bytes

This returns a value only if the bytes represent (in big-endian encoding) an integer that is less than n, where n is the group order. It requires that the fixed length encoding (with zero prefix) be used. It also rejects inputs that encode zero. Thus the accepted range is [1,n)

If the input is rejected then nullptr is returned

Definition at line 262 of file ec_inner_data.cpp.

262 {
263 if(bytes.size() != m_order_bytes) {
264 return nullptr;
265 }
266
267 if(m_pcurve) {
268 if(auto s = m_pcurve->deserialize_scalar(bytes)) {
269 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
270 } else {
271 return nullptr;
272 }
273 } else {
274#if defined(BOTAN_HAS_LEGACY_EC_POINT)
275 BigInt r(bytes);
276
277 if(r.is_zero() || r >= m_order) {
278 return nullptr;
279 }
280
281 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
282#else
283 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
284#endif
285 }
286}

References Botan::BigInt::is_zero().

◆ scalar_from_bigint()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_from_bigint ( const BigInt & bn) const

Scalar from BigInt

This returns a value only if bn is in [1,n) where n is the group order. Otherwise it returns nullptr

Definition at line 221 of file ec_inner_data.cpp.

221 {
222 if(bn <= 0 || bn >= m_order) {
223 return {};
224 }
225
226 if(m_pcurve) {
227 return this->scalar_deserialize(bn.serialize(m_order_bytes));
228 } else {
229#if defined(BOTAN_HAS_LEGACY_EC_POINT)
230 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
231#else
232 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
233#endif
234 }
235}
std::unique_ptr< EC_Scalar_Data > scalar_deserialize(std::span< const uint8_t > bytes) const

References Botan::BigInt::serialize().

◆ scalar_from_bytes_mod_order()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_from_bytes_mod_order ( std::span< const uint8_t > bytes) const

Scalar from bytes with modular reduction

This returns a value only if bytes represents (in big-endian encoding) an integer that is at most the square of the scalar group size. Otherwise it returns nullptr.

Definition at line 164 of file ec_inner_data.cpp.

164 {
165 if(bytes.size() > 2 * order_bytes()) {
166 return {};
167 }
168
169 if(m_pcurve) {
170 if(auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
171 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
172 } else {
173 return {};
174 }
175 } else {
176#if defined(BOTAN_HAS_LEGACY_EC_POINT)
177 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(BigInt(bytes)));
178#else
179 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
180#endif
181 }
182}
size_t order_bytes() const

◆ scalar_from_bytes_with_trunc()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_from_bytes_with_trunc ( std::span< const uint8_t > bytes) const

Scalar from bytes with ECDSA style trunction

This should always succeed

Definition at line 134 of file ec_inner_data.cpp.

134 {
135 const size_t bit_length = 8 * bytes.size();
136
137 if(bit_length < order_bits()) {
138 // No shifting required, but might still need to reduce by modulus
139 return this->scalar_from_bytes_mod_order(bytes);
140 } else {
141 const size_t shift = bit_length - order_bits();
142
143 const size_t new_length = bytes.size() - (shift / 8);
144 const size_t bit_shift = shift % 8;
145
146 if(bit_shift == 0) {
147 // Easy case just read different bytes
148 return this->scalar_from_bytes_mod_order(bytes.first(new_length));
149 } else {
150 std::vector<uint8_t> sbytes(new_length);
151
152 uint8_t carry = 0;
153 for(size_t i = 0; i != new_length; ++i) {
154 const uint8_t w = bytes[i];
155 sbytes[i] = (w >> bit_shift) | carry;
156 carry = w << (8 - bit_shift);
157 }
158
159 return this->scalar_from_bytes_mod_order(sbytes);
160 }
161 }
162}
std::unique_ptr< EC_Scalar_Data > scalar_from_bytes_mod_order(std::span< const uint8_t > bytes) const
size_t order_bits() const
void carry(int64_t &h0, int64_t &h1)

References Botan::carry().

◆ scalar_one()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_one ( ) const

Definition at line 209 of file ec_inner_data.cpp.

209 {
210 if(m_pcurve) {
211 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_one());
212 } else {
213#if defined(BOTAN_HAS_LEGACY_EC_POINT)
214 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::one());
215#else
216 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
217#endif
218 }
219}
static BigInt one()
Definition bigint.h:55

◆ scalar_random()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_random ( RandomNumberGenerator & rng) const

Return a random scalar

This will be in the range [1,n) where n is the group order

Definition at line 184 of file ec_inner_data.cpp.

184 {
185 if(m_pcurve) {
186 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->random_scalar(rng));
187 } else {
188#if defined(BOTAN_HAS_LEGACY_EC_POINT)
189 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
190 BigInt::random_integer(rng, BigInt::one(), m_order));
191#else
192 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
193#endif
194 }
195}
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:43

◆ scalar_zero()

std::unique_ptr< EC_Scalar_Data > Botan::EC_Group_Data::scalar_zero ( ) const

Definition at line 197 of file ec_inner_data.cpp.

197 {
198 if(m_pcurve) {
199 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_zero());
200 } else {
201#if defined(BOTAN_HAS_LEGACY_EC_POINT)
202 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::zero());
203#else
204 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
205#endif
206 }
207}
static BigInt zero()
Definition bigint.h:50

◆ set_oid()

void Botan::EC_Group_Data::set_oid ( const OID & oid)

Definition at line 125 of file ec_inner_data.cpp.

125 {
126 BOTAN_ARG_CHECK(!oid.empty(), "OID should be set");
127 BOTAN_STATE_CHECK(m_oid.empty() && m_der_named_curve.empty());
128 m_oid = oid;
129
130 DER_Encoder der(m_der_named_curve);
131 der.encode(m_oid);
132}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29

References BOTAN_ARG_CHECK, BOTAN_STATE_CHECK, Botan::OID::empty(), and Botan::DER_Encoder::encode().

◆ source()

EC_Group_Source Botan::EC_Group_Data::source ( ) const
inline

Definition at line 204 of file ec_inner_data.h.

204{ return m_source; }

The documentation for this class was generated from the following files: