ZeroTierOne/node/Identity.cpp

279 lines
7.4 KiB
C++
Raw Normal View History

/*
2019-08-23 16:23:39 +00:00
* Copyright (c)2019 ZeroTier, Inc.
*
2019-08-23 16:23:39 +00:00
* Use of this software is governed by the Business Source License included
* in the LICENSE.TXT file in the project's root directory.
*
2019-08-23 16:23:39 +00:00
* Change Date: 2023-01-01
*
2019-08-23 16:23:39 +00:00
* On the date above, in accordance with the Business Source License, use
* of this software will be governed by version 2.0 of the Apache License.
*/
2019-08-23 16:23:39 +00:00
/****/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "Constants.hpp"
#include "Identity.hpp"
#include "SHA512.hpp"
#include "Salsa20.hpp"
#include "Utils.hpp"
namespace ZeroTier {
2019-08-08 04:50:47 +00:00
namespace {
// These can't be changed without a new identity type. They define the
2019-08-20 20:32:23 +00:00
// parameters of the hashcash hashing/searching algorithm for type 0
// identities.
#define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
#define ZT_IDENTITY_GEN_MEMORY 2097152
// A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
2019-08-08 04:50:47 +00:00
static void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
{
// Digest publicKey[] to obtain initial digest
2019-08-07 21:41:58 +00:00
SHA512(digest,publicKey,publicKeyBytes);
// Initialize genmem[] using Salsa20 in a CBC-like configuration since
2018-06-08 00:25:27 +00:00
// ordinary Salsa20 is randomly seek-able. This is good for a cipher
// but is not what we want for sequential memory-hardness.
memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
Salsa20 s20(digest,(char *)digest + 32);
s20.crypt20((char *)genmem,(char *)genmem,64);
for(unsigned long i=64;i<ZT_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);
}
// Render final digest using genmem as a lookup table
for(unsigned long i=0;i<(ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t));) {
unsigned long idx1 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (64 / sizeof(uint64_t)));
unsigned long idx2 = (unsigned long)(Utils::ntoh(((uint64_t *)genmem)[i++]) % (ZT_IDENTITY_GEN_MEMORY / sizeof(uint64_t)));
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);
}
}
// Hashcash generation halting condition -- halt when first byte is less than
// threshold value.
struct _Identity_generate_cond
{
2019-08-14 22:50:25 +00:00
inline _Identity_generate_cond() {}
inline _Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
2019-08-20 20:32:23 +00:00
inline bool operator()(const uint8_t pub[ZT_C25519_PUBLIC_KEY_LEN]) const
{
2019-08-20 20:32:23 +00:00
_computeMemoryHardHash(pub,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
}
unsigned char *digest;
char *genmem;
};
2019-08-08 04:50:47 +00:00
} // anonymous namespace
void Identity::generate(const Type t)
{
uint8_t digest[64];
2019-08-20 20:32:23 +00:00
_type = t;
_hasPrivate = true;
2019-08-08 04:50:47 +00:00
char *const genmem = new char[ZT_IDENTITY_GEN_MEMORY];
2019-08-20 20:32:23 +00:00
do {
C25519::generateSatisfying(_Identity_generate_cond(digest,genmem),_pub.c25519,_priv.c25519);
_address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
} while (_address.isReserved());
2019-08-08 04:50:47 +00:00
delete [] genmem;
2019-08-20 20:32:23 +00:00
if (t == P384) {
2019-08-20 20:32:23 +00:00
ECC384GenerateKey(_pub.p384,_priv.p384);
SHA384(digest,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
ECC384ECDSASign(_priv.p384,digest,_pub.p384s);
}
}
bool Identity::locallyValidate() const
{
uint8_t digest[64];
if (_address.isReserved())
return false;
2019-08-20 20:32:23 +00:00
if (_type == P384) {
// Check that the C25519 public key is blessed by the P-384 key.
SHA384(digest,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN);
if (!ECC384ECDSAVerify(_pub.p384,digest,_pub.p384s))
return false;
}
2019-08-08 04:50:47 +00:00
char *genmem = nullptr;
try {
genmem = new char[ZT_IDENTITY_GEN_MEMORY];
2019-08-20 20:32:23 +00:00
_computeMemoryHardHash(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
2019-08-08 04:50:47 +00:00
delete [] genmem;
2019-08-20 20:32:23 +00:00
return ((_address == Address(digest + 59,ZT_ADDRESS_LENGTH))&&(!_address.isReserved())&&(digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN));
2019-08-08 04:50:47 +00:00
} catch ( ... ) {
if (genmem) delete [] genmem;
}
2019-08-20 20:32:23 +00:00
return false;
2019-08-08 04:50:47 +00:00
}
2017-07-06 23:11:11 +00:00
char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
{
switch(_type) {
2019-08-20 20:32:23 +00:00
case C25519: {
char *p = buf;
Utils::hex10(_address.toInt(),p);
p += 10;
*(p++) = ':';
*(p++) = '0';
*(p++) = ':';
2019-08-20 20:32:23 +00:00
Utils::hex(_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN,p);
p += ZT_C25519_PUBLIC_KEY_LEN * 2;
if ((_hasPrivate)&&(includePrivate)) {
*(p++) = ':';
2019-08-20 20:32:23 +00:00
Utils::hex(_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN,p);
p += ZT_C25519_PRIVATE_KEY_LEN * 2;
}
*p = (char)0;
return buf;
} break;
2019-08-20 20:32:23 +00:00
case P384: {
char *p = buf;
Utils::hex10(_address.toInt(),p);
p += 10;
*(p++) = ':';
*(p++) = '1';
*(p++) = ':';
int el = Utils::b32e((const uint8_t *)(&_pub),sizeof(_pub),p,(unsigned int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
2019-08-20 20:32:23 +00:00
if (el <= 0) return nullptr;
p += el;
if ((_hasPrivate)&&(includePrivate)) {
*(p++) = ':';
el = Utils::b32e((const uint8_t *)(&_priv),sizeof(_priv),p,(unsigned int)(ZT_IDENTITY_STRING_BUFFER_LENGTH - (uintptr_t)(p - buf)));
2019-08-20 20:32:23 +00:00
if (el <= 0) return nullptr;
p += el;
}
*p = (char)0;
return buf;
} break;
2019-08-20 20:32:23 +00:00
}
return nullptr;
}
bool Identity::fromString(const char *str)
{
2019-08-20 20:32:23 +00:00
_hasPrivate = false;
2017-07-13 22:08:57 +00:00
if (!str) {
_address.zero();
return false;
2017-07-13 22:08:57 +00:00
}
2019-08-20 20:32:23 +00:00
2017-07-06 23:11:11 +00:00
char tmp[ZT_IDENTITY_STRING_BUFFER_LENGTH];
2017-07-13 22:08:57 +00:00
if (!Utils::scopy(tmp,sizeof(tmp),str)) {
_address.zero();
return false;
2017-07-13 22:08:57 +00:00
}
int fno = 0;
2017-07-13 22:08:57 +00:00
char *saveptr = (char *)0;
2019-08-20 20:32:23 +00:00
for(char *f=Utils::stok(tmp,":",&saveptr);((f)&&(fno < 4));f=Utils::stok((char *)0,":",&saveptr)) {
switch(fno++) {
2019-08-20 20:32:23 +00:00
case 0:
2017-02-04 08:23:31 +00:00
_address = Address(Utils::hexStrToU64(f));
2017-07-13 22:08:57 +00:00
if (_address.isReserved()) {
_address.zero();
return false;
2017-07-13 22:08:57 +00:00
}
break;
2019-08-20 20:32:23 +00:00
case 1:
if ((f[0] == '0')&&(!f[1])) {
_type = C25519;
} else if ((f[0] == '1')&&(!f[1])) {
_type = P384;
} else {
2017-07-13 22:08:57 +00:00
_address.zero();
return false;
2017-07-13 22:08:57 +00:00
}
break;
2019-08-20 20:32:23 +00:00
case 2:
switch(_type) {
2019-08-20 20:32:23 +00:00
case C25519:
2019-08-20 20:32:23 +00:00
if (Utils::unhex(f,_pub.c25519,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
_address.zero();
return false;
}
break;
2019-08-20 20:32:23 +00:00
case P384:
if (Utils::b32d(f,(uint8_t *)(&_pub),sizeof(_pub)) != sizeof(_pub)) {
_address.zero();
return false;
}
break;
2019-08-20 20:32:23 +00:00
2017-07-13 22:08:57 +00:00
}
break;
2019-08-20 20:32:23 +00:00
case 3:
2019-08-20 20:32:23 +00:00
if (strlen(f) > 1) {
switch(_type) {
case C25519:
if (Utils::unhex(f,_priv.c25519,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
_address.zero();
return false;
} else {
_hasPrivate = true;
}
break;
case P384:
if (Utils::b32d(f,(uint8_t *)(&_priv),sizeof(_priv)) != sizeof(_priv)) {
2019-08-20 20:32:23 +00:00
_address.zero();
return false;
} else {
_hasPrivate = true;
}
break;
}
break;
2017-07-13 22:08:57 +00:00
}
2019-08-20 20:32:23 +00:00
}
}
2019-08-20 20:32:23 +00:00
2017-07-13 22:08:57 +00:00
if (fno < 3) {
_address.zero();
return false;
2017-07-13 22:08:57 +00:00
}
return true;
}
} // namespace ZeroTier