diff --git a/libdevcore/Exceptions.h b/libdevcore/Exceptions.h index 932276d01..7576050e6 100644 --- a/libdevcore/Exceptions.h +++ b/libdevcore/Exceptions.h @@ -24,7 +24,6 @@ #include #include #include -#include #include "CommonData.h" #include "FixedHash.h" diff --git a/libdevcrypto/Common.cpp b/libdevcrypto/Common.cpp index 14dad127e..38d2eb934 100644 --- a/libdevcrypto/Common.cpp +++ b/libdevcrypto/Common.cpp @@ -110,34 +110,36 @@ bool dev::verify(Public _p, Signature _s, h256 _hash) h256 Nonce::get(bool _commit) { // todo: atomic efface bit, periodic save, kdf, rr, rng - static h256 seed; - static string seedFile(getDataDir() + "/seed"); - static mutex x; - lock_guard l(x); - if (!seed) + // todo: encrypt + static h256 s_seed; + static string s_seedFile(getDataDir() + "/seed"); + static mutex s_x; + lock_guard l(s_x); + if (!s_seed) { - static Nonce nonce; - bytes b = contents(seedFile); + static Nonce s_nonce; + bytes b = contents(s_seedFile); if (b.size() == 32) - memcpy(seed.data(), b.data(), 32); + memcpy(s_seed.data(), b.data(), 32); else { + // todo: replace w/entropy from user and system std::mt19937_64 s_eng(time(0)); std::uniform_int_distribution d(0, 255); for (unsigned i = 0; i < 32; ++i) - seed[i] = (byte)d(s_eng); + s_seed[i] = (byte)d(s_eng); } - if (!seed) + if (!s_seed) throw InvalidState(); // prevent seed reuse if process terminates abnormally - writeFile(seedFile, bytes()); + writeFile(s_seedFile, bytes()); } - h256 prev(seed); - sha3(prev.ref(), seed.ref()); + h256 prev(s_seed); + sha3(prev.ref(), s_seed.ref()); if (_commit) - writeFile(seedFile, seed.asBytes()); - return seed; + writeFile(s_seedFile, s_seed.asBytes()); + return std::move(s_seed); } Nonce::~Nonce() diff --git a/libdevcrypto/Common.h b/libdevcrypto/Common.h index 7e74c754d..6fcda73cb 100644 --- a/libdevcrypto/Common.h +++ b/libdevcrypto/Common.h @@ -114,8 +114,8 @@ private: namespace crypto { -struct InvalidState: virtual Exception {}; - +struct InvalidState: public dev::Exception {}; + /** * @brief Generator for nonce material */ diff --git a/libwhisper/Message.h b/libwhisper/Message.h index 6b28073b7..677d16f00 100644 --- a/libwhisper/Message.h +++ b/libwhisper/Message.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "Common.h" diff --git a/test/crypto.cpp b/test/crypto.cpp index 82950f09d..b0785aca1 100644 --- a/test/crypto.cpp +++ b/test/crypto.cpp @@ -92,7 +92,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_vs_secp256k1) } } -BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_ecdsav) +BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_secp256k1libport) { // cryptopp implementation of secp256k1lib sign_compact w/recid parameter and recovery of public key from signature @@ -128,6 +128,12 @@ BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_ecdsav) Integer s = (kInv * (Integer(secret.asBytes().data(), 32)*r + heInt)) % q; BOOST_REQUIRE(!!r && !!s); +/* + // For future reference: + // According to maths, this codepath can't be reached, however, it's in secp256k1. + // Commenting this out diverges from codebase implementation. + // To be removed after upstream PR and proof are evaulated. + if (s > params.GetSubgroupOrder()) { // note: this rarely happens @@ -135,6 +141,7 @@ BOOST_AUTO_TEST_CASE(cryptopp_cryptopp_ecdsav) if (recid) recid ^= 1; } + */ Signature sig; r.Encode(sig.data(), 32);