Skip to content

Commit

Permalink
Split up the curves one per module
Browse files Browse the repository at this point in the history
  • Loading branch information
randombit committed Jun 10, 2024
1 parent 2988574 commit c2f91bd
Show file tree
Hide file tree
Showing 26 changed files with 672 additions and 301 deletions.
1 change: 1 addition & 0 deletions src/lib/math/pcurves/info.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ pcurves_id.h
pcurves_impl.h
pcurves_util.h
pcurves_wrap.h
pcurves_instance.h
</header:internal>
277 changes: 138 additions & 139 deletions src/lib/math/pcurves/pcurves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,178 +6,177 @@

#include <botan/internal/pcurves.h>

#include <botan/internal/pcurves_wrap.h>
#include <botan/internal/pcurves_instance.h>

namespace Botan::PCurve {

namespace {

// clang-format off

namespace secp256r1 {

class Params final : public EllipticCurveParameters<
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
"FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
"5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
"FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
"6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296",
"4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
-10> {
};

class Curve final : public EllipticCurve<Params> {};

} // namespace secp256r1
#if defined(BOTAN_HAS_ASN1)
#include <botan/asn1_obj.h>
#endif

namespace secp384r1 {

class Params final : public EllipticCurveParameters<
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC",
"B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973",
"AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7",
"3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F",
-12> {
};

class Curve final : public EllipticCurve<Params> {};
namespace Botan::PCurve {

#if !defined(BOTAN_HAS_PCURVES_SECP256R1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::secp256r1() {
return nullptr;
}
#endif

namespace secp521r1 {

class Params final : public EllipticCurveParameters<
"1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
"1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC",
"51953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
"1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
"C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66",
"11839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650",
-4> {
};

class Curve final : public EllipticCurve<Params, P521Rep> {};

#if !defined(BOTAN_HAS_PCURVES_SECP384R1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::secp384r1() {
return nullptr;
}
#endif

namespace secp256k1 {

class Params final : public EllipticCurveParameters<
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
"0",
"7",
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
"79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798",
"483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8"> {
};

class Curve final : public EllipticCurve<Params> {};

#if !defined(BOTAN_HAS_PCURVES_SECP521R1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::secp521r1() {
return nullptr;
}
#endif

namespace brainpool256r1 {

class Params final : public EllipticCurveParameters<
"A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377",
"7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9",
"26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6",
"A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7",
"8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262",
"547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"> {
};

class Curve final : public EllipticCurve<Params> {};

#if !defined(BOTAN_HAS_PCURVES_SECP256K1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::secp256k1() {
return nullptr;
}
#endif

namespace brainpool384r1 {

class Params final : public EllipticCurveParameters<
"8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53",
"7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826",
"4A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11",
"8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565",
"1D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E",
"8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315"> {
};

class Curve final : public EllipticCurve<Params> {};

#if !defined(BOTAN_HAS_PCURVES_BRAINPOOL256R1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::brainpool256r1() {
return nullptr;
}
#endif

namespace brainpool512r1 {

class Params final : public EllipticCurveParameters<
"AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3",
"7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA",
"3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723",
"AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069",
"81AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F822",
"7DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892"> {
};

class Curve final : public EllipticCurve<Params> {};

#if !defined(BOTAN_HAS_PCURVES_BRAINPOOL384R1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::brainpool384r1() {
return nullptr;
}
#endif

namespace frp256v1 {

class Params final : public EllipticCurveParameters<
"F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C03",
"F1FD178C0B3AD58F10126DE8CE42435B3961ADBCABC8CA6DE8FCF353D86E9C00",
"EE353FCA5428A9300D4ABA754A44C00FDFEC0C9AE4B1A1803075ED967B7BB73F",
"F1FD178C0B3AD58F10126DE8CE42435B53DC67E140D2BF941FFDD459C6D655E1",
"B6B3D4C356C139EB31183D4749D423958C27D2DCAF98B70164C97A2DD98F5CFF",
"6142E0F7C8B204911F9271F0F3ECEF8C2701C307E8E4C9E183115A1554062CFB"> {
};

class Curve final : public EllipticCurve<Params> {};

#if !defined(BOTAN_HAS_PCURVES_BRAINPOOL512R1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::brainpool512r1() {
return nullptr;
}
#endif

namespace sm2p256v1 {

class Params final : public EllipticCurveParameters<
"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF",
"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC",
"28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93",
"FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123",
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7",
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0"> {
};

class Curve final : public EllipticCurve<Params> {};

#if !defined(BOTAN_HAS_PCURVES_FRP256V1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::frp256v1() {
return nullptr;
}
#endif

// clang-format on

} // namespace
#if !defined(BOTAN_HAS_PCURVES_SM2P256V1)
//static
std::shared_ptr<const PrimeOrderCurve> PCurveInstance::sm2p256v1() {
return nullptr;
}
#endif

std::shared_ptr<const PrimeOrderCurve> PrimeOrderCurve::from_id(PrimeOrderCurveId id) {
switch(id.code()) {
case PrimeOrderCurveId::secp256r1:
return PrimeOrderCurveImpl<secp256r1::Curve>::instance();
return PCurveInstance::secp256r1();
case PrimeOrderCurveId::secp384r1:
return PrimeOrderCurveImpl<secp384r1::Curve>::instance();
return PCurveInstance::secp384r1();
case PrimeOrderCurveId::secp521r1:
return PrimeOrderCurveImpl<secp521r1::Curve>::instance();
return PCurveInstance::secp521r1();
case PrimeOrderCurveId::secp256k1:
return PrimeOrderCurveImpl<secp256k1::Curve>::instance();
return PCurveInstance::secp256k1();
case PrimeOrderCurveId::brainpool256r1:
return PrimeOrderCurveImpl<brainpool256r1::Curve>::instance();
return PCurveInstance::brainpool256r1();
case PrimeOrderCurveId::brainpool384r1:
return PrimeOrderCurveImpl<brainpool384r1::Curve>::instance();
return PCurveInstance::brainpool384r1();
case PrimeOrderCurveId::brainpool512r1:
return PrimeOrderCurveImpl<brainpool512r1::Curve>::instance();
return PCurveInstance::brainpool512r1();
case PrimeOrderCurveId::frp256v1:
return PrimeOrderCurveImpl<frp256v1::Curve>::instance();
return PCurveInstance::frp256v1();
case PrimeOrderCurveId::sm2p256v1:
return PrimeOrderCurveImpl<sm2p256v1::Curve>::instance();
return PCurveInstance::sm2p256v1();
}
return {};
}

std::vector<PrimeOrderCurveId> PrimeOrderCurveId::all() {
return {
PrimeOrderCurveId::secp256r1,
PrimeOrderCurveId::secp384r1,
PrimeOrderCurveId::secp521r1,
PrimeOrderCurveId::secp256k1,
PrimeOrderCurveId::brainpool256r1,
PrimeOrderCurveId::brainpool384r1,
PrimeOrderCurveId::brainpool512r1,
PrimeOrderCurveId::frp256v1,
PrimeOrderCurveId::sm2p256v1,
};
}

std::string PrimeOrderCurveId::to_string() const {
switch(this->code()) {
case PrimeOrderCurveId::secp256r1:
return "secp256r1";
case PrimeOrderCurveId::secp384r1:
return "secp384r1";
case PrimeOrderCurveId::secp521r1:
return "secp521r1";
case PrimeOrderCurveId::secp256k1:
return "secp256k1";
case PrimeOrderCurveId::brainpool256r1:
return "brainpool256r1";
case PrimeOrderCurveId::brainpool384r1:
return "brainpool384r1";
case PrimeOrderCurveId::brainpool512r1:
return "brainpool512r1";
case PrimeOrderCurveId::frp256v1:
return "frp256v1";
case PrimeOrderCurveId::sm2p256v1:
return "sm2p256v1";
}

return "unknown";
}

//static
std::optional<PrimeOrderCurveId> PrimeOrderCurveId::from_string(std::string_view name) {
if(name == "secp256r1") {
return PCurve::PrimeOrderCurveId::secp256r1;
} else if(name == "secp384r1") {
return PCurve::PrimeOrderCurveId::secp384r1;
} else if(name == "secp521r1") {
return PCurve::PrimeOrderCurveId::secp521r1;
} else if(name == "secp256k1") {
return PCurve::PrimeOrderCurveId::secp256k1;
} else if(name == "brainpool256r1") {
return PCurve::PrimeOrderCurveId::brainpool256r1;
} else if(name == "brainpool384r1") {
return PCurve::PrimeOrderCurveId::brainpool384r1;
} else if(name == "brainpool512r1") {
return PCurve::PrimeOrderCurveId::brainpool512r1;
} else if(name == "frp256v1") {
return PCurve::PrimeOrderCurveId::frp256v1;
} else if(name == "sm2p256v1") {
return PCurve::PrimeOrderCurveId::sm2p256v1;
} else {
return {};
}
}

#if defined(BOTAN_HAS_ASN1)

//static
std::optional<PrimeOrderCurveId> PrimeOrderCurveId::from_oid(const OID& oid) {
const std::string name = oid.human_name_or_empty();
if(name.empty()) {
return {};
} else {
return PrimeOrderCurveId::from_string(name);
}
}

#endif

} // namespace Botan::PCurve
4 changes: 0 additions & 4 deletions src/lib/math/pcurves/pcurves.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ namespace Botan {

class RandomNumberGenerator;

#if defined(BOTAN_HAS_ASN1)
class OID;
#endif

} // namespace Botan

namespace Botan::PCurve {
Expand Down
9 changes: 9 additions & 0 deletions src/lib/math/pcurves/pcurves_brainpool256r1/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<defines>
PCURVES_BRAINPOOL256R1 -> 20240608
</defines>

<module_info>
name -> "PCurve brainpool256r1"
brief -> "brainpool256r1"
type -> "Internal"
</module_info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* (C) 2024 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/internal/pcurves_instance.h>

#include <botan/internal/pcurves_wrap.h>

namespace Botan::PCurve {

namespace {

// clang-format off
namespace brainpool256r1 {

class Params final : public EllipticCurveParameters<
"A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377",
"7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9",
"26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6",
"A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7",
"8BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262",
"547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997"> {
};

class Curve final : public EllipticCurve<Params> {};

}

// clang-format on

} // namespace

std::shared_ptr<const PrimeOrderCurve> PCurveInstance::brainpool256r1() {
static auto g_brainpool256r1 = std::make_shared<const PrimeOrderCurveImpl<brainpool256r1::Curve>>();
return g_brainpool256r1;
}

} // namespace Botan::PCurve
9 changes: 9 additions & 0 deletions src/lib/math/pcurves/pcurves_brainpool384r1/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<defines>
PCURVES_BRAINPOOL384R1 -> 20240608
</defines>

<module_info>
name -> "PCurve brainpool384r1"
brief -> "brainpool384r1"
type -> "Internal"
</module_info>
Loading

0 comments on commit c2f91bd

Please sign in to comment.