11#include <botan/ec_group.h>
13#include <botan/ber_dec.h>
14#include <botan/der_enc.h>
15#include <botan/mutex.h>
16#include <botan/numthry.h>
18#include <botan/reducer.h>
20#include <botan/internal/ec_inner_data.h>
21#include <botan/internal/fmt.h>
22#include <botan/internal/primality.h>
27class EC_Group_Data_Map
final {
29 EC_Group_Data_Map() =
default;
33 size_t count = m_registered_curves.size();
34 m_registered_curves.clear();
38 std::shared_ptr<EC_Group_Data> lookup(
const OID& oid) {
41 for(
auto i : m_registered_curves) {
51 for(
auto curve : m_registered_curves) {
52 if(curve->oid().empty() ==
true && curve->params_match(*data)) {
58 m_registered_curves.push_back(data);
63 return std::shared_ptr<EC_Group_Data>();
66 std::shared_ptr<EC_Group_Data> lookup_or_create(
const BigInt& p,
72 const BigInt& cofactor,
77 for(
auto i : m_registered_curves) {
83 if(!oid.empty() && !i->oid().empty() && i->oid() != oid) {
87 const bool same_oid = !oid.empty() && i->oid() == oid;
88 const bool same_params = i->params_match(p, a,
b, g_x, g_y, order, cofactor);
94 if(same_params && same_oid) {
101 if(same_params && oid.empty()) {
108 if(same_oid && !same_params) {
109 throw Invalid_Argument(
"Attempting to register a curve using OID " + oid.to_string() +
110 " but a distinct curve is already registered using that OID");
117 if(same_params && i->oid().empty() && !oid.empty()) {
132 if(oid.has_value()) {
134 if(data !=
nullptr && !new_group->params_match(*data)) {
135 throw Invalid_Argument(
"Attempting to register an EC group under OID of hardcoded group");
140 if(oid_from_store.has_value()) {
154 if(new_group->params_match(*data)) {
155 new_group->set_oid(oid_from_store);
160 m_registered_curves.push_back(new_group);
166 std::vector<std::shared_ptr<EC_Group_Data>> m_registered_curves;
170EC_Group_Data_Map& EC_Group::ec_group_data() {
176 static Allocator_Initializer g_init_allocator;
177 static EC_Group_Data_Map g_ec_data;
183 return ec_group_data().clear();
187std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(
const char* p_str,
192 const char* order_str,
197 const BigInt g_x(g_x_str);
198 const BigInt g_y(g_y_str);
199 const BigInt order(order_str);
206std::pair<std::shared_ptr<EC_Group_Data>,
bool> EC_Group::BER_decode_EC_group(std::span<const uint8_t> bits,
208 BER_Decoder ber(bits);
210 auto next_obj_type = ber.peek_next_object().type_tag();
216 auto data = ec_group_data().lookup(oid);
218 throw Decoding_Error(
fmt(
"Unknown namedCurve OID '{}'", oid.to_string()));
221 return std::make_pair(data,
false);
223 BigInt p, a,
b, order, cofactor;
224 std::vector<uint8_t> base_pt;
225 std::vector<uint8_t> seed;
228 .decode_and_check<
size_t>(1,
"Unknown ECC param version code")
230 .decode_and_check(OID(
"1.2.840.10045.1.1"),
"Only prime ECC fields supported")
234 .decode_octet_string_bigint(a)
235 .decode_octet_string_bigint(
b)
244 if(p.bits() < 112 || p.bits() > 521 || p.is_negative()) {
245 throw Decoding_Error(
"ECC p parameter is invalid size");
250 throw Decoding_Error(
"ECC p parameter is not a prime");
253 if(a.is_negative() || a >= p) {
254 throw Decoding_Error(
"Invalid ECC a parameter");
258 throw Decoding_Error(
"Invalid ECC b parameter");
261 if(order.is_negative() || order.is_zero() || order >= 2 * p) {
262 throw Decoding_Error(
"Invalid ECC group order");
267 throw Decoding_Error(
"Invalid ECC order parameter");
270 if(cofactor <= 0 || cofactor >= 16) {
271 throw Decoding_Error(
"Invalid ECC cofactor parameter");
274 const size_t p_bytes = p.bytes();
275 if(base_pt.size() != 1 + p_bytes && base_pt.size() != 1 + 2 * p_bytes) {
276 throw Decoding_Error(
"Invalid ECC base point encoding");
279 auto [g_x, g_y] = [&]() {
280 const uint8_t hdr = base_pt[0];
282 if(hdr == 0x04 && base_pt.size() == 1 + 2 * p_bytes) {
287 return std::make_pair(x, y);
289 }
else if((hdr == 0x02 || hdr == 0x03) && base_pt.size() == 1 + p_bytes) {
293 if(x < p && y >= 0) {
294 const bool y_mod_2 = (hdr & 0x01) == 1;
295 if(y.get_bit(0) != y_mod_2) {
299 return std::make_pair(x, y);
303 throw Decoding_Error(
"Invalid ECC base point encoding");
306 auto y2 = mod_p.square(g_y);
307 auto x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) +
b);
309 throw Decoding_Error(
"Invalid ECC base point");
312 auto data = ec_group_data().lookup_or_create(p, a,
b, g_x, g_y, order, cofactor, OID(),
source);
313 return std::make_pair(data,
true);
315 throw Decoding_Error(
"Decoding ImplicitCA ECC parameters is not supported");
317 throw Decoding_Error(
335#if defined(BOTAN_HAS_LEGACY_EC_POINT) || defined(BOTAN_HAS_PCURVES_GENERIC)
344 auto data = ec_group_data().lookup(oid);
355 std::shared_ptr<EC_Group_Data> data;
358 data = ec_group_data().lookup(oid.value());
376 m_data = ec_group_data().lookup(oid);
380 if(m_data ==
nullptr) {
381 if(str.size() > 30 && str.substr(0, 29) ==
"-----BEGIN EC PARAMETERS-----") {
386 this->m_data = data.first;
387 this->m_explicit_encoding = data.second;
391 if(m_data ==
nullptr) {
424#if defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
425 constexpr size_t p_bits_lower_bound = 192;
427 constexpr size_t p_bits_lower_bound = 128;
433 if(p.
bits() == 521) {
435 BOTAN_ARG_CHECK(p == p521,
"EC_Group with p of 521 bits must be 2**521-1");
436 }
else if(p.
bits() == 239) {
437 const auto x962_p239 = []() {
439 for(
size_t i = 0; i != 239; ++i) {
440 if(i < 47 || ((i >= 94) && (i != 143))) {
447 BOTAN_ARG_CHECK(p == x962_p239,
"EC_Group with p of 239 bits must be the X9.62 prime");
452 BOTAN_ARG_CHECK(p % 4 == 3,
"EC_Group p must be congruent to 3 modulo 4");
456 BOTAN_ARG_CHECK(base_x >= 0 && base_x < p,
"EC_Group base_x is invalid");
457 BOTAN_ARG_CHECK(base_y >= 0 && base_y < p,
"EC_Group base_y is invalid");
471 const auto discriminant = mod_p.reduce(mod_p.multiply(4, mod_p.cube(a)) + mod_p.multiply(27, mod_p.square(
b)));
472 BOTAN_ARG_CHECK(discriminant != 0,
"EC_Group discriminant is invalid");
475 auto y2 = mod_p.square(base_y);
476 auto x3_ax_b = mod_p.reduce(mod_p.cube(base_x) + mod_p.multiply(a, base_x) +
b);
477 BOTAN_ARG_CHECK(y2 == x3_ax_b,
"EC_Group generator is not on the curve");
488 m_explicit_encoding = data.second;
492 if(m_data ==
nullptr) {
499 return data().p_bits();
503 return data().p_bytes();
507 return data().order_bits();
511 return data().order_bytes();
526#if defined(BOTAN_HAS_LEGACY_EC_POINT)
527const EC_Point& EC_Group::get_base_point()
const {
528 return data().base_point();
531const EC_Point& EC_Group::generator()
const {
532 return data().base_point();
535bool EC_Group::verify_public_element(
const EC_Point& point)
const {
537 if(point.is_zero()) {
542 if(point.on_the_curve() ==
false) {
547 if((point *
get_order()).is_zero() ==
false) {
563 return data().order();
575 return data().cofactor();
579 return data().has_cofactor();
587 return data().source();
591 return data().engine();
595 const auto& der_named_curve = data().der_named_curve();
597 if(der_named_curve.empty()) {
598 throw Encoding_Error(
"Cannot encode EC_Group as OID because OID not set");
601 return der_named_curve;
606 std::vector<uint8_t> output;
608 const size_t ecpVers1 = 1;
609 const OID curve_type(
"1.2.840.10045.1.1");
645 if(m_data == other.m_data) {
657 if(is_builtin && !strong) {
670 if(p <= 3 || order <= 0) {
673 if(a < 0 || a >= p) {
680 const size_t test_prob = 128;
681 const bool is_randomly_generated = is_builtin;
684 if(!
is_prime(p, rng, test_prob, is_randomly_generated)) {
689 if(!
is_prime(order, rng, test_prob, is_randomly_generated)) {
696 const BigInt discriminant = mod_p.reduce(mod_p.multiply(4, mod_p.cube(a)) + mod_p.multiply(27, mod_p.square(
b)));
698 if(discriminant == 0) {
707#if defined(BOTAN_HAS_LEGACY_EC_POINT)
708 const EC_Point& base_point = get_base_point();
717 if(!(base_point * order).is_zero()) {
753 return this->mul2_vartime_x_mod_order_eq(v, c * x, c * y);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
static BigInt decode(const uint8_t buf[], size_t length)
static BigInt power_of_2(size_t n)
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
T serialize_uncompressed() const
static EC_AffinePoint _from_inner(std::unique_ptr< EC_AffinePoint_Data > inner)
static EC_AffinePoint generator(const EC_Group &group)
Return the standard group generator.
std::optional< EC_AffinePoint > mul2_vartime(const EC_Scalar &x, const EC_Scalar &y) const
Mul2Table(const EC_AffinePoint &h)
bool mul2_vartime_x_mod_order_eq(const EC_Scalar &v, const EC_Scalar &x, const EC_Scalar &y) const
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)
static EC_Group from_name(std::string_view name)
static EC_Group from_PEM(std::string_view pem)
const BigInt & get_b() const
const BigInt & get_a() const
const BigInt & get_g_y() const
const BigInt & get_cofactor() const
BigInt mod_order(const BigInt &x) const
bool operator==(const EC_Group &other) const
EC_Group_Engine engine() const
EC_Group_Source source() const
const BigInt & get_p() const
bool verify_group(RandomNumberGenerator &rng, bool strong=false) const
const BigInt & get_order() const
size_t get_p_bits() const
static EC_Group from_OID(const OID &oid)
static std::shared_ptr< EC_Group_Data > EC_group_info(const OID &oid)
std::vector< uint8_t > DER_encode() const
std::string PEM_encode() const
const BigInt & get_g_x() const
const OID & get_curve_oid() const
static bool supports_application_specific_group()
bool has_cofactor() const
static size_t clear_registered_curve_data()
EC_Group & operator=(const EC_Group &)
size_t get_p_bytes() const
static OID EC_group_identity_from_order(const BigInt &order)
size_t get_order_bits() const
size_t get_order_bytes() const
bool on_the_curve() const
const EC_Scalar_Data & _inner() const
static Modular_Reducer for_public_modulus(const BigInt &m)
static std::optional< OID > from_name(std::string_view name)
std::string to_string() const
static OID from_string(std::string_view str)
int(* final)(unsigned char *, CTX *)
std::string encode(const uint8_t der[], size_t length, std::string_view label, size_t width)
secure_vector< uint8_t > decode_check_label(DataSource &source, std::string_view label_want)
std::string asn1_tag_to_string(ASN1_Type type)
std::string fmt(std::string_view format, const T &... args)
BigInt abs(const BigInt &n)
secure_vector< T > lock(const std::vector< T > &in)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
bool is_bailie_psw_probable_prime(const BigInt &n, const Modular_Reducer &mod_n)
lock_guard< T > lock_guard_type
BigInt sqrt_modulo_prime(const BigInt &a, const BigInt &p)