Poly1305.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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__))
  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. /* interpret eight 8 bit unsigned integers as a 64 bit unsigned integer in little endian */
  154. static inline unsigned long long
  155. U8TO64(const unsigned char *p) {
  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. /* store a 64 bit unsigned integer as eight 8 bit unsigned integers in little endian */
  167. static inline void
  168. U64TO8(unsigned char *p, unsigned long long v) {
  169. p[0] = (v ) & 0xff;
  170. p[1] = (v >> 8) & 0xff;
  171. p[2] = (v >> 16) & 0xff;
  172. p[3] = (v >> 24) & 0xff;
  173. p[4] = (v >> 32) & 0xff;
  174. p[5] = (v >> 40) & 0xff;
  175. p[6] = (v >> 48) & 0xff;
  176. p[7] = (v >> 56) & 0xff;
  177. }
  178. static inline void
  179. poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
  180. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  181. unsigned long long t0,t1;
  182. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  183. t0 = U8TO64(&key[0]);
  184. t1 = U8TO64(&key[8]);
  185. st->r[0] = ( t0 ) & 0xffc0fffffff;
  186. st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
  187. st->r[2] = ((t1 >> 24) ) & 0x00ffffffc0f;
  188. /* h = 0 */
  189. st->h[0] = 0;
  190. st->h[1] = 0;
  191. st->h[2] = 0;
  192. /* save pad for later */
  193. st->pad[0] = U8TO64(&key[16]);
  194. st->pad[1] = U8TO64(&key[24]);
  195. st->leftover = 0;
  196. st->final = 0;
  197. }
  198. static inline void
  199. poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
  200. const unsigned long long hibit = (st->final) ? 0 : ((unsigned long long)1 << 40); /* 1 << 128 */
  201. unsigned long long r0,r1,r2;
  202. unsigned long long s1,s2;
  203. unsigned long long h0,h1,h2;
  204. unsigned long long c;
  205. uint128_t d0,d1,d2,d;
  206. r0 = st->r[0];
  207. r1 = st->r[1];
  208. r2 = st->r[2];
  209. h0 = st->h[0];
  210. h1 = st->h[1];
  211. h2 = st->h[2];
  212. s1 = r1 * (5 << 2);
  213. s2 = r2 * (5 << 2);
  214. while (bytes >= poly1305_block_size) {
  215. unsigned long long t0,t1;
  216. /* h += m[i] */
  217. t0 = U8TO64(&m[0]);
  218. t1 = U8TO64(&m[8]);
  219. h0 += (( t0 ) & 0xfffffffffff);
  220. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
  221. h2 += (((t1 >> 24) ) & 0x3ffffffffff) | hibit;
  222. /* h *= r */
  223. MUL(d0, h0, r0); MUL(d, h1, s2); ADD(d0, d); MUL(d, h2, s1); ADD(d0, d);
  224. MUL(d1, h0, r1); MUL(d, h1, r0); ADD(d1, d); MUL(d, h2, s2); ADD(d1, d);
  225. MUL(d2, h0, r2); MUL(d, h1, r1); ADD(d2, d); MUL(d, h2, r0); ADD(d2, d);
  226. /* (partial) h %= p */
  227. c = SHR(d0, 44); h0 = LO(d0) & 0xfffffffffff;
  228. ADDLO(d1, c); c = SHR(d1, 44); h1 = LO(d1) & 0xfffffffffff;
  229. ADDLO(d2, c); c = SHR(d2, 42); h2 = LO(d2) & 0x3ffffffffff;
  230. h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffff;
  231. h1 += c;
  232. m += poly1305_block_size;
  233. bytes -= poly1305_block_size;
  234. }
  235. st->h[0] = h0;
  236. st->h[1] = h1;
  237. st->h[2] = h2;
  238. }
  239. static inline void
  240. poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
  241. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  242. unsigned long long h0,h1,h2,c;
  243. unsigned long long g0,g1,g2;
  244. unsigned long long t0,t1;
  245. /* process the remaining block */
  246. if (st->leftover) {
  247. size_t i = st->leftover;
  248. st->buffer[i] = 1;
  249. for (i = i + 1; i < poly1305_block_size; i++)
  250. st->buffer[i] = 0;
  251. st->final = 1;
  252. poly1305_blocks(st, st->buffer, poly1305_block_size);
  253. }
  254. /* fully carry h */
  255. h0 = st->h[0];
  256. h1 = st->h[1];
  257. h2 = st->h[2];
  258. c = (h1 >> 44); h1 &= 0xfffffffffff;
  259. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  260. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  261. h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  262. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  263. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  264. h1 += c;
  265. /* compute h + -p */
  266. g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
  267. g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
  268. g2 = h2 + c - ((unsigned long long)1 << 42);
  269. /* select h if h < p, or h + -p if h >= p */
  270. c = (g2 >> ((sizeof(unsigned long long) * 8) - 1)) - 1;
  271. g0 &= c;
  272. g1 &= c;
  273. g2 &= c;
  274. c = ~c;
  275. h0 = (h0 & c) | g0;
  276. h1 = (h1 & c) | g1;
  277. h2 = (h2 & c) | g2;
  278. /* h = (h + pad) */
  279. t0 = st->pad[0];
  280. t1 = st->pad[1];
  281. h0 += (( t0 ) & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff;
  282. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  283. h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c; h2 &= 0x3ffffffffff;
  284. /* mac = h % (2^128) */
  285. h0 = ((h0 ) | (h1 << 44));
  286. h1 = ((h1 >> 20) | (h2 << 24));
  287. U64TO8(&mac[0], h0);
  288. U64TO8(&mac[8], h1);
  289. /* zero out the state */
  290. st->h[0] = 0;
  291. st->h[1] = 0;
  292. st->h[2] = 0;
  293. st->r[0] = 0;
  294. st->r[1] = 0;
  295. st->r[2] = 0;
  296. st->pad[0] = 0;
  297. st->pad[1] = 0;
  298. }
  299. //////////////////////////////////////////////////////////////////////////////
  300. #else
  301. //////////////////////////////////////////////////////////////////////////////
  302. // More portable 64-bit implementation
  303. #define poly1305_block_size 16
  304. /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
  305. typedef struct poly1305_state_internal_t {
  306. unsigned long r[5];
  307. unsigned long h[5];
  308. unsigned long pad[4];
  309. size_t leftover;
  310. unsigned char buffer[poly1305_block_size];
  311. unsigned char final;
  312. } poly1305_state_internal_t;
  313. /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
  314. static unsigned long
  315. U8TO32(const unsigned char *p) {
  316. return
  317. (((unsigned long)(p[0] & 0xff) ) |
  318. ((unsigned long)(p[1] & 0xff) << 8) |
  319. ((unsigned long)(p[2] & 0xff) << 16) |
  320. ((unsigned long)(p[3] & 0xff) << 24));
  321. }
  322. /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */
  323. static void
  324. U32TO8(unsigned char *p, unsigned long v) {
  325. p[0] = (v ) & 0xff;
  326. p[1] = (v >> 8) & 0xff;
  327. p[2] = (v >> 16) & 0xff;
  328. p[3] = (v >> 24) & 0xff;
  329. }
  330. static inline void
  331. poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
  332. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  333. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  334. st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
  335. st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
  336. st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
  337. st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
  338. st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
  339. /* h = 0 */
  340. st->h[0] = 0;
  341. st->h[1] = 0;
  342. st->h[2] = 0;
  343. st->h[3] = 0;
  344. st->h[4] = 0;
  345. /* save pad for later */
  346. st->pad[0] = U8TO32(&key[16]);
  347. st->pad[1] = U8TO32(&key[20]);
  348. st->pad[2] = U8TO32(&key[24]);
  349. st->pad[3] = U8TO32(&key[28]);
  350. st->leftover = 0;
  351. st->final = 0;
  352. }
  353. static inline void
  354. poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
  355. const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */
  356. unsigned long r0,r1,r2,r3,r4;
  357. unsigned long s1,s2,s3,s4;
  358. unsigned long h0,h1,h2,h3,h4;
  359. unsigned long long d0,d1,d2,d3,d4;
  360. unsigned long c;
  361. r0 = st->r[0];
  362. r1 = st->r[1];
  363. r2 = st->r[2];
  364. r3 = st->r[3];
  365. r4 = st->r[4];
  366. s1 = r1 * 5;
  367. s2 = r2 * 5;
  368. s3 = r3 * 5;
  369. s4 = r4 * 5;
  370. h0 = st->h[0];
  371. h1 = st->h[1];
  372. h2 = st->h[2];
  373. h3 = st->h[3];
  374. h4 = st->h[4];
  375. while (bytes >= poly1305_block_size) {
  376. /* h += m[i] */
  377. h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
  378. h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
  379. h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
  380. h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
  381. h4 += (U8TO32(m+12) >> 8) | hibit;
  382. /* h *= r */
  383. 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);
  384. 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);
  385. 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);
  386. 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);
  387. 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);
  388. /* (partial) h %= p */
  389. c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff;
  390. d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff;
  391. d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff;
  392. d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff;
  393. d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff;
  394. h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
  395. h1 += c;
  396. m += poly1305_block_size;
  397. bytes -= poly1305_block_size;
  398. }
  399. st->h[0] = h0;
  400. st->h[1] = h1;
  401. st->h[2] = h2;
  402. st->h[3] = h3;
  403. st->h[4] = h4;
  404. }
  405. static inline void
  406. poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
  407. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  408. unsigned long h0,h1,h2,h3,h4,c;
  409. unsigned long g0,g1,g2,g3,g4;
  410. unsigned long long f;
  411. unsigned long mask;
  412. /* process the remaining block */
  413. if (st->leftover) {
  414. size_t i = st->leftover;
  415. st->buffer[i++] = 1;
  416. for (; i < poly1305_block_size; i++)
  417. st->buffer[i] = 0;
  418. st->final = 1;
  419. poly1305_blocks(st, st->buffer, poly1305_block_size);
  420. }
  421. /* fully carry h */
  422. h0 = st->h[0];
  423. h1 = st->h[1];
  424. h2 = st->h[2];
  425. h3 = st->h[3];
  426. h4 = st->h[4];
  427. c = h1 >> 26; h1 = h1 & 0x3ffffff;
  428. h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
  429. h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
  430. h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
  431. h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
  432. h1 += c;
  433. /* compute h + -p */
  434. g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
  435. g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
  436. g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
  437. g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
  438. g4 = h4 + c - (1 << 26);
  439. /* select h if h < p, or h + -p if h >= p */
  440. mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
  441. g0 &= mask;
  442. g1 &= mask;
  443. g2 &= mask;
  444. g3 &= mask;
  445. g4 &= mask;
  446. mask = ~mask;
  447. h0 = (h0 & mask) | g0;
  448. h1 = (h1 & mask) | g1;
  449. h2 = (h2 & mask) | g2;
  450. h3 = (h3 & mask) | g3;
  451. h4 = (h4 & mask) | g4;
  452. /* h = h % (2^128) */
  453. h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
  454. h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
  455. h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
  456. h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
  457. /* mac = (h + pad) % (2^128) */
  458. f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f;
  459. f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f;
  460. f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
  461. f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
  462. U32TO8(mac + 0, h0);
  463. U32TO8(mac + 4, h1);
  464. U32TO8(mac + 8, h2);
  465. U32TO8(mac + 12, h3);
  466. /* zero out the state */
  467. st->h[0] = 0;
  468. st->h[1] = 0;
  469. st->h[2] = 0;
  470. st->h[3] = 0;
  471. st->h[4] = 0;
  472. st->r[0] = 0;
  473. st->r[1] = 0;
  474. st->r[2] = 0;
  475. st->r[3] = 0;
  476. st->r[4] = 0;
  477. st->pad[0] = 0;
  478. st->pad[1] = 0;
  479. st->pad[2] = 0;
  480. st->pad[3] = 0;
  481. }
  482. //////////////////////////////////////////////////////////////////////////////
  483. #endif // MSC/GCC or not
  484. static inline void
  485. poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
  486. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  487. size_t i;
  488. /* handle leftover */
  489. if (st->leftover) {
  490. size_t want = (poly1305_block_size - st->leftover);
  491. if (want > bytes)
  492. want = bytes;
  493. for (i = 0; i < want; i++)
  494. st->buffer[st->leftover + i] = m[i];
  495. bytes -= want;
  496. m += want;
  497. st->leftover += want;
  498. if (st->leftover < poly1305_block_size)
  499. return;
  500. poly1305_blocks(st, st->buffer, poly1305_block_size);
  501. st->leftover = 0;
  502. }
  503. /* process full blocks */
  504. if (bytes >= poly1305_block_size) {
  505. size_t want = (bytes & ~(poly1305_block_size - 1));
  506. poly1305_blocks(st, m, want);
  507. m += want;
  508. bytes -= want;
  509. }
  510. /* store leftover */
  511. if (bytes) {
  512. for (i = 0; i < bytes; i++)
  513. st->buffer[st->leftover + i] = m[i];
  514. st->leftover += bytes;
  515. }
  516. }
  517. } // anonymous namespace
  518. void Poly1305::compute(void *auth,const void *data,unsigned int len,const void *key)
  519. throw()
  520. {
  521. poly1305_context ctx;
  522. poly1305_init(&ctx,reinterpret_cast<const unsigned char *>(key));
  523. poly1305_update(&ctx,reinterpret_cast<const unsigned char *>(data),(size_t)len);
  524. poly1305_finish(&ctx,reinterpret_cast<unsigned char *>(auth));
  525. }
  526. } // namespace ZeroTier