2
0

poly1305.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. #include "crypto/poly1305.h"
  13. #include "poly1305_local.h"
  14. size_t Poly1305_ctx_size(void)
  15. {
  16. return sizeof(struct poly1305_context);
  17. }
  18. /* pick 32-bit unsigned integer in little endian order */
  19. static unsigned int U8TOU32(const unsigned char *p)
  20. {
  21. return (((unsigned int)(p[0] & 0xff)) |
  22. ((unsigned int)(p[1] & 0xff) << 8) |
  23. ((unsigned int)(p[2] & 0xff) << 16) |
  24. ((unsigned int)(p[3] & 0xff) << 24));
  25. }
  26. /*
  27. * Implementations can be classified by amount of significant bits in
  28. * words making up the multi-precision value, or in other words radix
  29. * or base of numerical representation, e.g. base 2^64, base 2^32,
  30. * base 2^26. Complementary characteristic is how wide is the result of
  31. * multiplication of pair of digits, e.g. it would take 128 bits to
  32. * accommodate multiplication result in base 2^64 case. These are used
  33. * interchangeably. To describe implementation that is. But interface
  34. * is designed to isolate this so that low-level primitives implemented
  35. * in assembly can be self-contained/self-coherent.
  36. */
  37. #ifndef POLY1305_ASM
  38. /*
  39. * Even though there is __int128 reference implementation targeting
  40. * 64-bit platforms provided below, it's not obvious that it's optimal
  41. * choice for every one of them. Depending on instruction set overall
  42. * amount of instructions can be comparable to one in __int64
  43. * implementation. Amount of multiplication instructions would be lower,
  44. * but not necessarily overall. And in out-of-order execution context,
  45. * it is the latter that can be crucial...
  46. *
  47. * On related note. Poly1305 author, D. J. Bernstein, discusses and
  48. * provides floating-point implementations of the algorithm in question.
  49. * It made a lot of sense by the time of introduction, because most
  50. * then-modern processors didn't have pipelined integer multiplier.
  51. * [Not to mention that some had non-constant timing for integer
  52. * multiplications.] Floating-point instructions on the other hand could
  53. * be issued every cycle, which allowed to achieve better performance.
  54. * Nowadays, with SIMD and/or out-or-order execution, shared or
  55. * even emulated FPU, it's more complicated, and floating-point
  56. * implementation is not necessarily optimal choice in every situation,
  57. * rather contrary...
  58. *
  59. * <appro@openssl.org>
  60. */
  61. typedef unsigned int u32;
  62. /*
  63. * poly1305_blocks processes a multiple of POLY1305_BLOCK_SIZE blocks
  64. * of |inp| no longer than |len|. Behaviour for |len| not divisible by
  65. * block size is unspecified in general case, even though in reference
  66. * implementation the trailing chunk is simply ignored. Per algorithm
  67. * specification, every input block, complete or last partial, is to be
  68. * padded with a bit past most significant byte. The latter kind is then
  69. * padded with zeros till block size. This last partial block padding
  70. * is caller(*)'s responsibility, and because of this the last partial
  71. * block is always processed with separate call with |len| set to
  72. * POLY1305_BLOCK_SIZE and |padbit| to 0. In all other cases |padbit|
  73. * should be set to 1 to perform implicit padding with 128th bit.
  74. * poly1305_blocks does not actually check for this constraint though,
  75. * it's caller(*)'s responsibility to comply.
  76. *
  77. * (*) In the context "caller" is not application code, but higher
  78. * level Poly1305_* from this very module, so that quirks are
  79. * handled locally.
  80. */
  81. static void
  82. poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, u32 padbit);
  83. /*
  84. * Type-agnostic "rip-off" from constant_time.h
  85. */
  86. # define CONSTANT_TIME_CARRY(a,b) ( \
  87. (a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1) \
  88. )
  89. # if (defined(__SIZEOF_INT128__) && __SIZEOF_INT128__==16) && \
  90. (defined(__SIZEOF_LONG__) && __SIZEOF_LONG__==8)
  91. typedef unsigned long u64;
  92. typedef __uint128_t u128;
  93. typedef struct {
  94. u64 h[3];
  95. u64 r[2];
  96. } poly1305_internal;
  97. /* pick 32-bit unsigned integer in little endian order */
  98. static u64 U8TOU64(const unsigned char *p)
  99. {
  100. return (((u64)(p[0] & 0xff)) |
  101. ((u64)(p[1] & 0xff) << 8) |
  102. ((u64)(p[2] & 0xff) << 16) |
  103. ((u64)(p[3] & 0xff) << 24) |
  104. ((u64)(p[4] & 0xff) << 32) |
  105. ((u64)(p[5] & 0xff) << 40) |
  106. ((u64)(p[6] & 0xff) << 48) |
  107. ((u64)(p[7] & 0xff) << 56));
  108. }
  109. /* store a 32-bit unsigned integer in little endian */
  110. static void U64TO8(unsigned char *p, u64 v)
  111. {
  112. p[0] = (unsigned char)((v) & 0xff);
  113. p[1] = (unsigned char)((v >> 8) & 0xff);
  114. p[2] = (unsigned char)((v >> 16) & 0xff);
  115. p[3] = (unsigned char)((v >> 24) & 0xff);
  116. p[4] = (unsigned char)((v >> 32) & 0xff);
  117. p[5] = (unsigned char)((v >> 40) & 0xff);
  118. p[6] = (unsigned char)((v >> 48) & 0xff);
  119. p[7] = (unsigned char)((v >> 56) & 0xff);
  120. }
  121. static void poly1305_init(void *ctx, const unsigned char key[16])
  122. {
  123. poly1305_internal *st = (poly1305_internal *) ctx;
  124. /* h = 0 */
  125. st->h[0] = 0;
  126. st->h[1] = 0;
  127. st->h[2] = 0;
  128. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  129. st->r[0] = U8TOU64(&key[0]) & 0x0ffffffc0fffffff;
  130. st->r[1] = U8TOU64(&key[8]) & 0x0ffffffc0ffffffc;
  131. }
  132. static void
  133. poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, u32 padbit)
  134. {
  135. poly1305_internal *st = (poly1305_internal *)ctx;
  136. u64 r0, r1;
  137. u64 s1;
  138. u64 h0, h1, h2, c;
  139. u128 d0, d1;
  140. r0 = st->r[0];
  141. r1 = st->r[1];
  142. s1 = r1 + (r1 >> 2);
  143. h0 = st->h[0];
  144. h1 = st->h[1];
  145. h2 = st->h[2];
  146. while (len >= POLY1305_BLOCK_SIZE) {
  147. /* h += m[i] */
  148. h0 = (u64)(d0 = (u128)h0 + U8TOU64(inp + 0));
  149. h1 = (u64)(d1 = (u128)h1 + (d0 >> 64) + U8TOU64(inp + 8));
  150. /*
  151. * padbit can be zero only when original len was
  152. * POLY1306_BLOCK_SIZE, but we don't check
  153. */
  154. h2 += (u64)(d1 >> 64) + padbit;
  155. /* h *= r "%" p, where "%" stands for "partial remainder" */
  156. d0 = ((u128)h0 * r0) +
  157. ((u128)h1 * s1);
  158. d1 = ((u128)h0 * r1) +
  159. ((u128)h1 * r0) +
  160. (h2 * s1);
  161. h2 = (h2 * r0);
  162. /* last reduction step: */
  163. /* a) h2:h0 = h2<<128 + d1<<64 + d0 */
  164. h0 = (u64)d0;
  165. h1 = (u64)(d1 += d0 >> 64);
  166. h2 += (u64)(d1 >> 64);
  167. /* b) (h2:h0 += (h2:h0>>130) * 5) %= 2^130 */
  168. c = (h2 >> 2) + (h2 & ~3UL);
  169. h2 &= 3;
  170. h0 += c;
  171. h1 += (c = CONSTANT_TIME_CARRY(h0,c));
  172. h2 += CONSTANT_TIME_CARRY(h1,c);
  173. /*
  174. * Occasional overflows to 3rd bit of h2 are taken care of
  175. * "naturally". If after this point we end up at the top of
  176. * this loop, then the overflow bit will be accounted for
  177. * in next iteration. If we end up in poly1305_emit, then
  178. * comparison to modulus below will still count as "carry
  179. * into 131st bit", so that properly reduced value will be
  180. * picked in conditional move.
  181. */
  182. inp += POLY1305_BLOCK_SIZE;
  183. len -= POLY1305_BLOCK_SIZE;
  184. }
  185. st->h[0] = h0;
  186. st->h[1] = h1;
  187. st->h[2] = h2;
  188. }
  189. static void poly1305_emit(void *ctx, unsigned char mac[16],
  190. const u32 nonce[4])
  191. {
  192. poly1305_internal *st = (poly1305_internal *) ctx;
  193. u64 h0, h1, h2;
  194. u64 g0, g1, g2;
  195. u128 t;
  196. u64 mask;
  197. h0 = st->h[0];
  198. h1 = st->h[1];
  199. h2 = st->h[2];
  200. /* compare to modulus by computing h + -p */
  201. g0 = (u64)(t = (u128)h0 + 5);
  202. g1 = (u64)(t = (u128)h1 + (t >> 64));
  203. g2 = h2 + (u64)(t >> 64);
  204. /* if there was carry into 131st bit, h1:h0 = g1:g0 */
  205. mask = 0 - (g2 >> 2);
  206. g0 &= mask;
  207. g1 &= mask;
  208. mask = ~mask;
  209. h0 = (h0 & mask) | g0;
  210. h1 = (h1 & mask) | g1;
  211. /* mac = (h + nonce) % (2^128) */
  212. h0 = (u64)(t = (u128)h0 + nonce[0] + ((u64)nonce[1]<<32));
  213. h1 = (u64)(t = (u128)h1 + nonce[2] + ((u64)nonce[3]<<32) + (t >> 64));
  214. U64TO8(mac + 0, h0);
  215. U64TO8(mac + 8, h1);
  216. }
  217. # else
  218. # if defined(_WIN32) && !defined(__MINGW32__)
  219. typedef unsigned __int64 u64;
  220. # elif defined(__arch64__)
  221. typedef unsigned long u64;
  222. # else
  223. typedef unsigned long long u64;
  224. # endif
  225. typedef struct {
  226. u32 h[5];
  227. u32 r[4];
  228. } poly1305_internal;
  229. /* store a 32-bit unsigned integer in little endian */
  230. static void U32TO8(unsigned char *p, unsigned int v)
  231. {
  232. p[0] = (unsigned char)((v) & 0xff);
  233. p[1] = (unsigned char)((v >> 8) & 0xff);
  234. p[2] = (unsigned char)((v >> 16) & 0xff);
  235. p[3] = (unsigned char)((v >> 24) & 0xff);
  236. }
  237. static void poly1305_init(void *ctx, const unsigned char key[16])
  238. {
  239. poly1305_internal *st = (poly1305_internal *) ctx;
  240. /* h = 0 */
  241. st->h[0] = 0;
  242. st->h[1] = 0;
  243. st->h[2] = 0;
  244. st->h[3] = 0;
  245. st->h[4] = 0;
  246. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  247. st->r[0] = U8TOU32(&key[0]) & 0x0fffffff;
  248. st->r[1] = U8TOU32(&key[4]) & 0x0ffffffc;
  249. st->r[2] = U8TOU32(&key[8]) & 0x0ffffffc;
  250. st->r[3] = U8TOU32(&key[12]) & 0x0ffffffc;
  251. }
  252. static void
  253. poly1305_blocks(void *ctx, const unsigned char *inp, size_t len, u32 padbit)
  254. {
  255. poly1305_internal *st = (poly1305_internal *)ctx;
  256. u32 r0, r1, r2, r3;
  257. u32 s1, s2, s3;
  258. u32 h0, h1, h2, h3, h4, c;
  259. u64 d0, d1, d2, d3;
  260. r0 = st->r[0];
  261. r1 = st->r[1];
  262. r2 = st->r[2];
  263. r3 = st->r[3];
  264. s1 = r1 + (r1 >> 2);
  265. s2 = r2 + (r2 >> 2);
  266. s3 = r3 + (r3 >> 2);
  267. h0 = st->h[0];
  268. h1 = st->h[1];
  269. h2 = st->h[2];
  270. h3 = st->h[3];
  271. h4 = st->h[4];
  272. while (len >= POLY1305_BLOCK_SIZE) {
  273. /* h += m[i] */
  274. h0 = (u32)(d0 = (u64)h0 + U8TOU32(inp + 0));
  275. h1 = (u32)(d1 = (u64)h1 + (d0 >> 32) + U8TOU32(inp + 4));
  276. h2 = (u32)(d2 = (u64)h2 + (d1 >> 32) + U8TOU32(inp + 8));
  277. h3 = (u32)(d3 = (u64)h3 + (d2 >> 32) + U8TOU32(inp + 12));
  278. h4 += (u32)(d3 >> 32) + padbit;
  279. /* h *= r "%" p, where "%" stands for "partial remainder" */
  280. d0 = ((u64)h0 * r0) +
  281. ((u64)h1 * s3) +
  282. ((u64)h2 * s2) +
  283. ((u64)h3 * s1);
  284. d1 = ((u64)h0 * r1) +
  285. ((u64)h1 * r0) +
  286. ((u64)h2 * s3) +
  287. ((u64)h3 * s2) +
  288. (h4 * s1);
  289. d2 = ((u64)h0 * r2) +
  290. ((u64)h1 * r1) +
  291. ((u64)h2 * r0) +
  292. ((u64)h3 * s3) +
  293. (h4 * s2);
  294. d3 = ((u64)h0 * r3) +
  295. ((u64)h1 * r2) +
  296. ((u64)h2 * r1) +
  297. ((u64)h3 * r0) +
  298. (h4 * s3);
  299. h4 = (h4 * r0);
  300. /* last reduction step: */
  301. /* a) h4:h0 = h4<<128 + d3<<96 + d2<<64 + d1<<32 + d0 */
  302. h0 = (u32)d0;
  303. h1 = (u32)(d1 += d0 >> 32);
  304. h2 = (u32)(d2 += d1 >> 32);
  305. h3 = (u32)(d3 += d2 >> 32);
  306. h4 += (u32)(d3 >> 32);
  307. /* b) (h4:h0 += (h4:h0>>130) * 5) %= 2^130 */
  308. c = (h4 >> 2) + (h4 & ~3U);
  309. h4 &= 3;
  310. h0 += c;
  311. h1 += (c = CONSTANT_TIME_CARRY(h0,c));
  312. h2 += (c = CONSTANT_TIME_CARRY(h1,c));
  313. h3 += (c = CONSTANT_TIME_CARRY(h2,c));
  314. h4 += CONSTANT_TIME_CARRY(h3,c);
  315. /*
  316. * Occasional overflows to 3rd bit of h4 are taken care of
  317. * "naturally". If after this point we end up at the top of
  318. * this loop, then the overflow bit will be accounted for
  319. * in next iteration. If we end up in poly1305_emit, then
  320. * comparison to modulus below will still count as "carry
  321. * into 131st bit", so that properly reduced value will be
  322. * picked in conditional move.
  323. */
  324. inp += POLY1305_BLOCK_SIZE;
  325. len -= POLY1305_BLOCK_SIZE;
  326. }
  327. st->h[0] = h0;
  328. st->h[1] = h1;
  329. st->h[2] = h2;
  330. st->h[3] = h3;
  331. st->h[4] = h4;
  332. }
  333. static void poly1305_emit(void *ctx, unsigned char mac[16],
  334. const u32 nonce[4])
  335. {
  336. poly1305_internal *st = (poly1305_internal *) ctx;
  337. u32 h0, h1, h2, h3, h4;
  338. u32 g0, g1, g2, g3, g4;
  339. u64 t;
  340. u32 mask;
  341. h0 = st->h[0];
  342. h1 = st->h[1];
  343. h2 = st->h[2];
  344. h3 = st->h[3];
  345. h4 = st->h[4];
  346. /* compare to modulus by computing h + -p */
  347. g0 = (u32)(t = (u64)h0 + 5);
  348. g1 = (u32)(t = (u64)h1 + (t >> 32));
  349. g2 = (u32)(t = (u64)h2 + (t >> 32));
  350. g3 = (u32)(t = (u64)h3 + (t >> 32));
  351. g4 = h4 + (u32)(t >> 32);
  352. /* if there was carry into 131st bit, h3:h0 = g3:g0 */
  353. mask = 0 - (g4 >> 2);
  354. g0 &= mask;
  355. g1 &= mask;
  356. g2 &= mask;
  357. g3 &= mask;
  358. mask = ~mask;
  359. h0 = (h0 & mask) | g0;
  360. h1 = (h1 & mask) | g1;
  361. h2 = (h2 & mask) | g2;
  362. h3 = (h3 & mask) | g3;
  363. /* mac = (h + nonce) % (2^128) */
  364. h0 = (u32)(t = (u64)h0 + nonce[0]);
  365. h1 = (u32)(t = (u64)h1 + (t >> 32) + nonce[1]);
  366. h2 = (u32)(t = (u64)h2 + (t >> 32) + nonce[2]);
  367. h3 = (u32)(t = (u64)h3 + (t >> 32) + nonce[3]);
  368. U32TO8(mac + 0, h0);
  369. U32TO8(mac + 4, h1);
  370. U32TO8(mac + 8, h2);
  371. U32TO8(mac + 12, h3);
  372. }
  373. # endif
  374. #else
  375. int poly1305_init(void *ctx, const unsigned char key[16], void *func);
  376. void poly1305_blocks(void *ctx, const unsigned char *inp, size_t len,
  377. unsigned int padbit);
  378. void poly1305_emit(void *ctx, unsigned char mac[16],
  379. const unsigned int nonce[4]);
  380. #endif
  381. void Poly1305_Init(POLY1305 *ctx, const unsigned char key[32])
  382. {
  383. ctx->nonce[0] = U8TOU32(&key[16]);
  384. ctx->nonce[1] = U8TOU32(&key[20]);
  385. ctx->nonce[2] = U8TOU32(&key[24]);
  386. ctx->nonce[3] = U8TOU32(&key[28]);
  387. #ifndef POLY1305_ASM
  388. poly1305_init(ctx->opaque, key);
  389. #else
  390. /*
  391. * Unlike reference poly1305_init assembly counterpart is expected
  392. * to return a value: non-zero if it initializes ctx->func, and zero
  393. * otherwise. Latter is to simplify assembly in cases when there no
  394. * multiple code paths to switch between.
  395. */
  396. if (!poly1305_init(ctx->opaque, key, &ctx->func)) {
  397. ctx->func.blocks = poly1305_blocks;
  398. ctx->func.emit = poly1305_emit;
  399. }
  400. #endif
  401. ctx->num = 0;
  402. }
  403. #ifdef POLY1305_ASM
  404. /*
  405. * This "eclipses" poly1305_blocks and poly1305_emit, but it's
  406. * conscious choice imposed by -Wshadow compiler warnings.
  407. */
  408. # define poly1305_blocks (*poly1305_blocks_p)
  409. # define poly1305_emit (*poly1305_emit_p)
  410. #endif
  411. void Poly1305_Update(POLY1305 *ctx, const unsigned char *inp, size_t len)
  412. {
  413. #ifdef POLY1305_ASM
  414. /*
  415. * As documented, poly1305_blocks is never called with input
  416. * longer than single block and padbit argument set to 0. This
  417. * property is fluently used in assembly modules to optimize
  418. * padbit handling on loop boundary.
  419. */
  420. poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
  421. #endif
  422. size_t rem, num;
  423. if ((num = ctx->num)) {
  424. rem = POLY1305_BLOCK_SIZE - num;
  425. if (len >= rem) {
  426. memcpy(ctx->data + num, inp, rem);
  427. poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 1);
  428. inp += rem;
  429. len -= rem;
  430. } else {
  431. /* Still not enough data to process a block. */
  432. memcpy(ctx->data + num, inp, len);
  433. ctx->num = num + len;
  434. return;
  435. }
  436. }
  437. rem = len % POLY1305_BLOCK_SIZE;
  438. len -= rem;
  439. if (len >= POLY1305_BLOCK_SIZE) {
  440. poly1305_blocks(ctx->opaque, inp, len, 1);
  441. inp += len;
  442. }
  443. if (rem)
  444. memcpy(ctx->data, inp, rem);
  445. ctx->num = rem;
  446. }
  447. void Poly1305_Final(POLY1305 *ctx, unsigned char mac[16])
  448. {
  449. #ifdef POLY1305_ASM
  450. poly1305_blocks_f poly1305_blocks_p = ctx->func.blocks;
  451. poly1305_emit_f poly1305_emit_p = ctx->func.emit;
  452. #endif
  453. size_t num;
  454. if ((num = ctx->num)) {
  455. ctx->data[num++] = 1; /* pad bit */
  456. while (num < POLY1305_BLOCK_SIZE)
  457. ctx->data[num++] = 0;
  458. poly1305_blocks(ctx->opaque, ctx->data, POLY1305_BLOCK_SIZE, 0);
  459. }
  460. poly1305_emit(ctx->opaque, mac, ctx->nonce);
  461. /* zero out the state */
  462. OPENSSL_cleanse(ctx, sizeof(*ctx));
  463. }