Imported Upstream version 1.2.4

This commit is contained in:
didyouexpectthat 2018-01-12 18:20:00 -08:00
commit 4722a0b75a
398 changed files with 38633 additions and 24919 deletions

View file

@ -49,13 +49,17 @@
#include "osdep/OSUtils.hpp"
#include "osdep/Phy.hpp"
#include "osdep/Http.hpp"
#include "osdep/BackgroundResolver.hpp"
#include "osdep/PortMapper.hpp"
#include "osdep/Thread.hpp"
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
#include "controller/SqliteNetworkController.hpp"
#endif // ZT_ENABLE_NETWORK_CONTROLLER
#include "controller/JSONDB.hpp"
#ifdef ZT_USE_X64_ASM_SALSA2012
#include "ext/x64-salsa2012-asm/salsa2012.h"
#endif
#ifdef ZT_USE_ARM32_NEON_ASM_SALSA2012
#include "ext/arm32-neon-salsa2012-asm/salsa2012.h"
#endif
#ifdef __WINDOWS__
#include <tchar.h>
@ -137,8 +141,6 @@ static const C25519TestVector C25519_TEST_VECTORS[ZT_NUM_C25519_TEST_VECTORS] =
//////////////////////////////////////////////////////////////////////////////
static unsigned char fuzzbuf[1048576];
static int testCrypto()
{
unsigned char buf1[16384];
@ -156,27 +158,27 @@ static int testCrypto()
memset(buf2,0,sizeof(buf2));
memset(buf3,0,sizeof(buf3));
Salsa20 s20;
s20.init("12345678123456781234567812345678",256,"12345678");
s20.encrypt20(buf1,buf2,sizeof(buf1));
s20.init("12345678123456781234567812345678",256,"12345678");
s20.decrypt20(buf2,buf3,sizeof(buf2));
s20.init("12345678123456781234567812345678","12345678");
s20.crypt20(buf1,buf2,sizeof(buf1));
s20.init("12345678123456781234567812345678","12345678");
s20.crypt20(buf2,buf3,sizeof(buf2));
if (memcmp(buf1,buf3,sizeof(buf1))) {
std::cout << "FAIL (encrypt/decrypt test)" << std::endl;
return -1;
}
}
Salsa20 s20(s20TV0Key,256,s20TV0Iv);
Salsa20 s20(s20TV0Key,s20TV0Iv);
memset(buf1,0,sizeof(buf1));
memset(buf2,0,sizeof(buf2));
s20.encrypt20(buf1,buf2,64);
s20.crypt20(buf1,buf2,64);
if (memcmp(buf2,s20TV0Ks,64)) {
std::cout << "FAIL (test vector 0)" << std::endl;
return -1;
}
s20.init(s2012TV0Key,256,s2012TV0Iv);
s20.init(s2012TV0Key,s2012TV0Iv);
memset(buf1,0,sizeof(buf1));
memset(buf2,0,sizeof(buf2));
s20.encrypt12(buf1,buf2,64);
s20.crypt12(buf1,buf2,64);
if (memcmp(buf2,s2012TV0Ks,64)) {
std::cout << "FAIL (test vector 1)" << std::endl;
return -1;
@ -194,34 +196,68 @@ static int testCrypto()
unsigned char *bb = (unsigned char *)::malloc(1234567);
for(unsigned int i=0;i<1234567;++i)
bb[i] = (unsigned char)i;
Salsa20 s20(s20TV0Key,256,s20TV0Iv);
double bytes = 0.0;
Salsa20 s20(s20TV0Key,s20TV0Iv);
long double bytes = 0.0;
uint64_t start = OSUtils::now();
for(unsigned int i=0;i<200;++i) {
s20.encrypt12(bb,bb,1234567);
s20.crypt12(bb,bb,1234567);
bytes += 1234567.0;
}
uint64_t end = OSUtils::now();
SHA512::hash(buf1,bb,1234567);
std::cout << ((bytes / 1048576.0) / ((double)(end - start) / 1000.0)) << " MiB/second (" << Utils::hex(buf1,16) << ')' << std::endl;
std::cout << ((bytes / 1048576.0) / ((long double)(end - start) / 1024.0)) << " MiB/second (" << Utils::hex(buf1,16) << ')' << std::endl;
::free((void *)bb);
}
#ifdef ZT_USE_X64_ASM_SALSA2012
std::cout << "[crypto] Benchmarking Salsa20/12 fast x64 ASM... "; std::cout.flush();
{
unsigned char *bb = (unsigned char *)::malloc(1234567);
double bytes = 0.0;
uint64_t start = OSUtils::now();
for(unsigned int i=0;i<200;++i) {
zt_salsa2012_amd64_xmm6(bb,1234567,s20TV0Iv,s20TV0Key);
bytes += 1234567.0;
}
uint64_t end = OSUtils::now();
std::cout << ((bytes / 1048576.0) / ((double)(end - start) / 1024.0)) << " MiB/second" << std::endl;
::free((void *)bb);
}
#endif
#ifdef ZT_USE_ARM32_NEON_ASM_SALSA2012
if (zt_arm_has_neon()) {
std::cout << "[crypto] Benchmarking Salsa20/12 fast arm32/neon ASM... "; std::cout.flush();
{
unsigned char *bb = (unsigned char *)::malloc(1234567);
double bytes = 0.0;
uint64_t start = OSUtils::now();
for(unsigned int i=0;i<200;++i) {
zt_salsa2012_armneon3_xor(bb,(const unsigned char *)0,1234567,s20TV0Iv,s20TV0Key);
bytes += 1234567.0;
}
uint64_t end = OSUtils::now();
std::cout << ((bytes / 1048576.0) / ((double)(end - start) / 1024.0)) << " MiB/second" << std::endl;
::free((void *)bb);
}
}
#endif
std::cout << "[crypto] Benchmarking Salsa20/20... "; std::cout.flush();
{
unsigned char *bb = (unsigned char *)::malloc(1234567);
for(unsigned int i=0;i<1234567;++i)
bb[i] = (unsigned char)i;
Salsa20 s20(s20TV0Key,256,s20TV0Iv);
double bytes = 0.0;
Salsa20 s20(s20TV0Key,s20TV0Iv);
long double bytes = 0.0;
uint64_t start = OSUtils::now();
for(unsigned int i=0;i<200;++i) {
s20.encrypt20(bb,bb,1234567);
s20.crypt20(bb,bb,1234567);
bytes += 1234567.0;
}
uint64_t end = OSUtils::now();
SHA512::hash(buf1,bb,1234567);
std::cout << ((bytes / 1048576.0) / ((double)(end - start) / 1000.0)) << " MiB/second (" << Utils::hex(buf1,16) << ')' << std::endl;
std::cout << ((bytes / 1048576.0) / ((long double)(end - start) / 1024.0)) << " MiB/second (" << Utils::hex(buf1,16) << ')' << std::endl;
::free((void *)bb);
}
@ -251,14 +287,14 @@ static int testCrypto()
unsigned char *bb = (unsigned char *)::malloc(1234567);
for(unsigned int i=0;i<1234567;++i)
bb[i] = (unsigned char)i;
double bytes = 0.0;
long double bytes = 0.0;
uint64_t start = OSUtils::now();
for(unsigned int i=0;i<200;++i) {
Poly1305::compute(buf1,bb,1234567,poly1305TV0Key);
bytes += 1234567.0;
}
uint64_t end = OSUtils::now();
std::cout << ((bytes / 1048576.0) / ((double)(end - start) / 1000.0)) << " MiB/second" << std::endl;
std::cout << ((bytes / 1048576.0) / ((long double)(end - start) / 1000.0)) << " MiB/second" << std::endl;
::free((void *)bb);
}
@ -329,6 +365,17 @@ static int testCrypto()
}
std::cout << "PASS" << std::endl;
std::cout << "[crypto] Benchmarking C25519 ECC key agreement... "; std::cout.flush();
C25519::Pair bp[8];
for(int k=0;k<8;++k)
bp[k] = C25519::generate();
const uint64_t st = OSUtils::now();
for(unsigned int k=0;k<50;++k) {
C25519::agree(bp[~k & 7],bp[k & 7].pub,buf1,64);
}
const uint64_t et = OSUtils::now();
std::cout << ((double)(et - st) / 50.0) << "ms per agreement." << std::endl;
std::cout << "[crypto] Testing Ed25519 ECC signatures... "; std::cout.flush();
C25519::Pair didntSign = C25519::generate();
for(unsigned int i=0;i<10;++i) {
@ -378,11 +425,15 @@ static int testIdentity()
std::cout << "FAIL (1)" << std::endl;
return -1;
}
if (!id.locallyValidate()) {
std::cout << "FAIL (2)" << std::endl;
return -1;
const uint64_t vst = OSUtils::now();
for(int k=0;k<10;++k) {
if (!id.locallyValidate()) {
std::cout << "FAIL (2)" << std::endl;
return -1;
}
}
std::cout << "PASS" << std::endl;
const uint64_t vet = OSUtils::now();
std::cout << "PASS (" << ((double)(vet - vst) / 10.0) << "ms per validation)" << std::endl;
std::cout << "[identity] Validate known-bad identity... "; std::cout.flush();
if (!id.fromString(KNOWN_BAD_IDENTITY)) {
@ -505,19 +556,6 @@ static int testCertificate()
return -1;
}
std::cout << "[certificate] Testing string serialization... ";
CertificateOfMembership copyA(cA.toString());
CertificateOfMembership copyB(cB.toString());
if (copyA != cA) {
std::cout << "FAIL" << std::endl;
return -1;
}
if (copyB != cB) {
std::cout << "FAIL" << std::endl;
return -1;
}
std::cout << "PASS" << std::endl;
std::cout << "[certificate] Generating two certificates that should not agree...";
cA = CertificateOfMembership(10000,100,1,idA.address());
cB = CertificateOfMembership(10101,100,1,idB.address());
@ -573,7 +611,7 @@ static int testPacket()
return -1;
}
a.armor(salsaKey,true);
a.armor(salsaKey,true,0);
if (!a.dearmor(salsaKey)) {
std::cout << "FAIL (encrypt-decrypt/verify)" << std::endl;
return -1;
@ -583,8 +621,35 @@ static int testPacket()
return 0;
}
static void _testExcept(int &depth)
{
if (depth >= 16) {
throw std::runtime_error("LOL!");
} else {
++depth;
_testExcept(depth);
}
}
static int testOther()
{
std::cout << "[other] Testing C++ exceptions... "; std::cout.flush();
int depth = 0;
try {
_testExcept(depth);
} catch (std::runtime_error &e) {
if (depth == 16) {
std::cout << "OK" << std::endl;
} else {
std::cout << "ERROR (depth not 16)" << std::endl;
return -1;
}
} catch ( ... ) {
std::cout << "ERROR (exception not std::runtime_error)" << std::endl;
return -1;
}
#if 0
std::cout << "[other] Testing Hashtable... "; std::cout.flush();
{
Hashtable<uint64_t,std::string> ht;
@ -748,42 +813,29 @@ static int testOther()
}
}
std::cout << "PASS" << std::endl;
std::cout << "[other] Testing hex encode/decode... "; std::cout.flush();
for(unsigned int k=0;k<1000;++k) {
unsigned int flen = (rand() % 8194) + 1;
for(unsigned int i=0;i<flen;++i)
fuzzbuf[i] = (unsigned char)(rand() & 0xff);
std::string dec = Utils::unhex(Utils::hex(fuzzbuf,flen).c_str());
if ((dec.length() != flen)||(memcmp(dec.data(),fuzzbuf,dec.length()))) {
std::cout << "FAILED!" << std::endl;
std::cout << Utils::hex(fuzzbuf,flen) << std::endl;
std::cout << Utils::hex(dec.data(),(unsigned int)dec.length()) << std::endl;
return -1;
}
}
std::cout << "PASS" << std::endl;
#endif
std::cout << "[other] Testing/fuzzing Dictionary... "; std::cout.flush();
for(int k=0;k<1000;++k) {
Dictionary<8194> test;
Dictionary<8194> *test = new Dictionary<8194>();
char key[32][16];
char value[32][128];
memset(key, 0, sizeof(key));
memset(value, 0, sizeof(value));
for(unsigned int q=0;q<32;++q) {
Utils::snprintf(key[q],16,"%.8lx",(unsigned long)rand());
Utils::snprintf(key[q],16,"%.8lx",(unsigned long)(rand() % 1000) + (q * 1000));
int r = rand() % 128;
for(int x=0;x<r;++x)
value[q][x] = ("0123456789\0\t\r\n= ")[rand() % 16];
value[q][r] = (char)0;
test.add(key[q],value[q],r);
test->add(key[q],value[q],r);
}
for(unsigned int q=0;q<1024;++q) {
//int r = rand() % 128;
int r = 31;
int r = rand() % 32;
char tmp[128];
if (test.get(key[r],tmp,sizeof(tmp)) >= 0) {
if (test->get(key[r],tmp,sizeof(tmp)) >= 0) {
if (strcmp(value[r],tmp)) {
std::cout << "FAILED (invalid value)!" << std::endl;
std::cout << "FAILED (invalid value '" << value[r] << "' != '" << tmp << "')!" << std::endl;
return -1;
}
} else {
@ -791,36 +843,27 @@ static int testOther()
return -1;
}
}
for(unsigned int q=0;q<31;++q) {
char tmp[128];
test.erase(key[q]);
if (test.get(key[q],tmp,sizeof(tmp)) >= 0) {
std::cout << "FAILED (key should have been erased)!" << std::endl;
return -1;
}
if (test.get(key[q+1],tmp,sizeof(tmp)) < 0) {
std::cout << "FAILED (key should NOT have been erased)!" << std::endl;
return -1;
}
}
delete test;
}
int foo = 0;
volatile int *volatile bar = &foo; // force compiler not to optimize out test.get() below
for(int k=0;k<200;++k) {
int r = rand() % 8194;
unsigned char tmp[8194];
unsigned char *tmp = new unsigned char[8194];
for(int q=0;q<r;++q)
tmp[q] = (unsigned char)((rand() % 254) + 1); // don't put nulls since those will always just terminate scan
tmp[r] = (r % 32) ? (char)(rand() & 0xff) : (char)0; // every 32nd iteration don't terminate the string maybe...
Dictionary<8194> test((const char *)tmp);
Dictionary<8194> *test = new Dictionary<8194>((const char *)tmp);
for(unsigned int q=0;q<100;++q) {
char tmp[128];
for(unsigned int x=0;x<128;++x)
tmp[x] = (char)(rand() & 0xff);
tmp[127] = (char)0;
char value[8194];
*bar += test.get(tmp,value,sizeof(value));
*bar += test->get(tmp,value,sizeof(value));
}
delete test;
delete[] tmp;
}
std::cout << "PASS (junk value to prevent optimization-out of test: " << foo << ")" << std::endl;
@ -975,23 +1018,6 @@ static int testPhy()
return 0;
}
static int testResolver()
{
std::cout << "[resolver] Testing BackgroundResolver..."; std::cout.flush();
BackgroundResolver r("tcp-fallback.zerotier.com");
r.resolveNow();
r.wait();
std::vector<InetAddress> ips(r.get());
for(std::vector<InetAddress>::const_iterator ip(ips.begin());ip!=ips.end();++ip) {
std::cout << ' ' << ip->toString();
}
std::cout << std::endl;
return 0;
}
/*
static int testHttp()
{
@ -1038,7 +1064,7 @@ static int testHttp()
*/
#ifdef __WINDOWS__
int _tmain(int argc, _TCHAR* argv[])
int __cdecl _tmain(int argc, _TCHAR* argv[])
#else
int main(int argc,char **argv)
#endif
@ -1099,7 +1125,6 @@ int main(int argc,char **argv)
r |= testIdentity();
r |= testCertificate();
r |= testPhy();
r |= testResolver();
//r |= testHttp();
//*/