This commit is contained in:
Adam Ierymenko 2019-08-14 16:05:09 -07:00
parent 1b20cc6075
commit b23d551d00
No known key found for this signature in database
GPG Key ID: 1657198823E52A61
4 changed files with 20 additions and 32 deletions

View File

@ -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<const uint8_t *>(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;

View File

@ -106,7 +106,8 @@ static inline void U64TO8(unsigned char *p, unsigned long long v)
#define U64TO8(p,v) ((*reinterpret_cast<unsigned long long *>(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<const unsigned char *>(key));

View File

@ -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

View File

@ -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();