Poly1305.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /*
  2. 20080912
  3. D. J. Bernstein
  4. Public domain.
  5. */
  6. #include "Constants.hpp"
  7. #include "Poly1305.hpp"
  8. #include <cstring>
  9. #ifdef __WINDOWS__
  10. #pragma warning(disable: 4146)
  11. #endif
  12. namespace ZeroTier {
  13. namespace {
  14. typedef struct poly1305_context {
  15. size_t aligner;
  16. unsigned char opaque[136];
  17. } poly1305_context;
  18. #if (defined(_MSC_VER) || defined(__GNUC__) || defined(__clang)) && (defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64))
  19. //////////////////////////////////////////////////////////////////////////////
  20. // 128-bit implementation for MSC and GCC from Poly1305-donna
  21. #if defined(_MSC_VER)
  22. #include <intrin.h>
  23. typedef struct uint128_t {
  24. unsigned long long lo;
  25. unsigned long long hi;
  26. } uint128_t;
  27. #define MUL(out, x, y) out.lo = _umul128((x), (y), &out.hi)
  28. #define ADD(out, in) { unsigned long long t = out.lo; out.lo += in.lo; out.hi += (out.lo < t) + in.hi; }
  29. #define ADDLO(out, in) { unsigned long long t = out.lo; out.lo += in; out.hi += (out.lo < t); }
  30. #define SHR(in, shift) (__shiftright128(in.lo, in.hi, (shift)))
  31. #define LO(in) (in.lo)
  32. // #define POLY1305_NOINLINE __declspec(noinline)
  33. #elif defined(__GNUC__)
  34. #if defined(__SIZEOF_INT128__)
  35. typedef unsigned __int128 uint128_t;
  36. #else
  37. typedef unsigned uint128_t __attribute__((mode(TI)));
  38. #endif
  39. #define MUL(out, x, y) out = ((uint128_t)x * y)
  40. #define ADD(out, in) out += in
  41. #define ADDLO(out, in) out += in
  42. #define SHR(in, shift) (unsigned long long)(in >> (shift))
  43. #define LO(in) (unsigned long long)(in)
  44. // #define POLY1305_NOINLINE __attribute__((noinline))
  45. #endif
  46. #define poly1305_block_size 16
  47. /* 17 + sizeof(size_t) + 8*sizeof(unsigned long long) */
  48. typedef struct poly1305_state_internal_t {
  49. unsigned long long r[3];
  50. unsigned long long h[3];
  51. unsigned long long pad[2];
  52. size_t leftover;
  53. unsigned char buffer[poly1305_block_size];
  54. unsigned char final;
  55. } poly1305_state_internal_t;
  56. #if defined(ZT_NO_UNALIGNED_ACCESS) || (__BYTE_ORDER != __LITTLE_ENDIAN)
  57. static inline unsigned long long U8TO64(const unsigned char *p)
  58. {
  59. return
  60. (((unsigned long long)(p[0] & 0xff) ) |
  61. ((unsigned long long)(p[1] & 0xff) << 8) |
  62. ((unsigned long long)(p[2] & 0xff) << 16) |
  63. ((unsigned long long)(p[3] & 0xff) << 24) |
  64. ((unsigned long long)(p[4] & 0xff) << 32) |
  65. ((unsigned long long)(p[5] & 0xff) << 40) |
  66. ((unsigned long long)(p[6] & 0xff) << 48) |
  67. ((unsigned long long)(p[7] & 0xff) << 56));
  68. }
  69. #else
  70. #define U8TO64(p) (*reinterpret_cast<const unsigned long long *>(p))
  71. #endif
  72. #if defined(ZT_NO_UNALIGNED_ACCESS) || (__BYTE_ORDER != __LITTLE_ENDIAN)
  73. static inline void U64TO8(unsigned char *p, unsigned long long v)
  74. {
  75. p[0] = (v ) & 0xff;
  76. p[1] = (v >> 8) & 0xff;
  77. p[2] = (v >> 16) & 0xff;
  78. p[3] = (v >> 24) & 0xff;
  79. p[4] = (v >> 32) & 0xff;
  80. p[5] = (v >> 40) & 0xff;
  81. p[6] = (v >> 48) & 0xff;
  82. p[7] = (v >> 56) & 0xff;
  83. }
  84. #else
  85. #define U64TO8(p,v) ((*reinterpret_cast<unsigned long long *>(p)) = (v))
  86. #endif
  87. static inline void poly1305_init(poly1305_context *ctx, const unsigned char key[32])
  88. {
  89. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  90. unsigned long long t0,t1;
  91. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  92. t0 = U8TO64(&key[0]);
  93. t1 = U8TO64(&key[8]);
  94. st->r[0] = ( t0 ) & 0xffc0fffffff;
  95. st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
  96. st->r[2] = ((t1 >> 24) ) & 0x00ffffffc0f;
  97. /* h = 0 */
  98. st->h[0] = 0;
  99. st->h[1] = 0;
  100. st->h[2] = 0;
  101. /* save pad for later */
  102. st->pad[0] = U8TO64(&key[16]);
  103. st->pad[1] = U8TO64(&key[24]);
  104. st->leftover = 0;
  105. st->final = 0;
  106. }
  107. static inline void poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes)
  108. {
  109. const unsigned long long hibit = (st->final) ? 0 : ((unsigned long long)1 << 40); /* 1 << 128 */
  110. unsigned long long r0,r1,r2;
  111. unsigned long long s1,s2;
  112. unsigned long long h0,h1,h2;
  113. uint128_t d0,d1,d2,d;
  114. r0 = st->r[0];
  115. r1 = st->r[1];
  116. r2 = st->r[2];
  117. h0 = st->h[0];
  118. h1 = st->h[1];
  119. h2 = st->h[2];
  120. s1 = r1 * (5 << 2);
  121. s2 = r2 * (5 << 2);
  122. while (bytes >= poly1305_block_size) {
  123. unsigned long long t0,t1;
  124. /* h += m[i] */
  125. t0 = U8TO64(&m[0]);
  126. t1 = U8TO64(&m[8]);
  127. h0 += (( t0 ) & 0xfffffffffff);
  128. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
  129. h2 += (((t1 >> 24) ) & 0x3ffffffffff) | hibit;
  130. /* h *= r */
  131. MUL(d0, h0, r0); MUL(d, h1, s2); ADD(d0, d); MUL(d, h2, s1); ADD(d0, d);
  132. MUL(d1, h0, r1); MUL(d, h1, r0); ADD(d1, d); MUL(d, h2, s2); ADD(d1, d);
  133. MUL(d2, h0, r2); MUL(d, h1, r1); ADD(d2, d); MUL(d, h2, r0); ADD(d2, d);
  134. /* (partial) h %= p */
  135. unsigned long long c = SHR(d0, 44); h0 = LO(d0) & 0xfffffffffff;
  136. ADDLO(d1, c); c = SHR(d1, 44); h1 = LO(d1) & 0xfffffffffff;
  137. ADDLO(d2, c); c = SHR(d2, 42); h2 = LO(d2) & 0x3ffffffffff;
  138. h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffff;
  139. h1 += c;
  140. m += poly1305_block_size;
  141. bytes -= poly1305_block_size;
  142. }
  143. st->h[0] = h0;
  144. st->h[1] = h1;
  145. st->h[2] = h2;
  146. }
  147. static inline void poly1305_finish(poly1305_context *ctx, unsigned char mac[16])
  148. {
  149. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  150. unsigned long long h0,h1,h2,c;
  151. unsigned long long g0,g1,g2;
  152. unsigned long long t0,t1;
  153. /* process the remaining block */
  154. if (st->leftover) {
  155. size_t i = st->leftover;
  156. st->buffer[i] = 1;
  157. for (i = i + 1; i < poly1305_block_size; i++)
  158. st->buffer[i] = 0;
  159. st->final = 1;
  160. poly1305_blocks(st, st->buffer, poly1305_block_size);
  161. }
  162. /* fully carry h */
  163. h0 = st->h[0];
  164. h1 = st->h[1];
  165. h2 = st->h[2];
  166. c = (h1 >> 44); h1 &= 0xfffffffffff;
  167. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  168. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  169. h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  170. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  171. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  172. h1 += c;
  173. /* compute h + -p */
  174. g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
  175. g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
  176. g2 = h2 + c - ((unsigned long long)1 << 42);
  177. /* select h if h < p, or h + -p if h >= p */
  178. c = (g2 >> ((sizeof(unsigned long long) * 8) - 1)) - 1;
  179. g0 &= c;
  180. g1 &= c;
  181. g2 &= c;
  182. c = ~c;
  183. h0 = (h0 & c) | g0;
  184. h1 = (h1 & c) | g1;
  185. h2 = (h2 & c) | g2;
  186. /* h = (h + pad) */
  187. t0 = st->pad[0];
  188. t1 = st->pad[1];
  189. h0 += (( t0 ) & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff;
  190. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  191. h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c; h2 &= 0x3ffffffffff;
  192. /* mac = h % (2^128) */
  193. h0 = ((h0 ) | (h1 << 44));
  194. h1 = ((h1 >> 20) | (h2 << 24));
  195. U64TO8(&mac[0], h0);
  196. U64TO8(&mac[8], h1);
  197. /* zero out the state */
  198. st->h[0] = 0;
  199. st->h[1] = 0;
  200. st->h[2] = 0;
  201. st->r[0] = 0;
  202. st->r[1] = 0;
  203. st->r[2] = 0;
  204. st->pad[0] = 0;
  205. st->pad[1] = 0;
  206. }
  207. //////////////////////////////////////////////////////////////////////////////
  208. #else
  209. //////////////////////////////////////////////////////////////////////////////
  210. // More portable 64-bit implementation
  211. #define poly1305_block_size 16
  212. /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
  213. typedef struct poly1305_state_internal_t {
  214. unsigned long r[5];
  215. unsigned long h[5];
  216. unsigned long pad[4];
  217. size_t leftover;
  218. unsigned char buffer[poly1305_block_size];
  219. unsigned char final;
  220. } poly1305_state_internal_t;
  221. /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
  222. static inline unsigned long
  223. U8TO32(const unsigned char *p) {
  224. return
  225. (((unsigned long)(p[0] & 0xff) ) |
  226. ((unsigned long)(p[1] & 0xff) << 8) |
  227. ((unsigned long)(p[2] & 0xff) << 16) |
  228. ((unsigned long)(p[3] & 0xff) << 24));
  229. }
  230. /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */
  231. static inline void
  232. U32TO8(unsigned char *p, unsigned long v) {
  233. p[0] = (v ) & 0xff;
  234. p[1] = (v >> 8) & 0xff;
  235. p[2] = (v >> 16) & 0xff;
  236. p[3] = (v >> 24) & 0xff;
  237. }
  238. static inline void
  239. poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
  240. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  241. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  242. st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
  243. st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
  244. st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
  245. st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
  246. st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
  247. /* h = 0 */
  248. st->h[0] = 0;
  249. st->h[1] = 0;
  250. st->h[2] = 0;
  251. st->h[3] = 0;
  252. st->h[4] = 0;
  253. /* save pad for later */
  254. st->pad[0] = U8TO32(&key[16]);
  255. st->pad[1] = U8TO32(&key[20]);
  256. st->pad[2] = U8TO32(&key[24]);
  257. st->pad[3] = U8TO32(&key[28]);
  258. st->leftover = 0;
  259. st->final = 0;
  260. }
  261. static inline void
  262. poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
  263. const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */
  264. unsigned long r0,r1,r2,r3,r4;
  265. unsigned long s1,s2,s3,s4;
  266. unsigned long h0,h1,h2,h3,h4;
  267. r0 = st->r[0];
  268. r1 = st->r[1];
  269. r2 = st->r[2];
  270. r3 = st->r[3];
  271. r4 = st->r[4];
  272. s1 = r1 * 5;
  273. s2 = r2 * 5;
  274. s3 = r3 * 5;
  275. s4 = r4 * 5;
  276. h0 = st->h[0];
  277. h1 = st->h[1];
  278. h2 = st->h[2];
  279. h3 = st->h[3];
  280. h4 = st->h[4];
  281. while (bytes >= poly1305_block_size) {
  282. /* h += m[i] */
  283. h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
  284. h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
  285. h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
  286. h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
  287. h4 += (U8TO32(m+12) >> 8) | hibit;
  288. /* h *= r */
  289. unsigned long long d0 = ((unsigned long long)h0 * r0) + ((unsigned long long)h1 * s4) + ((unsigned long long)h2 * s3) + ((unsigned long long)h3 * s2) + ((unsigned long long)h4 * s1);
  290. unsigned long long d1 = ((unsigned long long)h0 * r1) + ((unsigned long long)h1 * r0) + ((unsigned long long)h2 * s4) + ((unsigned long long)h3 * s3) + ((unsigned long long)h4 * s2);
  291. unsigned long long d2 = ((unsigned long long)h0 * r2) + ((unsigned long long)h1 * r1) + ((unsigned long long)h2 * r0) + ((unsigned long long)h3 * s4) + ((unsigned long long)h4 * s3);
  292. unsigned long long d3 = ((unsigned long long)h0 * r3) + ((unsigned long long)h1 * r2) + ((unsigned long long)h2 * r1) + ((unsigned long long)h3 * r0) + ((unsigned long long)h4 * s4);
  293. unsigned long long d4 = ((unsigned long long)h0 * r4) + ((unsigned long long)h1 * r3) + ((unsigned long long)h2 * r2) + ((unsigned long long)h3 * r1) + ((unsigned long long)h4 * r0);
  294. /* (partial) h %= p */
  295. unsigned long c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff;
  296. d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff;
  297. d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff;
  298. d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff;
  299. d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff;
  300. h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
  301. h1 += c;
  302. m += poly1305_block_size;
  303. bytes -= poly1305_block_size;
  304. }
  305. st->h[0] = h0;
  306. st->h[1] = h1;
  307. st->h[2] = h2;
  308. st->h[3] = h3;
  309. st->h[4] = h4;
  310. }
  311. static inline void
  312. poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
  313. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  314. unsigned long h0,h1,h2,h3,h4,c;
  315. unsigned long g0,g1,g2,g3,g4;
  316. unsigned long long f;
  317. unsigned long mask;
  318. /* process the remaining block */
  319. if (st->leftover) {
  320. size_t i = st->leftover;
  321. st->buffer[i++] = 1;
  322. for (; i < poly1305_block_size; i++)
  323. st->buffer[i] = 0;
  324. st->final = 1;
  325. poly1305_blocks(st, st->buffer, poly1305_block_size);
  326. }
  327. /* fully carry h */
  328. h0 = st->h[0];
  329. h1 = st->h[1];
  330. h2 = st->h[2];
  331. h3 = st->h[3];
  332. h4 = st->h[4];
  333. c = h1 >> 26; h1 = h1 & 0x3ffffff;
  334. h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
  335. h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
  336. h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
  337. h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
  338. h1 += c;
  339. /* compute h + -p */
  340. g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
  341. g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
  342. g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
  343. g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
  344. g4 = h4 + c - (1 << 26);
  345. /* select h if h < p, or h + -p if h >= p */
  346. mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
  347. g0 &= mask;
  348. g1 &= mask;
  349. g2 &= mask;
  350. g3 &= mask;
  351. g4 &= mask;
  352. mask = ~mask;
  353. h0 = (h0 & mask) | g0;
  354. h1 = (h1 & mask) | g1;
  355. h2 = (h2 & mask) | g2;
  356. h3 = (h3 & mask) | g3;
  357. h4 = (h4 & mask) | g4;
  358. /* h = h % (2^128) */
  359. h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
  360. h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
  361. h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
  362. h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
  363. /* mac = (h + pad) % (2^128) */
  364. f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f;
  365. f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f;
  366. f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
  367. f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
  368. U32TO8(mac + 0, h0);
  369. U32TO8(mac + 4, h1);
  370. U32TO8(mac + 8, h2);
  371. U32TO8(mac + 12, h3);
  372. /* zero out the state */
  373. st->h[0] = 0;
  374. st->h[1] = 0;
  375. st->h[2] = 0;
  376. st->h[3] = 0;
  377. st->h[4] = 0;
  378. st->r[0] = 0;
  379. st->r[1] = 0;
  380. st->r[2] = 0;
  381. st->r[3] = 0;
  382. st->r[4] = 0;
  383. st->pad[0] = 0;
  384. st->pad[1] = 0;
  385. st->pad[2] = 0;
  386. st->pad[3] = 0;
  387. }
  388. //////////////////////////////////////////////////////////////////////////////
  389. #endif // MSC/GCC or not
  390. static inline void poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
  391. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  392. size_t i;
  393. /* handle leftover */
  394. if (st->leftover) {
  395. size_t want = (poly1305_block_size - st->leftover);
  396. if (want > bytes)
  397. want = bytes;
  398. for (i = 0; i < want; i++)
  399. st->buffer[st->leftover + i] = m[i];
  400. bytes -= want;
  401. m += want;
  402. st->leftover += want;
  403. if (st->leftover < poly1305_block_size)
  404. return;
  405. poly1305_blocks(st, st->buffer, poly1305_block_size);
  406. st->leftover = 0;
  407. }
  408. /* process full blocks */
  409. if (bytes >= poly1305_block_size) {
  410. size_t want = (bytes & ~(poly1305_block_size - 1));
  411. poly1305_blocks(st, m, want);
  412. m += want;
  413. bytes -= want;
  414. }
  415. /* store leftover */
  416. if (bytes) {
  417. for (i = 0; i < bytes; i++)
  418. st->buffer[st->leftover + i] = m[i];
  419. st->leftover += bytes;
  420. }
  421. }
  422. } // anonymous namespace
  423. void poly1305(void *auth,const void *data,unsigned int len,const void *key)
  424. {
  425. poly1305_context ctx;
  426. poly1305_init(&ctx,reinterpret_cast<const unsigned char *>(key));
  427. poly1305_update(&ctx,reinterpret_cast<const unsigned char *>(data),(size_t)len);
  428. poly1305_finish(&ctx,reinterpret_cast<unsigned char *>(auth));
  429. }
  430. } // namespace ZeroTier