Browse Source

replace asserts with exception

cl-refactor
subtly 10 years ago
parent
commit
8eaa26e716
  1. 5
      libdevcrypto/Common.cpp
  2. 3
      libdevcrypto/Common.h
  3. 12
      libdevcrypto/EC.cpp

5
libdevcrypto/Common.cpp

@ -127,9 +127,12 @@ h256 Nonce::get(bool _commit)
for (unsigned i = 0; i < 32; ++i)
seed[i] = (byte)d(s_eng);
}
if (!seed)
throw InvalidState();
// prevent seed reuse if process terminates abnormally
writeFile(seedFile, bytes());
}
assert(seed);
h256 prev(seed);
sha3(prev.ref(), seed.ref());
if (_commit)

3
libdevcrypto/Common.h

@ -26,6 +26,7 @@
#include <libdevcore/Common.h>
#include <libdevcore/FixedHash.h>
#include <libdevcore/Exceptions.h>
namespace dev
{
@ -113,6 +114,8 @@ private:
namespace crypto
{
struct InvalidState: virtual Exception {};
/**
* @brief Generator for nonce material
*/

12
libdevcrypto/EC.cpp

@ -44,10 +44,15 @@ void crypto::toPublic(Secret const& _s, Public& o_public)
h256 crypto::kdf(Secret const& _priv, h256 const& _hash)
{
// H(H(r||k)^h)
h256 s;
sha3mac(Nonce::get().ref(), _priv.ref(), s.ref());
assert(s);
return sha3((_hash ^ s).asBytes());
s ^= _hash;
sha3(s.ref(), s.ref());
if (!s || !_hash || !_priv)
throw InvalidState();
return std::move(s);
}
void crypto::encrypt(Public const& _k, bytes& io_cipher)
@ -96,7 +101,8 @@ Signature crypto::sign(Secret const& _key, h256 const& _hash)
Integer e(_hash.asBytes().data(), 32);
Integer k(kdf(_key, _hash).data(), 32);
assert(k);
if (k == 0)
throw InvalidState();
k = 1 + (k % (qs - 1));
ECP::Point rp = secp256k1Params.ExponentiateBase(k);

Loading…
Cancel
Save