From 903a18db1da5341d45fd07dd6c54d196793e7de3 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 23 Aug 2019 08:41:22 -0700 Subject: [PATCH] work in progress --- node/IncomingPacket.cpp | 3 +++ node/Multicaster.cpp | 4 +++ node/SHA512.cpp | 11 +++++++++ node/SHA512.hpp | 55 +++++++++++++++++++++++++++++++++++++++++ selftest.cpp | 10 ++++++++ 5 files changed, 83 insertions(+) diff --git a/node/IncomingPacket.cpp b/node/IncomingPacket.cpp index fca4e13c8..9eee71d55 100644 --- a/node/IncomingPacket.cpp +++ b/node/IncomingPacket.cpp @@ -933,6 +933,8 @@ bool IncomingPacket::_doMULTICAST_FRAME(const RuntimeEnvironment *RR,void *tPtr, } if (!recipients.empty()) { + // TODO + /* const std::vector
anchors = network->config().anchors(); const bool amAnchor = (std::find(anchors.begin(),anchors.end(),RR->identity.address()) != anchors.end()); @@ -959,6 +961,7 @@ bool IncomingPacket::_doMULTICAST_FRAME(const RuntimeEnvironment *RR,void *tPtr, outp.append(field(afterRecipientsOffset,size() - afterRecipientsOffset),size() - afterRecipientsOffset); RR->sw->send(tPtr,outp,true); } + */ } if (gatherLimit) { // DEPRECATED but still supported diff --git a/node/Multicaster.cpp b/node/Multicaster.cpp index 50199b868..2c6ff6d8e 100644 --- a/node/Multicaster.cpp +++ b/node/Multicaster.cpp @@ -245,6 +245,7 @@ void Multicaster::send( explicitGatherPeers[numExplicitGatherPeers++] = network->controller(); + /* Address ac[ZT_MAX_NETWORK_SPECIALISTS]; const unsigned int accnt = network->config().alwaysContactAddresses(ac); unsigned int shuffled[ZT_MAX_NETWORK_SPECIALISTS]; @@ -263,7 +264,9 @@ void Multicaster::send( if (numExplicitGatherPeers == 16) break; } + */ + /* std::vector
anchors(network->config().anchors()); for(std::vector
::const_iterator a(anchors.begin());a!=anchors.end();++a) { if (*a != RR->identity.address()) { @@ -272,6 +275,7 @@ void Multicaster::send( break; } } + */ for(unsigned int k=0;kconfig().com) ? &(network->config().com) : (const CertificateOfMembership *)0) : (const CertificateOfMembership *)0; diff --git a/node/SHA512.cpp b/node/SHA512.cpp index 93956e6d4..ed46914ef 100644 --- a/node/SHA512.cpp +++ b/node/SHA512.cpp @@ -228,6 +228,17 @@ void SHA384(void *digest,const void *data,unsigned int len) memcpy(digest,tmp,48); } +void SHA384ab(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) +{ + uint8_t tmp[64]; + sha512_state state; + sha384_init(&state); + sha512_process(&state,(uint8_t *)data0,(unsigned long)len0); + sha512_process(&state,(uint8_t *)data1,(unsigned long)len1); + sha512_done(&state,tmp); + memcpy(digest,tmp,48); +} + } // namespace ZeroTier #endif // !ZT_HAVE_NATIVE_SHA512 diff --git a/node/SHA512.hpp b/node/SHA512.hpp index 1e37fffe7..36cdca47b 100644 --- a/node/SHA512.hpp +++ b/node/SHA512.hpp @@ -40,6 +40,11 @@ #define ZT_SHA512_DIGEST_LEN 64 #define ZT_SHA384_DIGEST_LEN 48 +#define ZT_SHA512_BLOCK_SIZE 128 +#define ZT_SHA384_BLOCK_SIZE 128 + +#define ZT_HMACSHA384_LEN 48 + namespace ZeroTier { #ifdef __APPLE__ @@ -58,6 +63,14 @@ static inline void SHA384(void *digest,const void *data,unsigned int len) CC_SHA384_Update(&ctx,data,len); CC_SHA384_Final(reinterpret_cast(digest),&ctx); } +static inline void SHA384ab(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) +{ + CC_SHA512_CTX ctx; + CC_SHA384_Init(&ctx); + CC_SHA384_Update(&ctx,data0,len0); + CC_SHA384_Update(&ctx,data1,len1); + CC_SHA384_Final(reinterpret_cast(digest),&ctx); +} #endif #ifdef ZT_USE_LIBCRYPTO @@ -76,13 +89,55 @@ static inline void SHA384(void *digest,const void *data,unsigned int len) SHA384_Update(&ctx,data,len); SHA384_Final(reinterpret_cast(digest),&ctx); } +static inline void SHA384ab(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1) +{ + SHA512_CTX ctx; + SHA384_Init(&ctx); + SHA384_Update(&ctx,data0,len0); + SHA384_Update(&ctx,data1,len1); + SHA384_Final(reinterpret_cast(digest),&ctx); +} #endif #ifndef ZT_HAVE_NATIVE_SHA512 void SHA512(void *digest,const void *data,unsigned int len); void SHA384(void *digest,const void *data,unsigned int len); +void SHA384ab(void *digest,const void *data0,unsigned int len0,const void *data1,unsigned int len1); #endif +static inline void HMACSHA384(const uint8_t key[32],const void *msg,const unsigned int msglen,uint8_t mac[48]) +{ + uint64_t kInPadded[16]; + uint64_t outer[22]; // output padded key | H(input padded key | msg) + +#ifdef ZT_NO_TYPE_PUNNING + for(int i=0;i<32;++i) ((uint8_t *)kInPadded)[i] = key[i] ^ 0x36; + for(int i=4;i<16;++i) kInPadded[i] = 0x3636363636363636ULL; + for(int i=0;i<32;++i) ((uint8_t *)outer)[i] = key[i] ^ 0x5c; + for(int i=4;i<16;++i) outer[i] = 0x5c5c5c5c5c5c5c5cULL; +#else + { + const uint64_t k0 = ((const uint64_t *)key)[0]; + const uint64_t k1 = ((const uint64_t *)key)[1]; + const uint64_t k2 = ((const uint64_t *)key)[2]; + const uint64_t k3 = ((const uint64_t *)key)[3]; + kInPadded[0] = k0 ^ 0x3636363636363636ULL; + kInPadded[0] = k1 ^ 0x3636363636363636ULL; + kInPadded[0] = k2 ^ 0x3636363636363636ULL; + kInPadded[0] = k3 ^ 0x3636363636363636ULL; + for(int i=4;i<16;++i) kInPadded[i] = 0x3636363636363636ULL; + outer[0] = k0 ^ 0x5c5c5c5c5c5c5c5cULL; + outer[1] = k1 ^ 0x5c5c5c5c5c5c5c5cULL; + outer[2] = k2 ^ 0x5c5c5c5c5c5c5c5cULL; + outer[3] = k3 ^ 0x5c5c5c5c5c5c5c5cULL; + for(int i=4;i<16;++i) outer[i] = 0x5c5c5c5c5c5c5c5cULL; + } +#endif + + SHA384ab(((uint8_t *)outer) + 128,kInPadded,128,msg,msglen); // H(input padded key | msg) + SHA384(mac,outer,176); // H(output padded key | H(input padded key | msg)) +} + } // namespace ZeroTier #endif diff --git a/selftest.cpp b/selftest.cpp index 991d22b6d..72715b7bf 100644 --- a/selftest.cpp +++ b/selftest.cpp @@ -342,6 +342,7 @@ static int testCrypto() } end = OSUtils::now(); std::cout << (uint64_t)(2000000.0 / ((double)(end - start) / 1000.0)) << " hashes/second" ZT_EOL_S; + std::cout << "[crypto] Testing SHA-384... "; std::cout.flush(); SHA384(buf1,sha512TV0Input,(unsigned int)strlen(sha512TV0Input)); if (memcmp(buf1,sha384TV0Digest,48)) { @@ -357,6 +358,15 @@ static int testCrypto() end = OSUtils::now(); std::cout << (uint64_t)(2000000.0 / ((double)(end - start) / 1000.0)) << " hashes/second" ZT_EOL_S; + std::cout << "[crypto] Benchmarking HMAC-SHA384 (2800 byte messages)... "; std::cout.flush(); + start = OSUtils::now(); + for(unsigned int i=0;i<200000;++i) { + HMACSHA384((const uint8_t *)hexbuf,buf1,2800,buf2); + hexbuf[0] = buf2[0]; // begone, optimizer! + } + end = OSUtils::now(); + std::cout << (uint64_t)(((200000.0 * 2800.0) / 1048576.0) / ((double)(end - start) / 1000.0)) << " MiB/second" ZT_EOL_S; + std::cout << "[crypto] Testing Poly1305... "; std::cout.flush(); poly1305(buf1,poly1305TV0Input,sizeof(poly1305TV0Input),poly1305TV0Key); if (memcmp(buf1,poly1305TV0Tag,16)) {