From 990333e7ec24b905436d479fd3ade079c0a2fcb0 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 26 Aug 2019 16:35:28 -0700 Subject: [PATCH] cleanup, re-enable AES-NI check --- node/AES.cpp | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/node/AES.cpp b/node/AES.cpp index 6dd5ae040..c92876f05 100644 --- a/node/AES.cpp +++ b/node/AES.cpp @@ -14,6 +14,10 @@ #include "AES.hpp" #include "Constants.hpp" +// This file contains the software implementations of AES and GHASH. They're +// only used if your CPU lacks hardware acceleration as the hardware +// accelerated code is 10-20X as fast in most cases. + #ifdef __WINDOWS__ #include #endif @@ -23,7 +27,7 @@ namespace ZeroTier { namespace { #ifdef ZT_NO_TYPE_PUNNING -static ZT_ALWAYS_INLINE uint32_t GETU32(const void *in) +static ZT_ALWAYS_INLINE uint32_t readuint32_t(const void *in) { uint32_t v = ((const uint8_t *)in)[0]; v <<= 8; @@ -34,7 +38,7 @@ static ZT_ALWAYS_INLINE uint32_t GETU32(const void *in) v |= ((const uint8_t *)in)[3]; return v; } -static ZT_ALWAYS_INLINE void PUTU32(void *out,const uint32_t v) +static ZT_ALWAYS_INLINE void writeuint32_t(void *out,const uint32_t v) { ((uint8_t *)out)[0] = (uint8_t)(v >> 24); ((uint8_t *)out)[1] = (uint8_t)(v >> 16); @@ -42,14 +46,13 @@ static ZT_ALWAYS_INLINE void PUTU32(void *out,const uint32_t v) ((uint8_t *)out)[3] = (uint8_t)v; } #else -#define GETU32(i) (Utils::ntoh(*((const uint32_t *)(i)))) -#define PUTU32(o,v) (*((uint32_t *)(o)) = Utils::hton(v)) +#define readuint32_t(i) (Utils::ntoh(*((const uint32_t *)(i)))) +#define writeuint32_t(o,v) (*((uint32_t *)(o)) = Utils::hton(v)) #endif } // anonymous namespace #if (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64)) - static bool _zt_aesni_supported() { #ifdef __WINDOWS__ @@ -66,12 +69,9 @@ static bool _zt_aesni_supported() return ((ecx & (1 << 25)) != 0); #endif } -const bool AES::HW_ACCEL = false; //_zt_aesni_supported(); - +const bool AES::HW_ACCEL = _zt_aesni_supported(); #else - const bool AES::HW_ACCEL = false; - #endif const uint32_t AES::Te0[256] = { 0xc66363a5,0xf87c7c84,0xee777799,0xf67b7b8d,0xfff2f20d,0xd66b6bbd,0xde6f6fb1,0x91c5c554,0x60303050,0x02010103,0xce6767a9,0x562b2b7d,0xe7fefe19,0xb5d7d762,0x4dababe6,0xec76769a,0x8fcaca45,0x1f82829d,0x89c9c940,0xfa7d7d87,0xeffafa15,0xb25959eb,0x8e4747c9,0xfbf0f00b,0x41adadec,0xb3d4d467,0x5fa2a2fd,0x45afafea,0x239c9cbf,0x53a4a4f7,0xe4727296,0x9bc0c05b,0x75b7b7c2,0xe1fdfd1c,0x3d9393ae,0x4c26266a,0x6c36365a,0x7e3f3f41,0xf5f7f702,0x83cccc4f,0x6834345c,0x51a5a5f4,0xd1e5e534,0xf9f1f108,0xe2717193,0xabd8d873,0x62313153,0x2a15153f,0x0804040c,0x95c7c752,0x46232365,0x9dc3c35e,0x30181828,0x379696a1,0x0a05050f,0x2f9a9ab5,0x0e070709,0x24121236,0x1b80809b,0xdfe2e23d,0xcdebeb26,0x4e272769,0x7fb2b2cd,0xea75759f,0x1209091b,0x1d83839e,0x582c2c74,0x341a1a2e,0x361b1b2d,0xdc6e6eb2,0xb45a5aee,0x5ba0a0fb,0xa45252f6,0x763b3b4d,0xb7d6d661,0x7db3b3ce,0x5229297b,0xdde3e33e,0x5e2f2f71,0x13848497,0xa65353f5,0xb9d1d168,0x00000000,0xc1eded2c,0x40202060,0xe3fcfc1f,0x79b1b1c8,0xb65b5bed,0xd46a6abe,0x8dcbcb46,0x67bebed9,0x7239394b,0x944a4ade,0x984c4cd4,0xb05858e8,0x85cfcf4a,0xbbd0d06b,0xc5efef2a,0x4faaaae5,0xedfbfb16,0x864343c5,0x9a4d4dd7,0x66333355,0x11858594,0x8a4545cf,0xe9f9f910,0x04020206,0xfe7f7f81,0xa05050f0,0x783c3c44,0x259f9fba,0x4ba8a8e3,0xa25151f3,0x5da3a3fe,0x804040c0,0x058f8f8a,0x3f9292ad,0x219d9dbc,0x70383848,0xf1f5f504,0x63bcbcdf,0x77b6b6c1,0xafdada75,0x42212163,0x20101030,0xe5ffff1a,0xfdf3f30e,0xbfd2d26d,0x81cdcd4c,0x180c0c14,0x26131335,0xc3ecec2f,0xbe5f5fe1,0x359797a2,0x884444cc,0x2e171739,0x93c4c457,0x55a7a7f2,0xfc7e7e82,0x7a3d3d47,0xc86464ac,0xba5d5de7,0x3219192b,0xe6737395,0xc06060a0,0x19818198,0x9e4f4fd1,0xa3dcdc7f,0x44222266,0x542a2a7e,0x3b9090ab,0x0b888883,0x8c4646ca,0xc7eeee29,0x6bb8b8d3,0x2814143c,0xa7dede79,0xbc5e5ee2,0x160b0b1d,0xaddbdb76,0xdbe0e03b,0x64323256,0x743a3a4e,0x140a0a1e,0x924949db,0x0c06060a,0x4824246c,0xb85c5ce4,0x9fc2c25d,0xbdd3d36e,0x43acacef,0xc46262a6,0x399191a8,0x319595a4,0xd3e4e437,0xf279798b,0xd5e7e732,0x8bc8c843,0x6e373759,0xda6d6db7,0x018d8d8c,0xb1d5d564,0x9c4e4ed2,0x49a9a9e0,0xd86c6cb4,0xac5656fa,0xf3f4f407,0xcfeaea25,0xca6565af,0xf47a7a8e,0x47aeaee9,0x10080818,0x6fbabad5,0xf0787888,0x4a25256f,0x5c2e2e72,0x381c1c24,0x57a6a6f1,0x73b4b4c7,0x97c6c651,0xcbe8e823,0xa1dddd7c,0xe874749c,0x3e1f1f21,0x964b4bdd,0x61bdbddc,0x0d8b8b86,0x0f8a8a85,0xe0707090,0x7c3e3e42,0x71b5b5c4,0xcc6666aa,0x904848d8,0x06030305,0xf7f6f601,0x1c0e0e12,0xc26161a3,0x6a35355f,0xae5757f9,0x69b9b9d0,0x17868691,0x99c1c158,0x3a1d1d27,0x279e9eb9,0xd9e1e138,0xebf8f813,0x2b9898b3,0x22111133,0xd26969bb,0xa9d9d970,0x078e8e89,0x339494a7,0x2d9b9bb6,0x3c1e1e22,0x15878792,0xc9e9e920,0x87cece49,0xaa5555ff,0x50282878,0xa5dfdf7a,0x038c8c8f,0x59a1a1f8,0x09898980,0x1a0d0d17,0x65bfbfda,0xd7e6e631,0x844242c6,0xd06868b8,0x824141c3,0x299999b0,0x5a2d2d77,0x1e0f0f11,0x7bb0b0cb,0xa85454fc,0x6dbbbbd6,0x2c16163a }; @@ -84,14 +84,14 @@ void AES::_initSW(const uint8_t key[32]) { uint32_t *rk = _k.sw.ek; - rk[0] = GETU32(key); - rk[1] = GETU32(key + 4); - rk[2] = GETU32(key + 8); - rk[3] = GETU32(key + 12); - rk[4] = GETU32(key + 16); - rk[5] = GETU32(key + 20); - rk[6] = GETU32(key + 24); - rk[7] = GETU32(key + 28); + rk[0] = readuint32_t(key); + rk[1] = readuint32_t(key + 4); + rk[2] = readuint32_t(key + 8); + rk[3] = readuint32_t(key + 12); + rk[4] = readuint32_t(key + 16); + rk[5] = readuint32_t(key + 20); + rk[6] = readuint32_t(key + 24); + rk[7] = readuint32_t(key + 28); for(int i=0;;) { uint32_t temp = rk[7]; rk[8] = rk[0] ^ (Te2[(temp >> 16) & 0xff] & 0xff000000) ^ (Te3[(temp >> 8) & 0xff] & 0x00ff0000) ^ (Te0[(temp) & 0xff] & 0x0000ff00) ^ (Te1[(temp >> 24)] & 0x000000ff) ^ rcon[i]; @@ -119,10 +119,10 @@ void AES::_encryptSW(const uint8_t in[16],uint8_t out[16]) const const uint32_t *rk = _k.sw.ek; uint32_t s0, s1, s2, s3, t0, t1, t2, t3; - s0 = GETU32(in) ^ rk[0]; - s1 = GETU32(in + 4) ^ rk[1]; - s2 = GETU32(in + 8) ^ rk[2]; - s3 = GETU32(in + 12) ^ rk[3]; + s0 = readuint32_t(in) ^ rk[0]; + s1 = readuint32_t(in + 4) ^ rk[1]; + s2 = readuint32_t(in + 8) ^ rk[2]; + s3 = readuint32_t(in + 12) ^ rk[3]; t0 = Te0[s0 >> 24] ^ Te1[(s1 >> 16) & 0xff] ^ Te2[(s2 >> 8) & 0xff] ^ Te3[s3 & 0xff] ^ rk[ 4]; t1 = Te0[s1 >> 24] ^ Te1[(s2 >> 16) & 0xff] ^ Te2[(s3 >> 8) & 0xff] ^ Te3[s0 & 0xff] ^ rk[ 5]; @@ -179,13 +179,13 @@ void AES::_encryptSW(const uint8_t in[16],uint8_t out[16]) const rk += 56; s0 = (Te2[(t0 >> 24)] & 0xff000000) ^ (Te3[(t1 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t2 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t3) & 0xff] & 0x000000ff) ^ rk[0]; - PUTU32(out, s0); + writeuint32_t(out, s0); s1 = (Te2[(t1 >> 24)] & 0xff000000) ^ (Te3[(t2 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t3 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t0) & 0xff] & 0x000000ff) ^ rk[1]; - PUTU32(out + 4, s1); + writeuint32_t(out + 4, s1); s2 = (Te2[(t2 >> 24)] & 0xff000000) ^ (Te3[(t3 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t0 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t1) & 0xff] & 0x000000ff) ^ rk[2]; - PUTU32(out + 8, s2); + writeuint32_t(out + 8, s2); s3 = (Te2[(t3 >> 24)] & 0xff000000) ^ (Te3[(t0 >> 16) & 0xff] & 0x00ff0000) ^ (Te0[(t1 >> 8) & 0xff] & 0x0000ff00) ^ (Te1[(t2) & 0xff] & 0x000000ff) ^ rk[3]; - PUTU32(out + 12, s3); + writeuint32_t(out + 12, s3); } #if (defined(__GNUC__) || defined(__clang)) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(__aarch64__))