2013-07-04 20:56:19 +00:00
|
|
|
/*
|
2019-08-23 16:23:39 +00:00
|
|
|
* Copyright (c)2019 ZeroTier, Inc.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
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.
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
2020-08-20 19:51:39 +00:00
|
|
|
* Change Date: 2025-01-01
|
2013-07-04 20:56:19 +00:00
|
|
|
*
|
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.
|
2013-07-04 20:56:19 +00:00
|
|
|
*/
|
2019-08-23 16:23:39 +00:00
|
|
|
/****/
|
2013-07-04 20:56:19 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2013-09-17 20:49:16 +00:00
|
|
|
#include "Constants.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
#include "Identity.hpp"
|
2013-09-16 17:57:57 +00:00
|
|
|
#include "SHA512.hpp"
|
2013-09-16 18:47:48 +00:00
|
|
|
#include "Salsa20.hpp"
|
2013-10-05 10:00:47 +00:00
|
|
|
#include "Utils.hpp"
|
2013-07-04 20:56:19 +00:00
|
|
|
|
2013-10-05 18:15:59 +00:00
|
|
|
// These can't be changed without a new identity type. They define the
|
|
|
|
// parameters of the hashcash hashing/searching algorithm.
|
2013-10-07 16:48:27 +00:00
|
|
|
|
2013-10-20 19:46:36 +00:00
|
|
|
#define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 17
|
|
|
|
#define ZT_IDENTITY_GEN_MEMORY 2097152
|
2013-10-18 21:39:48 +00:00
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
namespace ZeroTier {
|
|
|
|
|
2013-10-06 09:28:25 +00:00
|
|
|
// A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
|
2013-10-07 16:48:27 +00:00
|
|
|
static inline void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
|
2013-10-05 18:15:59 +00:00
|
|
|
{
|
2013-10-20 19:31:32 +00:00
|
|
|
// Digest publicKey[] to obtain initial digest
|
2020-08-21 21:23:31 +00:00
|
|
|
SHA512(digest,publicKey,publicKeyBytes);
|
2013-10-05 18:15:59 +00:00
|
|
|
|
2013-10-20 19:31:32 +00:00
|
|
|
// 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.
|
2013-10-07 16:48:27 +00:00
|
|
|
memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
|
2017-04-17 23:43:03 +00:00
|
|
|
Salsa20 s20(digest,(char *)digest + 32);
|
2017-02-06 00:19:03 +00:00
|
|
|
s20.crypt20((char *)genmem,(char *)genmem,64);
|
2013-10-20 19:31:32 +00:00
|
|
|
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));
|
2017-02-06 00:19:03 +00:00
|
|
|
s20.crypt20((char *)genmem + i,(char *)genmem + i,64);
|
2013-10-07 16:48:27 +00:00
|
|
|
}
|
|
|
|
|
2013-10-20 19:31:32 +00:00
|
|
|
// 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;
|
2017-02-06 00:19:03 +00:00
|
|
|
s20.crypt20(digest,digest,64);
|
2013-10-20 19:31:32 +00:00
|
|
|
}
|
2013-10-05 18:15:59 +00:00
|
|
|
}
|
|
|
|
|
2013-10-06 09:28:25 +00:00
|
|
|
// Hashcash generation halting condition -- halt when first byte is less than
|
|
|
|
// threshold value.
|
2013-10-05 10:00:47 +00:00
|
|
|
struct _Identity_generate_cond
|
|
|
|
{
|
2017-07-17 21:21:09 +00:00
|
|
|
_Identity_generate_cond() {}
|
|
|
|
_Identity_generate_cond(unsigned char *sb,char *gm) : digest(sb),genmem(gm) {}
|
2013-10-05 10:00:47 +00:00
|
|
|
inline bool operator()(const C25519::Pair &kp) const
|
|
|
|
{
|
2018-01-27 01:00:37 +00:00
|
|
|
_computeMemoryHardHash(kp.pub.data,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
|
2013-10-07 19:00:38 +00:00
|
|
|
return (digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN);
|
2013-10-05 10:00:47 +00:00
|
|
|
}
|
2013-10-07 19:00:38 +00:00
|
|
|
unsigned char *digest;
|
2013-10-07 16:48:27 +00:00
|
|
|
char *genmem;
|
2013-10-05 10:00:47 +00:00
|
|
|
};
|
|
|
|
|
2013-07-04 20:56:19 +00:00
|
|
|
void Identity::generate()
|
|
|
|
{
|
2013-10-07 19:00:38 +00:00
|
|
|
unsigned char digest[64];
|
2013-10-07 16:48:27 +00:00
|
|
|
char *genmem = new char[ZT_IDENTITY_GEN_MEMORY];
|
2013-10-05 10:00:47 +00:00
|
|
|
|
2013-09-16 13:20:59 +00:00
|
|
|
C25519::Pair kp;
|
2013-07-04 20:56:19 +00:00
|
|
|
do {
|
2013-10-07 19:00:38 +00:00
|
|
|
kp = C25519::generateSatisfying(_Identity_generate_cond(digest,genmem));
|
|
|
|
_address.setTo(digest + 59,ZT_ADDRESS_LENGTH); // last 5 bytes are address
|
2013-07-04 20:56:19 +00:00
|
|
|
} while (_address.isReserved());
|
2013-09-16 13:20:59 +00:00
|
|
|
|
|
|
|
_publicKey = kp.pub;
|
|
|
|
if (!_privateKey)
|
|
|
|
_privateKey = new C25519::Private();
|
|
|
|
*_privateKey = kp.priv;
|
2013-10-06 09:28:25 +00:00
|
|
|
|
|
|
|
delete [] genmem;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 11:00:55 +00:00
|
|
|
bool Identity::locallyValidate() const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2013-10-05 14:19:12 +00:00
|
|
|
if (_address.isReserved())
|
|
|
|
return false;
|
2013-10-05 18:15:59 +00:00
|
|
|
|
2013-10-07 19:00:38 +00:00
|
|
|
unsigned char digest[64];
|
2013-10-07 16:48:27 +00:00
|
|
|
char *genmem = new char[ZT_IDENTITY_GEN_MEMORY];
|
2018-01-27 01:00:37 +00:00
|
|
|
_computeMemoryHardHash(_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN,digest,genmem);
|
2013-10-06 09:28:25 +00:00
|
|
|
delete [] genmem;
|
2013-10-05 18:15:59 +00:00
|
|
|
|
|
|
|
unsigned char addrb[5];
|
2013-10-05 11:00:55 +00:00
|
|
|
_address.copyTo(addrb,5);
|
2013-10-20 15:04:58 +00:00
|
|
|
|
2013-10-05 11:00:55 +00:00
|
|
|
return (
|
2013-10-07 19:00:38 +00:00
|
|
|
(digest[0] < ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN)&&
|
|
|
|
(digest[59] == addrb[0])&&
|
|
|
|
(digest[60] == addrb[1])&&
|
|
|
|
(digest[61] == addrb[2])&&
|
|
|
|
(digest[62] == addrb[3])&&
|
|
|
|
(digest[63] == addrb[4]));
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
2017-07-06 23:11:11 +00:00
|
|
|
char *Identity::toString(bool includePrivate,char buf[ZT_IDENTITY_STRING_BUFFER_LENGTH]) const
|
2013-07-04 20:56:19 +00:00
|
|
|
{
|
2017-07-06 23:11:11 +00:00
|
|
|
char *p = buf;
|
|
|
|
Utils::hex10(_address.toInt(),p);
|
|
|
|
p += 10;
|
|
|
|
*(p++) = ':';
|
|
|
|
*(p++) = '0';
|
|
|
|
*(p++) = ':';
|
|
|
|
Utils::hex(_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN,p);
|
|
|
|
p += ZT_C25519_PUBLIC_KEY_LEN * 2;
|
2013-09-16 17:02:10 +00:00
|
|
|
if ((_privateKey)&&(includePrivate)) {
|
2017-07-06 23:11:11 +00:00
|
|
|
*(p++) = ':';
|
|
|
|
Utils::hex(_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN,p);
|
|
|
|
p += ZT_C25519_PRIVATE_KEY_LEN * 2;
|
2013-09-16 17:02:10 +00:00
|
|
|
}
|
2017-07-13 17:51:05 +00:00
|
|
|
*p = (char)0;
|
2017-07-06 23:11:11 +00:00
|
|
|
return buf;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Identity::fromString(const char *str)
|
|
|
|
{
|
2017-07-13 22:08:57 +00:00
|
|
|
if (!str) {
|
|
|
|
_address.zero();
|
2015-03-18 23:10:48 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +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();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
|
|
|
|
delete _privateKey;
|
|
|
|
_privateKey = (C25519::Private *)0;
|
|
|
|
|
|
|
|
int fno = 0;
|
2017-07-13 22:08:57 +00:00
|
|
|
char *saveptr = (char *)0;
|
2013-09-16 17:02:10 +00:00
|
|
|
for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
|
|
|
|
switch(fno++) {
|
|
|
|
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();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2017-07-13 22:08:57 +00:00
|
|
|
if ((f[0] != '0')||(f[1])) {
|
|
|
|
_address.zero();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2018-01-27 01:00:37 +00:00
|
|
|
if (Utils::unhex(f,_publicKey.data,ZT_C25519_PUBLIC_KEY_LEN) != ZT_C25519_PUBLIC_KEY_LEN) {
|
2017-07-13 22:08:57 +00:00
|
|
|
_address.zero();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
_privateKey = new C25519::Private();
|
2018-01-27 01:00:37 +00:00
|
|
|
if (Utils::unhex(f,_privateKey->data,ZT_C25519_PRIVATE_KEY_LEN) != ZT_C25519_PRIVATE_KEY_LEN) {
|
2017-07-13 22:08:57 +00:00
|
|
|
_address.zero();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
2017-07-13 22:08:57 +00:00
|
|
|
_address.zero();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-07-13 22:08:57 +00:00
|
|
|
if (fno < 3) {
|
|
|
|
_address.zero();
|
2013-09-16 17:02:10 +00:00
|
|
|
return false;
|
2017-07-13 22:08:57 +00:00
|
|
|
}
|
2013-09-16 17:02:10 +00:00
|
|
|
|
|
|
|
return true;
|
2013-07-04 20:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace ZeroTier
|