Browse Source

coding standards

cl-refactor
subtly 10 years ago
parent
commit
d15c679eeb
  1. 4
      CodingStandards.txt
  2. 8
      libdevcrypto/Common.cpp
  3. 9
      libdevcrypto/Common.h
  4. 22
      libdevcrypto/EC.cpp
  5. 4
      libdevcrypto/EC.h

4
CodingStandards.txt

@ -72,8 +72,8 @@ All other entities' first alpha is lower case.
4. Variable prefixes:
a. Leading underscore "_" to parameter names (both normal and template).
- Exception: "o_parameterName" when it is used exclusively for output. See 7(f).
- Exception: "io_parameterName" when it is used for both input and output. See 7(f).
- Exception: "o_parameterName" when it is used exclusively for output. See 6(f).
- Exception: "io_parameterName" when it is used for both input and output. See 6(f).
b. Leading "c_" to const variables (unless part of an external API).
c. Leading "g_" to global (non-const) variables.
d. Leading "s_" to static (non-const, non-global) variables.

8
libdevcrypto/Common.cpp

@ -109,20 +109,20 @@ KeyPair KeyPair::fromEncryptedSeed(bytesConstRef _seed, std::string const& _pass
return KeyPair(sha3(aesDecrypt(_seed, _password)));
}
void dev::encrypt(Public _k, bytesConstRef _plain, bytes& _cipher)
void dev::encrypt(Public _k, bytesConstRef _plain, bytes& o_cipher)
{
bytes io = _plain.toBytes();
crypto::encrypt(_k, io);
_cipher = std::move(io);
o_cipher = std::move(io);
}
bool dev::decrypt(Secret _k, bytesConstRef _cipher, bytes& _plain)
bool dev::decrypt(Secret _k, bytesConstRef _cipher, bytes& o_plaintext)
{
bytes io = _cipher.toBytes();
crypto::decrypt(_k, io);
if (io.empty())
return false;
_plain = std::move(io);
o_plaintext = std::move(io);
return true;
}

9
libdevcrypto/Common.h

@ -57,9 +57,16 @@ using Secrets = h256s;
/// @returns 0 if it's not a valid secret key.
Address toAddress(Secret _secret);
/// Encrypts plain text using Public key.
void encrypt(Public _k, bytesConstRef _plain, bytes& o_cipher);
bool decrypt(Secret _k, bytesConstRef _cipher, bytes& o_plain);
/// Decrypts cipher using Secret key.
bool decrypt(Secret _k, bytesConstRef _cipher, bytes& o_plaintext);
/// Recovers Public key from signed message.
Public recover(Signature _sig, h256 _message);
/// Returns siganture of message hash.
Signature sign(Secret _k, h256 _message);
/// Simple class that represents a "key pair".

22
libdevcrypto/EC.cpp

@ -42,32 +42,32 @@ using namespace dev;
using namespace dev::crypto;
using namespace CryptoPP;
void dev::crypto::encrypt(Public const& _key, bytes& _plain)
void dev::crypto::encrypt(Public const& _key, bytes& io_cipher)
{
ECIES<ECP>::Encryptor e;
e.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1());
e.AccessKey().SetPublicElement(pp::PointFromPublic(_key));
size_t plen = _plain.size();
size_t plen = io_cipher.size();
bytes c;
c.resize(e.CiphertextLength(plen));
// todo: use StringSource with _plain as input and output.
e.Encrypt(pp::PRNG(), _plain.data(), plen, c.data());
bzero(_plain.data(), _plain.size());
_plain = std::move(c);
e.Encrypt(pp::PRNG(), io_cipher.data(), plen, c.data());
bzero(io_cipher.data(), io_cipher.size());
io_cipher = std::move(c);
}
void dev::crypto::decrypt(Secret const& _k, bytes& _c)
void dev::crypto::decrypt(Secret const& _k, bytes& io_text)
{
CryptoPP::ECIES<CryptoPP::ECP>::Decryptor d;
d.AccessKey().AccessGroupParameters().Initialize(pp::secp256k1());
d.AccessKey().SetPrivateExponent(pp::ExponentFromSecret(_k));
size_t clen = _c.size();
size_t clen = io_text.size();
bytes p;
p.resize(d.MaxPlaintextLength(_c.size()));
p.resize(d.MaxPlaintextLength(io_text.size()));
// todo: use StringSource with _c as input and output.
DecodingResult r = d.Decrypt(pp::PRNG(), _c.data(), clen, p.data());
DecodingResult r = d.Decrypt(pp::PRNG(), io_text.data(), clen, p.data());
assert(r.messageLength);
_c.resize(r.messageLength);
_c = std::move(p);
io_text.resize(r.messageLength);
io_text = std::move(p);
}

4
libdevcrypto/EC.h

@ -31,10 +31,10 @@ namespace crypto
{
/// Encrypts text (in place).
void encrypt(Public const& _k, bytes& _text);
void encrypt(Public const& _k, bytes& io_cipher);
/// Decrypts text (in place).
void decrypt(Secret const& _k, bytes& _text);
void decrypt(Secret const& _k, bytes& io_text);
}
}

Loading…
Cancel
Save