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>
19#include <botan/internal/barrett.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 const size_t count = m_registered_curves.size();
34 m_registered_curves.clear();
38 bool unregister(
const OID& oid) {
41 throw Invalid_Argument(
"OID must not be empty");
45 for(
size_t i = 0; i < m_registered_curves.size(); i++) {
46 if(m_registered_curves[i]->oid() == oid) {
47 m_registered_curves.erase(m_registered_curves.begin() + i);
54 std::shared_ptr<EC_Group_Data> lookup(
const OID& oid) {
57 for(
auto i : m_registered_curves) {
67 m_registered_curves.push_back(data);
72 return std::shared_ptr<EC_Group_Data>();
75 std::shared_ptr<EC_Group_Data> lookup_or_create(
const BigInt& p,
81 const BigInt& cofactor,
88 for(
auto i : m_registered_curves) {
98 if(!i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
99 throw Invalid_Argument(
"Attempting to register a curve using OID " + oid.to_string() +
100 " but a distinct curve is already registered using that OID");
113 if(i->oid().empty() && i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
122 auto new_group = [&] {
129 "Attempting to register an EC group under OID of hardcoded group");
140 m_registered_curves.push_back(new_group);
144 std::shared_ptr<EC_Group_Data> lookup_from_params(
const BigInt& p,
147 std::span<const uint8_t> base_pt,
149 const BigInt& cofactor) {
152 for(
auto i : m_registered_curves) {
153 if(i->params_match(p, a, b, base_pt, order, cofactor)) {
160 if(oid_from_order.has_value()) {
164 if(new_group && new_group->params_match(p, a, b, base_pt, order, cofactor)) {
165 m_registered_curves.push_back(new_group);
174 std::shared_ptr<EC_Group_Data> lookup_or_create_without_oid(
const BigInt& p,
180 const BigInt& cofactor,
184 for(
auto i : m_registered_curves) {
185 if(i->params_match(p, a, b, g_x, g_y, order, cofactor)) {
192 if(oid_from_order.has_value()) {
196 if(new_group && new_group->params_match(p, a, b, g_x, g_y, order, cofactor)) {
197 m_registered_curves.push_back(new_group);
210 m_registered_curves.push_back(new_group);
217 std::vector<std::shared_ptr<EC_Group_Data>> m_registered_curves;
221EC_Group_Data_Map& EC_Group::ec_group_data() {
227 static const Allocator_Initializer g_init_allocator;
228 static EC_Group_Data_Map g_ec_data;
234 return ec_group_data().clear();
238std::shared_ptr<EC_Group_Data> EC_Group::load_EC_group_info(
const char* p_str,
243 const char* order_str,
250 const BigInt g_x(g_x_str);
251 const BigInt g_y(g_y_str);
252 const BigInt order(order_str);
259std::pair<std::shared_ptr<EC_Group_Data>,
bool> EC_Group::BER_decode_EC_group(std::span<const uint8_t> bits,
261 BER_Decoder ber(bits);
263 auto next_obj_type = ber.peek_next_object().type_tag();
269 auto data = ec_group_data().lookup(oid);
271 throw Decoding_Error(
fmt(
"Unknown namedCurve OID '{}'", oid.to_string()));
274 return std::make_pair(data,
false);
281 std::vector<uint8_t> base_pt;
282 std::vector<uint8_t> seed;
285 .decode_and_check<
size_t>(1,
"Unknown ECC param version code")
287 .decode_and_check(OID({1, 2, 840, 10045, 1, 1}),
"Only prime ECC fields supported")
291 .decode_octet_string_bigint(a)
292 .decode_octet_string_bigint(b)
302 if(cofactor <= 0 || cofactor >= 16) {
303 throw Decoding_Error(
"Invalid ECC cofactor parameter");
306 if(p.bits() < 112 || p.bits() > 521 || p.is_negative()) {
307 throw Decoding_Error(
"ECC p parameter is invalid size");
310 if(a.is_negative() || a >= p) {
311 throw Decoding_Error(
"Invalid ECC a parameter");
314 if(b <= 0 || b >= p) {
315 throw Decoding_Error(
"Invalid ECC b parameter");
318 if(order.is_negative() || order.is_zero() || order >= 2 * p) {
319 throw Decoding_Error(
"Invalid ECC group order");
322 if(
auto data = ec_group_data().lookup_from_params(p, a, b, base_pt, order, cofactor)) {
323 return std::make_pair(data,
true);
334 throw Decoding_Error(
"ECC p parameter is not a prime");
339 throw Decoding_Error(
"Invalid ECC order parameter");
342 const size_t p_bytes = p.bytes();
343 if(base_pt.size() != 1 + p_bytes && base_pt.size() != 1 + 2 * p_bytes) {
344 throw Decoding_Error(
"Invalid ECC base point encoding");
347 auto [g_x, g_y] = [&]() {
348 const uint8_t hdr = base_pt[0];
350 if(hdr == 0x04 && base_pt.size() == 1 + 2 * p_bytes) {
352 const BigInt y =
BigInt::from_bytes(std::span{base_pt}.subspan(1 + p_bytes, p_bytes));
355 return std::make_pair(x, y);
357 }
else if((hdr == 0x02 || hdr == 0x03) && base_pt.size() == 1 + p_bytes) {
362 if(x < p && y >= 0) {
363 const bool y_mod_2 = (hdr & 0x01) == 1;
364 if(y.get_bit(0) != y_mod_2) {
368 return std::make_pair(x, y);
372 throw Decoding_Error(
"Invalid ECC base point encoding");
376 auto y2 = mod_p.square(g_y);
377 auto x3_ax_b = mod_p.reduce(mod_p.cube(g_x) + mod_p.multiply(a, g_x) + b);
379 throw Decoding_Error(
"Invalid ECC base point");
382 auto data = ec_group_data().lookup_or_create_without_oid(p, a, b, g_x, g_y, order, cofactor,
source);
383 return std::make_pair(data,
true);
385 throw Decoding_Error(
"Decoding ImplicitCA ECC parameters is not supported");
387 throw Decoding_Error(
410#if defined(BOTAN_HAS_LEGACY_EC_POINT) || defined(BOTAN_HAS_PCURVES_GENERIC)
419#if defined(BOTAN_HAS_LEGACY_EC_POINT)
428 auto data = ec_group_data().lookup(oid);
439 std::shared_ptr<EC_Group_Data> data;
442 data = ec_group_data().lookup(oid.value());
460 m_data = ec_group_data().lookup(oid);
464 if(m_data ==
nullptr) {
465 if(str.size() > 30 && str.starts_with(
"-----BEGIN EC PARAMETERS-----")) {
470 this->m_data = data.first;
471 this->m_explicit_encoding = data.second;
475 if(m_data ==
nullptr) {
495 m_data = ec_group_data().lookup_or_create(
498 m_data = ec_group_data().lookup_or_create_without_oid(
513#if defined(BOTAN_DISABLE_DEPRECATED_FEATURES)
514 constexpr size_t p_bits_lower_bound = 192;
516 constexpr size_t p_bits_lower_bound = 128;
522 if(p.
bits() == 521) {
524 BOTAN_ARG_CHECK(p == p521,
"EC_Group with p of 521 bits must be 2**521-1");
525 }
else if(p.
bits() == 239) {
526 const auto x962_p239 = []() {
528 for(
size_t i = 0; i != 239; ++i) {
529 if(i < 47 || ((i >= 94) && (i != 143))) {
536 BOTAN_ARG_CHECK(p == x962_p239,
"EC_Group with p of 239 bits must be the X9.62 prime");
541 BOTAN_ARG_CHECK(p % 4 == 3,
"EC_Group p must be congruent to 3 modulo 4");
545 BOTAN_ARG_CHECK(base_x >= 0 && base_x < p,
"EC_Group base_x is invalid");
546 BOTAN_ARG_CHECK(base_y >= 0 && base_y < p,
"EC_Group base_y is invalid");
560 const auto discriminant = mod_p.reduce(mod_p.multiply(
BigInt::from_s32(4), mod_p.cube(a)) +
562 BOTAN_ARG_CHECK(discriminant != 0,
"EC_Group discriminant is invalid");
565 auto y2 = mod_p.square(base_y);
566 auto x3_ax_b = mod_p.reduce(mod_p.cube(base_x) + mod_p.multiply(a, base_x) + b);
567 BOTAN_ARG_CHECK(y2 == x3_ax_b,
"EC_Group generator is not on the curve");
578 m_explicit_encoding = data.second;
583 return ec_group_data().unregister(oid);
587 if(m_data ==
nullptr) {
594 return data().p_bits();
598 return data().p_bytes();
602 return data().order_bits();
606 return data().order_bytes();
621#if defined(BOTAN_HAS_LEGACY_EC_POINT)
622const EC_Point& EC_Group::get_base_point()
const {
623 return data().base_point();
626const EC_Point& EC_Group::generator()
const {
627 return data().base_point();
630bool EC_Group::verify_public_element(
const EC_Point& point)
const {
632 if(point.is_zero()) {
637 if(point.on_the_curve() ==
false) {
642 if((point *
get_order()).is_zero() ==
false) {
658 return data().order();
670 return data().cofactor();
674 return data().has_cofactor();
682 return data().source();
686 return data().engine();
690 const auto& der_named_curve = data().der_named_curve();
692 if(der_named_curve.empty()) {
693 throw Encoding_Error(
"Cannot encode EC_Group as OID because OID not set");
696 return der_named_curve;
701 std::vector<uint8_t> output;
703 const size_t ecpVers1 = 1;
704 const OID curve_type(
"1.2.840.10045.1.1");
735 const std::vector<uint8_t> der =
DER_encode(form);
740 if(m_data == other.m_data) {
752 if(is_builtin && !strong) {
765 if(p <= 3 || order <= 0) {
768 if(a < 0 || a >= p) {
771 if(b <= 0 || b >= p) {
775 const size_t test_prob = 128;
776 const bool is_randomly_generated = is_builtin;
779 if(!
is_prime(p, rng, test_prob, is_randomly_generated)) {
784 if(!
is_prime(order, rng, test_prob, is_randomly_generated)) {
794 if(discriminant == 0) {
803#if defined(BOTAN_HAS_LEGACY_EC_POINT)
804 const EC_Point& base_point = get_base_point();
813 if(!(base_point * order).is_zero()) {
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ARG_CHECK(expr, msg)
static Barrett_Reduction for_public_modulus(const BigInt &m)
static BigInt from_bytes(std::span< const uint8_t > bytes)
static BigInt power_of_2(size_t n)
static BigInt from_s32(int32_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.
Table for computing g*x + h*y.
Mul2Table & operator=(const Mul2Table &other)=delete
std::optional< EC_AffinePoint > mul2_vartime(const EC_Scalar &x, const EC_Scalar &y) const
BOTAN_FUTURE_EXPLICIT 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
static bool supports_application_specific_group_with_cofactor()
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
const BigInt & get_g_x() const
EC_Group(const BigInt &p, const BigInt &a, const BigInt &b, const BigInt &base_x, const BigInt &base_y, const BigInt &order, const BigInt &cofactor, const OID &oid=OID())
const OID & get_curve_oid() const
static bool supports_application_specific_group()
static const std::set< std::string > & known_named_groups()
bool has_cofactor() const
static size_t clear_registered_curve_data()
static bool unregister(const OID &oid)
static bool supports_named_group(std::string_view name)
EC_Group & operator=(const EC_Group &)
size_t get_p_bytes() const
static OID EC_group_identity_from_order(const BigInt &order)
std::string PEM_encode(EC_Group_Encoding form=EC_Group_Encoding::Explicit) const
size_t get_order_bits() const
size_t get_order_bytes() const
bool on_the_curve() const
const EC_Scalar_Data & _inner() const
static std::optional< OID > from_name(std::string_view name)
std::string to_string() const
static OID from_string(std::string_view str)
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)
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
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_bailie_psw_probable_prime(const BigInt &n, const Barrett_Reduction &mod_n)
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
lock_guard< T > lock_guard_type
BigInt sqrt_modulo_prime(const BigInt &a, const BigInt &p)