Poly1305.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. 20080912
  3. D. J. Bernstein
  4. Public domain.
  5. */
  6. // Small modifications have been made for ZeroTier, but this code remains in the public domain.
  7. #include "Constants.hpp"
  8. #include "Poly1305.hpp"
  9. #include "Utils.hpp"
  10. #include <cstring>
  11. #ifdef __WINDOWS__
  12. #pragma warning(disable: 4146)
  13. #endif
  14. #define U8TO64(p) Utils::loadLittleEndian<uint64_t>(p)
  15. #define U64TO8(p,v) Utils::storeLittleEndian<uint64_t>(p,v)
  16. #define U8TO32(p) Utils::loadLittleEndian<uint32_t>(p)
  17. #define U32TO8(p,v) Utils::storeLittleEndian<uint32_t>(p,v)
  18. namespace ZeroTier {
  19. namespace {
  20. typedef struct poly1305_context {
  21. size_t aligner;
  22. unsigned char opaque[136];
  23. } poly1305_context;
  24. #ifdef ZT_HAVE_UINT128
  25. #define MUL(out, x, y) out = ((uint128_t)x * y)
  26. #define ADD(out, in) out += in
  27. #define ADDLO(out, in) out += in
  28. #define SHR(in, shift) (unsigned long long)(in >> (shift))
  29. #define LO(in) (unsigned long long)(in)
  30. #define poly1305_block_size 16
  31. typedef struct poly1305_state_internal_t {
  32. unsigned long long r[3];
  33. unsigned long long h[3];
  34. unsigned long long pad[2];
  35. size_t leftover;
  36. unsigned char buffer[poly1305_block_size];
  37. unsigned char final;
  38. } poly1305_state_internal_t;
  39. ZT_INLINE void poly1305_init(poly1305_context *ctx,const unsigned char key[32])
  40. {
  41. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  42. unsigned long long t0,t1;
  43. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  44. t0 = U8TO64(&key[0]);
  45. t1 = U8TO64(&key[8]);
  46. st->r[0] = ( t0 ) & 0xffc0fffffff;
  47. st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
  48. st->r[2] = ((t1 >> 24) ) & 0x00ffffffc0f;
  49. /* h = 0 */
  50. st->h[0] = 0;
  51. st->h[1] = 0;
  52. st->h[2] = 0;
  53. /* save pad for later */
  54. st->pad[0] = U8TO64(&key[16]);
  55. st->pad[1] = U8TO64(&key[24]);
  56. st->leftover = 0;
  57. st->final = 0;
  58. }
  59. void poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes)
  60. {
  61. const unsigned long long hibit = (st->final) ? 0 : ((unsigned long long)1 << 40); /* 1 << 128 */
  62. unsigned long long r0,r1,r2;
  63. unsigned long long s1,s2;
  64. unsigned long long h0,h1,h2;
  65. uint128_t d0,d1,d2,d;
  66. r0 = st->r[0];
  67. r1 = st->r[1];
  68. r2 = st->r[2];
  69. h0 = st->h[0];
  70. h1 = st->h[1];
  71. h2 = st->h[2];
  72. s1 = r1 * (5 << 2);
  73. s2 = r2 * (5 << 2);
  74. while (bytes >= poly1305_block_size) {
  75. unsigned long long t0,t1;
  76. /* h += m[i] */
  77. t0 = U8TO64(&m[0]);
  78. t1 = U8TO64(&m[8]);
  79. h0 += (( t0 ) & 0xfffffffffff);
  80. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
  81. h2 += (((t1 >> 24) ) & 0x3ffffffffff) | hibit;
  82. /* h *= r */
  83. MUL(d0, h0, r0); MUL(d, h1, s2); ADD(d0, d); MUL(d, h2, s1); ADD(d0, d);
  84. MUL(d1, h0, r1); MUL(d, h1, r0); ADD(d1, d); MUL(d, h2, s2); ADD(d1, d);
  85. MUL(d2, h0, r2); MUL(d, h1, r1); ADD(d2, d); MUL(d, h2, r0); ADD(d2, d);
  86. /* (partial) h %= p */
  87. unsigned long long c = SHR(d0, 44); h0 = LO(d0) & 0xfffffffffff;
  88. ADDLO(d1, c); c = SHR(d1, 44); h1 = LO(d1) & 0xfffffffffff;
  89. ADDLO(d2, c); c = SHR(d2, 42); h2 = LO(d2) & 0x3ffffffffff;
  90. h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffff;
  91. h1 += c;
  92. m += poly1305_block_size;
  93. bytes -= poly1305_block_size;
  94. }
  95. st->h[0] = h0;
  96. st->h[1] = h1;
  97. st->h[2] = h2;
  98. }
  99. ZT_INLINE void poly1305_finish(poly1305_context *ctx,unsigned char mac[16])
  100. {
  101. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  102. unsigned long long h0,h1,h2,c;
  103. unsigned long long g0,g1,g2;
  104. unsigned long long t0,t1;
  105. /* process the remaining block */
  106. if (st->leftover) {
  107. size_t i = st->leftover;
  108. st->buffer[i] = 1;
  109. for (i = i + 1; i < poly1305_block_size; i++)
  110. st->buffer[i] = 0;
  111. st->final = 1;
  112. poly1305_blocks(st, st->buffer, poly1305_block_size);
  113. }
  114. /* fully carry h */
  115. h0 = st->h[0];
  116. h1 = st->h[1];
  117. h2 = st->h[2];
  118. c = (h1 >> 44); h1 &= 0xfffffffffff;
  119. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  120. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  121. h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  122. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  123. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  124. h1 += c;
  125. /* compute h + -p */
  126. g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
  127. g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
  128. g2 = h2 + c - ((unsigned long long)1 << 42);
  129. /* select h if h < p, or h + -p if h >= p */
  130. c = (g2 >> ((sizeof(unsigned long long) * 8) - 1)) - 1;
  131. g0 &= c;
  132. g1 &= c;
  133. g2 &= c;
  134. c = ~c;
  135. h0 = (h0 & c) | g0;
  136. h1 = (h1 & c) | g1;
  137. h2 = (h2 & c) | g2;
  138. /* h = (h + pad) */
  139. t0 = st->pad[0];
  140. t1 = st->pad[1];
  141. h0 += (( t0 ) & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff;
  142. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  143. h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c; h2 &= 0x3ffffffffff;
  144. /* mac = h % (2^128) */
  145. h0 = ((h0 ) | (h1 << 44));
  146. h1 = ((h1 >> 20) | (h2 << 24));
  147. U64TO8(&mac[0], h0);
  148. U64TO8(&mac[8], h1);
  149. /* zero out the state */
  150. st->h[0] = 0;
  151. st->h[1] = 0;
  152. st->h[2] = 0;
  153. st->r[0] = 0;
  154. st->r[1] = 0;
  155. st->r[2] = 0;
  156. st->pad[0] = 0;
  157. st->pad[1] = 0;
  158. }
  159. #else // no uint128_t
  160. #define poly1305_block_size 16
  161. typedef struct poly1305_state_internal_t {
  162. unsigned long r[5];
  163. unsigned long h[5];
  164. unsigned long pad[4];
  165. size_t leftover;
  166. unsigned char buffer[poly1305_block_size];
  167. unsigned char final;
  168. } poly1305_state_internal_t;
  169. ZT_INLINE void poly1305_init(poly1305_context *ctx, const unsigned char key[32])
  170. {
  171. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  172. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  173. st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
  174. st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
  175. st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
  176. st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
  177. st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
  178. /* h = 0 */
  179. st->h[0] = 0;
  180. st->h[1] = 0;
  181. st->h[2] = 0;
  182. st->h[3] = 0;
  183. st->h[4] = 0;
  184. /* save pad for later */
  185. st->pad[0] = U8TO32(&key[16]);
  186. st->pad[1] = U8TO32(&key[20]);
  187. st->pad[2] = U8TO32(&key[24]);
  188. st->pad[3] = U8TO32(&key[28]);
  189. st->leftover = 0;
  190. st->final = 0;
  191. }
  192. void poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes)
  193. {
  194. const unsigned long hibit = (st->final) ? 0 : (1 << 24); /* 1 << 128 */
  195. unsigned long r0,r1,r2,r3,r4;
  196. unsigned long s1,s2,s3,s4;
  197. unsigned long h0,h1,h2,h3,h4;
  198. r0 = st->r[0];
  199. r1 = st->r[1];
  200. r2 = st->r[2];
  201. r3 = st->r[3];
  202. r4 = st->r[4];
  203. s1 = r1 * 5;
  204. s2 = r2 * 5;
  205. s3 = r3 * 5;
  206. s4 = r4 * 5;
  207. h0 = st->h[0];
  208. h1 = st->h[1];
  209. h2 = st->h[2];
  210. h3 = st->h[3];
  211. h4 = st->h[4];
  212. while (bytes >= poly1305_block_size) {
  213. /* h += m[i] */
  214. h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
  215. h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
  216. h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
  217. h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
  218. h4 += (U8TO32(m+12) >> 8) | hibit;
  219. /* h *= r */
  220. 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);
  221. 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);
  222. 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);
  223. 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);
  224. 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);
  225. /* (partial) h %= p */
  226. unsigned long c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff;
  227. d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff;
  228. d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff;
  229. d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff;
  230. d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff;
  231. h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
  232. h1 += c;
  233. m += poly1305_block_size;
  234. bytes -= poly1305_block_size;
  235. }
  236. st->h[0] = h0;
  237. st->h[1] = h1;
  238. st->h[2] = h2;
  239. st->h[3] = h3;
  240. st->h[4] = h4;
  241. }
  242. ZT_INLINE void poly1305_finish(poly1305_context *ctx, unsigned char mac[16])
  243. {
  244. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  245. unsigned long h0,h1,h2,h3,h4,c;
  246. unsigned long g0,g1,g2,g3,g4;
  247. unsigned long long f;
  248. unsigned long mask;
  249. /* process the remaining block */
  250. if (st->leftover) {
  251. size_t i = st->leftover;
  252. st->buffer[i++] = 1;
  253. for (; i < poly1305_block_size; i++)
  254. st->buffer[i] = 0;
  255. st->final = 1;
  256. poly1305_blocks(st, st->buffer, poly1305_block_size);
  257. }
  258. /* fully carry h */
  259. h0 = st->h[0];
  260. h1 = st->h[1];
  261. h2 = st->h[2];
  262. h3 = st->h[3];
  263. h4 = st->h[4];
  264. c = h1 >> 26; h1 = h1 & 0x3ffffff;
  265. h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
  266. h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
  267. h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
  268. h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
  269. h1 += c;
  270. /* compute h + -p */
  271. g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
  272. g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
  273. g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
  274. g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
  275. g4 = h4 + c - (1 << 26);
  276. /* select h if h < p, or h + -p if h >= p */
  277. mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
  278. g0 &= mask;
  279. g1 &= mask;
  280. g2 &= mask;
  281. g3 &= mask;
  282. g4 &= mask;
  283. mask = ~mask;
  284. h0 = (h0 & mask) | g0;
  285. h1 = (h1 & mask) | g1;
  286. h2 = (h2 & mask) | g2;
  287. h3 = (h3 & mask) | g3;
  288. h4 = (h4 & mask) | g4;
  289. /* h = h % (2^128) */
  290. h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
  291. h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
  292. h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
  293. h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
  294. /* mac = (h + pad) % (2^128) */
  295. f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f;
  296. f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f;
  297. f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
  298. f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
  299. U32TO8(mac + 0, h0);
  300. U32TO8(mac + 4, h1);
  301. U32TO8(mac + 8, h2);
  302. U32TO8(mac + 12, h3);
  303. /* zero out the state */
  304. st->h[0] = 0;
  305. st->h[1] = 0;
  306. st->h[2] = 0;
  307. st->h[3] = 0;
  308. st->h[4] = 0;
  309. st->r[0] = 0;
  310. st->r[1] = 0;
  311. st->r[2] = 0;
  312. st->r[3] = 0;
  313. st->r[4] = 0;
  314. st->pad[0] = 0;
  315. st->pad[1] = 0;
  316. st->pad[2] = 0;
  317. st->pad[3] = 0;
  318. }
  319. #endif // uint128_t or portable version?
  320. ZT_INLINE void poly1305_update(poly1305_context *ctx,const unsigned char *m,size_t bytes) noexcept
  321. {
  322. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  323. size_t i;
  324. /* handle leftover */
  325. if (st->leftover) {
  326. size_t want = (poly1305_block_size - st->leftover);
  327. if (want > bytes)
  328. want = bytes;
  329. for (i = 0; i < want; i++)
  330. st->buffer[st->leftover + i] = m[i];
  331. bytes -= want;
  332. m += want;
  333. st->leftover += want;
  334. if (st->leftover < poly1305_block_size)
  335. return;
  336. poly1305_blocks(st, st->buffer, poly1305_block_size);
  337. st->leftover = 0;
  338. }
  339. /* process full blocks */
  340. if (bytes >= poly1305_block_size) {
  341. size_t want = (bytes & ~(poly1305_block_size - 1));
  342. poly1305_blocks(st, m, want);
  343. m += want;
  344. bytes -= want;
  345. }
  346. /* store leftover */
  347. if (bytes) {
  348. for (i = 0; i < bytes; i++)
  349. st->buffer[st->leftover + i] = m[i];
  350. st->leftover += bytes;
  351. }
  352. }
  353. } // anonymous namespace
  354. void Poly1305::init(const void *key) noexcept
  355. {
  356. static_assert(sizeof(ctx) >= sizeof(poly1305_context),"buffer in class smaller than required structure size");
  357. poly1305_init(reinterpret_cast<poly1305_context *>(&ctx),reinterpret_cast<const unsigned char *>(key));
  358. }
  359. void Poly1305::update(const void *data,unsigned int len) noexcept
  360. {
  361. poly1305_update(reinterpret_cast<poly1305_context *>(&ctx),reinterpret_cast<const unsigned char *>(data),(size_t)len);
  362. }
  363. void Poly1305::finish(void *auth) noexcept
  364. {
  365. poly1305_finish(reinterpret_cast<poly1305_context *>(&ctx),reinterpret_cast<unsigned char *>(auth));
  366. }
  367. } // namespace ZeroTier