Poly1305.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. }
  161. st->final = 1;
  162. poly1305_blocks(st, st->buffer, poly1305_block_size);
  163. }
  164. /* fully carry h */
  165. h0 = st->h[0];
  166. h1 = st->h[1];
  167. h2 = st->h[2];
  168. c = (h1 >> 44); h1 &= 0xfffffffffff;
  169. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  170. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  171. h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  172. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  173. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  174. h1 += c;
  175. /* compute h + -p */
  176. g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
  177. g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
  178. g2 = h2 + c - ((unsigned long long)1 << 42);
  179. /* select h if h < p, or h + -p if h >= p */
  180. c = (g2 >> ((sizeof(unsigned long long) * 8) - 1)) - 1;
  181. g0 &= c;
  182. g1 &= c;
  183. g2 &= c;
  184. c = ~c;
  185. h0 = (h0 & c) | g0;
  186. h1 = (h1 & c) | g1;
  187. h2 = (h2 & c) | g2;
  188. /* h = (h + pad) */
  189. t0 = st->pad[0];
  190. t1 = st->pad[1];
  191. h0 += (( t0 ) & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff;
  192. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  193. h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c; h2 &= 0x3ffffffffff;
  194. /* mac = h % (2^128) */
  195. h0 = ((h0 ) | (h1 << 44));
  196. h1 = ((h1 >> 20) | (h2 << 24));
  197. U64TO8(&mac[0], h0);
  198. U64TO8(&mac[8], h1);
  199. /* zero out the state */
  200. st->h[0] = 0;
  201. st->h[1] = 0;
  202. st->h[2] = 0;
  203. st->r[0] = 0;
  204. st->r[1] = 0;
  205. st->r[2] = 0;
  206. st->pad[0] = 0;
  207. st->pad[1] = 0;
  208. }
  209. //////////////////////////////////////////////////////////////////////////////
  210. #else
  211. //////////////////////////////////////////////////////////////////////////////
  212. // More portable 64-bit implementation
  213. #define poly1305_block_size 16
  214. /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
  215. typedef struct poly1305_state_internal_t {
  216. unsigned long r[5];
  217. unsigned long h[5];
  218. unsigned long pad[4];
  219. size_t leftover;
  220. unsigned char buffer[poly1305_block_size];
  221. unsigned char final;
  222. } poly1305_state_internal_t;
  223. /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
  224. static unsigned long
  225. U8TO32(const unsigned char *p) {
  226. return
  227. (((unsigned long)(p[0] & 0xff) ) |
  228. ((unsigned long)(p[1] & 0xff) << 8) |
  229. ((unsigned long)(p[2] & 0xff) << 16) |
  230. ((unsigned long)(p[3] & 0xff) << 24));
  231. }
  232. /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */
  233. static void
  234. U32TO8(unsigned char *p, unsigned long v) {
  235. p[0] = (v ) & 0xff;
  236. p[1] = (v >> 8) & 0xff;
  237. p[2] = (v >> 16) & 0xff;
  238. p[3] = (v >> 24) & 0xff;
  239. }
  240. static inline void
  241. poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
  242. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  243. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  244. st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
  245. st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
  246. st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
  247. st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
  248. st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
  249. /* h = 0 */
  250. st->h[0] = 0;
  251. st->h[1] = 0;
  252. st->h[2] = 0;
  253. st->h[3] = 0;
  254. st->h[4] = 0;
  255. /* save pad for later */
  256. st->pad[0] = U8TO32(&key[16]);
  257. st->pad[1] = U8TO32(&key[20]);
  258. st->pad[2] = U8TO32(&key[24]);
  259. st->pad[3] = U8TO32(&key[28]);
  260. st->leftover = 0;
  261. st->final = 0;
  262. }
  263. static inline void
  264. poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
  265. const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */
  266. unsigned long r0,r1,r2,r3,r4;
  267. unsigned long s1,s2,s3,s4;
  268. unsigned long h0,h1,h2,h3,h4;
  269. unsigned long long d0,d1,d2,d3,d4;
  270. unsigned long c;
  271. r0 = st->r[0];
  272. r1 = st->r[1];
  273. r2 = st->r[2];
  274. r3 = st->r[3];
  275. r4 = st->r[4];
  276. s1 = r1 * 5;
  277. s2 = r2 * 5;
  278. s3 = r3 * 5;
  279. s4 = r4 * 5;
  280. h0 = st->h[0];
  281. h1 = st->h[1];
  282. h2 = st->h[2];
  283. h3 = st->h[3];
  284. h4 = st->h[4];
  285. while (bytes >= poly1305_block_size) {
  286. /* h += m[i] */
  287. h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
  288. h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
  289. h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
  290. h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
  291. h4 += (U8TO32(m+12) >> 8) | hibit;
  292. /* h *= r */
  293. 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);
  294. 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);
  295. 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);
  296. 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);
  297. 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);
  298. /* (partial) h %= p */
  299. c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff;
  300. d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff;
  301. d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff;
  302. d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff;
  303. d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff;
  304. h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
  305. h1 += c;
  306. m += poly1305_block_size;
  307. bytes -= poly1305_block_size;
  308. }
  309. st->h[0] = h0;
  310. st->h[1] = h1;
  311. st->h[2] = h2;
  312. st->h[3] = h3;
  313. st->h[4] = h4;
  314. }
  315. static inline void
  316. poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
  317. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  318. unsigned long h0,h1,h2,h3,h4,c;
  319. unsigned long g0,g1,g2,g3,g4;
  320. unsigned long long f;
  321. unsigned long mask;
  322. /* process the remaining block */
  323. if (st->leftover) {
  324. size_t i = st->leftover;
  325. st->buffer[i++] = 1;
  326. for (; i < poly1305_block_size; i++) {
  327. st->buffer[i] = 0;
  328. }
  329. st->final = 1;
  330. poly1305_blocks(st, st->buffer, poly1305_block_size);
  331. }
  332. /* fully carry h */
  333. h0 = st->h[0];
  334. h1 = st->h[1];
  335. h2 = st->h[2];
  336. h3 = st->h[3];
  337. h4 = st->h[4];
  338. c = h1 >> 26; h1 = h1 & 0x3ffffff;
  339. h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
  340. h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
  341. h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
  342. h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
  343. h1 += c;
  344. /* compute h + -p */
  345. g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
  346. g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
  347. g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
  348. g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
  349. g4 = h4 + c - (1 << 26);
  350. /* select h if h < p, or h + -p if h >= p */
  351. mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
  352. g0 &= mask;
  353. g1 &= mask;
  354. g2 &= mask;
  355. g3 &= mask;
  356. g4 &= mask;
  357. mask = ~mask;
  358. h0 = (h0 & mask) | g0;
  359. h1 = (h1 & mask) | g1;
  360. h2 = (h2 & mask) | g2;
  361. h3 = (h3 & mask) | g3;
  362. h4 = (h4 & mask) | g4;
  363. /* h = h % (2^128) */
  364. h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
  365. h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
  366. h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
  367. h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
  368. /* mac = (h + pad) % (2^128) */
  369. f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f;
  370. f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f;
  371. f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
  372. f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
  373. U32TO8(mac + 0, h0);
  374. U32TO8(mac + 4, h1);
  375. U32TO8(mac + 8, h2);
  376. U32TO8(mac + 12, h3);
  377. /* zero out the state */
  378. st->h[0] = 0;
  379. st->h[1] = 0;
  380. st->h[2] = 0;
  381. st->h[3] = 0;
  382. st->h[4] = 0;
  383. st->r[0] = 0;
  384. st->r[1] = 0;
  385. st->r[2] = 0;
  386. st->r[3] = 0;
  387. st->r[4] = 0;
  388. st->pad[0] = 0;
  389. st->pad[1] = 0;
  390. st->pad[2] = 0;
  391. st->pad[3] = 0;
  392. }
  393. //////////////////////////////////////////////////////////////////////////////
  394. #endif // MSC/GCC or not
  395. static inline void poly1305_update(poly1305_context *ctx, const unsigned char *m, size_t bytes) {
  396. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  397. size_t i;
  398. /* handle leftover */
  399. if (st->leftover) {
  400. size_t want = (poly1305_block_size - st->leftover);
  401. if (want > bytes) {
  402. want = bytes;
  403. }
  404. for (i = 0; i < want; i++) {
  405. st->buffer[st->leftover + i] = m[i];
  406. }
  407. bytes -= want;
  408. m += want;
  409. st->leftover += want;
  410. if (st->leftover < poly1305_block_size) {
  411. return;
  412. }
  413. poly1305_blocks(st, st->buffer, poly1305_block_size);
  414. st->leftover = 0;
  415. }
  416. /* process full blocks */
  417. if (bytes >= poly1305_block_size) {
  418. size_t want = (bytes & ~(poly1305_block_size - 1));
  419. poly1305_blocks(st, m, want);
  420. m += want;
  421. bytes -= want;
  422. }
  423. /* store leftover */
  424. if (bytes) {
  425. for (i = 0; i < bytes; i++) {
  426. st->buffer[st->leftover + i] = m[i];
  427. }
  428. st->leftover += bytes;
  429. }
  430. }
  431. } // anonymous namespace
  432. void Poly1305::compute(void *auth,const void *data,unsigned int len,const void *key)
  433. {
  434. poly1305_context ctx;
  435. poly1305_init(&ctx,reinterpret_cast<const unsigned char *>(key));
  436. poly1305_update(&ctx,reinterpret_cast<const unsigned char *>(data),(size_t)len);
  437. poly1305_finish(&ctx,reinterpret_cast<unsigned char *>(auth));
  438. }
  439. } // namespace ZeroTier