diff --git a/node/CMakeLists.txt b/node/CMakeLists.txt index 0eb8a62a5..466247280 100644 --- a/node/CMakeLists.txt +++ b/node/CMakeLists.txt @@ -16,7 +16,7 @@ set(core_headers ECC384.hpp Expect.hpp FCV.hpp - Hash.hpp + Fingerprint.hpp Hashtable.hpp Identity.hpp InetAddress.hpp diff --git a/node/Hash.hpp b/node/Fingerprint.hpp similarity index 52% rename from node/Hash.hpp rename to node/Fingerprint.hpp index 045f490a2..129002b5c 100644 --- a/node/Hash.hpp +++ b/node/Fingerprint.hpp @@ -11,8 +11,8 @@ */ /****/ -#ifndef ZT_HASH_HPP -#define ZT_HASH_HPP +#ifndef ZT_FINGERPRINT_HPP +#define ZT_FINGERPRINT_HPP #include "Constants.hpp" #include "TriviallyCopyable.hpp" @@ -20,7 +20,7 @@ namespace ZeroTier { /** - * Container for cryptographic hashes + * Container for 384-bit identity hashes * * The size of the hash used with this container must be a multiple of 64 bits. * Currently it's used as H<384> and H<512>. @@ -29,25 +29,17 @@ namespace ZeroTier { * * @tparam BITS Bits in hash, must be a multiple of 64 */ -template -class Hash : public TriviallyCopyable +class Fingerprint : public TriviallyCopyable { public: - ZT_ALWAYS_INLINE Hash() noexcept {} + ZT_ALWAYS_INLINE Fingerprint() noexcept {} + explicit ZT_ALWAYS_INLINE Fingerprint(const void *h384) noexcept { memcpy(_h,h384,48); } - /** - * @param h Hash value of size BITS / 8 - */ - explicit ZT_ALWAYS_INLINE Hash(const void *h) noexcept { memcpy(_h,h,BITS / 8); } - - /** - * @param h Hash value of size BITS / 8 - */ - ZT_ALWAYS_INLINE void set(const void *h) noexcept { memcpy(_h,h,BITS / 8); } + ZT_ALWAYS_INLINE void set(const void *h384) noexcept { memcpy(_h,h384,48); } ZT_ALWAYS_INLINE void zero() noexcept { - for(int i=0;i<(BITS / (sizeof(unsigned long) * 8));++i) + for(int i=0;i<(384 / (sizeof(unsigned long) * 8));++i) _h[i] = 0; } @@ -57,28 +49,28 @@ public: ZT_ALWAYS_INLINE uint8_t operator[](const unsigned int i) const noexcept { return reinterpret_cast(_h)[i]; } ZT_ALWAYS_INLINE uint8_t &operator[](const unsigned int i) noexcept { return reinterpret_cast(_h)[i]; } - static constexpr unsigned int size() noexcept { return BITS / 8; } + static constexpr unsigned int size() noexcept { return 48; } ZT_ALWAYS_INLINE unsigned long hashCode() const noexcept { return _h[0]; } ZT_ALWAYS_INLINE operator bool() const noexcept { - for(int i=0;i<(BITS / (sizeof(unsigned long) * 8));++i) { + for(int i=0;i<(384 / (sizeof(unsigned long) * 8));++i) { if (_h[i] != 0) return true; } return false; } - ZT_ALWAYS_INLINE bool operator==(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) == 0; } - ZT_ALWAYS_INLINE bool operator!=(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) != 0; } - ZT_ALWAYS_INLINE bool operator<(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) < 0; } - ZT_ALWAYS_INLINE bool operator>(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) > 0; } - ZT_ALWAYS_INLINE bool operator<=(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) <= 0; } - ZT_ALWAYS_INLINE bool operator>=(const Hash &h) const noexcept { return memcmp(_h,h._h,BITS / 8) >= 0; } + ZT_ALWAYS_INLINE bool operator==(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) == 0; } + ZT_ALWAYS_INLINE bool operator!=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) != 0; } + ZT_ALWAYS_INLINE bool operator<(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) < 0; } + ZT_ALWAYS_INLINE bool operator>(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) > 0; } + ZT_ALWAYS_INLINE bool operator<=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) <= 0; } + ZT_ALWAYS_INLINE bool operator>=(const Fingerprint &h) const noexcept { return memcmp(_h,h._h,48) >= 0; } private: - unsigned long _h[BITS / (sizeof(unsigned long) * 8)]; + unsigned long _h[384 / (sizeof(unsigned long) * 8)]; }; } // namespace ZeroTier diff --git a/node/Identity.hpp b/node/Identity.hpp index a9cff39cb..d9ae7552f 100644 --- a/node/Identity.hpp +++ b/node/Identity.hpp @@ -21,7 +21,7 @@ #include "SHA512.hpp" #include "ECC384.hpp" #include "TriviallyCopyable.hpp" -#include "Hash.hpp" +#include "Fingerprint.hpp" #include #include @@ -55,11 +55,6 @@ public: P384 = ZT_CRYPTO_ALG_P384 // Type 1 -- NIST P-384 with linked Curve25519/Ed25519 secondaries (2.x+) }; - /** - * 384-bit full hash of identity's public key(s) - */ - typedef Hash<384> Fingerprint; - /** * A nil/empty identity instance */ diff --git a/node/Tests.cpp b/node/Tests.cpp index e7eded25b..664ad0601 100644 --- a/node/Tests.cpp +++ b/node/Tests.cpp @@ -40,6 +40,7 @@ #include "SHA512.hpp" #include "Defragmenter.hpp" #include "MIMC52.hpp" +#include "Fingerprint.hpp" #include #include @@ -284,8 +285,7 @@ extern "C" const char *ZTT_general() ZT_T_ASSERT(sizeof(sockaddr_in) <= sizeof(InetAddress)); ZT_T_ASSERT(sizeof(sockaddr_in6) <= sizeof(InetAddress)); ZT_T_ASSERT(sizeof(sockaddr) <= sizeof(InetAddress)); - ZT_T_ASSERT(sizeof(Hash<384>) == 48); - ZT_T_ASSERT(sizeof(Hash<512>) == 64); + ZT_T_ASSERT(sizeof(Fingerprint) == 48); ZT_T_PRINTF("OK" ZT_EOL_S); } diff --git a/node/Topology.hpp b/node/Topology.hpp index a61c9a3d8..b2f21f02d 100644 --- a/node/Topology.hpp +++ b/node/Topology.hpp @@ -30,7 +30,7 @@ #include "Hashtable.hpp" #include "SharedPtr.hpp" #include "ScopedPtr.hpp" -#include "Hash.hpp" +#include "Fingerprint.hpp" namespace ZeroTier { @@ -94,7 +94,7 @@ public: * @param hash Identity hash * @return Peer or NULL if no peer is currently in memory for this hash (cache is not checked in this case) */ - ZT_ALWAYS_INLINE SharedPtr peerByHash(const Hash<384> &hash) + ZT_ALWAYS_INLINE SharedPtr peerByHash(const Fingerprint &hash) { RWMutex::RLock _l(_peers_l); const SharedPtr *const ap = _peersByIdentityHash.get(hash); @@ -364,7 +364,7 @@ private: Hashtable< Address,SharedPtr > _peers; Hashtable< uint64_t,SharedPtr > _peersByIncomingProbe; - Hashtable< Hash<384>,SharedPtr > _peersByIdentityHash; + Hashtable< Fingerprint,SharedPtr > _peersByIdentityHash; Hashtable< uint64_t,SharedPtr > _paths; std::set< Identity > _roots; // locked by _peers_l std::vector< SharedPtr > _rootPeers; // locked by _peers_l