Poly1305.cpp 15 KB

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