Exit if OpenSSL doesn't have secp256k1 support (RIPD-900)

This commit is contained in:
Nik Bougalis
2015-08-01 01:08:24 -07:00
parent b8f2fdb6ac
commit f38b373cb6
2 changed files with 27 additions and 19 deletions

View File

@@ -22,6 +22,7 @@
#include <ripple/crypto/impl/ec_key.h> #include <ripple/crypto/impl/ec_key.h>
#include <ripple/crypto/impl/openssl.h> #include <ripple/crypto/impl/openssl.h>
#include <ripple/protocol/digest.h> #include <ripple/protocol/digest.h>
#include <beast/module/core/diagnostic/FatalError.h>
#include <array> #include <array>
#include <string> #include <string>
#include <openssl/pem.h> #include <openssl/pem.h>
@@ -31,10 +32,24 @@ namespace ripple {
namespace openssl { namespace openssl {
static EC_GROUP const* const secp256k1_group = struct secp256k1_data
EC_GROUP_new_by_curve_name (NID_secp256k1); {
static bignum const secp256k1_order = EC_GROUP const* group;
get_order (secp256k1_group); bignum order;
secp256k1_data ()
{
group = EC_GROUP_new_by_curve_name (NID_secp256k1);
if (!group)
beast::FatalError ("The OpenSSL library on this system lacks elliptic curve support.");
bn_ctx ctx;
order = get_order (group, ctx);
}
};
static secp256k1_data const secp256k1curve;
} // namespace openssl } // namespace openssl
@@ -83,7 +98,7 @@ static bignum generateRootDeterministicKey (uint128 const& seed)
privKey.assign ((unsigned char const*) &root, sizeof (root)); privKey.assign ((unsigned char const*) &root, sizeof (root));
root.zero(); // security erase root.zero(); // security erase
} }
while (privKey.is_zero() || privKey >= secp256k1_order); while (privKey.is_zero() || privKey >= secp256k1curve.order);
return privKey; return privKey;
} }
@@ -97,7 +112,7 @@ Blob generateRootDeterministicPublicKey (uint128 const& seed)
bignum privKey = generateRootDeterministicKey (seed); bignum privKey = generateRootDeterministicKey (seed);
// compute the corresponding public key point // compute the corresponding public key point
ec_point pubKey = multiply (secp256k1_group, privKey, ctx); ec_point pubKey = multiply (secp256k1curve.group, privKey, ctx);
privKey.clear(); // security erase privKey.clear(); // security erase
@@ -116,7 +131,7 @@ uint256 generateRootDeterministicPrivateKey (uint128 const& seed)
// <-- root public generator in EC format // <-- root public generator in EC format
static ec_point generateRootPubKey (bignum&& pubGenerator) static ec_point generateRootPubKey (bignum&& pubGenerator)
{ {
ec_point pubPoint = bn2point (secp256k1_group, pubGenerator.get()); ec_point pubPoint = bn2point (secp256k1curve.group, pubGenerator.get());
return pubPoint; return pubPoint;
} }
@@ -155,13 +170,13 @@ Blob generatePublicDeterministicKey (Blob const& pubGen, int seq)
bn_ctx ctx; bn_ctx ctx;
// Calculate the private additional key. // Calculate the private additional key.
bignum hash = makeHash (pubGen, seq, secp256k1_order); bignum hash = makeHash (pubGen, seq, secp256k1curve.order);
// Calculate the corresponding public key. // Calculate the corresponding public key.
ec_point newPoint = multiply (secp256k1_group, hash, ctx); ec_point newPoint = multiply (secp256k1curve.group, hash, ctx);
// Add the master public key and set. // Add the master public key and set.
add_to (secp256k1_group, rootPubKey, newPoint, ctx); add_to (secp256k1curve.group, rootPubKey, newPoint, ctx);
return serialize_ec_point (newPoint); return serialize_ec_point (newPoint);
} }
@@ -176,10 +191,10 @@ uint256 generatePrivateDeterministicKey (
bn_ctx ctx; bn_ctx ctx;
// calculate the private additional key // calculate the private additional key
bignum privKey = makeHash (pubGen, seq, secp256k1_order); bignum privKey = makeHash (pubGen, seq, secp256k1curve.order);
// calculate the final private key // calculate the final private key
add_to (rootPrivKey, privKey, secp256k1_order, ctx); add_to (rootPrivKey, privKey, secp256k1curve.order, ctx);
rootPrivKey.clear(); // security erase rootPrivKey.clear(); // security erase

View File

@@ -130,13 +130,6 @@ public:
bignum get_order (EC_GROUP const* group, bn_ctx& ctx); bignum get_order (EC_GROUP const* group, bn_ctx& ctx);
inline bignum get_order (EC_GROUP const* group)
{
bn_ctx ctx;
return get_order (group, ctx);
}
inline void add_to (bignum const& a, inline void add_to (bignum const& a,
bignum& b, bignum& b,
bignum const& modulus, bignum const& modulus,