Packet.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2026-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include <stdint.h>
  14. #include <stddef.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include "Packet.hpp"
  19. #include "ECC.hpp"
  20. #if defined(ZT_USE_X64_ASM_SALSA2012) && defined(ZT_ARCH_X64)
  21. #include "../ext/x64-salsa2012-asm/salsa2012.h"
  22. #endif
  23. #ifdef ZT_USE_ARM32_NEON_ASM_SALSA2012
  24. #include "../ext/arm32-neon-salsa2012-asm/salsa2012.h"
  25. #endif
  26. #ifdef _MSC_VER
  27. #define FORCE_INLINE static __forceinline
  28. #include <intrin.h>
  29. #pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */
  30. #pragma warning(disable : 4293) /* disable: C4293: too large shift (32-bits) */
  31. #else
  32. #define FORCE_INLINE static inline
  33. #endif
  34. namespace ZeroTier {
  35. /************************************************************************** */
  36. /* Set up macros for fast single-pass ASM Salsa20/12 crypto, if we have it */
  37. // x64 SSE crypto
  38. #if defined(ZT_USE_X64_ASM_SALSA2012) && defined(ZT_ARCH_X64)
  39. #define ZT_HAS_FAST_CRYPTO() (true)
  40. #define ZT_FAST_SINGLE_PASS_SALSA2012(b,l,n,k) zt_salsa2012_amd64_xmm6(reinterpret_cast<unsigned char *>(b),(l),reinterpret_cast<const unsigned char *>(n),reinterpret_cast<const unsigned char *>(k))
  41. #endif
  42. // ARM (32-bit) NEON crypto (must be detected)
  43. #ifdef ZT_USE_ARM32_NEON_ASM_SALSA2012
  44. class _FastCryptoChecker
  45. {
  46. public:
  47. _FastCryptoChecker() : canHas(zt_arm_has_neon()) {}
  48. bool canHas;
  49. };
  50. static const _FastCryptoChecker _ZT_FAST_CRYPTO_CHECK;
  51. #define ZT_HAS_FAST_CRYPTO() (_ZT_FAST_CRYPTO_CHECK.canHas)
  52. #define ZT_FAST_SINGLE_PASS_SALSA2012(b,l,n,k) zt_salsa2012_armneon3_xor(reinterpret_cast<unsigned char *>(b),(const unsigned char *)0,(l),reinterpret_cast<const unsigned char *>(n),reinterpret_cast<const unsigned char *>(k))
  53. #endif
  54. // No fast crypto available
  55. #ifndef ZT_HAS_FAST_CRYPTO
  56. #define ZT_HAS_FAST_CRYPTO() (false)
  57. #define ZT_FAST_SINGLE_PASS_SALSA2012(b,l,n,k) {}
  58. #endif
  59. /************************************************************************** */
  60. /* LZ4 is shipped encapsulated into Packet in an anonymous namespace.
  61. *
  62. * We're doing this as a deliberate workaround for various Linux distribution
  63. * policies that forbid static linking of support libraries.
  64. *
  65. * The reason is that relying on distribution versions of LZ4 has been too
  66. * big a source of bugs and compatibility issues. The LZ4 API is not stable
  67. * enough across versions, and dependency hell ensues. So fark it. */
  68. /* Needless to say the code in this anonymous namespace should be considered
  69. * BSD 2-clause licensed. */
  70. namespace {
  71. /* lz4.h ------------------------------------------------------------------ */
  72. /*
  73. * LZ4 - Fast LZ compression algorithm
  74. * Header File
  75. * Copyright (C) 2011-2016, Yann Collet.
  76. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  77. Redistribution and use in source and binary forms, with or without
  78. modification, are permitted provided that the following conditions are
  79. met:
  80. * Redistributions of source code must retain the above copyright
  81. notice, this list of conditions and the following disclaimer.
  82. * Redistributions in binary form must reproduce the above
  83. copyright notice, this list of conditions and the following disclaimer
  84. in the documentation and/or other materials provided with the
  85. distribution.
  86. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  87. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  88. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  89. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  90. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  91. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  92. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  93. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  94. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  95. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  96. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  97. You can contact the author at :
  98. - LZ4 homepage : http://www.lz4.org
  99. - LZ4 source repository : https://github.com/lz4/lz4
  100. */
  101. /**
  102. Introduction
  103. LZ4 is lossless compression algorithm, providing compression speed at 400 MB/s per core,
  104. scalable with multi-cores CPU. It features an extremely fast decoder, with speed in
  105. multiple GB/s per core, typically reaching RAM speed limits on multi-core systems.
  106. The LZ4 compression library provides in-memory compression and decompression functions.
  107. Compression can be done in:
  108. - a single step (described as Simple Functions)
  109. - a single step, reusing a context (described in Advanced Functions)
  110. - unbounded multiple steps (described as Streaming compression)
  111. lz4.h provides block compression functions. It gives full buffer control to user.
  112. Decompressing an lz4-compressed block also requires metadata (such as compressed size).
  113. Each application is free to encode such metadata in whichever way it wants.
  114. An additional format, called LZ4 frame specification (doc/lz4_Frame_format.md),
  115. take care of encoding standard metadata alongside LZ4-compressed blocks.
  116. If your application requires interoperability, it's recommended to use it.
  117. A library is provided to take care of it, see lz4frame.h.
  118. */
  119. #define LZ4_VERSION_MAJOR 1 /* for breaking interface changes */
  120. #define LZ4_VERSION_MINOR 7 /* for new (non-breaking) interface capabilities */
  121. #define LZ4_VERSION_RELEASE 5 /* for tweaks, bug-fixes, or development */
  122. #define LZ4_VERSION_NUMBER (LZ4_VERSION_MAJOR *100*100 + LZ4_VERSION_MINOR *100 + LZ4_VERSION_RELEASE)
  123. #define LZ4_LIB_VERSION LZ4_VERSION_MAJOR.LZ4_VERSION_MINOR.LZ4_VERSION_RELEASE
  124. #define LZ4_QUOTE(str) #str
  125. #define LZ4_EXPAND_AND_QUOTE(str) LZ4_QUOTE(str)
  126. #define LZ4_VERSION_STRING LZ4_EXPAND_AND_QUOTE(LZ4_LIB_VERSION)
  127. #define LZ4_MEMORY_USAGE 14
  128. #define LZ4_MAX_INPUT_SIZE 0x7E000000 /* 2 113 929 216 bytes */
  129. #define LZ4_COMPRESSBOUND(isize) ((unsigned)(isize) > (unsigned)LZ4_MAX_INPUT_SIZE ? 0 : (isize) + ((isize)/255) + 16)
  130. typedef union LZ4_stream_u LZ4_stream_t; /* incomplete type (defined later) */
  131. static inline void LZ4_resetStream (LZ4_stream_t* streamPtr);
  132. #define LZ4_HASHLOG (LZ4_MEMORY_USAGE-2)
  133. #define LZ4_HASHTABLESIZE (1 << LZ4_MEMORY_USAGE)
  134. #define LZ4_HASH_SIZE_U32 (1 << LZ4_HASHLOG) /* required as macro for static allocation */
  135. typedef struct {
  136. uint32_t hashTable[LZ4_HASH_SIZE_U32];
  137. uint32_t currentOffset;
  138. uint32_t initCheck;
  139. const uint8_t* dictionary;
  140. uint8_t* bufferStart; /* obsolete, used for slideInputBuffer */
  141. uint32_t dictSize;
  142. } LZ4_stream_t_internal;
  143. typedef struct {
  144. const uint8_t* externalDict;
  145. size_t extDictSize;
  146. const uint8_t* prefixEnd;
  147. size_t prefixSize;
  148. } LZ4_streamDecode_t_internal;
  149. #define LZ4_STREAMSIZE_U64 ((1 << (LZ4_MEMORY_USAGE-3)) + 4)
  150. #define LZ4_STREAMSIZE (LZ4_STREAMSIZE_U64 * sizeof(unsigned long long))
  151. union LZ4_stream_u {
  152. unsigned long long table[LZ4_STREAMSIZE_U64];
  153. LZ4_stream_t_internal internal_donotuse;
  154. } ; /* previously typedef'd to LZ4_stream_t */
  155. #define LZ4_STREAMDECODESIZE_U64 4
  156. #define LZ4_STREAMDECODESIZE (LZ4_STREAMDECODESIZE_U64 * sizeof(unsigned long long))
  157. union LZ4_streamDecode_u {
  158. unsigned long long table[LZ4_STREAMDECODESIZE_U64];
  159. LZ4_streamDecode_t_internal internal_donotuse;
  160. } ; /* previously typedef'd to LZ4_streamDecode_t */
  161. #ifndef HEAPMODE
  162. #define HEAPMODE 0
  163. #endif
  164. #ifdef ZT_NO_TYPE_PUNNING
  165. #define LZ4_FORCE_MEMORY_ACCESS 0
  166. #else
  167. #define LZ4_FORCE_MEMORY_ACCESS 2
  168. #endif
  169. #if defined(_MSC_VER) && defined(_WIN32_WCE) /* Visual Studio for Windows CE does not support Hardware bit count */
  170. #define LZ4_FORCE_SW_BITCOUNT
  171. #endif
  172. #ifndef FORCE_INLINE
  173. #define FORCE_INLINE static inline
  174. #endif
  175. #define ALLOCATOR(n,s) calloc(n,s)
  176. #define FREEMEM free
  177. #define MEM_INIT memset
  178. typedef uint8_t BYTE;
  179. typedef uint16_t U16;
  180. typedef uint32_t U32;
  181. typedef int32_t S32;
  182. typedef uint64_t U64;
  183. typedef uintptr_t uptrval;
  184. typedef uintptr_t reg_t;
  185. static inline unsigned LZ4_isLittleEndian(void)
  186. {
  187. const union { U32 u; BYTE c[4]; } one = { 1 }; /* don't use static : performance detrimental */
  188. return one.c[0];
  189. }
  190. #if defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==2)
  191. static U16 LZ4_read16(const void* memPtr) { return *(const U16*) memPtr; }
  192. static U32 LZ4_read32(const void* memPtr) { return *(const U32*) memPtr; }
  193. static reg_t LZ4_read_ARCH(const void* memPtr) { return *(const reg_t*) memPtr; }
  194. static void LZ4_write16(void* memPtr, U16 value) { *(U16*)memPtr = value; }
  195. static void LZ4_write32(void* memPtr, U32 value) { *(U32*)memPtr = value; }
  196. #elif defined(LZ4_FORCE_MEMORY_ACCESS) && (LZ4_FORCE_MEMORY_ACCESS==1)
  197. typedef union { U16 u16; U32 u32; reg_t uArch; } __attribute__((packed)) unalign;
  198. static U16 LZ4_read16(const void* ptr) { return ((const unalign*)ptr)->u16; }
  199. static U32 LZ4_read32(const void* ptr) { return ((const unalign*)ptr)->u32; }
  200. static reg_t LZ4_read_ARCH(const void* ptr) { return ((const unalign*)ptr)->uArch; }
  201. static void LZ4_write16(void* memPtr, U16 value) { ((unalign*)memPtr)->u16 = value; }
  202. static void LZ4_write32(void* memPtr, U32 value) { ((unalign*)memPtr)->u32 = value; }
  203. #else /* safe and portable access through memcpy() */
  204. static inline U16 LZ4_read16(const void* memPtr)
  205. {
  206. U16 val; memcpy(&val, memPtr, sizeof(val)); return val;
  207. }
  208. static inline U32 LZ4_read32(const void* memPtr)
  209. {
  210. U32 val; memcpy(&val, memPtr, sizeof(val)); return val;
  211. }
  212. static inline reg_t LZ4_read_ARCH(const void* memPtr)
  213. {
  214. reg_t val; memcpy(&val, memPtr, sizeof(val)); return val;
  215. }
  216. static inline void LZ4_write16(void* memPtr, U16 value)
  217. {
  218. memcpy(memPtr, &value, sizeof(value));
  219. }
  220. static inline void LZ4_write32(void* memPtr, U32 value)
  221. {
  222. memcpy(memPtr, &value, sizeof(value));
  223. }
  224. #endif /* LZ4_FORCE_MEMORY_ACCESS */
  225. static inline U16 LZ4_readLE16(const void* memPtr)
  226. {
  227. if (LZ4_isLittleEndian()) {
  228. return LZ4_read16(memPtr);
  229. } else {
  230. const BYTE* p = (const BYTE*)memPtr;
  231. return (U16)((U16)p[0] + (p[1]<<8));
  232. }
  233. }
  234. static inline void LZ4_writeLE16(void* memPtr, U16 value)
  235. {
  236. if (LZ4_isLittleEndian()) {
  237. LZ4_write16(memPtr, value);
  238. } else {
  239. BYTE* p = (BYTE*)memPtr;
  240. p[0] = (BYTE) value;
  241. p[1] = (BYTE)(value>>8);
  242. }
  243. }
  244. static inline void LZ4_copy8(void* dst, const void* src)
  245. {
  246. memcpy(dst,src,8);
  247. }
  248. static inline void LZ4_wildCopy(void* dstPtr, const void* srcPtr, void* dstEnd)
  249. {
  250. BYTE* d = (BYTE*)dstPtr;
  251. const BYTE* s = (const BYTE*)srcPtr;
  252. BYTE* const e = (BYTE*)dstEnd;
  253. do {
  254. LZ4_copy8(d,s);
  255. d+=8;
  256. s+=8;
  257. } while (d<e);
  258. }
  259. #define MINMATCH 4
  260. #define WILDCOPYLENGTH 8
  261. #define LASTLITERALS 5
  262. #define MFLIMIT (WILDCOPYLENGTH+MINMATCH)
  263. static const int LZ4_minLength = (MFLIMIT+1);
  264. #define KB *(1 <<10)
  265. #define MB *(1 <<20)
  266. #define GB *(1U<<30)
  267. #define MAXD_LOG 16
  268. #define MAX_DISTANCE ((1 << MAXD_LOG) - 1)
  269. #define ML_BITS 4
  270. #define ML_MASK ((1U<<ML_BITS)-1)
  271. #define RUN_BITS (8-ML_BITS)
  272. #define RUN_MASK ((1U<<RUN_BITS)-1)
  273. #define LZ4_STATIC_ASSERT(c) { enum { LZ4_static_assert = 1/(int)(!!(c)) }; } /* use only *after* variable declarations */
  274. static inline unsigned LZ4_NbCommonBytes (reg_t val)
  275. {
  276. if (LZ4_isLittleEndian()) {
  277. if (sizeof(val)==8) {
  278. # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
  279. unsigned long r = 0;
  280. _BitScanForward64( &r, (U64)val );
  281. return (int)(r>>3);
  282. # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
  283. return (__builtin_ctzll((U64)val) >> 3);
  284. # else
  285. static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 };
  286. return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58];
  287. # endif
  288. } else /* 32 bits */ {
  289. # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  290. unsigned long r;
  291. _BitScanForward( &r, (U32)val );
  292. return (int)(r>>3);
  293. # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
  294. return (__builtin_ctz((U32)val) >> 3);
  295. # else
  296. static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 };
  297. return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27];
  298. # endif
  299. }
  300. } else /* Big Endian CPU */ {
  301. if (sizeof(val)==8) {
  302. # if defined(_MSC_VER) && defined(_WIN64) && !defined(LZ4_FORCE_SW_BITCOUNT)
  303. unsigned long r = 0;
  304. _BitScanReverse64( &r, val );
  305. return (unsigned)(r>>3);
  306. # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
  307. return (__builtin_clzll((U64)val) >> 3);
  308. # else
  309. unsigned r;
  310. if (!(val>>32)) {
  311. r=4;
  312. } else {
  313. r=0;
  314. val>>=32;
  315. }
  316. if (!(val>>16)) {
  317. r+=2;
  318. val>>=8;
  319. } else {
  320. val>>=24;
  321. }
  322. r += (!val);
  323. return r;
  324. # endif
  325. } else /* 32 bits */ {
  326. # if defined(_MSC_VER) && !defined(LZ4_FORCE_SW_BITCOUNT)
  327. unsigned long r = 0;
  328. _BitScanReverse( &r, (unsigned long)val );
  329. return (unsigned)(r>>3);
  330. # elif (defined(__clang__) || (defined(__GNUC__) && (__GNUC__>=3))) && !defined(LZ4_FORCE_SW_BITCOUNT)
  331. return (__builtin_clz((U32)val) >> 3);
  332. # else
  333. unsigned r;
  334. if (!(val>>16)) {
  335. r=2;
  336. val>>=8;
  337. } else {
  338. r=0;
  339. val>>=24;
  340. }
  341. r += (!val);
  342. return r;
  343. # endif
  344. }
  345. }
  346. }
  347. #define STEPSIZE sizeof(reg_t)
  348. static inline unsigned LZ4_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit)
  349. {
  350. const BYTE* const pStart = pIn;
  351. while (likely(pIn<pInLimit-(STEPSIZE-1))) {
  352. reg_t const diff = LZ4_read_ARCH(pMatch) ^ LZ4_read_ARCH(pIn);
  353. if (!diff) {
  354. pIn+=STEPSIZE;
  355. pMatch+=STEPSIZE; continue;
  356. }
  357. pIn += LZ4_NbCommonBytes(diff);
  358. return (unsigned)(pIn - pStart);
  359. }
  360. if ((STEPSIZE==8) && (pIn<(pInLimit-3)) && (LZ4_read32(pMatch) == LZ4_read32(pIn))) {
  361. pIn+=4; pMatch+=4;
  362. }
  363. if ((pIn<(pInLimit-1)) && (LZ4_read16(pMatch) == LZ4_read16(pIn))) {
  364. pIn+=2; pMatch+=2;
  365. }
  366. if ((pIn<pInLimit) && (*pMatch == *pIn)) {
  367. pIn++;
  368. }
  369. return (unsigned)(pIn - pStart);
  370. }
  371. static const int LZ4_64Klimit = ((64 KB) + (MFLIMIT-1));
  372. static const U32 LZ4_skipTrigger = 6; /* Increase this value ==> compression run slower on incompressible data */
  373. typedef enum { notLimited = 0, limitedOutput = 1 } limitedOutput_directive;
  374. typedef enum { byPtr, byU32, byU16 } tableType_t;
  375. typedef enum { noDict = 0, withPrefix64k, usingExtDict } dict_directive;
  376. typedef enum { noDictIssue = 0, dictSmall } dictIssue_directive;
  377. typedef enum { endOnOutputSize = 0, endOnInputSize = 1 } endCondition_directive;
  378. typedef enum { full = 0, partial = 1 } earlyEnd_directive;
  379. static inline int LZ4_compressBound(int isize) { return LZ4_COMPRESSBOUND(isize); }
  380. static inline U32 LZ4_hash4(U32 sequence, tableType_t const tableType)
  381. {
  382. if (tableType == byU16) {
  383. return ((sequence * 2654435761U) >> ((MINMATCH*8)-(LZ4_HASHLOG+1)));
  384. } else {
  385. return ((sequence * 2654435761U) >> ((MINMATCH*8)-LZ4_HASHLOG));
  386. }
  387. }
  388. static inline U32 LZ4_hash5(U64 sequence, tableType_t const tableType)
  389. {
  390. static const U64 prime5bytes = 889523592379ULL;
  391. static const U64 prime8bytes = 11400714785074694791ULL;
  392. const U32 hashLog = (tableType == byU16) ? LZ4_HASHLOG+1 : LZ4_HASHLOG;
  393. if (LZ4_isLittleEndian()) {
  394. return (U32)(((sequence << 24) * prime5bytes) >> (64 - hashLog));
  395. } else {
  396. return (U32)(((sequence >> 24) * prime8bytes) >> (64 - hashLog));
  397. }
  398. }
  399. FORCE_INLINE U32 LZ4_hashPosition(const void* const p, tableType_t const tableType)
  400. {
  401. if ((sizeof(reg_t)==8) && (tableType != byU16)) {
  402. return LZ4_hash5(LZ4_read_ARCH(p), tableType);
  403. }
  404. return LZ4_hash4(LZ4_read32(p), tableType);
  405. }
  406. static inline void LZ4_putPositionOnHash(const BYTE* p, U32 h, void* tableBase, tableType_t const tableType, const BYTE* srcBase)
  407. {
  408. switch (tableType) {
  409. case byPtr: {
  410. const BYTE** hashTable = (const BYTE**)tableBase; hashTable[h] = p;
  411. return;
  412. }
  413. case byU32: {
  414. U32* hashTable = (U32*) tableBase; hashTable[h] = (U32)(p-srcBase);
  415. return;
  416. }
  417. case byU16: {
  418. U16* hashTable = (U16*) tableBase; hashTable[h] = (U16)(p-srcBase);
  419. return;
  420. }
  421. }
  422. }
  423. FORCE_INLINE void LZ4_putPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
  424. {
  425. U32 const h = LZ4_hashPosition(p, tableType);
  426. LZ4_putPositionOnHash(p, h, tableBase, tableType, srcBase);
  427. }
  428. static inline const BYTE* LZ4_getPositionOnHash(U32 h, void* tableBase, tableType_t tableType, const BYTE* srcBase)
  429. {
  430. if (tableType == byPtr) {
  431. const BYTE** hashTable = (const BYTE**) tableBase;
  432. return hashTable[h];
  433. }
  434. if (tableType == byU32) {
  435. const U32* const hashTable = (U32*) tableBase;
  436. return hashTable[h] + srcBase;
  437. }
  438. { /* default, to ensure a return */
  439. const U16* const hashTable = (U16*) tableBase;
  440. return hashTable[h] + srcBase;
  441. }
  442. }
  443. FORCE_INLINE const BYTE* LZ4_getPosition(const BYTE* p, void* tableBase, tableType_t tableType, const BYTE* srcBase)
  444. {
  445. U32 const h = LZ4_hashPosition(p, tableType);
  446. return LZ4_getPositionOnHash(h, tableBase, tableType, srcBase);
  447. }
  448. FORCE_INLINE int LZ4_compress_generic(
  449. LZ4_stream_t_internal* const cctx,
  450. const char* const source,
  451. char* const dest,
  452. const int inputSize,
  453. const int maxOutputSize,
  454. const limitedOutput_directive outputLimited,
  455. const tableType_t tableType,
  456. const dict_directive dict,
  457. const dictIssue_directive dictIssue,
  458. const U32 acceleration)
  459. {
  460. const BYTE* ip = (const BYTE*) source;
  461. const BYTE* base;
  462. const BYTE* lowLimit;
  463. const BYTE* const lowRefLimit = ip - cctx->dictSize;
  464. const BYTE* const dictionary = cctx->dictionary;
  465. const BYTE* const dictEnd = dictionary + cctx->dictSize;
  466. const ptrdiff_t dictDelta = dictEnd - (const BYTE*)source;
  467. const BYTE* anchor = (const BYTE*) source;
  468. const BYTE* const iend = ip + inputSize;
  469. const BYTE* const mflimit = iend - MFLIMIT;
  470. const BYTE* const matchlimit = iend - LASTLITERALS;
  471. BYTE* op = (BYTE*) dest;
  472. BYTE* const olimit = op + maxOutputSize;
  473. U32 forwardH;
  474. /* Init conditions */
  475. if ((U32)inputSize > (U32)LZ4_MAX_INPUT_SIZE) {
  476. return 0; /* Unsupported inputSize, too large (or negative) */
  477. }
  478. switch(dict) {
  479. case noDict:
  480. default:
  481. base = (const BYTE*)source;
  482. lowLimit = (const BYTE*)source;
  483. break;
  484. case withPrefix64k:
  485. base = (const BYTE*)source - cctx->currentOffset;
  486. lowLimit = (const BYTE*)source - cctx->dictSize;
  487. break;
  488. case usingExtDict:
  489. base = (const BYTE*)source - cctx->currentOffset;
  490. lowLimit = (const BYTE*)source;
  491. break;
  492. }
  493. if ((tableType == byU16) && (inputSize>=LZ4_64Klimit)) {
  494. return 0; /* Size too large (not within 64K limit) */
  495. }
  496. if (inputSize<LZ4_minLength) {
  497. goto _last_literals; /* Input too small, no compression (all literals) */
  498. }
  499. /* First Byte */
  500. LZ4_putPosition(ip, cctx->hashTable, tableType, base);
  501. ip++;
  502. forwardH = LZ4_hashPosition(ip, tableType);
  503. /* Main Loop */
  504. for ( ; ; ) {
  505. ptrdiff_t refDelta = 0;
  506. const BYTE* match;
  507. BYTE* token;
  508. /* Find a match */
  509. {
  510. const BYTE* forwardIp = ip;
  511. unsigned step = 1;
  512. unsigned searchMatchNb = acceleration << LZ4_skipTrigger;
  513. do {
  514. U32 const h = forwardH;
  515. ip = forwardIp;
  516. forwardIp += step;
  517. step = (searchMatchNb++ >> LZ4_skipTrigger);
  518. if (unlikely(forwardIp > mflimit)) {
  519. goto _last_literals;
  520. }
  521. match = LZ4_getPositionOnHash(h, cctx->hashTable, tableType, base);
  522. if (dict==usingExtDict) {
  523. if (match < (const BYTE*)source) {
  524. refDelta = dictDelta;
  525. lowLimit = dictionary;
  526. } else {
  527. refDelta = 0;
  528. lowLimit = (const BYTE*)source;
  529. }
  530. }
  531. forwardH = LZ4_hashPosition(forwardIp, tableType);
  532. LZ4_putPositionOnHash(ip, h, cctx->hashTable, tableType, base);
  533. } while ( ((dictIssue==dictSmall) ? (match < lowRefLimit) : 0)
  534. || ((tableType==byU16) ? 0 : (match + MAX_DISTANCE < ip))
  535. || (LZ4_read32(match+refDelta) != LZ4_read32(ip)) );
  536. }
  537. /* Catch up */
  538. while (((ip>anchor) & (match+refDelta > lowLimit)) && (unlikely(ip[-1]==match[refDelta-1]))) {
  539. ip--;
  540. match--;
  541. }
  542. /* Encode Literals */
  543. {
  544. unsigned const litLength = (unsigned)(ip - anchor);
  545. token = op++;
  546. if ((outputLimited) && /* Check output buffer overflow */
  547. (unlikely(op + litLength + (2 + 1 + LASTLITERALS) + (litLength/255) > olimit))) {
  548. return 0;
  549. }
  550. if (litLength >= RUN_MASK) {
  551. int len = (int)litLength-RUN_MASK;
  552. *token = (RUN_MASK<<ML_BITS);
  553. for(; len >= 255 ; len-=255) {
  554. *op++ = 255;
  555. }
  556. *op++ = (BYTE)len;
  557. } else {
  558. *token = (BYTE)(litLength<<ML_BITS);
  559. }
  560. /* Copy Literals */
  561. LZ4_wildCopy(op, anchor, op+litLength);
  562. op+=litLength;
  563. }
  564. _next_match:
  565. /* Encode Offset */
  566. LZ4_writeLE16(op, (U16)(ip-match));
  567. op+=2;
  568. /* Encode MatchLength */
  569. {
  570. unsigned matchCode;
  571. if ((dict==usingExtDict) && (lowLimit==dictionary)) {
  572. const BYTE* limit;
  573. match += refDelta;
  574. limit = ip + (dictEnd-match);
  575. if (limit > matchlimit) {
  576. limit = matchlimit;
  577. }
  578. matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, limit);
  579. ip += MINMATCH + matchCode;
  580. if (ip==limit) {
  581. unsigned const more = LZ4_count(ip, (const BYTE*)source, matchlimit);
  582. matchCode += more;
  583. ip += more;
  584. }
  585. } else {
  586. matchCode = LZ4_count(ip+MINMATCH, match+MINMATCH, matchlimit);
  587. ip += MINMATCH + matchCode;
  588. }
  589. if ( outputLimited && /* Check output buffer overflow */
  590. (unlikely(op + (1 + LASTLITERALS) + (matchCode>>8) > olimit)) ) {
  591. return 0;
  592. }
  593. if (matchCode >= ML_MASK) {
  594. *token += ML_MASK;
  595. matchCode -= ML_MASK;
  596. LZ4_write32(op, 0xFFFFFFFF);
  597. while (matchCode >= 4*255) {
  598. op+=4;
  599. LZ4_write32(op, 0xFFFFFFFF);
  600. matchCode -= 4*255;
  601. }
  602. op += matchCode / 255;
  603. *op++ = (BYTE)(matchCode % 255);
  604. } else {
  605. *token += (BYTE)(matchCode);
  606. }
  607. }
  608. anchor = ip;
  609. /* Test end of chunk */
  610. if (ip > mflimit) {
  611. break;
  612. }
  613. /* Fill table */
  614. LZ4_putPosition(ip-2, cctx->hashTable, tableType, base);
  615. /* Test next position */
  616. match = LZ4_getPosition(ip, cctx->hashTable, tableType, base);
  617. if (dict==usingExtDict) {
  618. if (match < (const BYTE*)source) {
  619. refDelta = dictDelta;
  620. lowLimit = dictionary;
  621. } else {
  622. refDelta = 0;
  623. lowLimit = (const BYTE*)source;
  624. }
  625. }
  626. LZ4_putPosition(ip, cctx->hashTable, tableType, base);
  627. if ( ((dictIssue==dictSmall) ? (match>=lowRefLimit) : 1)
  628. && (match+MAX_DISTANCE>=ip)
  629. && (LZ4_read32(match+refDelta)==LZ4_read32(ip)) ) {
  630. token=op++;
  631. *token=0;
  632. goto _next_match;
  633. }
  634. /* Prepare next loop */
  635. forwardH = LZ4_hashPosition(++ip, tableType);
  636. }
  637. _last_literals:
  638. /* Encode Last Literals */
  639. {
  640. size_t const lastRun = (size_t)(iend - anchor);
  641. if ( (outputLimited) && /* Check output buffer overflow */
  642. ((op - (BYTE*)dest) + lastRun + 1 + ((lastRun+255-RUN_MASK)/255) > (U32)maxOutputSize) ) {
  643. return 0;
  644. }
  645. if (lastRun >= RUN_MASK) {
  646. size_t accumulator = lastRun - RUN_MASK;
  647. *op++ = RUN_MASK << ML_BITS;
  648. for(; accumulator >= 255 ; accumulator-=255) {
  649. *op++ = 255;
  650. }
  651. *op++ = (BYTE) accumulator;
  652. } else {
  653. *op++ = (BYTE)(lastRun<<ML_BITS);
  654. }
  655. memcpy(op, anchor, lastRun);
  656. op += lastRun;
  657. }
  658. /* End */
  659. return (int) (((char*)op)-dest);
  660. }
  661. static inline int LZ4_compress_fast_extState(void* state, const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
  662. {
  663. LZ4_stream_t_internal* ctx = &((LZ4_stream_t*)state)->internal_donotuse;
  664. LZ4_resetStream((LZ4_stream_t*)state);
  665. //if (acceleration < 1) acceleration = ACCELERATION_DEFAULT;
  666. if (maxOutputSize >= LZ4_compressBound(inputSize)) {
  667. if (inputSize < LZ4_64Klimit) {
  668. return LZ4_compress_generic(ctx, source, dest, inputSize, 0, notLimited, byU16, noDict, noDictIssue, acceleration);
  669. } else {
  670. return LZ4_compress_generic(ctx, source, dest, inputSize, 0, notLimited, (sizeof(void*)==8) ? byU32 : byPtr, noDict, noDictIssue, acceleration);
  671. }
  672. } else {
  673. if (inputSize < LZ4_64Klimit) {
  674. return LZ4_compress_generic(ctx, source, dest, inputSize, maxOutputSize, limitedOutput, byU16, noDict, noDictIssue, acceleration);
  675. } else {
  676. return LZ4_compress_generic(ctx, source, dest, inputSize, maxOutputSize, limitedOutput, (sizeof(void*)==8) ? byU32 : byPtr, noDict, noDictIssue, acceleration);
  677. }
  678. }
  679. }
  680. static inline int LZ4_compress_fast(const char* source, char* dest, int inputSize, int maxOutputSize, int acceleration)
  681. {
  682. #if (HEAPMODE)
  683. void* ctxPtr = ALLOCATOR(1, sizeof(LZ4_stream_t)); /* malloc-calloc always properly aligned */
  684. #else
  685. LZ4_stream_t ctx;
  686. void* const ctxPtr = &ctx;
  687. #endif
  688. int const result = LZ4_compress_fast_extState(ctxPtr, source, dest, inputSize, maxOutputSize, acceleration);
  689. #if (HEAPMODE)
  690. FREEMEM(ctxPtr);
  691. #endif
  692. return result;
  693. }
  694. static inline void LZ4_resetStream (LZ4_stream_t* LZ4_stream)
  695. {
  696. MEM_INIT(LZ4_stream, 0, sizeof(LZ4_stream_t));
  697. }
  698. FORCE_INLINE int LZ4_decompress_generic(
  699. const char* const source,
  700. char* const dest,
  701. int inputSize,
  702. int outputSize, /* If endOnInput==endOnInputSize, this value is the max size of Output Buffer. */
  703. int endOnInput, /* endOnOutputSize, endOnInputSize */
  704. int partialDecoding, /* full, partial */
  705. int targetOutputSize, /* only used if partialDecoding==partial */
  706. int dict, /* noDict, withPrefix64k, usingExtDict */
  707. const BYTE* const lowPrefix, /* == dest when no prefix */
  708. const BYTE* const dictStart, /* only if dict==usingExtDict */
  709. const size_t dictSize /* note : = 0 if noDict */
  710. )
  711. {
  712. /* Local Variables */
  713. const BYTE* ip = (const BYTE*) source;
  714. const BYTE* const iend = ip + inputSize;
  715. BYTE* op = (BYTE*) dest;
  716. BYTE* const oend = op + outputSize;
  717. BYTE* cpy;
  718. BYTE* oexit = op + targetOutputSize;
  719. const BYTE* const lowLimit = lowPrefix - dictSize;
  720. const BYTE* const dictEnd = (const BYTE*)dictStart + dictSize;
  721. const unsigned dec32table[] = {0, 1, 2, 1, 4, 4, 4, 4};
  722. const int dec64table[] = {0, 0, 0, -1, 0, 1, 2, 3};
  723. const int safeDecode = (endOnInput==endOnInputSize);
  724. const int checkOffset = ((safeDecode) && (dictSize < (int)(64 KB)));
  725. /* Special cases */
  726. if ((partialDecoding) && (oexit > oend-MFLIMIT)) {
  727. oexit = oend-MFLIMIT; /* targetOutputSize too high => decode everything */
  728. }
  729. if ((endOnInput) && (unlikely(outputSize==0))) {
  730. return ((inputSize==1) && (*ip==0)) ? 0 : -1; /* Empty output buffer */
  731. }
  732. if ((!endOnInput) && (unlikely(outputSize==0))) {
  733. return (*ip==0?1:-1);
  734. }
  735. /* Main Loop : decode sequences */
  736. while (1) {
  737. size_t length;
  738. const BYTE* match;
  739. size_t offset;
  740. /* get literal length */
  741. unsigned const token = *ip++;
  742. if ((length=(token>>ML_BITS)) == RUN_MASK) {
  743. unsigned s;
  744. do {
  745. s = *ip++;
  746. length += s;
  747. } while ( likely(endOnInput ? ip<iend-RUN_MASK : 1) & (s==255) );
  748. if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)(op))) {
  749. goto _output_error; /* overflow detection */
  750. }
  751. if ((safeDecode) && unlikely((uptrval)(ip)+length<(uptrval)(ip))) {
  752. goto _output_error; /* overflow detection */
  753. }
  754. }
  755. /* copy literals */
  756. cpy = op+length;
  757. if ( ((endOnInput) && ((cpy>(partialDecoding?oexit:oend-MFLIMIT)) || (ip+length>iend-(2+1+LASTLITERALS))) )
  758. || ((!endOnInput) && (cpy>oend-WILDCOPYLENGTH)) ) {
  759. if (partialDecoding) {
  760. if (cpy > oend) {
  761. goto _output_error; /* Error : write attempt beyond end of output buffer */
  762. }
  763. if ((endOnInput) && (ip+length > iend)) {
  764. goto _output_error; /* Error : read attempt beyond end of input buffer */
  765. }
  766. } else {
  767. if ((!endOnInput) && (cpy != oend)) {
  768. goto _output_error; /* Error : block decoding must stop exactly there */
  769. }
  770. if ((endOnInput) && ((ip+length != iend) || (cpy > oend))) {
  771. goto _output_error; /* Error : input must be consumed */
  772. }
  773. }
  774. memcpy(op, ip, length);
  775. ip += length;
  776. op += length;
  777. break; /* Necessarily EOF, due to parsing restrictions */
  778. }
  779. LZ4_wildCopy(op, ip, cpy);
  780. ip += length;
  781. op = cpy;
  782. /* get offset */
  783. offset = LZ4_readLE16(ip);
  784. ip += 2;
  785. match = op - offset;
  786. if ((checkOffset) && (unlikely(match < lowLimit))) {
  787. goto _output_error; /* Error : offset outside buffers */
  788. }
  789. LZ4_write32(op, (U32)offset); /* costs ~1%; silence an msan warning when offset==0 */
  790. /* get matchlength */
  791. length = token & ML_MASK;
  792. if (length == ML_MASK) {
  793. unsigned s;
  794. do {
  795. s = *ip++;
  796. if ((endOnInput) && (ip > iend-LASTLITERALS)) {
  797. goto _output_error;
  798. }
  799. length += s;
  800. } while (s==255);
  801. if ((safeDecode) && unlikely((uptrval)(op)+length<(uptrval)op)) {
  802. goto _output_error; /* overflow detection */
  803. }
  804. }
  805. length += MINMATCH;
  806. /* check external dictionary */
  807. if ((dict==usingExtDict) && (match < lowPrefix)) {
  808. if (unlikely(op+length > oend-LASTLITERALS)) {
  809. goto _output_error; /* doesn't respect parsing restriction */
  810. }
  811. if (length <= (size_t)(lowPrefix-match)) {
  812. /* match can be copied as a single segment from external dictionary */
  813. memmove(op, dictEnd - (lowPrefix-match), length);
  814. op += length;
  815. } else {
  816. /* match encompass external dictionary and current block */
  817. size_t const copySize = (size_t)(lowPrefix-match);
  818. size_t const restSize = length - copySize;
  819. memcpy(op, dictEnd - copySize, copySize);
  820. op += copySize;
  821. if (restSize > (size_t)(op-lowPrefix)) { /* overlap copy */
  822. BYTE* const endOfMatch = op + restSize;
  823. const BYTE* copyFrom = lowPrefix;
  824. while (op < endOfMatch) {
  825. *op++ = *copyFrom++;
  826. }
  827. } else {
  828. memcpy(op, lowPrefix, restSize);
  829. op += restSize;
  830. }
  831. }
  832. continue;
  833. }
  834. /* copy match within block */
  835. cpy = op + length;
  836. if (unlikely(offset<8)) {
  837. const int dec64 = dec64table[offset];
  838. op[0] = match[0];
  839. op[1] = match[1];
  840. op[2] = match[2];
  841. op[3] = match[3];
  842. match += dec32table[offset];
  843. memcpy(op+4, match, 4);
  844. match -= dec64;
  845. } else {
  846. LZ4_copy8(op, match);
  847. match+=8;
  848. }
  849. op += 8;
  850. if (unlikely(cpy>oend-12)) {
  851. BYTE* const oCopyLimit = oend-(WILDCOPYLENGTH-1);
  852. if (cpy > oend-LASTLITERALS) {
  853. goto _output_error; /* Error : last LASTLITERALS bytes must be literals (uncompressed) */
  854. }
  855. if (op < oCopyLimit) {
  856. LZ4_wildCopy(op, match, oCopyLimit);
  857. match += oCopyLimit - op;
  858. op = oCopyLimit;
  859. }
  860. while (op<cpy) {
  861. *op++ = *match++;
  862. }
  863. } else {
  864. LZ4_copy8(op, match);
  865. if (length>16) {
  866. LZ4_wildCopy(op+8, match+8, cpy);
  867. }
  868. }
  869. op=cpy; /* correction */
  870. }
  871. /* end of decoding */
  872. if (endOnInput) {
  873. return (int) (((char*)op)-dest); /* Nb of output bytes decoded */
  874. } else {
  875. return (int) (((const char*)ip)-source); /* Nb of input bytes read */
  876. }
  877. /* Overflow error detected */
  878. _output_error:
  879. return (int) (-(((const char*)ip)-source))-1;
  880. }
  881. static inline int LZ4_decompress_safe(const char* source, char* dest, int compressedSize, int maxDecompressedSize)
  882. {
  883. return LZ4_decompress_generic(source, dest, compressedSize, maxDecompressedSize, endOnInputSize, full, 0, noDict, (BYTE*)dest, NULL, 0);
  884. }
  885. } // anonymous namespace
  886. /************************************************************************** */
  887. /************************************************************************** */
  888. const unsigned char Packet::ZERO_KEY[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  889. void Packet::armor(const void *key,bool encryptPayload,bool extendedArmor,const AES aesKeys[2],const Identity &identity)
  890. {
  891. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  892. this->setExtendedArmor(extendedArmor);
  893. if ((aesKeys) && (encryptPayload)) {
  894. setCipher(ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV);
  895. uint8_t *const payload = data + ZT_PACKET_IDX_VERB;
  896. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  897. AES::GMACSIVEncryptor enc(aesKeys[0],aesKeys[1]);
  898. enc.init(Utils::loadMachineEndian<uint64_t>(data + ZT_PACKET_IDX_IV),payload);
  899. enc.aad(data + ZT_PACKET_IDX_DEST,11);
  900. enc.update1(payload,payloadLen);
  901. enc.finish1();
  902. enc.update2(payload,payloadLen);
  903. const uint64_t *const tag = enc.finish2();
  904. #ifdef ZT_NO_UNALIGNED_ACCESS
  905. Utils::copy<8>(data,tag);
  906. Utils::copy<8>(data + ZT_PACKET_IDX_MAC,tag + 1);
  907. #else
  908. *reinterpret_cast<uint64_t *>(data + ZT_PACKET_IDX_IV) = tag[0];
  909. *reinterpret_cast<uint64_t *>(data + ZT_PACKET_IDX_MAC) = tag[1];
  910. #endif
  911. } else {
  912. setCipher(encryptPayload ? ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012 : ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE);
  913. uint8_t mangledKey[32];
  914. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  915. if (ZT_HAS_FAST_CRYPTO()) {
  916. const unsigned int payloadLen = (encryptPayload) ? (size() - ZT_PACKET_IDX_VERB) : 0;
  917. uint64_t keyStream[(ZT_PROTO_MAX_PACKET_LENGTH + 64 + 8) / 8];
  918. uint64_t mac[2];
  919. ZT_FAST_SINGLE_PASS_SALSA2012(keyStream,payloadLen + 64,(data + ZT_PACKET_IDX_IV),mangledKey);
  920. Salsa20::memxor(data + ZT_PACKET_IDX_VERB,reinterpret_cast<const uint8_t *>(keyStream + 8),payloadLen);
  921. Poly1305::compute(mac,data + ZT_PACKET_IDX_VERB,size() - ZT_PACKET_IDX_VERB,keyStream);
  922. #ifdef ZT_NO_TYPE_PUNNING
  923. memcpy(data + ZT_PACKET_IDX_MAC,mac,8);
  924. #else
  925. (*reinterpret_cast<uint64_t *>(data + ZT_PACKET_IDX_MAC)) = mac[0];
  926. #endif
  927. } else {
  928. uint64_t macKey[4];
  929. uint64_t mac[2];
  930. Salsa20 s20(mangledKey,data + ZT_PACKET_IDX_IV);
  931. s20.crypt12(ZERO_KEY,macKey,sizeof(macKey));
  932. uint8_t *const payload = data + ZT_PACKET_IDX_VERB;
  933. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  934. if (encryptPayload) {
  935. s20.crypt12(payload,payload,payloadLen);
  936. }
  937. Poly1305::compute(mac,payload,payloadLen,macKey);
  938. memcpy(data + ZT_PACKET_IDX_MAC,mac,8);
  939. }
  940. }
  941. if (extendedArmor) {
  942. ECC::Pair ephemeralKeyPair = ECC::generate();
  943. uint8_t ephemeralSymmetric[32];
  944. ECC::agree(ephemeralKeyPair, identity.publicKey(), ephemeralSymmetric, 32);
  945. AES cipher(ephemeralSymmetric);
  946. AES::CTR aesCtr(cipher);
  947. aesCtr.init(data, 0, data + ZT_PACKET_IDX_EXTENDED_ARMOR_START);
  948. aesCtr.crypt(data + ZT_PACKET_IDX_EXTENDED_ARMOR_START, size() - ZT_PACKET_IDX_EXTENDED_ARMOR_START);
  949. this->append(ephemeralKeyPair.pub.data, ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN);
  950. }
  951. }
  952. bool Packet::dearmor(const void *key,const AES aesKeys[2],const Identity &identity)
  953. {
  954. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  955. if (extendedArmor()) {
  956. if (size() < ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN) {
  957. return false;
  958. }
  959. uint8_t ephemeralSymmetric[32];
  960. ECC::Public ephemeralKey;
  961. memcpy(ephemeralKey.data, data + (size() - ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN), ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN);
  962. ECC::agree(identity.privateKeyPair(), ephemeralKey, ephemeralSymmetric, 32);
  963. AES cipher(ephemeralSymmetric);
  964. AES::CTR aesCtr(cipher);
  965. aesCtr.init(data, 0, data + ZT_PACKET_IDX_EXTENDED_ARMOR_START);
  966. aesCtr.crypt(data + ZT_PACKET_IDX_EXTENDED_ARMOR_START, size() - ZT_PACKET_IDX_EXTENDED_ARMOR_START);
  967. this->setSize(size() - ZT_ECC_EPHEMERAL_PUBLIC_KEY_LEN);
  968. /* Note: both the MAC and the data were encrypted with the ephemeral key. We don't need
  969. * a separate MAC for the ephemeral encryption because the MAC check below is obviously
  970. * going to fail if the ephemeral key was incorrect. */
  971. }
  972. const unsigned int payloadLen = size() - ZT_PACKET_IDX_VERB;
  973. unsigned char *const payload = data + ZT_PACKET_IDX_VERB;
  974. const unsigned int cs = cipher();
  975. if (cs == ZT_PROTO_CIPHER_SUITE__AES_GMAC_SIV) {
  976. if (aesKeys) {
  977. uint64_t tag[2];
  978. #ifdef ZT_NO_UNALIGNED_ACCESS
  979. Utils::copy<8>(tag, data);
  980. Utils::copy<8>(tag + 1, data + ZT_PACKET_IDX_MAC);
  981. #else
  982. tag[0] = *reinterpret_cast<uint64_t *>(data + ZT_PACKET_IDX_IV);
  983. tag[1] = *reinterpret_cast<uint64_t *>(data + ZT_PACKET_IDX_MAC);
  984. #endif
  985. AES::GMACSIVDecryptor dec(aesKeys[0],aesKeys[1]);
  986. dec.init(tag, payload);
  987. const uint8_t oldFlags = data[ZT_PACKET_IDX_FLAGS];
  988. data[ZT_PACKET_IDX_FLAGS] &= 0xf8;
  989. dec.aad(data + ZT_PACKET_IDX_DEST,11);
  990. data[ZT_PACKET_IDX_FLAGS] = oldFlags;
  991. dec.update(payload, payloadLen);
  992. return dec.finish();
  993. }
  994. } else if ((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_NONE)||(cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012)) {
  995. uint8_t mangledKey[32];
  996. _salsa20MangleKey((const unsigned char *)key,mangledKey);
  997. if (ZT_HAS_FAST_CRYPTO()) {
  998. uint64_t keyStream[(ZT_PROTO_MAX_PACKET_LENGTH + 64 + 8) / 8];
  999. ZT_FAST_SINGLE_PASS_SALSA2012(keyStream,((cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) ? (payloadLen + 64) : 64),(data + ZT_PACKET_IDX_IV),mangledKey);
  1000. uint64_t mac[2];
  1001. Poly1305::compute(mac,payload,payloadLen,keyStream);
  1002. #ifdef ZT_NO_TYPE_PUNNING
  1003. if (!Utils::secureEq(mac,data + ZT_PACKET_IDX_MAC,8)) {
  1004. return false;
  1005. }
  1006. #else
  1007. if ((*reinterpret_cast<const uint64_t *>(data + ZT_PACKET_IDX_MAC)) != mac[0]) { // also secure, constant time
  1008. return false;
  1009. }
  1010. #endif
  1011. if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) {
  1012. Salsa20::memxor(data + ZT_PACKET_IDX_VERB,reinterpret_cast<const uint8_t *>(keyStream + 8),payloadLen);
  1013. }
  1014. } else {
  1015. Salsa20 s20(mangledKey,data + ZT_PACKET_IDX_IV);
  1016. uint64_t macKey[4];
  1017. s20.crypt12(ZERO_KEY,macKey,sizeof(macKey));
  1018. uint64_t mac[2];
  1019. Poly1305::compute(mac,payload,payloadLen,macKey);
  1020. #ifdef ZT_NO_TYPE_PUNNING
  1021. if (!Utils::secureEq(mac,data + ZT_PACKET_IDX_MAC,8)) {
  1022. return false;
  1023. }
  1024. #else
  1025. if ((*reinterpret_cast<const uint64_t *>(data + ZT_PACKET_IDX_MAC)) != mac[0]) { // also secure, constant time
  1026. return false;
  1027. }
  1028. #endif
  1029. if (cs == ZT_PROTO_CIPHER_SUITE__C25519_POLY1305_SALSA2012) {
  1030. s20.crypt12(payload,payload,payloadLen);
  1031. }
  1032. }
  1033. return true;
  1034. }
  1035. return false;
  1036. }
  1037. void Packet::cryptField(const void *key,unsigned int start,unsigned int len)
  1038. {
  1039. uint8_t *const data = reinterpret_cast<uint8_t *>(unsafeData());
  1040. uint8_t iv[8];
  1041. for(int i=0;i<8;++i) {
  1042. iv[i] = data[i];
  1043. }
  1044. iv[7] &= 0xf8; // mask off least significant 3 bits of packet ID / IV since this is unset when this function gets called
  1045. Salsa20 s20(key,iv);
  1046. s20.crypt12(data + start,data + start,len);
  1047. }
  1048. bool Packet::compress()
  1049. {
  1050. char *const data = reinterpret_cast<char *>(unsafeData());
  1051. char buf[ZT_PROTO_MAX_PACKET_LENGTH * 2];
  1052. if ((!compressed())&&(size() > (ZT_PACKET_IDX_PAYLOAD + 64))) { // don't bother compressing tiny packets
  1053. int pl = (int)(size() - ZT_PACKET_IDX_PAYLOAD);
  1054. int cl = LZ4_compress_fast(data + ZT_PACKET_IDX_PAYLOAD,buf,pl,ZT_PROTO_MAX_PACKET_LENGTH * 2,1);
  1055. if ((cl > 0)&&(cl < pl)) {
  1056. data[ZT_PACKET_IDX_VERB] |= (char)ZT_PROTO_VERB_FLAG_COMPRESSED;
  1057. setSize((unsigned int)cl + ZT_PACKET_IDX_PAYLOAD);
  1058. memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,cl);
  1059. return true;
  1060. }
  1061. }
  1062. data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  1063. return false;
  1064. }
  1065. bool Packet::uncompress()
  1066. {
  1067. char *const data = reinterpret_cast<char *>(unsafeData());
  1068. char buf[ZT_PROTO_MAX_PACKET_LENGTH];
  1069. if ((compressed())&&(size() >= ZT_PROTO_MIN_PACKET_LENGTH)) {
  1070. if (size() > ZT_PACKET_IDX_PAYLOAD) {
  1071. unsigned int compLen = size() - ZT_PACKET_IDX_PAYLOAD;
  1072. int ucl = LZ4_decompress_safe((const char *)data + ZT_PACKET_IDX_PAYLOAD,buf,compLen,sizeof(buf));
  1073. if ((ucl > 0)&&(ucl <= (int)(capacity() - ZT_PACKET_IDX_PAYLOAD))) {
  1074. setSize((unsigned int)ucl + ZT_PACKET_IDX_PAYLOAD);
  1075. memcpy(data + ZT_PACKET_IDX_PAYLOAD,buf,ucl);
  1076. } else {
  1077. return false;
  1078. }
  1079. }
  1080. data[ZT_PACKET_IDX_VERB] &= (char)(~ZT_PROTO_VERB_FLAG_COMPRESSED);
  1081. }
  1082. return true;
  1083. }
  1084. } // namespace ZeroTier