From cd0007a10e316331c3ce9be71331d0f2920a8cff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com> Date: Mon, 9 Jan 2023 16:47:20 -0500 Subject: [PATCH] eth: cleanup Change-Id: I0a1f4f60e47e71e7072b97a9acd90bfbea51ff3e --- src/jamidht/eth/libdevcore/CMakeLists.txt | 1 - src/jamidht/eth/libdevcore/Common.cpp | 39 -------------- src/jamidht/eth/libdevcore/Common.h | 63 ----------------------- src/jamidht/eth/libdevcore/FixedHash.h | 37 ------------- src/jamidht/eth/libdevcore/Makefile.am | 1 - src/jamidht/eth/libdevcore/SHA3.h | 56 -------------------- src/jamidht/eth/libdevcore/vector_ref.h | 34 ------------ src/jamidht/eth/libdevcrypto/Common.cpp | 27 ---------- src/jamidht/eth/libdevcrypto/Common.h | 32 ------------ src/meson.build | 1 - 10 files changed, 291 deletions(-) delete mode 100644 src/jamidht/eth/libdevcore/Common.cpp diff --git a/src/jamidht/eth/libdevcore/CMakeLists.txt b/src/jamidht/eth/libdevcore/CMakeLists.txt index 414f82c8af..70924025f0 100644 --- a/src/jamidht/eth/libdevcore/CMakeLists.txt +++ b/src/jamidht/eth/libdevcore/CMakeLists.txt @@ -3,7 +3,6 @@ ################################################################################ list (APPEND Source_Files__jamidht__eth__libdevcore "${CMAKE_CURRENT_SOURCE_DIR}/Address.h" - "${CMAKE_CURRENT_SOURCE_DIR}/Common.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/Common.h" "${CMAKE_CURRENT_SOURCE_DIR}/CommonData.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/CommonData.h" diff --git a/src/jamidht/eth/libdevcore/Common.cpp b/src/jamidht/eth/libdevcore/Common.cpp deleted file mode 100644 index e6f91a1c47..0000000000 --- a/src/jamidht/eth/libdevcore/Common.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - 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 diff --git a/src/jamidht/eth/libdevcore/Common.h b/src/jamidht/eth/libdevcore/Common.h index bcfb928acc..7b4445bedc 100644 --- a/src/jamidht/eth/libdevcore/Common.h +++ b/src/jamidht/eth/libdevcore/Common.h @@ -73,62 +73,6 @@ using bytes = std::vector<uint8_t>; using bytesRef = vector_ref<uint8_t>; 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. using StringMap = std::map<std::string, std::string>; using BytesMap = std::map<bytes, bytes>; @@ -136,7 +80,6 @@ using HexMap = std::map<bytes, bytes>; // Hash types. using StringHashMap = std::unordered_map<std::string, std::string>; -// using u256HashMap = std::unordered_map<u256, u256>; // String types. using strings = std::vector<std::string>; @@ -144,9 +87,6 @@ using strings = std::vector<std::string>; // Fixed-length string types. using string32 = std::array<char, 32>; -// Null/Invalid values for convenience. -extern bytes const NullBytes; - /// @returns the absolute distance between _a and _b. template<class N> inline N @@ -177,7 +117,4 @@ private: 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 diff --git a/src/jamidht/eth/libdevcore/FixedHash.h b/src/jamidht/eth/libdevcore/FixedHash.h index 29b2602a2f..1f8b2060b2 100644 --- a/src/jamidht/eth/libdevcore/FixedHash.h +++ b/src/jamidht/eth/libdevcore/FixedHash.h @@ -27,7 +27,6 @@ #include <cstdint> #include <algorithm> #include <random> -#include <sstream> #include <opendht/rng.h> #include "CommonData.h" @@ -323,10 +322,6 @@ public: ConstructFromHashType _t = FixedHash<T>::FailIfDifferent) : FixedHash<T>(_b, _t) {} - explicit SecureFixedHash(bytesSec const& _b, - ConstructFromHashType _t = FixedHash<T>::FailIfDifferent) - : FixedHash<T>(_b.ref(), _t) - {} template<unsigned M> explicit SecureFixedHash(FixedHash<M> const& _h, ConstructFromHashType _t = FixedHash<T>::AlignLeft) @@ -361,8 +356,6 @@ public: using FixedHash<T>::size; - bytesSec asBytesSec() const { return bytesSec(ref()); } - FixedHash<T> const& makeInsecure() const { return static_cast<FixedHash<T> const&>(*this); } FixedHash<T>& writable() { @@ -492,25 +485,6 @@ FixedHash<32>::operator==(FixedHash<32> const& _other) const && (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. using h2048 = FixedHash<256>; using h1024 = FixedHash<128>; @@ -537,15 +511,4 @@ right160(h256 const& _t) 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 diff --git a/src/jamidht/eth/libdevcore/Makefile.am b/src/jamidht/eth/libdevcore/Makefile.am index eaf34f74e4..c73f77d002 100644 --- a/src/jamidht/eth/libdevcore/Makefile.am +++ b/src/jamidht/eth/libdevcore/Makefile.am @@ -2,7 +2,6 @@ noinst_LTLIBRARIES += libdevcore.la libdevcore_la_SOURCES = \ ./jamidht/eth/libdevcore/FixedHash.cpp \ - ./jamidht/eth/libdevcore/Common.cpp \ ./jamidht/eth/libdevcore/SHA3.cpp \ ./jamidht/eth/libdevcore/CommonData.cpp diff --git a/src/jamidht/eth/libdevcore/SHA3.h b/src/jamidht/eth/libdevcore/SHA3.h index 25f097195b..8d2a28b809 100644 --- a/src/jamidht/eth/libdevcore/SHA3.h +++ b/src/jamidht/eth/libdevcore/SHA3.h @@ -43,13 +43,6 @@ sha3(bytesConstRef _input) sha3(_input, ret.ref()); 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. inline h256 @@ -57,11 +50,6 @@ sha3(bytes const& _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 /// 256-bit hash. @@ -70,11 +58,6 @@ sha3(std::string const& _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. template<unsigned N> @@ -83,36 +66,6 @@ sha3(FixedHash<N> const& _input) { 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 /// hash as a string filled with binary data. @@ -122,13 +75,4 @@ sha3(std::string const& _input, bool _isNibbles) 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 diff --git a/src/jamidht/eth/libdevcore/vector_ref.h b/src/jamidht/eth/libdevcore/vector_ref.h index cd5f885e01..09fac93e0a 100644 --- a/src/jamidht/eth/libdevcore/vector_ref.h +++ b/src/jamidht/eth/libdevcore/vector_ref.h @@ -58,12 +58,6 @@ public: : m_data(reinterpret_cast<_T*>(_data.data())) , 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; } bool contentsEqual(std::vector<mutable_value_type> const& _c) const @@ -138,30 +132,6 @@ public: m_data = _t.data(); 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. /// @note adapted from OpenSSL's implementation. void cleanse() @@ -206,10 +176,6 @@ public: } 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() { m_data = nullptr; diff --git a/src/jamidht/eth/libdevcrypto/Common.cpp b/src/jamidht/eth/libdevcrypto/Common.cpp index a1999af20a..2e80c2efde 100644 --- a/src/jamidht/eth/libdevcrypto/Common.cpp +++ b/src/jamidht/eth/libdevcrypto/Common.cpp @@ -104,30 +104,3 @@ KeyPair::create() 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); -} diff --git a/src/jamidht/eth/libdevcrypto/Common.h b/src/jamidht/eth/libdevcrypto/Common.h index e255bb7972..bb67143f34 100644 --- a/src/jamidht/eth/libdevcrypto/Common.h +++ b/src/jamidht/eth/libdevcrypto/Common.h @@ -123,38 +123,6 @@ public: : 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 dev diff --git a/src/meson.build b/src/meson.build index 9dd33839c7..b8030d18a0 100644 --- a/src/meson.build +++ b/src/meson.build @@ -34,7 +34,6 @@ libjami_sources = files( 'connectivity/utf8_utils.cpp', 'im/instant_messaging.cpp', 'im/message_engine.cpp', - 'jamidht/eth/libdevcore/Common.cpp', 'jamidht/eth/libdevcore/CommonData.cpp', 'jamidht/eth/libdevcore/FixedHash.cpp', 'jamidht/eth/libdevcore/SHA3.cpp', -- GitLab