mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-01-29 15:43:52 +00:00
AES builds now
This commit is contained in:
parent
06730c7d1d
commit
3fd8efe642
@ -125,11 +125,13 @@ ifeq ($(CC_MACH),x86_64)
|
||||
ZT_ARCHITECTURE=2
|
||||
ZT_USE_X64_ASM_SALSA=1
|
||||
ZT_USE_X64_ASM_ED25519=1
|
||||
override CFLAGS+=-msse -msse2 -mssse3 -msse4 -msse4.1 -maes -mpclmul
|
||||
endif
|
||||
ifeq ($(CC_MACH),amd64)
|
||||
ZT_ARCHITECTURE=2
|
||||
ZT_USE_X64_ASM_SALSA=1
|
||||
ZT_USE_X64_ASM_ED25519=1
|
||||
override CFLAGS+=-msse -msse2 -mssse3 -msse4 -msse4.1 -maes -mpclmul
|
||||
endif
|
||||
ifeq ($(CC_MACH),powerpc64le)
|
||||
ZT_ARCHITECTURE=8
|
||||
|
@ -1,10 +1,9 @@
|
||||
|
||||
CC=clang
|
||||
CXX=clang++
|
||||
INCLUDES=
|
||||
DEFS=
|
||||
LIBS=
|
||||
ARCH_FLAGS=
|
||||
ARCH_FLAGS=-msse -msse2 -mssse3 -msse4 -msse4.1 -maes -mpclmul
|
||||
CODESIGN=echo
|
||||
PRODUCTSIGN=echo
|
||||
CODESIGN_APP_CERT=
|
||||
@ -67,7 +66,7 @@ endif
|
||||
# Debug mode -- dump trace output, build binary with -g
|
||||
ifeq ($(ZT_DEBUG),1)
|
||||
ZT_TRACE=1
|
||||
CFLAGS+=-Wall -g $(INCLUDES) $(DEFS)
|
||||
CFLAGS+=-Wall -g $(INCLUDES) $(DEFS) $(ARCH_FLAGS)
|
||||
STRIP=echo
|
||||
# The following line enables optimization for the crypto code, since
|
||||
# C25519 in particular is almost UNUSABLE in heavy testing without it.
|
||||
|
1669
node/AES.cpp
Normal file
1669
node/AES.cpp
Normal file
File diff suppressed because it is too large
Load Diff
580
node/AES.hpp
Normal file
580
node/AES.hpp
Normal file
@ -0,0 +1,580 @@
|
||||
/*
|
||||
* Copyright (c)2013-2020 ZeroTier, Inc.
|
||||
*
|
||||
* Use of this software is governed by the Business Source License included
|
||||
* in the LICENSE.TXT file in the project's root directory.
|
||||
*
|
||||
* Change Date: 2025-01-01
|
||||
*
|
||||
* On the date above, in accordance with the Business Source License, use
|
||||
* of this software will be governed by version 2.0 of the Apache License.
|
||||
*/
|
||||
/****/
|
||||
|
||||
#ifndef ZT_AES_HPP
|
||||
#define ZT_AES_HPP
|
||||
|
||||
#include "Constants.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "SHA512.hpp"
|
||||
|
||||
//#define ZT_AES_NO_ACCEL
|
||||
|
||||
#if !defined(ZT_AES_NO_ACCEL) && defined(ZT_ARCH_X64)
|
||||
#define ZT_AES_AESNI 1
|
||||
#endif
|
||||
#if !defined(ZT_AES_NO_ACCEL) && defined(ZT_ARCH_ARM_HAS_NEON)
|
||||
#define ZT_AES_NEON 1
|
||||
#endif
|
||||
|
||||
#ifndef ZT_INLINE
|
||||
#define ZT_INLINE inline
|
||||
#endif
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
* AES-256 and pals including GMAC, CTR, etc.
|
||||
*
|
||||
* This includes hardware acceleration for certain processors. The software
|
||||
* mode is fallback and is significantly slower.
|
||||
*/
|
||||
class AES
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @return True if this system has hardware AES acceleration
|
||||
*/
|
||||
static ZT_INLINE bool accelerated()
|
||||
{
|
||||
#ifdef ZT_AES_AESNI
|
||||
return Utils::CPUID.aes;
|
||||
#else
|
||||
#ifdef ZT_AES_NEON
|
||||
return Utils::ARMCAP.aes;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an un-initialized AES instance (must call init() before use)
|
||||
*/
|
||||
ZT_INLINE AES() noexcept
|
||||
{}
|
||||
|
||||
/**
|
||||
* Create an AES instance with the given key
|
||||
*
|
||||
* @param key 256-bit key
|
||||
*/
|
||||
explicit ZT_INLINE AES(const void *const key) noexcept
|
||||
{ this->init(key); }
|
||||
|
||||
ZT_INLINE ~AES()
|
||||
{ Utils::burn(&_k, sizeof(_k)); }
|
||||
|
||||
/**
|
||||
* Set (or re-set) this AES256 cipher's key
|
||||
*
|
||||
* @param key 256-bit / 32-byte key
|
||||
*/
|
||||
ZT_INLINE void init(const void *const key) noexcept
|
||||
{
|
||||
#ifdef ZT_AES_AESNI
|
||||
if (likely(Utils::CPUID.aes)) {
|
||||
_init_aesni(reinterpret_cast<const uint8_t *>(key));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef ZT_AES_NEON
|
||||
if (Utils::ARMCAP.aes) {
|
||||
_init_armneon_crypto(reinterpret_cast<const uint8_t *>(key));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
_initSW(reinterpret_cast<const uint8_t *>(key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a single AES block
|
||||
*
|
||||
* @param in Input block
|
||||
* @param out Output block (can be same as input)
|
||||
*/
|
||||
ZT_INLINE void encrypt(const void *const in, void *const out) const noexcept
|
||||
{
|
||||
#ifdef ZT_AES_AESNI
|
||||
if (likely(Utils::CPUID.aes)) {
|
||||
_encrypt_aesni(in, out);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef ZT_AES_NEON
|
||||
if (Utils::ARMCAP.aes) {
|
||||
_encrypt_armneon_crypto(in, out);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
_encryptSW(reinterpret_cast<const uint8_t *>(in), reinterpret_cast<uint8_t *>(out));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt a single AES block
|
||||
*
|
||||
* @param in Input block
|
||||
* @param out Output block (can be same as input)
|
||||
*/
|
||||
ZT_INLINE void decrypt(const void *const in, void *const out) const noexcept
|
||||
{
|
||||
#ifdef ZT_AES_AESNI
|
||||
if (likely(Utils::CPUID.aes)) {
|
||||
_decrypt_aesni(in, out);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
#ifdef ZT_AES_NEON
|
||||
if (Utils::ARMCAP.aes) {
|
||||
_decrypt_armneon_crypto(in, out);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
_decryptSW(reinterpret_cast<const uint8_t *>(in), reinterpret_cast<uint8_t *>(out));
|
||||
}
|
||||
|
||||
class GMACSIVEncryptor;
|
||||
class GMACSIVDecryptor;
|
||||
|
||||
/**
|
||||
* Streaming GMAC calculator
|
||||
*/
|
||||
class GMAC
|
||||
{
|
||||
friend class GMACSIVEncryptor;
|
||||
friend class GMACSIVDecryptor;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @return True if this system has hardware GMAC acceleration
|
||||
*/
|
||||
static ZT_INLINE bool accelerated()
|
||||
{
|
||||
#ifdef ZT_AES_AESNI
|
||||
return Utils::CPUID.aes;
|
||||
#else
|
||||
#ifdef ZT_AES_NEON
|
||||
return Utils::ARMCAP.pmull;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of GMAC (must be initialized with init() before use)
|
||||
*
|
||||
* @param aes Keyed AES instance to use
|
||||
*/
|
||||
ZT_INLINE GMAC(const AES &aes) : _aes(aes)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Reset and initialize for a new GMAC calculation
|
||||
*
|
||||
* @param iv 96-bit initialization vector (pad with zeroes if actual IV is shorter)
|
||||
*/
|
||||
ZT_INLINE void init(const uint8_t iv[12]) noexcept
|
||||
{
|
||||
_rp = 0;
|
||||
_len = 0;
|
||||
// We fill the least significant 32 bits in the _iv field with 1 since in GCM mode
|
||||
// this would hold the counter, but we're not doing GCM. The counter is therefore
|
||||
// always 1.
|
||||
#ifdef ZT_AES_AESNI // also implies an x64 processor
|
||||
*reinterpret_cast<uint64_t *>(_iv) = *reinterpret_cast<const uint64_t *>(iv);
|
||||
*reinterpret_cast<uint32_t *>(_iv + 8) = *reinterpret_cast<const uint64_t *>(iv + 8);
|
||||
*reinterpret_cast<uint32_t *>(_iv + 12) = 0x01000000; // 0x00000001 in big-endian byte order
|
||||
#else
|
||||
for(int i=0;i<12;++i)
|
||||
_iv[i] = iv[i];
|
||||
_iv[12] = 0;
|
||||
_iv[13] = 0;
|
||||
_iv[14] = 0;
|
||||
_iv[15] = 1;
|
||||
#endif
|
||||
_y[0] = 0;
|
||||
_y[1] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process data through GMAC
|
||||
*
|
||||
* @param data Bytes to process
|
||||
* @param len Length of input
|
||||
*/
|
||||
void update(const void *data, unsigned int len) noexcept;
|
||||
|
||||
/**
|
||||
* Process any remaining cached bytes and generate tag
|
||||
*
|
||||
* Don't call finish() more than once or you'll get an invalid result.
|
||||
*
|
||||
* @param tag 128-bit GMAC tag (can be truncated)
|
||||
*/
|
||||
void finish(uint8_t tag[16]) noexcept;
|
||||
|
||||
private:
|
||||
const AES &_aes;
|
||||
unsigned int _rp;
|
||||
unsigned int _len;
|
||||
uint8_t _r[16]; // remainder
|
||||
uint8_t _iv[16];
|
||||
uint64_t _y[2];
|
||||
};
|
||||
|
||||
/**
|
||||
* Streaming AES-CTR encrypt/decrypt
|
||||
*
|
||||
* NOTE: this doesn't support overflow of the counter in the least significant 32 bits.
|
||||
* AES-GMAC-CTR doesn't need this, so we don't support it as an optimization.
|
||||
*/
|
||||
class CTR
|
||||
{
|
||||
friend class GMACSIVEncryptor;
|
||||
friend class GMACSIVDecryptor;
|
||||
|
||||
public:
|
||||
ZT_INLINE CTR(const AES &aes) noexcept: _aes(aes)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Initialize this CTR instance to encrypt a new stream
|
||||
*
|
||||
* @param iv Unique initialization vector and initial 32-bit counter (least significant 32 bits, big-endian)
|
||||
* @param output Buffer to which to store output (MUST be large enough for total bytes processed!)
|
||||
*/
|
||||
ZT_INLINE void init(const uint8_t iv[16], void *const output) noexcept
|
||||
{
|
||||
Utils::copy< 16 >(_ctr, iv);
|
||||
_out = reinterpret_cast<uint8_t *>(output);
|
||||
_len = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize this CTR instance to encrypt a new stream
|
||||
*
|
||||
* @param iv Unique initialization vector
|
||||
* @param ic Initial counter (must be in big-endian byte order!)
|
||||
* @param output Buffer to which to store output (MUST be large enough for total bytes processed!)
|
||||
*/
|
||||
ZT_INLINE void init(const uint8_t iv[12], const uint32_t ic, void *const output) noexcept
|
||||
{
|
||||
Utils::copy< 12 >(_ctr, iv);
|
||||
reinterpret_cast<uint32_t *>(_ctr)[3] = ic;
|
||||
_out = reinterpret_cast<uint8_t *>(output);
|
||||
_len = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt or decrypt data, writing result to the output provided to init()
|
||||
*
|
||||
* @param input Input data
|
||||
* @param len Length of input
|
||||
*/
|
||||
void crypt(const void *input, unsigned int len) noexcept;
|
||||
|
||||
/**
|
||||
* Finish any remaining bytes if total bytes processed wasn't a multiple of 16
|
||||
*
|
||||
* Don't call more than once for a given stream or data may be corrupted.
|
||||
*/
|
||||
void finish() noexcept;
|
||||
|
||||
private:
|
||||
const AES &_aes;
|
||||
uint64_t _ctr[2];
|
||||
uint8_t *_out;
|
||||
unsigned int _len;
|
||||
};
|
||||
|
||||
/**
|
||||
* Encryptor for AES-GMAC-SIV.
|
||||
*
|
||||
* Encryption requires two passes. The first pass starts after init
|
||||
* with aad (if any) followed by update1() and finish1(). Then the
|
||||
* update2() and finish2() methods must be used over the same data
|
||||
* (but NOT AAD) again.
|
||||
*
|
||||
* This supports encryption of a maximum of 2^31 bytes of data per
|
||||
* call to init().
|
||||
*/
|
||||
class GMACSIVEncryptor
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Create a new AES-GMAC-SIV encryptor keyed with the provided AES instances
|
||||
*
|
||||
* @param k0 First of two AES instances keyed with K0
|
||||
* @param k1 Second of two AES instances keyed with K1
|
||||
*/
|
||||
ZT_INLINE GMACSIVEncryptor(const AES &k0, const AES &k1) noexcept:
|
||||
_gmac(k0),
|
||||
_ctr(k1)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Initialize AES-GMAC-SIV
|
||||
*
|
||||
* @param iv IV in network byte order (byte order in which it will appear on the wire)
|
||||
* @param output Pointer to buffer to receive ciphertext, must be large enough for all to-be-processed data!
|
||||
*/
|
||||
ZT_INLINE void init(const uint64_t iv, void *const output) noexcept
|
||||
{
|
||||
// Output buffer to receive the result of AES-CTR encryption.
|
||||
_output = output;
|
||||
|
||||
// Initialize GMAC with 64-bit IV (and remaining 32 bits padded to zero).
|
||||
_tag[0] = iv;
|
||||
_tag[1] = 0;
|
||||
_gmac.init(reinterpret_cast<const uint8_t *>(_tag));
|
||||
}
|
||||
|
||||
/**
|
||||
* Process AAD (additional authenticated data) that is not being encrypted.
|
||||
*
|
||||
* If such data exists this must be called before update1() and finish1().
|
||||
*
|
||||
* Note: current code only supports one single chunk of AAD. Don't call this
|
||||
* multiple times per message.
|
||||
*
|
||||
* @param aad Additional authenticated data
|
||||
* @param len Length of AAD in bytes
|
||||
*/
|
||||
ZT_INLINE void aad(const void *const aad, unsigned int len) noexcept
|
||||
{
|
||||
// Feed ADD into GMAC first
|
||||
_gmac.update(aad, len);
|
||||
|
||||
// End of AAD is padded to a multiple of 16 bytes to ensure unique encoding.
|
||||
len &= 0xfU;
|
||||
if (len != 0)
|
||||
_gmac.update(Utils::ZERO256, 16 - len);
|
||||
}
|
||||
|
||||
/**
|
||||
* First pass plaintext input function
|
||||
*
|
||||
* @param input Plaintext chunk
|
||||
* @param len Length of plaintext chunk
|
||||
*/
|
||||
ZT_INLINE void update1(const void *const input, const unsigned int len) noexcept
|
||||
{ _gmac.update(input, len); }
|
||||
|
||||
/**
|
||||
* Finish first pass, compute CTR IV, initialize second pass.
|
||||
*/
|
||||
ZT_INLINE void finish1() noexcept
|
||||
{
|
||||
uint64_t tmp[2];
|
||||
|
||||
// Compute 128-bit GMAC tag.
|
||||
_gmac.finish(reinterpret_cast<uint8_t *>(tmp));
|
||||
|
||||
// Shorten to 64 bits, concatenate with message IV, and encrypt with AES to
|
||||
// yield the CTR IV and opaque IV/MAC blob. In ZeroTier's use of GMAC-SIV
|
||||
// this get split into the packet ID (64 bits) and the MAC (64 bits) in each
|
||||
// packet and then recombined on receipt for legacy reasons (but with no
|
||||
// cryptographic or performance impact).
|
||||
_tag[1] = tmp[0] ^ tmp[1];
|
||||
_ctr._aes.encrypt(_tag, _tag);
|
||||
|
||||
// Initialize CTR with 96-bit CTR nonce and 32-bit counter. The counter
|
||||
// incorporates 31 more bits of entropy which should raise our security margin
|
||||
// a bit, but this is not included in the worst case analysis of GMAC-SIV.
|
||||
// The most significant bit of the counter is masked to zero to allow up to
|
||||
// 2^31 bytes to be encrypted before the counter loops. Some CTR implementations
|
||||
// increment the whole big-endian 128-bit integer in which case this could be
|
||||
// used for more than 2^31 bytes, but ours does not for performance reasons
|
||||
// and so 2^31 should be considered the input limit.
|
||||
tmp[0] = _tag[0];
|
||||
tmp[1] = _tag[1] & ZT_CONST_TO_BE_UINT64(0xffffffff7fffffffULL);
|
||||
_ctr.init(reinterpret_cast<const uint8_t *>(tmp), _output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Second pass plaintext input function
|
||||
*
|
||||
* The same plaintext must be fed in the second time in the same order,
|
||||
* though chunk boundaries do not have to be the same.
|
||||
*
|
||||
* @param input Plaintext chunk
|
||||
* @param len Length of plaintext chunk
|
||||
*/
|
||||
ZT_INLINE void update2(const void *const input, const unsigned int len) noexcept
|
||||
{ _ctr.crypt(input, len); }
|
||||
|
||||
/**
|
||||
* Finish second pass and return a pointer to the opaque 128-bit IV+MAC block
|
||||
*
|
||||
* The returned pointer remains valid as long as this object exists and init()
|
||||
* is not called again.
|
||||
*
|
||||
* @return Pointer to 128-bit opaque IV+MAC (packed into two 64-bit integers)
|
||||
*/
|
||||
ZT_INLINE const uint64_t *finish2()
|
||||
{
|
||||
_ctr.finish();
|
||||
return _tag;
|
||||
}
|
||||
|
||||
private:
|
||||
void *_output;
|
||||
uint64_t _tag[2];
|
||||
AES::GMAC _gmac;
|
||||
AES::CTR _ctr;
|
||||
};
|
||||
|
||||
/**
|
||||
* Decryptor for AES-GMAC-SIV.
|
||||
*
|
||||
* GMAC-SIV decryption is single-pass. AAD (if any) must be processed first.
|
||||
*/
|
||||
class GMACSIVDecryptor
|
||||
{
|
||||
public:
|
||||
ZT_INLINE GMACSIVDecryptor(const AES &k0, const AES &k1) noexcept:
|
||||
_ctr(k1),
|
||||
_gmac(k0)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Initialize decryptor for a new message
|
||||
*
|
||||
* @param tag 128-bit combined IV/MAC originally created by GMAC-SIV encryption
|
||||
* @param output Buffer in which to write output plaintext (must be large enough!)
|
||||
*/
|
||||
ZT_INLINE void init(const uint64_t tag[2], void *const output) noexcept
|
||||
{
|
||||
uint64_t tmp[2];
|
||||
tmp[0] = tag[0];
|
||||
tmp[1] = tag[1] & ZT_CONST_TO_BE_UINT64(0xffffffff7fffffffULL);
|
||||
_ctr.init(reinterpret_cast<const uint8_t *>(tmp), output);
|
||||
|
||||
_ctr._aes.decrypt(tag, _ivMac);
|
||||
|
||||
tmp[0] = _ivMac[0];
|
||||
tmp[1] = 0;
|
||||
_gmac.init(reinterpret_cast<const uint8_t *>(tmp));
|
||||
|
||||
_output = output;
|
||||
_decryptedLen = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process AAD (additional authenticated data) that wasn't encrypted
|
||||
*
|
||||
* @param aad Additional authenticated data
|
||||
* @param len Length of AAD in bytes
|
||||
*/
|
||||
ZT_INLINE void aad(const void *const aad, unsigned int len) noexcept
|
||||
{
|
||||
_gmac.update(aad, len);
|
||||
len &= 0xfU;
|
||||
if (len != 0)
|
||||
_gmac.update(Utils::ZERO256, 16 - len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Feed ciphertext into the decryptor
|
||||
*
|
||||
* Unlike encryption, GMAC-SIV decryption requires only one pass.
|
||||
*
|
||||
* @param input Input ciphertext
|
||||
* @param len Length of ciphertext
|
||||
*/
|
||||
ZT_INLINE void update(const void *const input, const unsigned int len) noexcept
|
||||
{
|
||||
_ctr.crypt(input, len);
|
||||
_decryptedLen += len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush decryption, compute MAC, and verify
|
||||
*
|
||||
* @return True if resulting plaintext (and AAD) pass message authentication check
|
||||
*/
|
||||
ZT_INLINE bool finish() noexcept
|
||||
{
|
||||
_ctr.finish();
|
||||
|
||||
uint64_t gmacTag[2];
|
||||
_gmac.update(_output, _decryptedLen);
|
||||
_gmac.finish(reinterpret_cast<uint8_t *>(gmacTag));
|
||||
return (gmacTag[0] ^ gmacTag[1]) == _ivMac[1];
|
||||
}
|
||||
|
||||
private:
|
||||
uint64_t _ivMac[2];
|
||||
AES::CTR _ctr;
|
||||
AES::GMAC _gmac;
|
||||
void *_output;
|
||||
unsigned int _decryptedLen;
|
||||
};
|
||||
|
||||
private:
|
||||
static const uint32_t Te0[256];
|
||||
static const uint32_t Te4[256];
|
||||
static const uint32_t Td0[256];
|
||||
static const uint8_t Td4[256];
|
||||
static const uint32_t rcon[15];
|
||||
|
||||
void _initSW(const uint8_t key[32]) noexcept;
|
||||
void _encryptSW(const uint8_t in[16], uint8_t out[16]) const noexcept;
|
||||
void _decryptSW(const uint8_t in[16], uint8_t out[16]) const noexcept;
|
||||
|
||||
union
|
||||
{
|
||||
#ifdef ZT_AES_AESNI
|
||||
struct
|
||||
{
|
||||
__m128i k[28];
|
||||
__m128i h[4]; // h, hh, hhh, hhhh
|
||||
__m128i h2[4]; // _mm_xor_si128(_mm_shuffle_epi32(h, 78), h), etc.
|
||||
} ni;
|
||||
#endif
|
||||
|
||||
#ifdef ZT_AES_NEON
|
||||
struct
|
||||
{
|
||||
uint64_t hsw[2]; // in case it has AES but not PMULL, not sure if that ever happens
|
||||
uint8x16_t ek[15];
|
||||
uint8x16_t dk[15];
|
||||
uint8x16_t h;
|
||||
} neon;
|
||||
#endif
|
||||
|
||||
struct
|
||||
{
|
||||
uint64_t h[2];
|
||||
uint32_t ek[60];
|
||||
uint32_t dk[60];
|
||||
} sw;
|
||||
} _k;
|
||||
|
||||
#ifdef ZT_AES_AESNI
|
||||
void _init_aesni(const uint8_t key[32]) noexcept;
|
||||
void _encrypt_aesni(const void *in, void *out) const noexcept;
|
||||
void _decrypt_aesni(const void *in, void *out) const noexcept;
|
||||
#endif
|
||||
|
||||
#ifdef ZT_AES_NEON
|
||||
void _init_armneon_crypto(const uint8_t key[32]) noexcept;
|
||||
void _encrypt_armneon_crypto(const void *in, void *out) const noexcept;
|
||||
void _decrypt_armneon_crypto(const void *in, void *out) const noexcept;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
||||
#endif
|
@ -29,6 +29,12 @@
|
||||
// Also makes sure __BYTE_ORDER is defined reasonably.
|
||||
//
|
||||
|
||||
#ifndef ZT_INLINE
|
||||
#define ZT_INLINE inline
|
||||
#endif
|
||||
|
||||
#define restrict
|
||||
|
||||
// Hack: make sure __GCC__ is defined on old GCC compilers
|
||||
#ifndef __GCC__
|
||||
#if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
|
||||
@ -96,6 +102,15 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
|
||||
#define ZT_ARCH_X64 1
|
||||
#include <xmmintrin.h>
|
||||
#include <emmintrin.h>
|
||||
#include <immintrin.h>
|
||||
#include <tmmintrin.h>
|
||||
#include <mmintrin.h>
|
||||
#endif
|
||||
|
||||
// Define ZT_NO_TYPE_PUNNING to disable reckless casts on anything other than x86/x64.
|
||||
#if (!(defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_AMD64) || defined(_M_X64) || defined(i386) || defined(__i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86) || defined(__X86__) || defined(_X86_) || defined(__I86__) || defined(__INTEL__) || defined(__386)))
|
||||
#ifndef ZT_NO_TYPE_PUNNING
|
||||
|
@ -40,8 +40,87 @@
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
const uint64_t Utils::ZERO256[4] = {0ULL,0ULL,0ULL,0ULL};
|
||||
|
||||
const char Utils::HEXCHARS[16] = { '0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f' };
|
||||
|
||||
#ifdef ZT_ARCH_ARM_HAS_NEON
|
||||
Utils::ARMCapabilities::ARMCapabilities() noexcept
|
||||
{
|
||||
#ifdef HWCAP2_AES
|
||||
if (sizeof(void *) == 4) {
|
||||
const long hwcaps2 = getauxval(AT_HWCAP2);
|
||||
this->aes = (hwcaps2 & HWCAP2_AES) != 0;
|
||||
this->crc32 = (hwcaps2 & HWCAP2_CRC32) != 0;
|
||||
this->pmull = (hwcaps2 & HWCAP2_PMULL) != 0;
|
||||
this->sha1 = (hwcaps2 & HWCAP2_SHA1) != 0;
|
||||
this->sha2 = (hwcaps2 & HWCAP2_SHA2) != 0;
|
||||
} else {
|
||||
#endif
|
||||
const long hwcaps = getauxval(AT_HWCAP);
|
||||
this->aes = (hwcaps & HWCAP_AES) != 0;
|
||||
this->crc32 = (hwcaps & HWCAP_CRC32) != 0;
|
||||
this->pmull = (hwcaps & HWCAP_PMULL) != 0;
|
||||
this->sha1 = (hwcaps & HWCAP_SHA1) != 0;
|
||||
this->sha2 = (hwcaps & HWCAP_SHA2) != 0;
|
||||
#ifdef HWCAP2_AES
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
const Utils::ARMCapabilities Utils::ARMCAP;
|
||||
#endif
|
||||
|
||||
#ifdef ZT_ARCH_X64
|
||||
|
||||
Utils::CPUIDRegisters::CPUIDRegisters() noexcept
|
||||
{
|
||||
uint32_t eax, ebx, ecx, edx;
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
int regs[4];
|
||||
__cpuid(regs,1);
|
||||
eax = (uint32_t)regs[0];
|
||||
ebx = (uint32_t)regs[1];
|
||||
ecx = (uint32_t)regs[2];
|
||||
edx = (uint32_t)regs[3];
|
||||
#else
|
||||
__asm__ __volatile__ (
|
||||
"cpuid"
|
||||
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
|
||||
: "a"(1), "c"(0)
|
||||
);
|
||||
#endif
|
||||
|
||||
rdrand = ((ecx & (1U << 30U)) != 0);
|
||||
aes = (((ecx & (1U << 25U)) != 0) && ((ecx & (1U << 19U)) != 0) && ((ecx & (1U << 1U)) != 0));
|
||||
avx = ((ecx & (1U << 25U)) != 0);
|
||||
|
||||
#ifdef __WINDOWS__
|
||||
__cpuid(regs,7);
|
||||
eax = (uint32_t)regs[0];
|
||||
ebx = (uint32_t)regs[1];
|
||||
ecx = (uint32_t)regs[2];
|
||||
edx = (uint32_t)regs[3];
|
||||
#else
|
||||
__asm__ __volatile__ (
|
||||
"cpuid"
|
||||
: "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx)
|
||||
: "a"(7), "c"(0)
|
||||
);
|
||||
#endif
|
||||
|
||||
vaes = aes && avx && ((ecx & (1U << 9U)) != 0);
|
||||
vpclmulqdq = aes && avx && ((ecx & (1U << 10U)) != 0);
|
||||
avx2 = avx && ((ebx & (1U << 5U)) != 0);
|
||||
avx512f = avx && ((ebx & (1U << 16U)) != 0);
|
||||
sha = ((ebx & (1U << 29U)) != 0);
|
||||
fsrm = ((edx & (1U << 4U)) != 0);
|
||||
}
|
||||
|
||||
const Utils::CPUIDRegisters Utils::CPUID;
|
||||
#endif
|
||||
|
||||
// Crazy hack to force memory to be securely zeroed in spite of the best efforts of optimizing compilers.
|
||||
static void _Utils_doBurn(volatile uint8_t *ptr,unsigned int len)
|
||||
{
|
||||
|
444
node/Utils.hpp
444
node/Utils.hpp
@ -31,6 +31,27 @@
|
||||
|
||||
#include "Constants.hpp"
|
||||
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)((uint16_t)((uint16_t)(x) << 8U) | (uint16_t)((uint16_t)(x) >> 8U)))
|
||||
#define ZT_CONST_TO_BE_UINT64(x) ( \
|
||||
(((uint64_t)(x) & 0x00000000000000ffULL) << 56U) | \
|
||||
(((uint64_t)(x) & 0x000000000000ff00ULL) << 40U) | \
|
||||
(((uint64_t)(x) & 0x0000000000ff0000ULL) << 24U) | \
|
||||
(((uint64_t)(x) & 0x00000000ff000000ULL) << 8U) | \
|
||||
(((uint64_t)(x) & 0x000000ff00000000ULL) >> 8U) | \
|
||||
(((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24U) | \
|
||||
(((uint64_t)(x) & 0x00ff000000000000ULL) >> 40U) | \
|
||||
(((uint64_t)(x) & 0xff00000000000000ULL) >> 56U))
|
||||
#else
|
||||
#define ZT_CONST_TO_BE_UINT16(x) ((uint16_t)(x))
|
||||
#define ZT_CONST_TO_BE_UINT64(x) ((uint64_t)(x))
|
||||
#endif
|
||||
|
||||
#define ZT_ROR64(x, r) (((x) >> (r)) | ((x) << (64 - (r))))
|
||||
#define ZT_ROL64(x, r) (((x) << (r)) | ((x) >> (64 - (r))))
|
||||
#define ZT_ROR32(x, r) (((x) >> (r)) | ((x) << (32 - (r))))
|
||||
#define ZT_ROL32(x, r) (((x) << (r)) | ((x) >> (32 - (r))))
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
@ -39,6 +60,40 @@ namespace ZeroTier {
|
||||
class Utils
|
||||
{
|
||||
public:
|
||||
static const uint64_t ZERO256[4];
|
||||
|
||||
#ifdef ZT_ARCH_ARM_HAS_NEON
|
||||
struct ARMCapabilities
|
||||
{
|
||||
ARMCapabilities() noexcept;
|
||||
|
||||
bool aes;
|
||||
bool crc32;
|
||||
bool pmull;
|
||||
bool sha1;
|
||||
bool sha2;
|
||||
};
|
||||
static const ARMCapabilities ARMCAP;
|
||||
#endif
|
||||
|
||||
#ifdef ZT_ARCH_X64
|
||||
struct CPUIDRegisters
|
||||
{
|
||||
CPUIDRegisters() noexcept;
|
||||
|
||||
bool rdrand;
|
||||
bool aes;
|
||||
bool avx;
|
||||
bool vaes; // implies AVX
|
||||
bool vpclmulqdq; // implies AVX
|
||||
bool avx2;
|
||||
bool avx512f;
|
||||
bool sha;
|
||||
bool fsrm;
|
||||
};
|
||||
static const CPUIDRegisters CPUID;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Perform a time-invariant binary comparison
|
||||
*
|
||||
@ -363,72 +418,341 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
// Byte swappers for big/little endian conversion
|
||||
static inline uint8_t hton(uint8_t n) { return n; }
|
||||
static inline int8_t hton(int8_t n) { return n; }
|
||||
static inline uint16_t hton(uint16_t n) { return htons(n); }
|
||||
static inline int16_t hton(int16_t n) { return (int16_t)htons((uint16_t)n); }
|
||||
static inline uint32_t hton(uint32_t n) { return htonl(n); }
|
||||
static inline int32_t hton(int32_t n) { return (int32_t)htonl((uint32_t)n); }
|
||||
static inline uint64_t hton(uint64_t n)
|
||||
/**
|
||||
* Unconditionally swap bytes regardless of host byte order
|
||||
*
|
||||
* @param n Integer to swap
|
||||
* @return Integer with bytes reversed
|
||||
*/
|
||||
static ZT_INLINE uint16_t swapBytes(const uint16_t n) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#if defined(__GNUC__)
|
||||
#if defined(__FreeBSD__)
|
||||
return bswap64(n);
|
||||
#elif (!defined(__OpenBSD__))
|
||||
return __builtin_bswap64(n);
|
||||
#endif
|
||||
#else
|
||||
return (
|
||||
((n & 0x00000000000000FFULL) << 56) |
|
||||
((n & 0x000000000000FF00ULL) << 40) |
|
||||
((n & 0x0000000000FF0000ULL) << 24) |
|
||||
((n & 0x00000000FF000000ULL) << 8) |
|
||||
((n & 0x000000FF00000000ULL) >> 8) |
|
||||
((n & 0x0000FF0000000000ULL) >> 24) |
|
||||
((n & 0x00FF000000000000ULL) >> 40) |
|
||||
((n & 0xFF00000000000000ULL) >> 56)
|
||||
);
|
||||
#endif
|
||||
#else
|
||||
return n;
|
||||
#endif
|
||||
#if defined(__GNUC__)
|
||||
return __builtin_bswap16(n);
|
||||
#else
|
||||
#ifdef _MSC_VER
|
||||
return (uint16_t)_byteswap_ushort((unsigned short)n);
|
||||
#else
|
||||
return htons(n);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
static inline int64_t hton(int64_t n) { return (int64_t)hton((uint64_t)n); }
|
||||
|
||||
static inline uint8_t ntoh(uint8_t n) { return n; }
|
||||
static inline int8_t ntoh(int8_t n) { return n; }
|
||||
static inline uint16_t ntoh(uint16_t n) { return ntohs(n); }
|
||||
static inline int16_t ntoh(int16_t n) { return (int16_t)ntohs((uint16_t)n); }
|
||||
static inline uint32_t ntoh(uint32_t n) { return ntohl(n); }
|
||||
static inline int32_t ntoh(int32_t n) { return (int32_t)ntohl((uint32_t)n); }
|
||||
static inline uint64_t ntoh(uint64_t n)
|
||||
// These are helper adapters to load and swap integer types special cased by size
|
||||
// to work with all typedef'd variants, signed/unsigned, etc.
|
||||
template< typename I, unsigned int S >
|
||||
class _swap_bytes_bysize;
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 1 >
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
#if defined(__GNUC__)
|
||||
#if defined(__FreeBSD__)
|
||||
return bswap64(n);
|
||||
#elif (!defined(__OpenBSD__))
|
||||
return __builtin_bswap64(n);
|
||||
#endif
|
||||
#else
|
||||
return (
|
||||
((n & 0x00000000000000FFULL) << 56) |
|
||||
((n & 0x000000000000FF00ULL) << 40) |
|
||||
((n & 0x0000000000FF0000ULL) << 24) |
|
||||
((n & 0x00000000FF000000ULL) << 8) |
|
||||
((n & 0x000000FF00000000ULL) >> 8) |
|
||||
((n & 0x0000FF0000000000ULL) >> 24) |
|
||||
((n & 0x00FF000000000000ULL) >> 40) |
|
||||
((n & 0xFF00000000000000ULL) >> 56)
|
||||
);
|
||||
#endif
|
||||
#else
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return n; }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 2 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return (I)swapBytes((uint16_t)n); }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 4 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return (I)swapBytes((uint32_t)n); }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _swap_bytes_bysize< I, 8 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I s(const I n) noexcept
|
||||
{ return (I)swapBytes((uint64_t)n); }
|
||||
};
|
||||
|
||||
template< typename I, unsigned int S >
|
||||
class _load_be_bysize;
|
||||
|
||||
template< typename I >
|
||||
class _load_be_bysize< I, 1 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return p[0]; }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _load_be_bysize< I, 2 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return (I)(((unsigned int)p[0] << 8U) | (unsigned int)p[1]); }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _load_be_bysize< I, 4 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return (I)(((uint32_t)p[0] << 24U) | ((uint32_t)p[1] << 16U) | ((uint32_t)p[2] << 8U) | (uint32_t)p[3]); }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _load_be_bysize< I, 8 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return (I)(((uint64_t)p[0] << 56U) | ((uint64_t)p[1] << 48U) | ((uint64_t)p[2] << 40U) | ((uint64_t)p[3] << 32U) | ((uint64_t)p[4] << 24U) | ((uint64_t)p[5] << 16U) | ((uint64_t)p[6] << 8U) | (uint64_t)p[7]); }
|
||||
};
|
||||
|
||||
template< typename I, unsigned int S >
|
||||
class _load_le_bysize;
|
||||
|
||||
template< typename I >
|
||||
class _load_le_bysize< I, 1 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return p[0]; }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _load_le_bysize< I, 2 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return (I)((unsigned int)p[0] | ((unsigned int)p[1] << 8U)); }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _load_le_bysize< I, 4 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return (I)((uint32_t)p[0] | ((uint32_t)p[1] << 8U) | ((uint32_t)p[2] << 16U) | ((uint32_t)p[3] << 24U)); }
|
||||
};
|
||||
|
||||
template< typename I >
|
||||
class _load_le_bysize< I, 8 >
|
||||
{
|
||||
public:
|
||||
static ZT_INLINE I l(const uint8_t *const p) noexcept
|
||||
{ return (I)((uint64_t)p[0] | ((uint64_t)p[1] << 8U) | ((uint64_t)p[2] << 16U) | ((uint64_t)p[3] << 24U) | ((uint64_t)p[4] << 32U) | ((uint64_t)p[5] << 40U) | ((uint64_t)p[6] << 48U) | ((uint64_t)p[7]) << 56U); }
|
||||
};
|
||||
|
||||
/**
|
||||
* Convert any signed or unsigned integer type to big-endian ("network") byte order
|
||||
*
|
||||
* @tparam I Integer type (usually inferred)
|
||||
* @param n Value to convert
|
||||
* @return Value in big-endian order
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I hton(const I n) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return _swap_bytes_bysize< I, sizeof(I) >::s(n);
|
||||
#else
|
||||
return n;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert any signed or unsigned integer type to host byte order from big-endian ("network") byte order
|
||||
*
|
||||
* @tparam I Integer type (usually inferred)
|
||||
* @param n Value to convert
|
||||
* @return Value in host byte order
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I ntoh(const I n) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
||||
return _swap_bytes_bysize< I, sizeof(I) >::s(n);
|
||||
#else
|
||||
return n;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy bits from memory into an integer type without modifying their order
|
||||
*
|
||||
* @tparam I Type to load
|
||||
* @param p Byte stream, must be at least sizeof(I) in size
|
||||
* @return Loaded raw integer
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I loadMachineEndian(const void *const p) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
I tmp;
|
||||
for(int i=0;i<(int)sizeof(I);++i)
|
||||
reinterpret_cast<uint8_t *>(&tmp)[i] = reinterpret_cast<const uint8_t *>(p)[i];
|
||||
return tmp;
|
||||
#else
|
||||
return *reinterpret_cast<const I *>(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy bits from memory into an integer type without modifying their order
|
||||
*
|
||||
* @tparam I Type to store
|
||||
* @param p Byte array (must be at least sizeof(I))
|
||||
* @param i Integer to store
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE void storeMachineEndian(void *const p, const I i) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
for(unsigned int k=0;k<sizeof(I);++k)
|
||||
reinterpret_cast<uint8_t *>(p)[k] = reinterpret_cast<const uint8_t *>(&i)[k];
|
||||
#else
|
||||
*reinterpret_cast<I *>(p) = i;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a big-endian value from a byte stream
|
||||
*
|
||||
* @tparam I Type to decode (should be unsigned e.g. uint32_t or uint64_t)
|
||||
* @param p Byte stream, must be at least sizeof(I) in size
|
||||
* @return Decoded integer
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I loadBigEndian(const void *const p) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
return _load_be_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
|
||||
#else
|
||||
return ntoh(*reinterpret_cast<const I *>(p));
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Save an integer in big-endian format
|
||||
*
|
||||
* @tparam I Integer type to store (usually inferred)
|
||||
* @param p Byte stream to write (must be at least sizeof(I))
|
||||
* #param i Integer to write
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE void storeBigEndian(void *const p, I i) noexcept
|
||||
{
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
storeMachineEndian(p,hton(i));
|
||||
#else
|
||||
*reinterpret_cast<I *>(p) = hton(i);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode a little-endian value from a byte stream
|
||||
*
|
||||
* @tparam I Type to decode
|
||||
* @param p Byte stream, must be at least sizeof(I) in size
|
||||
* @return Decoded integer
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE I loadLittleEndian(const void *const p) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN || defined(ZT_NO_UNALIGNED_ACCESS)
|
||||
return _load_le_bysize<I,sizeof(I)>::l(reinterpret_cast<const uint8_t *>(p));
|
||||
#else
|
||||
return *reinterpret_cast<const I *>(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Save an integer in little-endian format
|
||||
*
|
||||
* @tparam I Integer type to store (usually inferred)
|
||||
* @param p Byte stream to write (must be at least sizeof(I))
|
||||
* #param i Integer to write
|
||||
*/
|
||||
template< typename I >
|
||||
static ZT_INLINE void storeLittleEndian(void *const p, const I i) noexcept
|
||||
{
|
||||
#if __BYTE_ORDER == __BIG_ENDIAN
|
||||
storeMachineEndian(p,_swap_bytes_bysize<I,sizeof(I)>::s(i));
|
||||
#else
|
||||
#ifdef ZT_NO_UNALIGNED_ACCESS
|
||||
storeMachineEndian(p,i);
|
||||
#else
|
||||
*reinterpret_cast<I *>(p) = i;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy memory block whose size is known at compile time.
|
||||
*
|
||||
* @tparam L Size of memory
|
||||
* @param dest Destination memory
|
||||
* @param src Source memory
|
||||
*/
|
||||
template< unsigned long L >
|
||||
static ZT_INLINE void copy(void *dest, const void *src) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
uintptr_t l = L;
|
||||
__asm__ __volatile__ ("cld ; rep movsb" : "+c"(l), "+S"(src), "+D"(dest) :: "memory");
|
||||
#else
|
||||
memcpy(dest, src, L);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy memory block whose size is known at run time
|
||||
*
|
||||
* @param dest Destination memory
|
||||
* @param src Source memory
|
||||
* @param len Bytes to copy
|
||||
*/
|
||||
static ZT_INLINE void copy(void *dest, const void *src, unsigned long len) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
__asm__ __volatile__ ("cld ; rep movsb" : "+c"(len), "+S"(src), "+D"(dest) :: "memory");
|
||||
#else
|
||||
memcpy(dest, src, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Zero memory block whose size is known at compile time
|
||||
*
|
||||
* @tparam L Size in bytes
|
||||
* @param dest Memory to zero
|
||||
*/
|
||||
template< unsigned long L >
|
||||
static ZT_INLINE void zero(void *dest) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
uintptr_t l = L;
|
||||
__asm__ __volatile__ ("cld ; rep stosb" :"+c" (l), "+D" (dest) : "a" (0) : "memory");
|
||||
#else
|
||||
memset(dest, 0, L);
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Zero memory block whose size is known at run time
|
||||
*
|
||||
* @param dest Memory to zero
|
||||
* @param len Size in bytes
|
||||
*/
|
||||
static ZT_INLINE void zero(void *dest, unsigned long len) noexcept
|
||||
{
|
||||
#if defined(ZT_ARCH_X64) && defined(__GNUC__)
|
||||
__asm__ __volatile__ ("cld ; rep stosb" :"+c" (len), "+D" (dest) : "a" (0) : "memory");
|
||||
#else
|
||||
memset(dest, 0, len);
|
||||
#endif
|
||||
}
|
||||
static inline int64_t ntoh(int64_t n) { return (int64_t)ntoh((uint64_t)n); }
|
||||
|
||||
/**
|
||||
* Hexadecimal characters 0-f
|
||||
|
@ -1,4 +1,5 @@
|
||||
CORE_OBJS=\
|
||||
node/AES.o \
|
||||
node/C25519.o \
|
||||
node/Capability.o \
|
||||
node/CertificateOfMembership.o \
|
||||
|
Loading…
x
Reference in New Issue
Block a user