diff --git a/src/ripple/crypto/GenerateDeterministicKey.h b/src/ripple/crypto/GenerateDeterministicKey.h index 6070d45bb2..ace2d52c7c 100644 --- a/src/ripple/crypto/GenerateDeterministicKey.h +++ b/src/ripple/crypto/GenerateDeterministicKey.h @@ -29,11 +29,12 @@ namespace ripple { -Blob GenerateRootDeterministicPublicKey (const uint128& passPhrase); -uint256 GenerateRootDeterministicPrivateKey (const uint128& passPhrase); +Blob generateRootDeterministicPublicKey (const uint128& passPhrase); +uint256 generateRootDeterministicPrivateKey (const uint128& passPhrase); -Blob GeneratePublicDeterministicKey (Blob const& generator, int n); -uint256 GeneratePrivateDeterministicKey (Blob const& family, uint128 const& seed, int n); +Blob generatePublicDeterministicKey (Blob const& generator, int n); +uint256 generatePrivateDeterministicKey ( + Blob const& family, uint128 const& seed, int n); } // ripple diff --git a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp index 8b10f76e99..804d2c4986 100644 --- a/src/ripple/crypto/impl/GenerateDeterministicKey.cpp +++ b/src/ripple/crypto/impl/GenerateDeterministicKey.cpp @@ -30,8 +30,10 @@ namespace ripple { namespace openssl { -static EC_GROUP const* const secp256k1_group = EC_GROUP_new_by_curve_name (NID_secp256k1); -static bignum const secp256k1_order = get_order (secp256k1_group); +static EC_GROUP const* const secp256k1_group = + EC_GROUP_new_by_curve_name (NID_secp256k1); +static bignum const secp256k1_order = + get_order (secp256k1_group); } // namespace openssl @@ -71,7 +73,7 @@ copy_uint32 (FwdIt out, std::uint32_t v) // --> seed // <-- private root generator + public root generator -static bignum GenerateRootDeterministicKey (uint128 const& seed) +static bignum generateRootDeterministicKey (uint128 const& seed) { // find non-zero private key less than the curve's order bignum privKey; @@ -97,11 +99,11 @@ static bignum GenerateRootDeterministicKey (uint128 const& seed) // --> seed // <-- private root generator + public root generator -Blob GenerateRootDeterministicPublicKey (uint128 const& seed) +Blob generateRootDeterministicPublicKey (uint128 const& seed) { bn_ctx ctx; - bignum privKey = GenerateRootDeterministicKey (seed); + bignum privKey = generateRootDeterministicKey (seed); // compute the corresponding public key point ec_point pubKey = multiply (secp256k1_group, privKey, ctx); @@ -111,9 +113,9 @@ Blob GenerateRootDeterministicPublicKey (uint128 const& seed) return serialize_ec_point (pubKey); } -uint256 GenerateRootDeterministicPrivateKey (uint128 const& seed) +uint256 generateRootDeterministicPrivateKey (uint128 const& seed) { - bignum key = GenerateRootDeterministicKey (seed); + bignum key = generateRootDeterministicKey (seed); return uint256_from_bignum_clear (key); } @@ -121,7 +123,7 @@ uint256 GenerateRootDeterministicPrivateKey (uint128 const& seed) // Take ripple address. // --> root public generator (consumes) // <-- 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()); @@ -155,10 +157,10 @@ static bignum makeHash (Blob const& pubGen, int seq, bignum const& order) } // --> public generator -Blob GeneratePublicDeterministicKey (Blob const& pubGen, int seq) +Blob generatePublicDeterministicKey (Blob const& pubGen, int seq) { // publicKey(n) = rootPublicKey EC_POINT_+ Hash(pubHash|seq)*point - ec_point rootPubKey = GenerateRootPubKey (bignum (pubGen)); + ec_point rootPubKey = generateRootPubKey (bignum (pubGen)); bn_ctx ctx; @@ -175,10 +177,11 @@ Blob GeneratePublicDeterministicKey (Blob const& pubGen, int seq) } // --> root private key -uint256 GeneratePrivateDeterministicKey (Blob const& pubGen, uint128 const& seed, int seq) +uint256 generatePrivateDeterministicKey ( + Blob const& pubGen, uint128 const& seed, int seq) { // privateKey(n) = (rootPrivateKey + Hash(pubHash|seq)) % order - bignum rootPrivKey = GenerateRootDeterministicKey (seed); + bignum rootPrivKey = generateRootDeterministicKey (seed); bn_ctx ctx; diff --git a/src/ripple/crypto/tests/CKey.test.cpp b/src/ripple/crypto/tests/CKey.test.cpp index 20b9455995..c17f70bbaf 100644 --- a/src/ripple/crypto/tests/CKey.test.cpp +++ b/src/ripple/crypto/tests/CKey.test.cpp @@ -40,14 +40,16 @@ public: seed1.SetHex ("71ED064155FFADFA38782C5E0158CB26"); seed2.SetHex ("CF0C3BE4485961858C4198515AE5B965"); - uint256 const priv1 = GenerateRootDeterministicPrivateKey (seed1); - uint256 const priv2 = GenerateRootDeterministicPrivateKey (seed2); + uint256 const priv1 = generateRootDeterministicPrivateKey (seed1); + uint256 const priv2 = generateRootDeterministicPrivateKey (seed2); - unexpected (to_string (priv1) != "7CFBA64F771E93E817E15039215430B53F7401C34931D111EAB3510B22DBB0D8", - "Incorrect private key for generator"); + unexpected (to_string (priv1) != "7CFBA64F771E93E817E15039215430B53F74" + "01C34931D111EAB3510B22DBB0D8", + "Incorrect private key for generator"); - unexpected (to_string (priv2) != "98BC2EACB26EB021D1A6293C044D88BA2F0B6729A2772DEEBF2E21A263C1740B", - "Incorrect private key for generator"); + unexpected (to_string (priv2) != "98BC2EACB26EB021D1A6293C044D88BA2F0B" + "6729A2772DEEBF2E21A263C1740B", + "Incorrect private key for generator"); } }; diff --git a/src/ripple/protocol/impl/RippleAddress.cpp b/src/ripple/protocol/impl/RippleAddress.cpp index 89bf975164..e70dbadbbc 100644 --- a/src/ripple/protocol/impl/RippleAddress.cpp +++ b/src/ripple/protocol/impl/RippleAddress.cpp @@ -60,12 +60,13 @@ bool isCanonicalEd25519Signature (std::uint8_t const* signature) } // <-- seed -static uint128 PassPhraseToKey (std::string const& passPhrase) +static +uint128 PassPhraseToKey (std::string const& passPhrase) { Serializer s; s.addRaw (passPhrase); - // NIKB TODO this caling sequence is a bit ugly; this should be improved. + // NIKB TODO this calling sequence is a bit ugly; this should be improved. uint256 hash256 = s.getSHA512Half (); uint128 ret (uint128::fromVoid (hash256.data())); @@ -74,7 +75,9 @@ static uint128 PassPhraseToKey (std::string const& passPhrase) return ret; } -static bool verifySignature (Blob const& pubkey, uint256 const& hash, Blob const& sig, ECDSA fullyCanonical) +static +bool verifySignature (Blob const& pubkey, uint256 const& hash, Blob const& sig, + ECDSA fullyCanonical) { if (! isCanonicalECDSASig (sig, fullyCanonical)) { @@ -106,7 +109,8 @@ bool RippleAddress::isSet () const // NodePublic // -static uint160 Hash160 (Blob const& vch) +static +uint160 Hash160 (Blob const& vch) { uint256 hash1; SHA256 (vch.data (), vch.size (), hash1.data ()); @@ -122,7 +126,7 @@ RippleAddress RippleAddress::createNodePublic (RippleAddress const& naSeed) RippleAddress naNew; // YYY Should there be a GetPubKey() equiv that returns a uint256? - naNew.setNodePublic (GenerateRootDeterministicPublicKey (naSeed.getSeed())); + naNew.setNodePublic (generateRootDeterministicPublicKey (naSeed.getSeed())); return naNew; } @@ -152,6 +156,12 @@ RippleAddress::toPublicKey() const return RipplePublicKey (vchData.begin(), vchData.end()); } +static +std::runtime_error badSourceError (int nVersion) +{ + return std::runtime_error ("bad source: " + std::to_string (nVersion)); +} + NodeID RippleAddress::getNodeID () const { switch (nVersion) @@ -167,7 +177,7 @@ NodeID RippleAddress::getNodeID () const } default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -182,7 +192,7 @@ Blob const& RippleAddress::getNodePublic () const return vchData; default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -197,13 +207,14 @@ std::string RippleAddress::humanNodePublic () const return ToString (); default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } bool RippleAddress::setNodePublic (std::string const& strPublic) { - mIsValid = SetString (strPublic, VER_NODE_PUBLIC, Base58::getRippleAlphabet ()); + mIsValid = SetString ( + strPublic, VER_NODE_PUBLIC, Base58::getRippleAlphabet ()); return mIsValid; } @@ -215,12 +226,14 @@ void RippleAddress::setNodePublic (Blob const& vPublic) SetData (VER_NODE_PUBLIC, vPublic); } -bool RippleAddress::verifyNodePublic (uint256 const& hash, Blob const& vchSig, ECDSA fullyCanonical) const +bool RippleAddress::verifyNodePublic ( + uint256 const& hash, Blob const& vchSig, ECDSA fullyCanonical) const { return verifySignature (getNodePublic(), hash, vchSig, fullyCanonical); } -bool RippleAddress::verifyNodePublic (uint256 const& hash, std::string const& strSig, ECDSA fullyCanonical) const +bool RippleAddress::verifyNodePublic ( + uint256 const& hash, std::string const& strSig, ECDSA fullyCanonical) const { Blob vchSig (strSig.begin (), strSig.end ()); @@ -235,7 +248,7 @@ RippleAddress RippleAddress::createNodePrivate (RippleAddress const& naSeed) { RippleAddress naNew; - naNew.setNodePrivate (GenerateRootDeterministicPrivateKey (naSeed.getSeed())); + naNew.setNodePrivate (generateRootDeterministicPrivateKey (naSeed.getSeed())); return naNew; } @@ -251,7 +264,7 @@ Blob const& RippleAddress::getNodePrivateData () const return vchData; default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -266,7 +279,7 @@ uint256 RippleAddress::getNodePrivate () const return uint256 (vchData); default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -281,13 +294,14 @@ std::string RippleAddress::humanNodePrivate () const return ToString (); default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } bool RippleAddress::setNodePrivate (std::string const& strPrivate) { - mIsValid = SetString (strPrivate, VER_NODE_PRIVATE, Base58::getRippleAlphabet ()); + mIsValid = SetString ( + strPrivate, VER_NODE_PRIVATE, Base58::getRippleAlphabet ()); return mIsValid; } @@ -337,7 +351,7 @@ Account RippleAddress::getAccountID () const } default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -412,7 +426,7 @@ std::string RippleAddress::humanAccountID () const } default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -468,7 +482,7 @@ Blob const& RippleAddress::getAccountPublic () const return vchData; default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -486,13 +500,14 @@ std::string RippleAddress::humanAccountPublic () const return ToString (); default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } bool RippleAddress::setAccountPublic (std::string const& strPublic) { - mIsValid = SetString (strPublic, VER_ACCOUNT_PUBLIC, Base58::getRippleAlphabet ()); + mIsValid = SetString ( + strPublic, VER_ACCOUNT_PUBLIC, Base58::getRippleAlphabet ()); return mIsValid; } @@ -506,7 +521,8 @@ void RippleAddress::setAccountPublic (Blob const& vPublic) void RippleAddress::setAccountPublic (RippleAddress const& generator, int seq) { - setAccountPublic (GeneratePublicDeterministicKey (generator.getGenerator(), seq)); + setAccountPublic (generatePublicDeterministicKey ( + generator.getGenerator(), seq)); } bool RippleAddress::accountPublicVerify ( @@ -522,16 +538,12 @@ bool RippleAddress::accountPublicVerify ( uint8_t const* publicKey = &vchData[1]; uint8_t const* signature = &vucSig[0]; - if (ed25519_sign_open (message.data(), message.size(), publicKey, signature) != 0) - { - return false; - } - - return isCanonicalEd25519Signature (signature); + return !ed25519_sign_open (message.data(), message.size(), + publicKey, signature) + && isCanonicalEd25519Signature (signature); } uint256 const uHash = getSHA512Half (message); - return verifySignature (getAccountPublic(), uHash, vucSig, fullyCanonical); } @@ -568,7 +580,7 @@ uint256 RippleAddress::getAccountPrivate () const return uint256::fromVoid (vchData.data() + (vchData.size() - 32)); default: - throw std::runtime_error ("bad source: " + std::to_string(nVersion)); + throw badSourceError (nVersion); } } @@ -595,7 +607,8 @@ void RippleAddress::setAccountPrivate (uint256 hash256) void RippleAddress::setAccountPrivate ( RippleAddress const& generator, RippleAddress const& naSeed, int seq) { - uint256 secretKey = GeneratePrivateDeterministicKey (generator.getGenerator(), naSeed.getSeed(), seq); + uint256 secretKey = generatePrivateDeterministicKey ( + generator.getGenerator(), naSeed.getSeed(), seq); setAccountPrivate (secretKey); } @@ -610,8 +623,10 @@ Blob RippleAddress::accountPrivateSign (Blob const& message) const ed25519_publickey (secretKey, publicKey); - ed25519_sign (message.data(), message.size(), secretKey, publicKey, &signature[0]); - + ed25519_sign ( + message.data(), message.size(), secretKey, publicKey, + &signature[0]); + assert (isCanonicalEd25519Signature (signature.data())); return signature; @@ -643,6 +658,7 @@ Blob RippleAddress::accountPrivateEncrypt ( } catch (...) { + // TODO: log this or explain why this is unimportant! } } @@ -664,6 +680,7 @@ Blob RippleAddress::accountPrivateDecrypt ( } catch (...) { + // TODO: log this or explain why this is unimportant! } } @@ -687,7 +704,7 @@ Blob const& RippleAddress::getGenerator () const return vchData; default: - throw std::runtime_error ("bad source: " + std::to_string(nVersion)); + throw badSourceError (nVersion); } } @@ -702,7 +719,7 @@ std::string RippleAddress::humanGenerator () const return ToString (); default: - throw std::runtime_error ("bad source: " + std::to_string(nVersion)); + throw badSourceError (nVersion); } } @@ -715,7 +732,7 @@ void RippleAddress::setGenerator (Blob const& vPublic) RippleAddress RippleAddress::createGeneratorPublic (RippleAddress const& naSeed) { RippleAddress naNew; - naNew.setGenerator (GenerateRootDeterministicPublicKey (naSeed.getSeed())); + naNew.setGenerator (generateRootDeterministicPublicKey (naSeed.getSeed())); return naNew; } @@ -734,7 +751,7 @@ uint128 RippleAddress::getSeed () const return uint128 (vchData); default: - throw std::runtime_error ("bad source: " + std::to_string(nVersion)); + throw badSourceError (nVersion); } } @@ -762,7 +779,7 @@ std::string RippleAddress::humanSeed1751 () const } default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -777,7 +794,7 @@ std::string RippleAddress::humanSeed () const return ToString (); default: - throw std::runtime_error (str (boost::format ("bad source: %d") % int (nVersion))); + throw badSourceError (nVersion); } } @@ -890,7 +907,7 @@ RippleAddress getSeedFromRPC (Json::Value const& params) { // This function is only called when `key_type` is present. assert (params.isMember (jss::key_type)); - + bool const has_passphrase = params.isMember (jss::passphrase); bool const has_seed = params.isMember (jss::seed); bool const has_seed_hex = params.isMember (jss::seed_hex);