Poly1305.cpp 17 KB

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