Browse Source

code review

cl-refactor
subtly 10 years ago
parent
commit
c99e09dc49
  1. 2
      libdevcrypto/AES.cpp
  2. 6
      libdevcrypto/AES.h
  3. 26
      libdevcrypto/Common.cpp
  4. 18
      libdevcrypto/Common.h

2
libdevcrypto/AES.cpp

@ -34,7 +34,7 @@ struct aes::Aes128Ctr
{
mode.SetKeyWithIV(_k.data(), sizeof(h128), Nonce::get().data());
}
CryptoPP::CTR_Mode<CryptoPP::AES>::Encryption mode;
CTR_Mode<AES>::Encryption mode;
};
Stream::Stream(StreamType _t, h128 _ckey):

6
libdevcrypto/AES.h

@ -53,6 +53,9 @@ public:
virtual size_t streamOut(bytes& o_bytes);
private:
Stream(Stream const&) = delete;
Stream& operator=(Stream const&) = delete;
h128 m_cSecret;
bytes m_text;
@ -74,6 +77,9 @@ public:
void adjustInterval(unsigned _interval) { m_macInterval = _interval; };
private:
AuthenticatedStream(AuthenticatedStream const&) = delete;
AuthenticatedStream& operator=(AuthenticatedStream const&) = delete;
std::atomic<unsigned> m_macInterval;
h128 m_macSecret;
};

26
libdevcrypto/Common.cpp

@ -33,33 +33,33 @@ using namespace dev::crypto;
static Secp256k1 s_secp256k1;
Public dev::toPublic(Secret _secret)
Public dev::toPublic(Secret const& _secret)
{
Public p;
s_secp256k1.toPublic(_secret, p);
return std::move(p);
}
Address dev::toAddress(Public _public)
Address dev::toAddress(Public const& _public)
{
return s_secp256k1.toAddress(_public);
}
Address dev::toAddress(Secret _secret)
Address dev::toAddress(Secret const& _secret)
{
Public p;
s_secp256k1.toPublic(_secret, p);
return s_secp256k1.toAddress(p);
}
void dev::encrypt(Public _k, bytesConstRef _plain, bytes& o_cipher)
void dev::encrypt(Public const& _k, bytesConstRef _plain, bytes& o_cipher)
{
bytes io = _plain.toBytes();
s_secp256k1.encrypt(_k, io);
o_cipher = std::move(io);
}
bool dev::decrypt(Secret _k, bytesConstRef _cipher, bytes& o_plaintext)
bool dev::decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext)
{
bytes io = _cipher.toBytes();
s_secp256k1.decrypt(_k, io);
@ -69,19 +69,19 @@ bool dev::decrypt(Secret _k, bytesConstRef _cipher, bytes& o_plaintext)
return true;
}
Public dev::recover(Signature _sig, h256 _message)
Public dev::recover(Signature const& _sig, h256 const& _message)
{
return s_secp256k1.recover(_sig, _message.ref());
}
Signature dev::sign(Secret _k, h256 _hash)
Signature dev::sign(Secret const& _k, h256 const& _hash)
{
return s_secp256k1.sign(_k, _hash);
}
bool dev::verify(Public _p, Signature _s, h256 _hash)
bool dev::verify(Public const& _p, Signature const& _s, h256 const& _hash)
{
return s_secp256k1.verify(_p, _s, bytesConstRef(_hash.data(), 32), true);
return s_secp256k1.verify(_p, _s, _hash.ref(), true);
}
KeyPair KeyPair::create()
@ -91,11 +91,7 @@ KeyPair KeyPair::create()
for (int i = 0; i < 100; ++i)
{
h256 sec;
for (unsigned i = 0; i < 32; ++i)
sec[i] = (byte)d(s_eng);
KeyPair ret(sec);
KeyPair ret(FixedHash<32>::random(s_eng));
if (ret.address())
return ret;
}
@ -144,7 +140,7 @@ h256 Nonce::get(bool _commit)
else
{
// todo: replace w/entropy from user and system
std::mt19937_64 s_eng(time(0));
std::mt19937_64 s_eng(time(0) + chrono::high_resolution_clock::now().time_since_epoch().count());
std::uniform_int_distribution<uint16_t> d(0, 255);
for (unsigned i = 0; i < 32; ++i)
s_seed[i] = (byte)d(s_eng);

18
libdevcrypto/Common.h

@ -30,7 +30,7 @@
namespace dev
{
/// A secret key: 32 bytes.
/// @NOTE This is not endian-specific; it's just a bunch of bytes.
using Secret = h256;
@ -59,29 +59,29 @@ using AddressSet = std::set<h160>;
using Secrets = h256s;
/// Convert a secret key into the public key equivalent.
Public toPublic(Secret _secret);
Public toPublic(Secret const& _secret);
/// Convert a public key to address.
Address toAddress(Public _public);
Address toAddress(Public const& _public);
/// Convert a secret key into address of public key equivalent.
/// @returns 0 if it's not a valid secret key.
Address toAddress(Secret _secret);
Address toAddress(Secret const& _secret);
/// Encrypts plain text using Public key.
void encrypt(Public _k, bytesConstRef _plain, bytes& o_cipher);
void encrypt(Public const& _k, bytesConstRef _plain, bytes& o_cipher);
/// Decrypts cipher using Secret key.
bool decrypt(Secret _k, bytesConstRef _cipher, bytes& o_plaintext);
bool decrypt(Secret const& _k, bytesConstRef _cipher, bytes& o_plaintext);
/// Recovers Public key from signed message hash.
Public recover(Signature _sig, h256 _hash);
Public recover(Signature const& _sig, h256 const& _hash);
/// Returns siganture of message hash.
Signature sign(Secret _k, h256 _hash);
Signature sign(Secret const& _k, h256 const& _hash);
/// Verify signature.
bool verify(Public _k, Signature _s, h256 _hash);
bool verify(Public const& _k, Signature const& _s, h256 const& _hash);
/// Simple class that represents a "key pair".
/// All of the data of the class can be regenerated from the secret key (m_secret) alone.

Loading…
Cancel
Save