Make Fingerprint (a full length identity hash) a first class type

This commit is contained in:
Adam Ierymenko 2020-02-26 11:39:18 -08:00
parent ea0961dfd2
commit 5cec5fe6b1
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
5 changed files with 24 additions and 37 deletions

View file

@ -16,7 +16,7 @@ set(core_headers
ECC384.hpp
Expect.hpp
FCV.hpp
Hash.hpp
Fingerprint.hpp
Hashtable.hpp
Identity.hpp
InetAddress.hpp

View file

@ -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<unsigned int BITS>
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<const uint8_t *>(_h)[i]; }
ZT_ALWAYS_INLINE uint8_t &operator[](const unsigned int i) noexcept { return reinterpret_cast<uint8_t *>(_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

View file

@ -21,7 +21,7 @@
#include "SHA512.hpp"
#include "ECC384.hpp"
#include "TriviallyCopyable.hpp"
#include "Hash.hpp"
#include "Fingerprint.hpp"
#include <cstdio>
#include <cstdlib>
@ -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
*/

View file

@ -40,6 +40,7 @@
#include "SHA512.hpp"
#include "Defragmenter.hpp"
#include "MIMC52.hpp"
#include "Fingerprint.hpp"
#include <cstdint>
#include <cstring>
@ -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);
}

View file

@ -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<Peer> peerByHash(const Hash<384> &hash)
ZT_ALWAYS_INLINE SharedPtr<Peer> peerByHash(const Fingerprint &hash)
{
RWMutex::RLock _l(_peers_l);
const SharedPtr<Peer> *const ap = _peersByIdentityHash.get(hash);
@ -364,7 +364,7 @@ private:
Hashtable< Address,SharedPtr<Peer> > _peers;
Hashtable< uint64_t,SharedPtr<Peer> > _peersByIncomingProbe;
Hashtable< Hash<384>,SharedPtr<Peer> > _peersByIdentityHash;
Hashtable< Fingerprint,SharedPtr<Peer> > _peersByIdentityHash;
Hashtable< uint64_t,SharedPtr<Path> > _paths;
std::set< Identity > _roots; // locked by _peers_l
std::vector< SharedPtr<Peer> > _rootPeers; // locked by _peers_l