poly1305.c 16 KB

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