From b23d551d008c19ef63e0110be4076f53cc3a6fa9 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 14 Aug 2019 16:05:09 -0700 Subject: [PATCH] cleanup --- node/Packet.cpp | 8 ++++---- node/Poly1305.cpp | 11 +++++++---- node/Poly1305.hpp | 27 ++++++--------------------- selftest.cpp | 6 +++--- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/node/Packet.cpp b/node/Packet.cpp index 1c2ac86a7..8f38fe78c 100644 --- a/node/Packet.cpp +++ b/node/Packet.cpp @@ -905,7 +905,7 @@ void Packet::armor(const void *key,bool encryptPayload) ZT_FAST_SINGLE_PASS_SALSA2012(keyStream,encryptLen + 64,(data + ZT_PACKET_IDX_IV),mangledKey); Salsa20::memxor(data + ZT_PACKET_IDX_VERB,reinterpret_cast(keyStream + 8),encryptLen); uint64_t mac[2]; - Poly1305::compute(mac,data + ZT_PACKET_IDX_VERB,size() - ZT_PACKET_IDX_VERB,keyStream); + poly1305(mac,data + ZT_PACKET_IDX_VERB,size() - ZT_PACKET_IDX_VERB,keyStream); #ifdef ZT_NO_TYPE_PUNNING memcpy(data + ZT_PACKET_IDX_MAC,mac,8); #else @@ -920,7 +920,7 @@ void Packet::armor(const void *key,bool encryptPayload) if (encryptPayload) s20.crypt12(payload,payload,payloadLen); uint64_t mac[2]; - Poly1305::compute(mac,payload,payloadLen,macKey); + poly1305(mac,payload,payloadLen,macKey); memcpy(data + ZT_PACKET_IDX_MAC,mac,8); } } @@ -939,7 +939,7 @@ bool Packet::dearmor(const void *key) uint64_t keyStream[(ZT_PROTO_MAX_PACKET_LENGTH + 64 + 8) / 8]; ZT_FAST_SINGLE_PASS_SALSA2012(keyStream,((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) ? (payloadLen + 64) : 64),(data + ZT_PACKET_IDX_IV),mangledKey); uint64_t mac[2]; - Poly1305::compute(mac,payload,payloadLen,keyStream); + poly1305(mac,payload,payloadLen,keyStream); #ifdef ZT_NO_TYPE_PUNNING if (!Utils::secureEq(mac,data + ZT_PACKET_IDX_MAC,8)) return false; @@ -954,7 +954,7 @@ bool Packet::dearmor(const void *key) uint64_t macKey[4]; s20.crypt12(ZERO_KEY,macKey,sizeof(macKey)); uint64_t mac[2]; - Poly1305::compute(mac,payload,payloadLen,macKey); + poly1305(mac,payload,payloadLen,macKey); #ifdef ZT_NO_TYPE_PUNNING if (!Utils::secureEq(mac,data + ZT_PACKET_IDX_MAC,8)) return false; diff --git a/node/Poly1305.cpp b/node/Poly1305.cpp index 8563a5448..36ceaedfa 100644 --- a/node/Poly1305.cpp +++ b/node/Poly1305.cpp @@ -106,7 +106,8 @@ static inline void U64TO8(unsigned char *p, unsigned long long v) #define U64TO8(p,v) ((*reinterpret_cast(p)) = (v)) #endif -static inline void poly1305_init(poly1305_context *ctx, const unsigned char key[32]) { +static inline void poly1305_init(poly1305_context *ctx, const unsigned char key[32]) +{ poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; unsigned long long t0,t1; @@ -131,7 +132,8 @@ static inline void poly1305_init(poly1305_context *ctx, const unsigned char key[ st->final = 0; } -static inline void poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) { +static inline void poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) +{ const unsigned long long hibit = (st->final) ? 0 : ((unsigned long long)1 << 40); /* 1 << 128 */ unsigned long long r0,r1,r2; unsigned long long s1,s2; @@ -181,7 +183,8 @@ static inline void poly1305_blocks(poly1305_state_internal_t *st, const unsigned st->h[2] = h2; } -static inline void poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) { +static inline void poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) +{ poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx; unsigned long long h0,h1,h2,c; unsigned long long g0,g1,g2; @@ -505,7 +508,7 @@ static inline void poly1305_update(poly1305_context *ctx, const unsigned char *m } // anonymous namespace -void Poly1305::compute(void *auth,const void *data,unsigned int len,const void *key) +void poly1305(void *auth,const void *data,unsigned int len,const void *key) { poly1305_context ctx; poly1305_init(&ctx,reinterpret_cast(key)); diff --git a/node/Poly1305.hpp b/node/Poly1305.hpp index 4614826e7..cdba35ac1 100644 --- a/node/Poly1305.hpp +++ b/node/Poly1305.hpp @@ -33,29 +33,14 @@ namespace ZeroTier { #define ZT_POLY1305_MAC_LEN 16 /** - * Poly1305 one-time authentication code + * Compute a one-time authentication code * - * This takes a one-time-use 32-byte key and generates a 16-byte message - * authentication code. The key must never be re-used for a different - * message. - * - * In Packet this is done by using the first 32 bytes of the stream cipher - * keystream as a one-time-use key. These 32 bytes are then discarded and - * the packet is encrypted with the next N bytes. + * @param auth Buffer to receive code -- MUST be 16 bytes in length + * @param data Data to authenticate + * @param len Length of data to authenticate in bytes + * @param key 32-byte one-time use key to authenticate data (must not be reused) */ -class Poly1305 -{ -public: - /** - * Compute a one-time authentication code - * - * @param auth Buffer to receive code -- MUST be 16 bytes in length - * @param data Data to authenticate - * @param len Length of data to authenticate in bytes - * @param key 32-byte one-time use key to authenticate data (must not be reused) - */ - static void compute(void *auth,const void *data,unsigned int len,const void *key); -}; +void poly1305(void *auth,const void *data,unsigned int len,const void *key); } // namespace ZeroTier diff --git a/selftest.cpp b/selftest.cpp index 7bc96e3f3..2b7973da0 100644 --- a/selftest.cpp +++ b/selftest.cpp @@ -294,12 +294,12 @@ static int testCrypto() std::cout << "PASS" << std::endl; std::cout << "[crypto] Testing Poly1305... "; std::cout.flush(); - Poly1305::compute(buf1,poly1305TV0Input,sizeof(poly1305TV0Input),poly1305TV0Key); + poly1305(buf1,poly1305TV0Input,sizeof(poly1305TV0Input),poly1305TV0Key); if (memcmp(buf1,poly1305TV0Tag,16)) { std::cout << "FAIL (1)" << std::endl; return -1; } - Poly1305::compute(buf1,poly1305TV1Input,sizeof(poly1305TV1Input),poly1305TV1Key); + poly1305(buf1,poly1305TV1Input,sizeof(poly1305TV1Input),poly1305TV1Key); if (memcmp(buf1,poly1305TV1Tag,16)) { std::cout << "FAIL (2)" << std::endl; return -1; @@ -314,7 +314,7 @@ static int testCrypto() long double bytes = 0.0; uint64_t start = OSUtils::now(); for(unsigned int i=0;i<200;++i) { - Poly1305::compute(buf1,bb,1234567,poly1305TV0Key); + poly1305(buf1,bb,1234567,poly1305TV0Key); bytes += 1234567.0; } uint64_t end = OSUtils::now();