Skip to content
Snippets Groups Projects
Commit cd0007a1 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

eth: cleanup

Change-Id: I0a1f4f60e47e71e7072b97a9acd90bfbea51ff3e
parent 1fc64372
Branches
No related tags found
No related merge requests found
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
################################################################################ ################################################################################
list (APPEND Source_Files__jamidht__eth__libdevcore list (APPEND Source_Files__jamidht__eth__libdevcore
"${CMAKE_CURRENT_SOURCE_DIR}/Address.h" "${CMAKE_CURRENT_SOURCE_DIR}/Address.h"
"${CMAKE_CURRENT_SOURCE_DIR}/Common.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/Common.h" "${CMAKE_CURRENT_SOURCE_DIR}/Common.h"
"${CMAKE_CURRENT_SOURCE_DIR}/CommonData.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/CommonData.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/CommonData.h" "${CMAKE_CURRENT_SOURCE_DIR}/CommonData.h"
......
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file Common.cpp
* @author Gav Wood <i@gavwood.com>
* @date 2014
*/
#include "Common.h"
using namespace std;
using namespace dev;
namespace dev {
bytes const NullBytes;
uint64_t
utcTime()
{
// TODO: Fix if possible to not use time(0) and merge only after testing in all platforms
// time_t t = time(0);
// return mktime(gmtime(&t));
return time(0);
}
} // namespace dev
...@@ -73,62 +73,6 @@ using bytes = std::vector<uint8_t>; ...@@ -73,62 +73,6 @@ using bytes = std::vector<uint8_t>;
using bytesRef = vector_ref<uint8_t>; using bytesRef = vector_ref<uint8_t>;
using bytesConstRef = vector_ref<uint8_t const>; using bytesConstRef = vector_ref<uint8_t const>;
template<class T>
class secure_vector
{
public:
secure_vector() {}
secure_vector(secure_vector<T> const& /*_c*/)
= default; // See https://github.com/ethereum/libweb3core/pull/44
explicit secure_vector(size_t _size)
: m_data(_size)
{}
explicit secure_vector(size_t _size, T _item)
: m_data(_size, _item)
{}
explicit secure_vector(std::vector<T> const& _c)
: m_data(_c)
{}
explicit secure_vector(vector_ref<T> _c)
: m_data(_c.data(), _c.data() + _c.size())
{}
explicit secure_vector(vector_ref<const T> _c)
: m_data(_c.data(), _c.data() + _c.size())
{}
~secure_vector() { ref().cleanse(); }
secure_vector<T>& operator=(secure_vector<T> const& _c)
{
if (&_c == this)
return *this;
ref().cleanse();
m_data = _c.m_data;
return *this;
}
std::vector<T>& writable()
{
clear();
return m_data;
}
std::vector<T> const& makeInsecure() const { return m_data; }
void clear() { ref().cleanse(); }
vector_ref<T> ref() { return vector_ref<T>(&m_data); }
vector_ref<T const> ref() const { return vector_ref<T const>(&m_data); }
size_t size() const { return m_data.size(); }
bool empty() const { return m_data.empty(); }
void swap(secure_vector<T>& io_other) { m_data.swap(io_other.m_data); }
private:
std::vector<T> m_data;
};
using bytesSec = secure_vector<uint8_t>;
// Map types. // Map types.
using StringMap = std::map<std::string, std::string>; using StringMap = std::map<std::string, std::string>;
using BytesMap = std::map<bytes, bytes>; using BytesMap = std::map<bytes, bytes>;
...@@ -136,7 +80,6 @@ using HexMap = std::map<bytes, bytes>; ...@@ -136,7 +80,6 @@ using HexMap = std::map<bytes, bytes>;
// Hash types. // Hash types.
using StringHashMap = std::unordered_map<std::string, std::string>; using StringHashMap = std::unordered_map<std::string, std::string>;
// using u256HashMap = std::unordered_map<u256, u256>;
// String types. // String types.
using strings = std::vector<std::string>; using strings = std::vector<std::string>;
...@@ -144,9 +87,6 @@ using strings = std::vector<std::string>; ...@@ -144,9 +87,6 @@ using strings = std::vector<std::string>;
// Fixed-length string types. // Fixed-length string types.
using string32 = std::array<char, 32>; using string32 = std::array<char, 32>;
// Null/Invalid values for convenience.
extern bytes const NullBytes;
/// @returns the absolute distance between _a and _b. /// @returns the absolute distance between _a and _b.
template<class N> template<class N>
inline N inline N
...@@ -177,7 +117,4 @@ private: ...@@ -177,7 +117,4 @@ private:
enum class WithExisting : int { Trust = 0, Verify, Rescue, Kill }; enum class WithExisting : int { Trust = 0, Verify, Rescue, Kill };
/// Get the current time in seconds since the epoch in UTC
uint64_t utcTime();
} // namespace dev } // namespace dev
...@@ -27,7 +27,6 @@ ...@@ -27,7 +27,6 @@
#include <cstdint> #include <cstdint>
#include <algorithm> #include <algorithm>
#include <random> #include <random>
#include <sstream>
#include <opendht/rng.h> #include <opendht/rng.h>
#include "CommonData.h" #include "CommonData.h"
...@@ -323,10 +322,6 @@ public: ...@@ -323,10 +322,6 @@ public:
ConstructFromHashType _t = FixedHash<T>::FailIfDifferent) ConstructFromHashType _t = FixedHash<T>::FailIfDifferent)
: FixedHash<T>(_b, _t) : FixedHash<T>(_b, _t)
{} {}
explicit SecureFixedHash(bytesSec const& _b,
ConstructFromHashType _t = FixedHash<T>::FailIfDifferent)
: FixedHash<T>(_b.ref(), _t)
{}
template<unsigned M> template<unsigned M>
explicit SecureFixedHash(FixedHash<M> const& _h, explicit SecureFixedHash(FixedHash<M> const& _h,
ConstructFromHashType _t = FixedHash<T>::AlignLeft) ConstructFromHashType _t = FixedHash<T>::AlignLeft)
...@@ -361,8 +356,6 @@ public: ...@@ -361,8 +356,6 @@ public:
using FixedHash<T>::size; using FixedHash<T>::size;
bytesSec asBytesSec() const { return bytesSec(ref()); }
FixedHash<T> const& makeInsecure() const { return static_cast<FixedHash<T> const&>(*this); } FixedHash<T> const& makeInsecure() const { return static_cast<FixedHash<T> const&>(*this); }
FixedHash<T>& writable() FixedHash<T>& writable()
{ {
...@@ -492,25 +485,6 @@ FixedHash<32>::operator==(FixedHash<32> const& _other) const ...@@ -492,25 +485,6 @@ FixedHash<32>::operator==(FixedHash<32> const& _other) const
&& (hash1[3] == hash2[3]); && (hash1[3] == hash2[3]);
} }
/// Stream I/O for the FixedHash class.
template<unsigned N>
inline std::ostream&
operator<<(std::ostream& _out, FixedHash<N> const& _h)
{
_out << toHex(_h);
return _out;
}
/// Stream I/O for the SecureFixedHash class.
template<unsigned N>
inline std::ostream&
operator<<(std::ostream& _out, SecureFixedHash<N> const& _h)
{
_out << "SecureFixedHash#" << std::hex << typename FixedHash<N>::hash()(_h.makeInsecure())
<< std::dec;
return _out;
}
// Common types of FixedHash. // Common types of FixedHash.
using h2048 = FixedHash<256>; using h2048 = FixedHash<256>;
using h1024 = FixedHash<128>; using h1024 = FixedHash<128>;
...@@ -537,15 +511,4 @@ right160(h256 const& _t) ...@@ -537,15 +511,4 @@ right160(h256 const& _t)
return ret; return ret;
} }
inline std::string
toString(h256s const& _bs)
{
std::ostringstream out;
out << "[ ";
for (h256 const& i : _bs)
out << i.abridged() << ", ";
out << "]";
return out.str();
}
} // namespace dev } // namespace dev
...@@ -2,7 +2,6 @@ noinst_LTLIBRARIES += libdevcore.la ...@@ -2,7 +2,6 @@ noinst_LTLIBRARIES += libdevcore.la
libdevcore_la_SOURCES = \ libdevcore_la_SOURCES = \
./jamidht/eth/libdevcore/FixedHash.cpp \ ./jamidht/eth/libdevcore/FixedHash.cpp \
./jamidht/eth/libdevcore/Common.cpp \
./jamidht/eth/libdevcore/SHA3.cpp \ ./jamidht/eth/libdevcore/SHA3.cpp \
./jamidht/eth/libdevcore/CommonData.cpp ./jamidht/eth/libdevcore/CommonData.cpp
......
...@@ -43,13 +43,6 @@ sha3(bytesConstRef _input) ...@@ -43,13 +43,6 @@ sha3(bytesConstRef _input)
sha3(_input, ret.ref()); sha3(_input, ret.ref());
return ret; return ret;
} }
inline SecureFixedHash<32>
sha3Secure(bytesConstRef _input)
{
SecureFixedHash<32> ret;
sha3(_input, ret.writable().ref());
return ret;
}
/// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash. /// Calculate SHA3-256 hash of the given input, returning as a 256-bit hash.
inline h256 inline h256
...@@ -57,11 +50,6 @@ sha3(bytes const& _input) ...@@ -57,11 +50,6 @@ sha3(bytes const& _input)
{ {
return sha3(bytesConstRef(&_input)); return sha3(bytesConstRef(&_input));
} }
inline SecureFixedHash<32>
sha3Secure(bytes const& _input)
{
return sha3Secure(bytesConstRef(&_input));
}
/// Calculate SHA3-256 hash of the given input (presented as a binary-filled string), returning as a /// Calculate SHA3-256 hash of the given input (presented as a binary-filled string), returning as a
/// 256-bit hash. /// 256-bit hash.
...@@ -70,11 +58,6 @@ sha3(std::string const& _input) ...@@ -70,11 +58,6 @@ sha3(std::string const& _input)
{ {
return sha3(bytesConstRef(_input)); return sha3(bytesConstRef(_input));
} }
inline SecureFixedHash<32>
sha3Secure(std::string const& _input)
{
return sha3Secure(bytesConstRef(_input));
}
/// Calculate SHA3-256 hash of the given input (presented as a FixedHash), returns a 256-bit hash. /// Calculate SHA3-256 hash of the given input (presented as a FixedHash), returns a 256-bit hash.
template<unsigned N> template<unsigned N>
...@@ -83,36 +66,6 @@ sha3(FixedHash<N> const& _input) ...@@ -83,36 +66,6 @@ sha3(FixedHash<N> const& _input)
{ {
return sha3(_input.ref()); return sha3(_input.ref());
} }
template<unsigned N>
inline SecureFixedHash<32>
sha3Secure(FixedHash<N> const& _input)
{
return sha3Secure(_input.ref());
}
/// Fully secure variants are equivalent for sha3 and sha3Secure.
inline SecureFixedHash<32>
sha3(bytesSec const& _input)
{
return sha3Secure(_input.ref());
}
inline SecureFixedHash<32>
sha3Secure(bytesSec const& _input)
{
return sha3Secure(_input.ref());
}
template<unsigned N>
inline SecureFixedHash<32>
sha3(SecureFixedHash<N> const& _input)
{
return sha3Secure(_input.ref());
}
template<unsigned N>
inline SecureFixedHash<32>
sha3Secure(SecureFixedHash<N> const& _input)
{
return sha3Secure(_input.ref());
}
/// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the /// Calculate SHA3-256 hash of the given input, possibly interpreting it as nibbles, and return the
/// hash as a string filled with binary data. /// hash as a string filled with binary data.
...@@ -122,13 +75,4 @@ sha3(std::string const& _input, bool _isNibbles) ...@@ -122,13 +75,4 @@ sha3(std::string const& _input, bool _isNibbles)
return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes()); return asString((_isNibbles ? sha3(fromHex(_input)) : sha3(bytesConstRef(&_input))).asBytes());
} }
/// Calculate SHA3-256 MAC
inline void
sha3mac(bytesConstRef _secret, bytesConstRef _plain, bytesRef _output)
{
sha3(_secret.toBytes() + _plain.toBytes()).ref().populate(_output);
}
extern h256 EmptySHA3;
} // namespace dev } // namespace dev
...@@ -58,12 +58,6 @@ public: ...@@ -58,12 +58,6 @@ public:
: m_data(reinterpret_cast<_T*>(_data.data())) : m_data(reinterpret_cast<_T*>(_data.data()))
, m_count(_data.size() / sizeof(_T)) , m_count(_data.size() / sizeof(_T))
{} {}
#if DEV_LDB
vector_ref(ldb::Slice const& _s)
: m_data(reinterpret_cast<_T*>(_s.data()))
, m_count(_s.size() / sizeof(_T))
{}
#endif
explicit operator bool() const { return m_data && m_count; } explicit operator bool() const { return m_data && m_count; }
bool contentsEqual(std::vector<mutable_value_type> const& _c) const bool contentsEqual(std::vector<mutable_value_type> const& _c) const
...@@ -138,30 +132,6 @@ public: ...@@ -138,30 +132,6 @@ public:
m_data = _t.data(); m_data = _t.data();
m_count = _t.size(); m_count = _t.size();
} }
template<class T>
bool overlapsWith(vector_ref<T> _t) const
{
void const* f1 = data();
void const* t1 = data() + size();
void const* f2 = _t.data();
void const* t2 = _t.data() + _t.size();
return f1 < t2 && t1 > f2;
}
/// Copies the contents of this vector_ref to the contents of @a _t, up to the max size of @a _t.
void copyTo(vector_ref<typename std::remove_const<_T>::type> _t) const
{
if (overlapsWith(_t))
memmove(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T));
else
memcpy(_t.data(), m_data, std::min(_t.size(), m_count) * sizeof(_T));
}
/// Copies the contents of this vector_ref to the contents of @a _t, and zeros further trailing
/// elements in @a _t.
void populate(vector_ref<typename std::remove_const<_T>::type> _t) const
{
copyTo(_t);
memset(_t.data() + m_count, 0, std::max(_t.size(), m_count) - m_count);
}
/// Securely overwrite the memory. /// Securely overwrite the memory.
/// @note adapted from OpenSSL's implementation. /// @note adapted from OpenSSL's implementation.
void cleanse() void cleanse()
...@@ -206,10 +176,6 @@ public: ...@@ -206,10 +176,6 @@ public:
} }
bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(_cmp); } bool operator!=(vector_ref<_T> const& _cmp) const { return !operator==(_cmp); }
#if DEV_LDB
operator ldb::Slice() const { return ldb::Slice((char const*) m_data, m_count * sizeof(_T)); }
#endif
void reset() void reset()
{ {
m_data = nullptr; m_data = nullptr;
......
...@@ -104,30 +104,3 @@ KeyPair::create() ...@@ -104,30 +104,3 @@ KeyPair::create()
return keyPair; return keyPair;
} }
} }
h256
crypto::kdf(Secret const& _priv, h256 const& _hash)
{
// H(H(r||k)^h)
h256 s;
sha3mac(Secret::random().ref(), _priv.ref(), s.ref());
s ^= _hash;
sha3(s.ref(), s.ref());
if (!s || !_hash || !_priv)
throw InvalidState();
return s;
}
Secret
Nonce::next()
{
std::lock_guard<std::mutex> l(x_value);
if (!m_value) {
m_value = Secret::random();
if (!m_value)
throw InvalidState();
}
m_value = sha3Secure(m_value.ref());
return sha3(~m_value);
}
...@@ -123,38 +123,6 @@ public: ...@@ -123,38 +123,6 @@ public:
: runtime_error(err) {}; : runtime_error(err) {};
}; };
/// Key derivation
h256 kdf(Secret const& _priv, h256 const& _hash);
/**
* @brief Generator for non-repeating nonce material.
* The Nonce class should only be used when a non-repeating nonce
* is required and, in its current form, not recommended for signatures.
* This is primarily because the key-material for signatures is
* encrypted on disk whereas the seed for Nonce is not.
* Thus, Nonce's primary intended use at this time is for networking
* where the key is also stored in plaintext.
*/
class Nonce
{
public:
/// Returns the next nonce (might be read from a file).
static Secret get()
{
static Nonce s;
return s.next();
}
private:
Nonce() = default;
/// @returns the next nonce.
Secret next();
std::mutex x_value;
Secret m_value;
};
} // namespace crypto } // namespace crypto
} // namespace dev } // namespace dev
...@@ -34,7 +34,6 @@ libjami_sources = files( ...@@ -34,7 +34,6 @@ libjami_sources = files(
'connectivity/utf8_utils.cpp', 'connectivity/utf8_utils.cpp',
'im/instant_messaging.cpp', 'im/instant_messaging.cpp',
'im/message_engine.cpp', 'im/message_engine.cpp',
'jamidht/eth/libdevcore/Common.cpp',
'jamidht/eth/libdevcore/CommonData.cpp', 'jamidht/eth/libdevcore/CommonData.cpp',
'jamidht/eth/libdevcore/FixedHash.cpp', 'jamidht/eth/libdevcore/FixedHash.cpp',
'jamidht/eth/libdevcore/SHA3.cpp', 'jamidht/eth/libdevcore/SHA3.cpp',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment