Botan 3.14.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 BigInt & a () const
bool a_is_minus_3 () const
bool a_is_zero () const
std::unique_ptr< EC_AffinePoint_Data > affine_add (const EC_AffinePoint_Data &p, const EC_AffinePoint_Data &q) const
std::unique_ptr< EC_AffinePoint_Data > affine_neg (const EC_AffinePoint_Data &p) const
const BigInt & b () const
const BigInt & cofactor () 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_Data (const EC_Group_Data &other)=delete
 EC_Group_Data (EC_Group_Data &&other)=delete
EC_Group_Engine engine () const
const BigInt & g_x () const
const BigInt & g_y () const
std::unique_ptr< EC_Scalar_Data > gk_x_mod_order (const EC_Scalar_Data &scalar, RandomNumberGenerator &rng) const
bool has_cofactor () const
bool hash_to_curve_supported (std::string_view hash_fn) const
std::unique_ptr< EC_Mul2Table_Data > make_mul2_table (const EC_AffinePoint_Data &pt) const
std::unique_ptr< EC_AffinePoint_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
const OID & oid () const
EC_Group_Data & operator= (const EC_Group_Data &other)=delete
EC_Group_Data & operator= (EC_Group_Data &&other)=delete
const BigInt & order () const
size_t order_bits () const
size_t order_bytes () const
bool order_is_less_than_p () const
const BigInt & p () 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 BigInt &p, const BigInt &a, const BigInt &b, std::span< const uint8_t > base_pt, const BigInt &order, const BigInt &cofactor) const
bool params_match (const EC_Group_Data &other) const
const PCurve::PrimeOrderCurve & pcurve () const
std::unique_ptr< EC_AffinePoint_Data > point_deserialize_compressed (std::span< const uint8_t > bytes) const
std::unique_ptr< EC_AffinePoint_Data > point_deserialize_uncompressed (std::span< const uint8_t > bytes) const
std::unique_ptr< EC_AffinePoint_Data > point_g_mul (const EC_Scalar_Data &scalar, RandomNumberGenerator &rng) const
std::unique_ptr< EC_AffinePoint_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
std::unique_ptr< EC_AffinePoint_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
std::unique_ptr< EC_AffinePoint_Data > point_identity () const
 Return the identity element (aka the point at infinity).
std::unique_ptr< EC_Scalar_Data > scalar_deserialize (std::span< const uint8_t > bytes) const
std::unique_ptr< EC_Scalar_Data > scalar_from_bigint (const BigInt &bn) const
std::unique_ptr< EC_Scalar_Data > scalar_from_bytes_mod_order (std::span< const uint8_t > bytes) const
std::unique_ptr< EC_Scalar_Data > scalar_from_bytes_with_trunc (std::span< const uint8_t > bytes) const
std::unique_ptr< EC_Scalar_Data > scalar_one () const
std::unique_ptr< EC_Scalar_Data > scalar_random (RandomNumberGenerator &rng) 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_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)

Detailed Description

Definition at line 135 of file ec_inner_data.h.

Constructor & Destructor Documentation

◆ ~EC_Group_Data()

Botan::EC_Group_Data::~EC_Group_Data ( )
default

◆ EC_Group_Data() [1/3]

Botan::EC_Group_Data::EC_Group_Data ( const EC_Group_Data & other)
delete

◆ EC_Group_Data() [2/3]

Botan::EC_Group_Data::EC_Group_Data ( EC_Group_Data && other)
delete

References EC_Group_Data().

◆ EC_Group_Data() [3/3]

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 31 of file ec_inner_data.cpp.

39 :
40 m_p(p),
41 m_a(a),
42 m_b(b),
43 m_g_x(g_x),
44 m_g_y(g_y),
45 m_order(order),
46 m_cofactor(cofactor),
47#if defined(BOTAN_HAS_LEGACY_EC_POINT)
50 m_monty(m_p, m_mod_field),
51#endif
52 m_oid(oid),
53 m_p_words(p.sig_words()),
54 m_p_bits(p.bits()),
55 m_order_bits(order.bits()),
56 m_order_bytes((m_order_bits + 7) / 8),
57 m_a_is_minus_3(a == p - 3),
58 m_a_is_zero(a.is_zero()),
59 m_has_cofactor(m_cofactor != 1),
60 m_order_is_less_than_p(m_order < p),
61 m_source(source) {
62 // Verify the generator (x, y) satisfies y^2 = x^3 + a*x + b (mod p)
63 const auto mod_p = Barrett_Reduction::for_public_modulus(p);
64 const BigInt y2 = mod_p.square(g_y);
65 const BigInt x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) + b);
66 if(y2 != x3_ax_b) {
67 throw Invalid_Argument("EC_Group generator is not on the curve");
68 }
69
70 // TODO(Botan4) we can assume/assert the OID is set
71 if(!m_oid.empty()) {
72 DER_Encoder der(m_der_named_curve);
73 der.encode(m_oid);
74
75 if(const auto name = m_oid.registered_name()) {
77 const bool same_params = [&]() {
78 if(m_source == EC_Group_Source::Builtin) {
79 return true;
80 }
81
82 if(const auto group_oid = OID::from_name(*name)) {
83 if(const auto group_info = EC_Group::EC_group_info(*group_oid)) {
84 return group_info->params_match(p, a, b, g_x, g_y, order, cofactor);
85 }
86 }
87
88 return false;
89 }();
90
91 if(same_params) {
92 m_pcurve = std::move(pcurve);
93 }
94 }
95 }
96 if(m_pcurve) {
98 }
99 }
100
101 // Try a generic pcurves instance
102 if(!m_pcurve && !m_has_cofactor) {
104 if(m_pcurve) {
105 m_engine = EC_Group_Engine::Generic;
106 }
107 // possibly still null here, if parameters unsuitable or if the
108 // pcurves_generic module wasn't included in the build
109 }
110
111#if defined(BOTAN_HAS_LEGACY_EC_POINT)
113 m_a_r = m_monty.mul(a, m_monty.R2(), ws);
114 m_b_r = m_monty.mul(b, m_monty.R2(), ws);
115 if(!m_pcurve) {
116 m_engine = EC_Group_Engine::Legacy;
117 }
118#else
119 if(!m_pcurve) {
120 if(m_oid.empty()) {
121 throw Not_Implemented("EC_Group this group is not supported in this build configuration");
122 } else {
123 throw Not_Implemented(
124 fmt("EC_Group the group {} is not supported in this build configuration", oid.to_string()));
125 }
126 }
127#endif
128}
static Barrett_Reduction for_public_modulus(const BigInt &m)
Definition barrett.cpp:33
const BigInt & p() const
const BigInt & g_x() const
const BigInt & a() const
const PCurve::PrimeOrderCurve & pcurve() 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 std::shared_ptr< EC_Group_Data > EC_group_info(const OID &oid)
Definition ec_named.cpp:16
static std::optional< OID > from_name(std::string_view name)
Definition asn1_oid.cpp:66
static std::shared_ptr< const PrimeOrderCurve > from_params(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &base_x, const BigInt &base_y, const BigInt &order)
Definition pcurves.cpp:21
static std::shared_ptr< const PrimeOrderCurve > for_named_curve(std::string_view name)
Definition pcurves.cpp:32
const char * name
Definition ffi_tls.h:59
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
@ Optimized
Using per curve implementation; fastest available.
Definition ec_group.h:50
@ Generic
A generic implementation that handles many curves in one implementation.
Definition ec_group.h:52
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128

References a(), b(), BOTAN_HAS_LEGACY_EC_POINT, Botan::Builtin, cofactor(), Botan::EC_Group::EC_group_info(), Botan::DER_Encoder::encode(), Botan::fmt(), Botan::PCurve::PrimeOrderCurve::for_named_curve(), Botan::Barrett_Reduction::for_public_modulus(), Botan::OID::from_name(), Botan::PCurve::PrimeOrderCurve::from_params(), g_x(), g_y(), Botan::Generic, Botan::Legacy, name, oid(), Botan::Optimized, order(), p(), pcurve(), source(), and Botan::BigInt::square().

Member Function Documentation

◆ a()

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

Definition at line 180 of file ec_inner_data.h.

180{ return m_a; }

Referenced by create(), EC_Group_Data(), operator=(), params_match(), params_match(), and params_match().

◆ a_is_minus_3()

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

Definition at line 220 of file ec_inner_data.h.

220{ return m_a_is_minus_3; }

◆ a_is_zero()

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

Definition at line 222 of file ec_inner_data.h.

222{ 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 621 of file ec_inner_data.cpp.

622 {
623 if(m_pcurve) {
624 const auto pt = m_pcurve->point_add(EC_AffinePoint_Data_PC::checked_ref(p).value(),
626
627 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_to_affine(pt));
628 } else {
629#if defined(BOTAN_HAS_LEGACY_EC_POINT)
630 auto pt = p.to_legacy_point() + q.to_legacy_point();
631 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
632#else
633 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
634#endif
635 }
636}
static const EC_AffinePoint_Data_PC & checked_ref(const EC_AffinePoint_Data &data)

References Botan::EC_AffinePoint_Data_PC::checked_ref(), and p().

◆ affine_neg()

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

Definition at line 638 of file ec_inner_data.cpp.

638 {
639 if(m_pcurve) {
640 const auto pt = m_pcurve->point_negate(EC_AffinePoint_Data_PC::checked_ref(p).value());
641 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), pt);
642 } else {
643#if defined(BOTAN_HAS_LEGACY_EC_POINT)
644 auto pt = p.to_legacy_point();
645 pt.negate(); // negates in place
646 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
647#else
648 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
649#endif
650 }
651}

References Botan::EC_AffinePoint_Data_PC::checked_ref(), and p().

◆ b()

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

Definition at line 182 of file ec_inner_data.h.

182{ return m_b; }

Referenced by create(), EC_Group_Data(), operator=(), params_match(), params_match(), and params_match().

◆ cofactor()

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

Definition at line 186 of file ec_inner_data.h.

186{ return m_cofactor; }

Referenced by create(), EC_Group_Data(), operator=(), params_match(), params_match(), and 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 130 of file ec_inner_data.cpp.

138 {
139 auto group = std::make_shared<EC_Group_Data>(p, a, b, g_x, g_y, order, cofactor, oid, source);
140
141#if defined(BOTAN_HAS_LEGACY_EC_POINT)
142 group->m_curve = CurveGFp(group.get());
143 group->m_base_point = EC_Point(group->m_curve, g_x, g_y);
144 if(!group->m_pcurve) {
145 group->m_base_mult = std::make_unique<EC_Point_Base_Point_Precompute>(group->m_base_point, group->m_mod_order);
146 }
147#endif
148
149 return group;
150}

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

◆ der_named_curve()

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

Definition at line 176 of file ec_inner_data.h.

176{ return m_der_named_curve; }

◆ engine()

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

Definition at line 226 of file ec_inner_data.h.

226{ return m_engine; }

◆ g_x()

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

Definition at line 206 of file ec_inner_data.h.

206{ return m_g_x; }

Referenced by create(), EC_Group_Data(), operator=(), params_match(), params_match(), and params_match().

◆ g_y()

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

Definition at line 208 of file ec_inner_data.h.

208{ return m_g_y; }

Referenced by create(), EC_Group_Data(), operator=(), params_match(), params_match(), and 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 ) const

Definition at line 351 of file ec_inner_data.cpp.

352 {
353 if(m_pcurve) {
354 const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
355 const auto gk_x_mod_order = m_pcurve->base_point_mul_x_mod_order(k.value(), rng);
356 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), gk_x_mod_order);
357 } else {
358#if defined(BOTAN_HAS_LEGACY_EC_POINT)
359 const auto& k = EC_Scalar_Data_BN::checked_ref(scalar);
360 BOTAN_STATE_CHECK(m_base_mult != nullptr);
361 std::vector<BigInt> ws;
362 const auto pt = m_base_mult->mul(k.value(), rng, m_order, ws);
363
364 if(pt.is_zero()) {
365 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::zero());
366 } else {
367 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(pt.get_affine_x()));
368 }
369#else
370 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
371#endif
372 }
373}
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
static BigInt zero()
Definition bigint.h:50
std::unique_ptr< EC_Scalar_Data > gk_x_mod_order(const EC_Scalar_Data &scalar, RandomNumberGenerator &rng) 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, Botan::EC_Scalar_Data_BN::checked_ref(), Botan::EC_Scalar_Data_PC::checked_ref(), gk_x_mod_order(), and Botan::BigInt::zero().

Referenced by gk_x_mod_order().

◆ has_cofactor()

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

Definition at line 204 of file ec_inner_data.h.

204{ return m_has_cofactor; }

◆ hash_to_curve_supported()

bool Botan::EC_Group_Data::hash_to_curve_supported ( std::string_view hash_fn) const

Return true if point_hash_to_curve_ro/point_hash_to_curve_nu will work for this group when using the specified hash function

Definition at line 503 of file ec_inner_data.cpp.

503 {
504#if defined(BOTAN_HAS_XMD)
505 if(!m_pcurve || !m_pcurve->supports_hash_to_curve()) {
506 return false;
507 }
508
509 // Consistent with h2c_expand_message; XOF based expansion is not implemented
510 if(hash_fn.starts_with("SHAKE")) {
511 return false;
512 }
513
514 auto hash = HashFunction::create(hash_fn);
515 if(hash == nullptr) {
516 return false;
517 }
518
519 // The same hash strength requirement enforced by h2c_expand_message
520 const size_t k = std::min<size_t>((order_bits() + 1) / 2, 256);
521 if(hash->security_level() < k) {
522 return false;
523 }
524
525 // The same requirements enforced by expand_message_xmd
526 return hash->hash_block_size() > 0 && hash->output_length() <= hash->hash_block_size();
527#else
528 BOTAN_UNUSED(hash_fn);
529 return false;
530#endif
531}
#define BOTAN_UNUSED
Definition assert.h:151
size_t order_bits() const
static std::unique_ptr< HashFunction > create(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:112

References BOTAN_UNUSED, Botan::HashFunction::create(), and order_bits().

◆ 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 653 of file ec_inner_data.cpp.

653 {
654 if(m_pcurve) {
655 return std::make_unique<EC_Mul2Table_Data_PC>(h);
656 } else {
657#if defined(BOTAN_HAS_LEGACY_EC_POINT)
658 const EC_AffinePoint_Data_BN g(shared_from_this(), this->base_point());
659 return std::make_unique<EC_Mul2Table_Data_BN>(g, h);
660#else
661 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
662#endif
663 }
664}

◆ 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 576 of file ec_inner_data.cpp.

580 {
581 if(m_pcurve) {
582 auto pt = m_pcurve->mul_px_qy(EC_AffinePoint_Data_PC::checked_ref(p).value(),
586 rng);
587
588 if(pt) {
589 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_to_affine(*pt));
590 } else {
591 return nullptr;
592 }
593 } else {
594#if defined(BOTAN_HAS_LEGACY_EC_POINT)
595 std::vector<BigInt> ws;
596 const auto& group = p.group();
597
598 // TODO this could be better!
599 const EC_Point_Var_Point_Precompute p_mul(p.to_legacy_point(), rng, ws);
600 const EC_Point_Var_Point_Precompute q_mul(q.to_legacy_point(), rng, ws);
601
602 const auto order = group->order() * group->cofactor(); // See #3800
603
604 const auto px = p_mul.mul(EC_Scalar_Data_BN::checked_ref(x).value(), rng, order, ws);
605 const auto qy = q_mul.mul(EC_Scalar_Data_BN::checked_ref(y).value(), rng, order, ws);
606
607 auto px_qy = px + qy;
608
609 if(!px_qy.is_zero()) {
610 px_qy.force_affine();
611 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(px_qy));
612 } else {
613 return nullptr;
614 }
615#else
616 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
617#endif
618 }
619}

References Botan::EC_AffinePoint_Data_PC::checked_ref(), Botan::EC_Scalar_Data_BN::checked_ref(), Botan::EC_Scalar_Data_PC::checked_ref(), Botan::EC_Point_Var_Point_Precompute::mul(), order(), and p().

◆ oid()

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

Definition at line 174 of file ec_inner_data.h.

174{ return m_oid; }

Referenced by create(), EC_Group_Data(), operator=(), and set_oid().

◆ operator=() [1/2]

EC_Group_Data & Botan::EC_Group_Data::operator= ( const EC_Group_Data & other)
delete

References EC_Group_Data().

◆ operator=() [2/2]

EC_Group_Data & Botan::EC_Group_Data::operator= ( EC_Group_Data && other)
delete

◆ order()

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

Definition at line 184 of file ec_inner_data.h.

184{ return m_order; }

Referenced by create(), EC_Group_Data(), mul_px_qy(), operator=(), params_match(), params_match(), and params_match().

◆ order_bits()

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

Definition at line 216 of file ec_inner_data.h.

216{ return m_order_bits; }

Referenced by hash_to_curve_supported(), point_hash_to_curve_nu(), point_hash_to_curve_ro(), and scalar_from_bytes_with_trunc().

◆ order_bytes()

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

Definition at line 218 of file ec_inner_data.h.

218{ return m_order_bytes; }

Referenced by scalar_from_bytes_mod_order().

◆ order_is_less_than_p()

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

Definition at line 202 of file ec_inner_data.h.

202{ return m_order_is_less_than_p; }

◆ p()

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

Definition at line 178 of file ec_inner_data.h.

178{ return m_p; }

Referenced by affine_add(), affine_neg(), create(), EC_Group_Data(), mul_px_qy(), operator=(), params_match(), params_match(), and params_match().

◆ p_bits()

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

Definition at line 212 of file ec_inner_data.h.

212{ return m_p_bits; }

◆ p_bytes()

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

Definition at line 214 of file ec_inner_data.h.

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

Referenced by params_match(), point_deserialize_compressed(), and point_deserialize_uncompressed().

◆ p_words()

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

Definition at line 210 of file ec_inner_data.h.

210{ return m_p_words; }

◆ params_match() [1/3]

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 152 of file ec_inner_data.cpp.

158 {
159 if(p != this->p()) {
160 return false;
161 }
162 if(a != this->a()) {
163 return false;
164 }
165 if(b != this->b()) {
166 return false;
167 }
168 if(order != this->order()) {
169 return false;
170 }
171 if(cofactor != this->cofactor()) {
172 return false;
173 }
174 if(g_x != this->g_x()) {
175 return false;
176 }
177 if(g_y != this->g_y()) {
178 return false;
179 }
180
181 return true;
182}

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

Referenced by params_match().

◆ params_match() [2/3]

bool Botan::EC_Group_Data::params_match ( const BigInt & p,
const BigInt & a,
const BigInt & b,
std::span< const uint8_t > base_pt,
const BigInt & order,
const BigInt & cofactor ) const

Definition at line 184 of file ec_inner_data.cpp.

189 {
190 if(p != this->p()) {
191 return false;
192 }
193 if(a != this->a()) {
194 return false;
195 }
196 if(b != this->b()) {
197 return false;
198 }
199 if(order != this->order()) {
200 return false;
201 }
202 if(cofactor != this->cofactor()) {
203 return false;
204 }
205
206 const size_t field_len = this->p_bytes();
207
208 if(base_pt.size() == 1 + field_len && (base_pt[0] == 0x02 || base_pt[0] == 0x03)) {
209 // compressed
210
211 const auto g_x = m_g_x.serialize(field_len);
212 const auto g_y = m_g_y.is_odd();
213
214 const auto sec1_x = base_pt.subspan(1, field_len);
215 const bool sec1_y = (base_pt[0] == 0x03);
216
217 if(!std::ranges::equal(sec1_x, g_x)) {
218 return false;
219 }
220
221 if(sec1_y != g_y) {
222 return false;
223 }
224
225 return true;
226 } else if(base_pt.size() == 1 + 2 * field_len && base_pt[0] == 0x04) {
227 const auto g_x = m_g_x.serialize(field_len);
228 const auto g_y = m_g_y.serialize(field_len);
229
230 const auto sec1_x = base_pt.subspan(1, field_len);
231 const auto sec1_y = base_pt.subspan(1 + field_len, field_len);
232
233 if(!std::ranges::equal(sec1_x, g_x)) {
234 return false;
235 }
236
237 if(!std::ranges::equal(sec1_y, g_y)) {
238 return false;
239 }
240
241 return true;
242 } else {
243 throw Decoding_Error("Invalid base point encoding in explicit group");
244 }
245}
size_t p_bytes() const

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

◆ params_match() [3/3]

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

Definition at line 247 of file ec_inner_data.cpp.

247 {
248 return params_match(other.p(), other.a(), other.b(), other.g_x(), other.g_y(), other.order(), other.cofactor());
249}
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(), EC_Group_Data(), g_x(), g_y(), order(), p(), and params_match().

◆ pcurve()

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

Definition at line 305 of file ec_inner_data.h.

305 {
306 BOTAN_ASSERT_NONNULL(m_pcurve);
307 return *m_pcurve;
308 }
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121

References BOTAN_ASSERT_NONNULL.

Referenced by EC_Group_Data().

◆ point_deserialize_compressed()

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

Deserialize a point in the SEC1 compressed format

Returns nullptr if the encoding was not in the compressed format, or if the point is not on the curve

Definition at line 427 of file ec_inner_data.cpp.

427 {
428 if(bytes.size() != 1 + p_bytes() || (bytes[0] != 0x02 && bytes[0] != 0x03)) {
429 return {};
430 }
431
432 if(m_pcurve) {
433 if(auto pt = m_pcurve->deserialize_point_compressed(bytes)) {
434 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
435 } else {
436 return {};
437 }
438 } else {
439#if defined(BOTAN_HAS_LEGACY_EC_POINT)
440 try {
441 auto pt = Botan::OS2ECP(bytes, m_curve);
442 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
443 } catch(...) {
444 return {};
445 }
446#else
447 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
448#endif
449 }
450}
EC_Point OS2ECP(std::span< const uint8_t > data, const CurveGFp &curve)
Definition ec_point.cpp:866

References Botan::OS2ECP(), and p_bytes().

◆ point_deserialize_uncompressed()

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

Deserialize a point in the SEC1 uncompressed format

Returns nullptr if the encoding was not in the uncompressed format, or if the point is not on the curve

Definition at line 401 of file ec_inner_data.cpp.

402 {
403 if(bytes.size() != 1 + 2 * p_bytes() || bytes[0] != 0x04) {
404 return {};
405 }
406
407 if(m_pcurve) {
408 if(auto pt = m_pcurve->deserialize_point_uncompressed(bytes)) {
409 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(*pt));
410 } else {
411 return {};
412 }
413 } else {
414#if defined(BOTAN_HAS_LEGACY_EC_POINT)
415 try {
416 auto pt = Botan::OS2ECP(bytes, m_curve);
417 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
418 } catch(...) {
419 return {};
420 }
421#else
422 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
423#endif
424 }
425}

References Botan::OS2ECP(), and p_bytes().

◆ point_g_mul()

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

Definition at line 555 of file ec_inner_data.cpp.

556 {
557 if(m_pcurve) {
558 const auto& k = EC_Scalar_Data_PC::checked_ref(scalar);
559 auto pt = m_pcurve->point_to_affine(m_pcurve->mul_by_g(k.value(), rng));
560 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
561 } else {
562#if defined(BOTAN_HAS_LEGACY_EC_POINT)
563 const auto& group = scalar.group();
564 const auto& bn = EC_Scalar_Data_BN::checked_ref(scalar);
565
566 BOTAN_STATE_CHECK(group->m_base_mult != nullptr);
567 std::vector<BigInt> ws;
568 auto pt = group->m_base_mult->mul(bn.value(), rng, m_order, ws);
569 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), std::move(pt));
570#else
571 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
572#endif
573 }
574}

References BOTAN_STATE_CHECK, Botan::EC_Scalar_Data_BN::checked_ref(), Botan::EC_Scalar_Data_PC::checked_ref(), 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 544 of file ec_inner_data.cpp.

546 {
547 if(m_pcurve && m_pcurve->supports_hash_to_curve()) {
548 auto pt = m_pcurve->hash_to_curve_nu(h2c_expand_message(hash_fn, order_bits(), input, domain_sep));
549 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), std::move(pt));
550 } else {
551 throw Not_Implemented("Hash to curve is not implemented for this curve");
552 }
553}
std::function< void(std::span< uint8_t >)> h2c_expand_message(std::string_view hash_fn, size_t order_bits, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep)

References Botan::h2c_expand_message(), and order_bits().

◆ 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 533 of file ec_inner_data.cpp.

535 {
536 if(m_pcurve && m_pcurve->supports_hash_to_curve()) {
537 const auto pt = m_pcurve->hash_to_curve_ro(h2c_expand_message(hash_fn, order_bits(), input, domain_sep));
538 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_to_affine(pt));
539 } else {
540 throw Not_Implemented("Hash to curve is not implemented for this curve");
541 }
542}

References Botan::h2c_expand_message(), and order_bits().

◆ point_identity()

std::unique_ptr< EC_AffinePoint_Data > Botan::EC_Group_Data::point_identity ( ) const

Return the identity element (aka the point at infinity).

Definition at line 452 of file ec_inner_data.cpp.

452 {
453 if(m_pcurve) {
454 return std::make_unique<EC_AffinePoint_Data_PC>(shared_from_this(), m_pcurve->point_identity());
455 } else {
456#if defined(BOTAN_HAS_LEGACY_EC_POINT)
457 return std::make_unique<EC_AffinePoint_Data_BN>(shared_from_this(), EC_Point(m_curve));
458#else
459 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
460#endif
461 }
462}

◆ 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 375 of file ec_inner_data.cpp.

375 {
376 if(bytes.size() != m_order_bytes) {
377 return nullptr;
378 }
379
380 if(m_pcurve) {
381 if(auto s = m_pcurve->deserialize_scalar(bytes)) {
382 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), *s);
383 } else {
384 return nullptr;
385 }
386 } else {
387#if defined(BOTAN_HAS_LEGACY_EC_POINT)
388 BigInt r(bytes);
389
390 if(r.is_zero() || r >= m_order) {
391 return nullptr;
392 }
393
394 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), std::move(r));
395#else
396 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
397#endif
398 }
399}

References Botan::BigInt::is_zero().

Referenced by scalar_from_bigint().

◆ 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 335 of file ec_inner_data.cpp.

335 {
336 if(bn <= 0 || bn >= m_order) {
337 return {};
338 }
339
340 if(m_pcurve) {
341 return this->scalar_deserialize(bn.serialize(m_order_bytes));
342 } else {
343#if defined(BOTAN_HAS_LEGACY_EC_POINT)
344 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), bn);
345#else
346 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
347#endif
348 }
349}
std::unique_ptr< EC_Scalar_Data > scalar_deserialize(std::span< const uint8_t > bytes) const

References scalar_deserialize(), and 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 290 of file ec_inner_data.cpp.

290 {
291 if(bytes.size() > 2 * order_bytes()) {
292 return {};
293 }
294
295 if(m_pcurve) {
296 if(auto s = m_pcurve->scalar_from_wide_bytes(bytes)) {
297 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), std::move(*s));
298 } else {
299 return {};
300 }
301 } else {
302#if defined(BOTAN_HAS_LEGACY_EC_POINT)
303 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), m_mod_order.reduce(BigInt(bytes)));
304#else
305 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
306#endif
307 }
308}
size_t order_bytes() const

References order_bytes().

Referenced by scalar_from_bytes_with_trunc().

◆ 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 truncation

This should always succeed

Definition at line 260 of file ec_inner_data.cpp.

260 {
261 const size_t bit_length = 8 * bytes.size();
262
263 if(bit_length < order_bits()) {
264 // No shifting required, but might still need to reduce by modulus
265 return this->scalar_from_bytes_mod_order(bytes);
266 } else {
267 const size_t shift = bit_length - order_bits();
268
269 const size_t new_length = bytes.size() - (shift / 8);
270 const size_t bit_shift = shift % 8;
271
272 if(bit_shift == 0) {
273 // Easy case just read different bytes
274 return this->scalar_from_bytes_mod_order(bytes.first(new_length));
275 } else {
276 std::vector<uint8_t> sbytes(new_length);
277
278 uint8_t carry = 0;
279 for(size_t i = 0; i != new_length; ++i) {
280 const uint8_t w = bytes[i];
281 sbytes[i] = (w >> bit_shift) | carry;
282 carry = w << (8 - bit_shift);
283 }
284
285 return this->scalar_from_bytes_mod_order(sbytes);
286 }
287 }
288}
std::unique_ptr< EC_Scalar_Data > scalar_from_bytes_mod_order(std::span< const uint8_t > bytes) const

References order_bits(), and scalar_from_bytes_mod_order().

◆ scalar_one()

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

Definition at line 323 of file ec_inner_data.cpp.

323 {
324 if(m_pcurve) {
325 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->scalar_one());
326 } else {
327#if defined(BOTAN_HAS_LEGACY_EC_POINT)
328 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(), BigInt::one());
329#else
330 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
331#endif
332 }
333}
static BigInt one()
Definition bigint.h:55

References Botan::BigInt::one().

◆ 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 310 of file ec_inner_data.cpp.

310 {
311 if(m_pcurve) {
312 return std::make_unique<EC_Scalar_Data_PC>(shared_from_this(), m_pcurve->random_scalar(rng));
313 } else {
314#if defined(BOTAN_HAS_LEGACY_EC_POINT)
315 return std::make_unique<EC_Scalar_Data_BN>(shared_from_this(),
316 BigInt::random_integer(rng, BigInt::one(), m_order));
317#else
318 throw Not_Implemented("Legacy EC interfaces disabled in this build configuration");
319#endif
320 }
321}
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:45

References Botan::BigInt::one(), and Botan::BigInt::random_integer().

◆ set_oid()

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

Definition at line 251 of file ec_inner_data.cpp.

251 {
252 BOTAN_ARG_CHECK(!oid.empty(), "OID should be set");
253 BOTAN_STATE_CHECK(m_oid.empty() && m_der_named_curve.empty());
254 m_oid = oid;
255
256 DER_Encoder der(m_der_named_curve);
257 der.encode(m_oid);
258}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

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

◆ source()

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

Definition at line 224 of file ec_inner_data.h.

224{ return m_source; }

Referenced by create(), and EC_Group_Data().


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