|
|
@@ -16,7 +16,6 @@
|
|
|
#include "SHA512.hpp"
|
|
|
#include "Salsa20.hpp"
|
|
|
#include "Utils.hpp"
|
|
|
-#include "AES.hpp"
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
@@ -26,102 +25,102 @@ namespace {
|
|
|
|
|
|
// This is the memory-intensive hash function used to compute v0 identities from v0 public keys.
|
|
|
#define ZT_V0_IDENTITY_GEN_MEMORY 2097152
|
|
|
-void identityV0ProofOfWorkFrankenhash(const void *const publicKey,unsigned int publicKeyBytes,void *const digest,void *const genmem) noexcept
|
|
|
+
|
|
|
+void identityV0ProofOfWorkFrankenhash(const void *const publicKey, unsigned int publicKeyBytes, void *const digest, void *const genmem) noexcept
|
|
|
{
|
|
|
// Digest publicKey[] to obtain initial digest
|
|
|
- SHA512(digest,publicKey,publicKeyBytes);
|
|
|
+ SHA512(digest, publicKey, publicKeyBytes);
|
|
|
|
|
|
// Initialize genmem[] using Salsa20 in a CBC-like configuration since
|
|
|
// ordinary Salsa20 is randomly seek-able. This is good for a cipher
|
|
|
// but is not what we want for sequential memory-hardness.
|
|
|
Utils::zero<ZT_V0_IDENTITY_GEN_MEMORY>(genmem);
|
|
|
- Salsa20 s20(digest,(char *)digest + 32);
|
|
|
- s20.crypt20((char *)genmem,(char *)genmem,64);
|
|
|
- for(unsigned long i=64;i<ZT_V0_IDENTITY_GEN_MEMORY;i+=64) {
|
|
|
+ Salsa20 s20(digest, (char *) digest + 32);
|
|
|
+ s20.crypt20((char *) genmem, (char *) genmem, 64);
|
|
|
+ for (unsigned long i = 64;i < ZT_V0_IDENTITY_GEN_MEMORY;i += 64) {
|
|
|
unsigned long k = i - 64;
|
|
|
- *((uint64_t *)((char *)genmem + i)) = *((uint64_t *)((char *)genmem + k));
|
|
|
- *((uint64_t *)((char *)genmem + i + 8)) = *((uint64_t *)((char *)genmem + k + 8));
|
|
|
- *((uint64_t *)((char *)genmem + i + 16)) = *((uint64_t *)((char *)genmem + k + 16));
|
|
|
- *((uint64_t *)((char *)genmem + i + 24)) = *((uint64_t *)((char *)genmem + k + 24));
|
|
|
- *((uint64_t *)((char *)genmem + i + 32)) = *((uint64_t *)((char *)genmem + k + 32));
|
|
|
- *((uint64_t *)((char *)genmem + i + 40)) = *((uint64_t *)((char *)genmem + k + 40));
|
|
|
- *((uint64_t *)((char *)genmem + i + 48)) = *((uint64_t *)((char *)genmem + k + 48));
|
|
|
- *((uint64_t *)((char *)genmem + i + 56)) = *((uint64_t *)((char *)genmem + k + 56));
|
|
|
- s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
|
|
|
+ *((uint64_t *) ((char *) genmem + i)) = *((uint64_t *) ((char *) genmem + k));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 8)) = *((uint64_t *) ((char *) genmem + k + 8));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 16)) = *((uint64_t *) ((char *) genmem + k + 16));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 24)) = *((uint64_t *) ((char *) genmem + k + 24));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 32)) = *((uint64_t *) ((char *) genmem + k + 32));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 40)) = *((uint64_t *) ((char *) genmem + k + 40));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 48)) = *((uint64_t *) ((char *) genmem + k + 48));
|
|
|
+ *((uint64_t *) ((char *) genmem + i + 56)) = *((uint64_t *) ((char *) genmem + k + 56));
|
|
|
+ s20.crypt20((char *) genmem + i, (char *) genmem + i, 64);
|
|
|
}
|
|
|
|
|
|
// Render final digest using genmem as a lookup table
|
|
|
- for(unsigned long i=0;i<(ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
|
|
|
- unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
|
|
|
- unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
|
|
|
- uint64_t tmp = ((uint64_t *)genmem)[idx2];
|
|
|
- ((uint64_t *)genmem)[idx2] = ((uint64_t *)digest)[idx1];
|
|
|
- ((uint64_t *)digest)[idx1] = tmp;
|
|
|
- s20.crypt20(digest,digest,64);
|
|
|
+ for (unsigned long i = 0;i < (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
|
|
|
+ unsigned long idx1 = (unsigned long) (Utils::ntoh(((uint64_t *) genmem)[i++]) % (64 / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
|
|
|
+ unsigned long idx2 = (unsigned long) (Utils::ntoh(((uint64_t *) genmem)[i++]) % (ZT_V0_IDENTITY_GEN_MEMORY / sizeof(uint64_t))); // NOLINT(hicpp-use-auto,modernize-use-auto)
|
|
|
+ uint64_t tmp = ((uint64_t *) genmem)[idx2];
|
|
|
+ ((uint64_t *) genmem)[idx2] = ((uint64_t *) digest)[idx1];
|
|
|
+ ((uint64_t *) digest)[idx1] = tmp;
|
|
|
+ s20.crypt20(digest, digest, 64);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
struct identityV0ProofOfWorkCriteria
|
|
|
{
|
|
|
- ZT_INLINE identityV0ProofOfWorkCriteria(unsigned char *sb,char *gm) noexcept : digest(sb),genmem(gm) {}
|
|
|
+ ZT_INLINE identityV0ProofOfWorkCriteria(unsigned char *sb, char *gm) noexcept: digest(sb), genmem(gm)
|
|
|
+ {}
|
|
|
+
|
|
|
ZT_INLINE bool operator()(const uint8_t pub[ZT_C25519_COMBINED_PUBLIC_KEY_SIZE]) const noexcept
|
|
|
{
|
|
|
- identityV0ProofOfWorkFrankenhash(pub,ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,digest,genmem);
|
|
|
+ identityV0ProofOfWorkFrankenhash(pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
|
|
|
return (digest[0] < 17);
|
|
|
}
|
|
|
+
|
|
|
unsigned char *digest;
|
|
|
char *genmem;
|
|
|
};
|
|
|
|
|
|
+#define ZT_IDENTITY_V1_POW_MEMORY_SIZE 131072
|
|
|
+
|
|
|
// This is a simpler memory-intensive hash function for V1 identity generation.
|
|
|
// It's not quite as heavy as the V0 frankenhash, is a little more orderly in
|
|
|
// its design, but remains relatively resistant to GPU acceleration due to memory
|
|
|
// requirements for efficient computation.
|
|
|
-#define ZT_IDENTITY_V1_POW_MEMORY_SIZE 262144
|
|
|
-#define ZT_IDENTITY_V1_POW_MEMORY_SIZE_U64 32768
|
|
|
-bool identityV1ProofOfWorkCriteria(const void *in,const unsigned int len)
|
|
|
+bool identityV1ProofOfWorkCriteria(const void *in, const unsigned int len)
|
|
|
{
|
|
|
- uint64_t b[ZT_IDENTITY_V1_POW_MEMORY_SIZE_U64];
|
|
|
-
|
|
|
- SHA512(b,in,len);
|
|
|
- AES c(b);
|
|
|
- for(unsigned int i=8;i<ZT_IDENTITY_V1_POW_MEMORY_SIZE_U64;i+=8) {
|
|
|
- SHA512(b + i,b + (i - 8),64);
|
|
|
- if (unlikely((b[i] % 31ULL) == (b[i - 1] >> 59U)))
|
|
|
- c.encrypt(b + i,b + i);
|
|
|
- }
|
|
|
+ uint64_t b[ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8];
|
|
|
+
|
|
|
+ SHA384(b, in, len);
|
|
|
+ Utils::zero<ZT_IDENTITY_V1_POW_MEMORY_SIZE - 48>(b + 6);
|
|
|
+ Salsa20(b,b + 4).crypt12(b,b,ZT_IDENTITY_V1_POW_MEMORY_SIZE);
|
|
|
|
|
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
- for(unsigned int i=0;i<ZT_IDENTITY_V1_POW_MEMORY_SIZE_U64;i+=4) {
|
|
|
+ for (unsigned int i=0;i<(ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8);) {
|
|
|
+ const unsigned int i1 = i + 1;
|
|
|
+ const unsigned int i2 = i + 2;
|
|
|
+ const unsigned int i3 = i + 3;
|
|
|
b[i] = Utils::swapBytes(b[i]);
|
|
|
- b[i + 1] = Utils::swapBytes(b[i + 1]);
|
|
|
- b[i + 2] = Utils::swapBytes(b[i + 2]);
|
|
|
- b[i + 3] = Utils::swapBytes(b[i + 3]);
|
|
|
+ i += 4;
|
|
|
+ b[i1] = Utils::swapBytes(b[i1]);
|
|
|
+ b[i2] = Utils::swapBytes(b[i2]);
|
|
|
+ b[i3] = Utils::swapBytes(b[i3]);
|
|
|
}
|
|
|
#endif
|
|
|
- std::sort(b,b + ZT_IDENTITY_V1_POW_MEMORY_SIZE_U64);
|
|
|
+
|
|
|
+ std::sort(b,b + (ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8));
|
|
|
+
|
|
|
#if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
- for(unsigned int i=0;i<ZT_IDENTITY_V1_POW_MEMORY_SIZE_U64;i+=4) {
|
|
|
+ for (unsigned int i=0;i<(ZT_IDENTITY_V1_POW_MEMORY_SIZE / 8);) {
|
|
|
+ const unsigned int i1 = i + 1;
|
|
|
+ const unsigned int i2 = i + 2;
|
|
|
+ const unsigned int i3 = i + 3;
|
|
|
b[i] = Utils::swapBytes(b[i]);
|
|
|
- b[i + 1] = Utils::swapBytes(b[i + 1]);
|
|
|
- b[i + 2] = Utils::swapBytes(b[i + 2]);
|
|
|
- b[i + 3] = Utils::swapBytes(b[i + 3]);
|
|
|
+ i += 4;
|
|
|
+ b[i1] = Utils::swapBytes(b[i1]);
|
|
|
+ b[i2] = Utils::swapBytes(b[i2]);
|
|
|
+ b[i3] = Utils::swapBytes(b[i3]);
|
|
|
}
|
|
|
#endif
|
|
|
|
|
|
- // Hash resulting sorted array to get final result for PoW criteria test.
|
|
|
- // We also include the original input after so that cryptographically this
|
|
|
- // is exactly like SHA384(in). This should make any FIPS types happy as
|
|
|
- // this means the identity hash is SHA384 and not some weird construction.
|
|
|
- SHA384(b,b,ZT_IDENTITY_V1_POW_MEMORY_SIZE,in,len);
|
|
|
+ SHA384(b, b, ZT_IDENTITY_V1_POW_MEMORY_SIZE, in, len);
|
|
|
|
|
|
- // PoW passes if sum of first two 64-bit integers (treated as little-endian) mod 180 is 0.
|
|
|
- // This value was picked to yield about 1-2s total on typical desktop and server cores in 2020.
|
|
|
-#if __BYTE_ORDER == __BIG_ENDIAN
|
|
|
- const uint64_t finalHash = Utils::swapBytes(b[0]) + Utils::swapBytes(b[1]);
|
|
|
-#else
|
|
|
- const uint64_t finalHash = b[0] + b[1];
|
|
|
-#endif
|
|
|
- return (finalHash % 180U) == 0;
|
|
|
+ return (b[0] % 1093U) == 0;
|
|
|
}
|
|
|
|
|
|
} // anonymous namespace
|
|
|
@@ -133,7 +132,7 @@ bool Identity::generate(const Type t)
|
|
|
m_type = t;
|
|
|
m_hasPrivate = true;
|
|
|
|
|
|
- switch(t) {
|
|
|
+ switch (t) {
|
|
|
|
|
|
case C25519: {
|
|
|
// Generate C25519/Ed25519 key pair whose hash satisfies a "hashcash" criterion and generate the
|
|
|
@@ -142,7 +141,7 @@ bool Identity::generate(const Type t)
|
|
|
char *const genmem = new char[ZT_V0_IDENTITY_GEN_MEMORY];
|
|
|
Address address;
|
|
|
do {
|
|
|
- C25519::generateSatisfying(identityV0ProofOfWorkCriteria(digest,genmem),m_pub,m_priv);
|
|
|
+ C25519::generateSatisfying(identityV0ProofOfWorkCriteria(digest, genmem), m_pub, m_priv);
|
|
|
address.setTo(digest + 59);
|
|
|
} while (address.isReserved());
|
|
|
delete[] genmem;
|
|
|
@@ -151,18 +150,18 @@ bool Identity::generate(const Type t)
|
|
|
} break;
|
|
|
|
|
|
case P384: {
|
|
|
- for(;;) {
|
|
|
+ for (;;) {
|
|
|
// Loop until we pass the PoW criteria. The nonce is only 8 bits, so generate
|
|
|
// some new key material every time it wraps. The ECC384 generator is slightly
|
|
|
// faster so use that one.
|
|
|
m_pub[0] = 0; // zero nonce
|
|
|
- C25519::generateCombined(m_pub + 1,m_priv + 1);
|
|
|
- ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
|
|
|
- for(;;) {
|
|
|
- if (identityV1ProofOfWorkCriteria(m_pub,sizeof(m_pub)))
|
|
|
+ C25519::generateCombined(m_pub + 1, m_priv + 1);
|
|
|
+ ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
|
|
|
+ for (;;) {
|
|
|
+ if (identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub)))
|
|
|
break;
|
|
|
if (++m_pub[0] == 0)
|
|
|
- ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
|
|
|
+ ECC384GenerateKey(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
|
|
|
}
|
|
|
|
|
|
// If we passed PoW then check that the address is valid, otherwise loop
|
|
|
@@ -183,25 +182,25 @@ bool Identity::generate(const Type t)
|
|
|
bool Identity::locallyValidate() const noexcept
|
|
|
{
|
|
|
try {
|
|
|
- if ((m_fp)&&((!m_fp.address().isReserved()))) {
|
|
|
+ if ((m_fp) && ((!m_fp.address().isReserved()))) {
|
|
|
switch (m_type) {
|
|
|
case C25519: {
|
|
|
uint8_t digest[64];
|
|
|
- char *const genmem = (char *)malloc(ZT_V0_IDENTITY_GEN_MEMORY);
|
|
|
+ char *const genmem = (char *) malloc(ZT_V0_IDENTITY_GEN_MEMORY);
|
|
|
if (!genmem)
|
|
|
return false;
|
|
|
- identityV0ProofOfWorkFrankenhash(m_pub,ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,digest,genmem);
|
|
|
+ identityV0ProofOfWorkFrankenhash(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, digest, genmem);
|
|
|
free(genmem);
|
|
|
return ((m_fp.address() == Address(digest + 59)) && (digest[0] < 17));
|
|
|
}
|
|
|
case P384: {
|
|
|
if (m_fp.address() != Address(m_fp.hash()))
|
|
|
return false;
|
|
|
- return identityV1ProofOfWorkCriteria(m_pub,sizeof(m_pub));
|
|
|
+ return identityV1ProofOfWorkCriteria(m_pub, sizeof(m_pub));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- } catch ( ... ) {}
|
|
|
+ } catch (...) {}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -210,10 +209,10 @@ void Identity::hashWithPrivate(uint8_t h[ZT_FINGERPRINT_HASH_SIZE]) const
|
|
|
if (m_hasPrivate) {
|
|
|
switch (m_type) {
|
|
|
case C25519:
|
|
|
- SHA384(h,m_pub,ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,m_priv,ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
|
|
|
+ SHA384(h, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE);
|
|
|
break;
|
|
|
case P384:
|
|
|
- SHA384(h,m_pub,sizeof(m_pub),m_priv,sizeof(m_priv));
|
|
|
+ SHA384(h, m_pub, sizeof(m_pub), m_priv, sizeof(m_priv));
|
|
|
break;
|
|
|
}
|
|
|
return;
|
|
|
@@ -221,21 +220,21 @@ void Identity::hashWithPrivate(uint8_t h[ZT_FINGERPRINT_HASH_SIZE]) const
|
|
|
Utils::zero<48>(h);
|
|
|
}
|
|
|
|
|
|
-unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned int siglen) const
|
|
|
+unsigned int Identity::sign(const void *data, unsigned int len, void *sig, unsigned int siglen) const
|
|
|
{
|
|
|
if (m_hasPrivate) {
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
case C25519:
|
|
|
if (siglen >= ZT_C25519_SIGNATURE_LEN) {
|
|
|
- C25519::sign(m_priv,m_pub,data,len,sig);
|
|
|
+ C25519::sign(m_priv, m_pub, data, len, sig);
|
|
|
return ZT_C25519_SIGNATURE_LEN;
|
|
|
}
|
|
|
case P384:
|
|
|
if (siglen >= ZT_ECC384_SIGNATURE_SIZE) {
|
|
|
// SECURITY: signatures also include the public keys to further enforce their coupling.
|
|
|
uint8_t h[48];
|
|
|
- SHA384(h,data,len,m_pub,sizeof(m_pub));
|
|
|
- ECC384ECDSASign(m_priv + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,h,(uint8_t *)sig);
|
|
|
+ SHA384(h, data, len, m_pub, sizeof(m_pub));
|
|
|
+ ECC384ECDSASign(m_priv + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (uint8_t *) sig);
|
|
|
return ZT_ECC384_SIGNATURE_SIZE;
|
|
|
}
|
|
|
}
|
|
|
@@ -243,23 +242,23 @@ unsigned int Identity::sign(const void *data,unsigned int len,void *sig,unsigned
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
-bool Identity::verify(const void *data,unsigned int len,const void *sig,unsigned int siglen) const
|
|
|
+bool Identity::verify(const void *data, unsigned int len, const void *sig, unsigned int siglen) const
|
|
|
{
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
case C25519:
|
|
|
- return C25519::verify(m_pub,data,len,sig,siglen);
|
|
|
+ return C25519::verify(m_pub, data, len, sig, siglen);
|
|
|
case P384:
|
|
|
if (siglen == ZT_ECC384_SIGNATURE_SIZE) {
|
|
|
uint8_t h[48];
|
|
|
- SHA384(h,data,len,m_pub,sizeof(m_pub));
|
|
|
- return ECC384ECDSAVerify(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,h,(const uint8_t *)sig);
|
|
|
+ SHA384(h, data, len, m_pub, sizeof(m_pub));
|
|
|
+ return ECC384ECDSAVerify(m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, h, (const uint8_t *) sig);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-bool Identity::agree(const Identity &id,uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const
|
|
|
+bool Identity::agree(const Identity &id, uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) const
|
|
|
{
|
|
|
uint8_t rawkey[128];
|
|
|
uint8_t h[64];
|
|
|
@@ -268,9 +267,9 @@ bool Identity::agree(const Identity &id,uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) cons
|
|
|
if ((id.m_type == C25519) || (id.m_type == P384)) {
|
|
|
// If we are a C25519 key we can agree with another C25519 key or with only the
|
|
|
// C25519 portion of a type 1 P-384 key.
|
|
|
- C25519::agree(m_priv,id.m_pub,rawkey);
|
|
|
- SHA512(h,rawkey,ZT_C25519_ECDH_SHARED_SECRET_SIZE);
|
|
|
- Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key,h);
|
|
|
+ C25519::agree(m_priv, id.m_pub, rawkey);
|
|
|
+ SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
|
|
|
+ Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
|
|
|
return true;
|
|
|
}
|
|
|
} else if (m_type == P384) {
|
|
|
@@ -280,16 +279,16 @@ bool Identity::agree(const Identity &id,uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) cons
|
|
|
// P384 to be kosher, the C25519 secret can be considered a "salt"
|
|
|
// or something. For those who don't trust P384 this means the privacy of
|
|
|
// your traffic is also protected by C25519.
|
|
|
- C25519::agree(m_priv,id.m_pub,rawkey);
|
|
|
- ECC384ECDH(id.m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE,rawkey + ZT_C25519_ECDH_SHARED_SECRET_SIZE);
|
|
|
- SHA384(h,rawkey,ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
|
|
|
- Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key,h);
|
|
|
+ C25519::agree(m_priv, id.m_pub, rawkey);
|
|
|
+ ECC384ECDH(id.m_pub + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, m_priv + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, rawkey + ZT_C25519_ECDH_SHARED_SECRET_SIZE);
|
|
|
+ SHA384(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE + ZT_ECC384_SHARED_SECRET_SIZE);
|
|
|
+ Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
|
|
|
return true;
|
|
|
} else if (id.m_type == C25519) {
|
|
|
// If the other identity is a C25519 identity we can agree using only that type.
|
|
|
- C25519::agree(m_priv,id.m_pub,rawkey);
|
|
|
- SHA512(h,rawkey,ZT_C25519_ECDH_SHARED_SECRET_SIZE);
|
|
|
- Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key,h);
|
|
|
+ C25519::agree(m_priv, id.m_pub, rawkey);
|
|
|
+ SHA512(h, rawkey, ZT_C25519_ECDH_SHARED_SECRET_SIZE);
|
|
|
+ Utils::copy<ZT_SYMMETRIC_KEY_SIZE>(key, h);
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
@@ -297,40 +296,40 @@ bool Identity::agree(const Identity &id,uint8_t key[ZT_SYMMETRIC_KEY_SIZE]) cons
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
|
|
|
+char *Identity::toString(bool includePrivate, char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
|
|
|
{
|
|
|
char *p = buf;
|
|
|
m_fp.address().toString(p);
|
|
|
p += 10;
|
|
|
*(p++) = ':';
|
|
|
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
case C25519: {
|
|
|
*(p++) = '0';
|
|
|
*(p++) = ':';
|
|
|
- Utils::hex(m_pub,ZT_C25519_COMBINED_PUBLIC_KEY_SIZE,p);
|
|
|
+ Utils::hex(m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE, p);
|
|
|
p += ZT_C25519_COMBINED_PUBLIC_KEY_SIZE * 2;
|
|
|
- if ((m_hasPrivate)&&(includePrivate)) {
|
|
|
+ if ((m_hasPrivate) && (includePrivate)) {
|
|
|
*(p++) = ':';
|
|
|
- Utils::hex(m_priv,ZT_C25519_COMBINED_PRIVATE_KEY_SIZE,p);
|
|
|
+ Utils::hex(m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE, p);
|
|
|
p += ZT_C25519_COMBINED_PRIVATE_KEY_SIZE * 2;
|
|
|
}
|
|
|
- *p = (char)0;
|
|
|
+ *p = (char) 0;
|
|
|
return buf;
|
|
|
}
|
|
|
case P384: {
|
|
|
*(p++) = '1';
|
|
|
*(p++) = ':';
|
|
|
- int el = Utils::b32e(m_pub,sizeof(m_pub),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
|
|
|
+ int el = Utils::b32e(m_pub, sizeof(m_pub), p, (int) (ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t) (p - buf)));
|
|
|
if (el <= 0) return nullptr;
|
|
|
p += el;
|
|
|
- if ((m_hasPrivate)&&(includePrivate)) {
|
|
|
+ if ((m_hasPrivate) && (includePrivate)) {
|
|
|
*(p++) = ':';
|
|
|
- el = Utils::b32e(m_priv,sizeof(m_priv),p,(int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
|
|
|
+ el = Utils::b32e(m_priv, sizeof(m_priv), p, (int) (ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t) (p - buf)));
|
|
|
if (el <= 0) return nullptr;
|
|
|
p += el;
|
|
|
}
|
|
|
- *p = (char)0;
|
|
|
+ *p = (char) 0;
|
|
|
return buf;
|
|
|
}
|
|
|
}
|
|
|
@@ -342,13 +341,13 @@ bool Identity::fromString(const char *str)
|
|
|
{
|
|
|
char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
|
|
|
memoryZero(this);
|
|
|
- if ((!str)||(!Utils::scopy(tmp,sizeof(tmp),str)))
|
|
|
+ if ((!str) || (!Utils::scopy(tmp, sizeof(tmp), str)))
|
|
|
return false;
|
|
|
|
|
|
int fno = 0;
|
|
|
char *saveptr = nullptr;
|
|
|
- for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok(nullptr,":",&saveptr)) {
|
|
|
- switch(fno++) {
|
|
|
+ for (char *f = Utils::stok(tmp, ":", &saveptr);((f) && (fno < 4));f = Utils::stok(nullptr, ":", &saveptr)) {
|
|
|
+ switch (fno++) {
|
|
|
|
|
|
case 0:
|
|
|
m_fp.m_cfp.address = Utils::hexStrToU64(f) & ZT_ADDRESS_MASK;
|
|
|
@@ -359,9 +358,9 @@ bool Identity::fromString(const char *str)
|
|
|
break;
|
|
|
|
|
|
case 1:
|
|
|
- if ((f[0] == '0')&&(!f[1])) {
|
|
|
+ if ((f[0] == '0') && (!f[1])) {
|
|
|
m_type = C25519;
|
|
|
- } else if ((f[0] == '1')&&(!f[1])) {
|
|
|
+ } else if ((f[0] == '1') && (!f[1])) {
|
|
|
m_type = P384;
|
|
|
} else {
|
|
|
memoryZero(this);
|
|
|
@@ -370,17 +369,17 @@ bool Identity::fromString(const char *str)
|
|
|
break;
|
|
|
|
|
|
case 2:
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
|
|
|
case C25519:
|
|
|
- if (Utils::unhex(f,strlen(f),m_pub,ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) != ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) {
|
|
|
+ if (Utils::unhex(f, strlen(f), m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) != ZT_C25519_COMBINED_PUBLIC_KEY_SIZE) {
|
|
|
memoryZero(this);
|
|
|
return false;
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case P384:
|
|
|
- if (Utils::b32d(f,m_pub,sizeof(m_pub)) != sizeof(m_pub)) {
|
|
|
+ if (Utils::b32d(f, m_pub, sizeof(m_pub)) != sizeof(m_pub)) {
|
|
|
memoryZero(this);
|
|
|
return false;
|
|
|
}
|
|
|
@@ -391,10 +390,10 @@ bool Identity::fromString(const char *str)
|
|
|
|
|
|
case 3:
|
|
|
if (strlen(f) > 1) {
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
|
|
|
case C25519:
|
|
|
- if (Utils::unhex(f,strlen(f),m_priv,ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) != ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
|
|
|
+ if (Utils::unhex(f, strlen(f), m_priv, ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) != ZT_C25519_COMBINED_PRIVATE_KEY_SIZE) {
|
|
|
memoryZero(this);
|
|
|
return false;
|
|
|
} else {
|
|
|
@@ -403,7 +402,7 @@ bool Identity::fromString(const char *str)
|
|
|
break;
|
|
|
|
|
|
case P384:
|
|
|
- if (Utils::b32d(f,m_priv,sizeof(m_priv)) != sizeof(m_priv)) {
|
|
|
+ if (Utils::b32d(f, m_priv, sizeof(m_priv)) != sizeof(m_priv)) {
|
|
|
memoryZero(this);
|
|
|
return false;
|
|
|
} else {
|
|
|
@@ -424,7 +423,7 @@ bool Identity::fromString(const char *str)
|
|
|
}
|
|
|
|
|
|
m_computeHash();
|
|
|
- if ((m_type == P384)&&(m_fp.address() != Address(m_fp.hash()))) {
|
|
|
+ if ((m_type == P384) && (m_fp.address() != Address(m_fp.hash()))) {
|
|
|
memoryZero(this);
|
|
|
return false;
|
|
|
}
|
|
|
@@ -432,17 +431,17 @@ bool Identity::fromString(const char *str)
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool includePrivate) const noexcept
|
|
|
+int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX], const bool includePrivate) const noexcept
|
|
|
{
|
|
|
m_fp.address().copyTo(data);
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
|
|
|
case C25519:
|
|
|
- data[ZT_ADDRESS_LENGTH] = (uint8_t)C25519;
|
|
|
- Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1,m_pub);
|
|
|
- if ((includePrivate)&&(m_hasPrivate)) {
|
|
|
+ data[ZT_ADDRESS_LENGTH] = (uint8_t) C25519;
|
|
|
+ Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1, m_pub);
|
|
|
+ if ((includePrivate) && (m_hasPrivate)) {
|
|
|
data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
|
|
|
- Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1,m_priv);
|
|
|
+ Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1, m_priv);
|
|
|
return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
|
|
|
} else {
|
|
|
data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE] = 0;
|
|
|
@@ -450,11 +449,11 @@ int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool incl
|
|
|
}
|
|
|
|
|
|
case P384:
|
|
|
- data[ZT_ADDRESS_LENGTH] = (uint8_t)P384;
|
|
|
- Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1,m_pub);
|
|
|
- if ((includePrivate)&&(m_hasPrivate)) {
|
|
|
+ data[ZT_ADDRESS_LENGTH] = (uint8_t) P384;
|
|
|
+ Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1, m_pub);
|
|
|
+ if ((includePrivate) && (m_hasPrivate)) {
|
|
|
data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
|
|
|
- Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1,m_priv);
|
|
|
+ Utils::copy<ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE>(data + ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1, m_priv);
|
|
|
return ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1 + ZT_IDENTITY_P384_COMPOUND_PRIVATE_KEY_SIZE;
|
|
|
} else {
|
|
|
data[ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE] = 0;
|
|
|
@@ -465,7 +464,7 @@ int Identity::marshal(uint8_t data[ZT_IDENTITY_MARSHAL_SIZE_MAX],const bool incl
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
-int Identity::unmarshal(const uint8_t *data,const int len) noexcept
|
|
|
+int Identity::unmarshal(const uint8_t *data, const int len) noexcept
|
|
|
{
|
|
|
memoryZero(this);
|
|
|
|
|
|
@@ -474,13 +473,13 @@ int Identity::unmarshal(const uint8_t *data,const int len) noexcept
|
|
|
m_fp.m_cfp.address = Address(data).toInt();
|
|
|
|
|
|
unsigned int privlen;
|
|
|
- switch((m_type = (Type)data[ZT_ADDRESS_LENGTH])) {
|
|
|
+ switch ((m_type = (Type) data[ZT_ADDRESS_LENGTH])) {
|
|
|
|
|
|
case C25519:
|
|
|
if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1))
|
|
|
return -1;
|
|
|
|
|
|
- Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(m_pub,data + ZT_ADDRESS_LENGTH + 1);
|
|
|
+ Utils::copy<ZT_C25519_COMBINED_PUBLIC_KEY_SIZE>(m_pub, data + ZT_ADDRESS_LENGTH + 1);
|
|
|
m_computeHash();
|
|
|
|
|
|
privlen = data[ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE];
|
|
|
@@ -488,7 +487,7 @@ int Identity::unmarshal(const uint8_t *data,const int len) noexcept
|
|
|
if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE))
|
|
|
return -1;
|
|
|
m_hasPrivate = true;
|
|
|
- Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(m_priv,data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1);
|
|
|
+ Utils::copy<ZT_C25519_COMBINED_PRIVATE_KEY_SIZE>(m_priv, data + ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1);
|
|
|
return ZT_ADDRESS_LENGTH + 1 + ZT_C25519_COMBINED_PUBLIC_KEY_SIZE + 1 + ZT_C25519_COMBINED_PRIVATE_KEY_SIZE;
|
|
|
} else if (privlen == 0) {
|
|
|
m_hasPrivate = false;
|
|
|
@@ -500,7 +499,7 @@ int Identity::unmarshal(const uint8_t *data,const int len) noexcept
|
|
|
if (len < (ZT_ADDRESS_LENGTH + 1 + ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE + 1))
|
|
|
return -1;
|
|
|
|
|
|
- Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(m_pub,data + ZT_ADDRESS_LENGTH + 1);
|
|
|
+ Utils::copy<ZT_IDENTITY_P384_COMPOUND_PUBLIC_KEY_SIZE>(m_pub, data + ZT_ADDRESS_LENGTH + 1);
|
|
|
m_computeHash(); // this sets the address for P384
|
|
|
if (m_fp.address() != Address(m_fp.hash())) // this sanity check is possible with V1 identities
|
|
|
return -1;
|
|
|
@@ -525,15 +524,15 @@ int Identity::unmarshal(const uint8_t *data,const int len) noexcept
|
|
|
|
|
|
void Identity::m_computeHash()
|
|
|
{
|
|
|
- switch(m_type) {
|
|
|
+ switch (m_type) {
|
|
|
default:
|
|
|
m_fp.zero();
|
|
|
break;
|
|
|
case C25519:
|
|
|
- SHA384(m_fp.m_cfp.hash,m_pub,ZT_C25519_COMBINED_PUBLIC_KEY_SIZE);
|
|
|
+ SHA384(m_fp.m_cfp.hash, m_pub, ZT_C25519_COMBINED_PUBLIC_KEY_SIZE);
|
|
|
break;
|
|
|
case P384:
|
|
|
- SHA384(m_fp.m_cfp.hash,m_pub,sizeof(m_pub));
|
|
|
+ SHA384(m_fp.m_cfp.hash, m_pub, sizeof(m_pub));
|
|
|
m_fp.m_cfp.address = Address(m_fp.m_cfp.hash).toInt();
|
|
|
break;
|
|
|
}
|
|
|
@@ -545,13 +544,13 @@ extern "C" {
|
|
|
|
|
|
ZT_Identity *ZT_Identity_new(enum ZT_Identity_Type type)
|
|
|
{
|
|
|
- if ((type != ZT_IDENTITY_TYPE_C25519)&&(type != ZT_IDENTITY_TYPE_P384))
|
|
|
+ if ((type != ZT_IDENTITY_TYPE_C25519) && (type != ZT_IDENTITY_TYPE_P384))
|
|
|
return nullptr;
|
|
|
try {
|
|
|
ZeroTier::Identity *const id = new ZeroTier::Identity(); // NOLINT(hicpp-use-auto,modernize-use-auto)
|
|
|
- id->generate((ZeroTier::Identity::Type)type);
|
|
|
+ id->generate((ZeroTier::Identity::Type) type);
|
|
|
return reinterpret_cast<ZT_Identity *>(id);
|
|
|
- } catch ( ... ) {
|
|
|
+ } catch (...) {
|
|
|
return nullptr;
|
|
|
}
|
|
|
}
|
|
|
@@ -567,7 +566,7 @@ ZT_Identity *ZT_Identity_fromString(const char *idStr)
|
|
|
return nullptr;
|
|
|
}
|
|
|
return reinterpret_cast<ZT_Identity *>(id);
|
|
|
- } catch ( ... ) {
|
|
|
+ } catch (...) {
|
|
|
return nullptr;
|
|
|
}
|
|
|
}
|
|
|
@@ -579,34 +578,34 @@ int ZT_Identity_validate(const ZT_Identity *id)
|
|
|
return reinterpret_cast<const ZeroTier::Identity *>(id)->locallyValidate() ? 1 : 0;
|
|
|
}
|
|
|
|
|
|
-unsigned int ZT_Identity_sign(const ZT_Identity *id,const void *data,unsigned int len,void *signature,unsigned int signatureBufferLength)
|
|
|
+unsigned int ZT_Identity_sign(const ZT_Identity *id, const void *data, unsigned int len, void *signature, unsigned int signatureBufferLength)
|
|
|
{
|
|
|
if (!id)
|
|
|
return 0;
|
|
|
if (signatureBufferLength < ZT_SIGNATURE_BUFFER_SIZE)
|
|
|
return 0;
|
|
|
- return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data,len,signature,signatureBufferLength);
|
|
|
+ return reinterpret_cast<const ZeroTier::Identity *>(id)->sign(data, len, signature, signatureBufferLength);
|
|
|
}
|
|
|
|
|
|
-int ZT_Identity_verify(const ZT_Identity *id,const void *data,unsigned int len,const void *signature,unsigned int sigLen)
|
|
|
+int ZT_Identity_verify(const ZT_Identity *id, const void *data, unsigned int len, const void *signature, unsigned int sigLen)
|
|
|
{
|
|
|
- if ((!id)||(!signature)||(!sigLen))
|
|
|
+ if ((!id) || (!signature) || (!sigLen))
|
|
|
return 0;
|
|
|
- return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data,len,signature,sigLen) ? 1 : 0;
|
|
|
+ return reinterpret_cast<const ZeroTier::Identity *>(id)->verify(data, len, signature, sigLen) ? 1 : 0;
|
|
|
}
|
|
|
|
|
|
enum ZT_Identity_Type ZT_Identity_type(const ZT_Identity *id)
|
|
|
{
|
|
|
if (!id)
|
|
|
- return (ZT_Identity_Type)0;
|
|
|
- return (enum ZT_Identity_Type)reinterpret_cast<const ZeroTier::Identity *>(id)->type();
|
|
|
+ return (ZT_Identity_Type) 0;
|
|
|
+ return (enum ZT_Identity_Type) reinterpret_cast<const ZeroTier::Identity *>(id)->type();
|
|
|
}
|
|
|
|
|
|
-char *ZT_Identity_toString(const ZT_Identity *id,char *buf,int capacity,int includePrivate)
|
|
|
+char *ZT_Identity_toString(const ZT_Identity *id, char *buf, int capacity, int includePrivate)
|
|
|
{
|
|
|
- if ((!id)||(!buf)||(capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
|
|
|
+ if ((!id) || (!buf) || (capacity < ZT_IDENTITY_STRING_BUFFER_LENGTH))
|
|
|
return nullptr;
|
|
|
- reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0,buf);
|
|
|
+ reinterpret_cast<const ZeroTier::Identity *>(id)->toString(includePrivate != 0, buf);
|
|
|
return buf;
|
|
|
}
|
|
|
|