/*
* ZeroTier One - Global Peer to Peer Ethernet
* Copyright (C) 2012-2013 ZeroTier Networks LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
* --
*
* ZeroTier may be used and distributed under the terms of the GPLv3, which
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
*
* If you would like to embed ZeroTier into a commercial application or
* redistribute it in a modified binary form, please contact ZeroTier Networks
* LLC. Start here: http://www.zerotier.com/
*/
#include
#include
#include
#include
#include "Constants.hpp"
#include "Identity.hpp"
#include "SHA512.hpp"
#include "Salsa20.hpp"
#include "Utils.hpp"
// These can't be changed without a new identity type. They define the
// parameters of the hashcash hashing/searching algorithm.
// Hashcash halting criteria
#define ZT_IDENTITY_GEN_HASHCASH_FIRST_BYTE_LESS_THAN 7
// Amount of memory for memory-hardness
#define ZT_IDENTITY_GEN_MEMORY 8388608
// Step distance for mixing genmem[]
#define ZT_IDENTITY_GEN_MEMORY_MIX_STEP 1024
// Rounds used for Salsa20 step
#define ZT_IDENTITY_GEN_SALSA20_ROUNDS 20
namespace ZeroTier {
// A memory-hard composition of SHA-512 and Salsa20 for hashcash hashing
static inline void _computeMemoryHardHash(const void *publicKey,unsigned int publicKeyBytes,void *digest,void *genmem)
{
// Hash publicKey[] to obtain Salsa20 key
SHA512::hash(digest,publicKey,publicKeyBytes);
// Generate genmem[] bytes of Salsa20 key stream
memset(genmem,0,ZT_IDENTITY_GEN_MEMORY);
Salsa20 s20(digest,256,(char *)digest + 32,ZT_IDENTITY_GEN_SALSA20_ROUNDS);
s20.encrypt(genmem,genmem,ZT_IDENTITY_GEN_MEMORY);
// Do something to genmem[] that iteratively makes every value
// possibly dependent on every other value with a nontrivial
// probability. Continue to use already-initialized Salsa20 as
// a random source.
for(unsigned int i=0;i>=8)
++((unsigned char *)genmem)[(uintptr_t)x % ZT_IDENTITY_GEN_MEMORY];
} else {
for(unsigned int k=0;k<8;++k,x>>=8)
--((unsigned char *)genmem)[(uintptr_t)x % ZT_IDENTITY_GEN_MEMORY];
}
}
// Mix in publicKey[] again, ensuring all entropy is used
for(unsigned int i=0;idata,_privateKey->size()));
}
return r;
}
bool Identity::fromString(const char *str)
{
char *saveptr = (char *)0;
char tmp[4096];
if (!Utils::scopy(tmp,sizeof(tmp),str))
return false;
delete _privateKey;
_privateKey = (C25519::Private *)0;
int fno = 0;
for(char *f=Utils::stok(tmp,":",&saveptr);(f);f=Utils::stok((char *)0,":",&saveptr)) {
switch(fno++) {
case 0:
_address = Address(f);
if (_address.isReserved())
return false;
break;
case 1:
if ((f[0] != '0')||(f[1]))
return false;
break;
case 2:
if (Utils::unhex(f,_publicKey.data,_publicKey.size()) != _publicKey.size())
return false;
break;
case 3:
_privateKey = new C25519::Private();
if (Utils::unhex(f,_privateKey->data,_privateKey->size()) != _privateKey->size())
return false;
break;
default:
return false;
}
}
if (fno < 3)
return false;
return true;
}
} // namespace ZeroTier