poly1305.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * cifra - embedded cryptography library
  3. * Written in 2014 by Joseph Birr-Pixton <jpixton@gmail.com>
  4. *
  5. * To the extent possible under law, the author(s) have dedicated all
  6. * copyright and related and neighboring rights to this software to the
  7. * public domain worldwide. This software is distributed without any
  8. * warranty.
  9. *
  10. * You should have received a copy of the CC0 Public Domain Dedication
  11. * along with this software. If not, see
  12. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  13. */
  14. #include "poly1305.h"
  15. #include "bitops.h"
  16. #include "handy.h"
  17. #include "blockwise.h"
  18. #include <string.h>
  19. #include <stdio.h>
  20. void cf_poly1305_init(cf_poly1305 *ctx,
  21. const uint8_t r[16],
  22. const uint8_t s[16])
  23. {
  24. memset(ctx, 0, sizeof *ctx);
  25. ctx->r[0] = r[0];
  26. ctx->r[1] = r[1];
  27. ctx->r[2] = r[2];
  28. ctx->r[3] = r[3] & 0x0f;
  29. ctx->r[4] = r[4] & 0xfc;
  30. ctx->r[5] = r[5];
  31. ctx->r[6] = r[6];
  32. ctx->r[7] = r[7] & 0x0f;
  33. ctx->r[8] = r[8] & 0xfc;
  34. ctx->r[9] = r[9];
  35. ctx->r[10] = r[10];
  36. ctx->r[11] = r[11] & 0x0f;
  37. ctx->r[12] = r[12] & 0xfc;
  38. ctx->r[13] = r[13];
  39. ctx->r[14] = r[14];
  40. ctx->r[15] = r[15] & 0x0f;
  41. ctx->r[16] = 0;
  42. memcpy(ctx->s, s, 16);
  43. }
  44. static void poly1305_add(uint32_t h[17],
  45. const uint32_t x[17])
  46. {
  47. uint32_t carry = 0;
  48. int i;
  49. for (i = 0; i < 17; i++)
  50. {
  51. carry += h[i] + x[i];
  52. h[i] = carry & 0xff;
  53. carry >>= 8;
  54. }
  55. }
  56. /* Minimal reduction/carry chain. */
  57. static void poly1305_min_reduce(uint32_t x[17])
  58. {
  59. uint32_t carry = 0;
  60. int i;
  61. for (i = 0; i < 16; i++)
  62. {
  63. carry += x[i];
  64. x[i] = carry & 0xff;
  65. carry >>= 8;
  66. }
  67. /* 2 ** 130 - 5 = 0x3fffffffffffffffffffffffffffffffb
  68. * ^
  69. * So 2 bits of carry are put into top word.
  70. * Remaining bits get multiplied by 5 and carried back
  71. * into bottom */
  72. carry += x[16];
  73. x[16] = carry & 0x03;
  74. carry = 5 * (carry >> 2);
  75. for (i = 0; i < 16; i++)
  76. {
  77. carry += x[i];
  78. x[i] = carry & 0xff;
  79. carry >>= 8;
  80. }
  81. x[16] += carry;
  82. }
  83. /* This is - 2 ** 130 - 5 in twos complement. */
  84. static const uint32_t negative_1305[17] = {
  85. 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0xfc
  87. };
  88. static void poly1305_full_reduce(uint32_t x[17])
  89. {
  90. uint32_t xsub[17];
  91. size_t i;
  92. for (i = 0; i < 17; i++)
  93. xsub[i] = x[i];
  94. poly1305_add(xsub, negative_1305);
  95. /* If x - (2 ** 130 - 5) is negative, then
  96. * x didn't need reduction: we discard the results.
  97. * Do this in a side-channel silent way. */
  98. uint32_t negative_mask = mask_u32(xsub[16] & 0x80, 0x80);
  99. uint32_t positive_mask = negative_mask ^ 0xffffffff;
  100. for (i = 0; i < 17; i++)
  101. x[i] = (x[i] & negative_mask) | (xsub[i] & positive_mask);
  102. }
  103. static void poly1305_mul(uint32_t x[17],
  104. const uint32_t y[17])
  105. {
  106. uint32_t r[17];
  107. int i;
  108. for (i = 0; i < 17; i++)
  109. {
  110. uint32_t accum = 0;
  111. int j;
  112. for (j = 0; j <= i; j++)
  113. accum += x[j] * y[i - j];
  114. /* Add in carries. These get shifted 130 bits
  115. * to the right, with a combination of byte indexing
  116. * and shifting (136 bits right, then 6 bits left).
  117. *
  118. * nb. 5 << 6 is made up of two parts:
  119. * 5: reduction of 2 ** 130 leaves a multiple 5
  120. * shift 6 places left
  121. * 17 * 8: byte indexing shift (136 bits)
  122. * 130: desired shift
  123. */
  124. for (j = i + 1; j < 17; j++)
  125. accum += (5 << 6) * x[j] * y[i + 17 - j];
  126. r[i] = accum;
  127. }
  128. poly1305_min_reduce(r);
  129. for (i = 0; i < 17; i++)
  130. x[i] = r[i];
  131. }
  132. static void poly1305_block(cf_poly1305 *ctx,
  133. const uint32_t c[17])
  134. {
  135. poly1305_add(ctx->h, c);
  136. poly1305_mul(ctx->h, ctx->r);
  137. }
  138. static void poly1305_whole_block(void *vctx,
  139. const uint8_t *buf)
  140. {
  141. cf_poly1305 *ctx = vctx;
  142. uint32_t c[17];
  143. int i;
  144. for (i = 0; i < 16; i++)
  145. c[i] = buf[i];
  146. c[16] = 1;
  147. poly1305_block(ctx, c);
  148. }
  149. static void poly1305_last_block(cf_poly1305 *ctx)
  150. {
  151. uint32_t c[17] = { 0 };
  152. size_t i;
  153. for (i = 0; i < ctx->npartial; i++)
  154. c[i] = ctx->partial[i];
  155. c[ctx->npartial] = 1;
  156. poly1305_block(ctx, c);
  157. }
  158. void cf_poly1305_update(cf_poly1305 *ctx,
  159. const uint8_t *buf,
  160. size_t nbytes)
  161. {
  162. cf_blockwise_accumulate(ctx->partial, &ctx->npartial,
  163. sizeof ctx->partial,
  164. buf, nbytes,
  165. poly1305_whole_block,
  166. ctx);
  167. }
  168. void cf_poly1305_finish(cf_poly1305 *ctx,
  169. uint8_t out[16])
  170. {
  171. if (ctx->npartial)
  172. poly1305_last_block(ctx);
  173. uint32_t s[17];
  174. size_t i;
  175. for (i = 0; i < 16; i++)
  176. s[i] = ctx->s[i];
  177. s[16] = 0;
  178. poly1305_full_reduce(ctx->h);
  179. poly1305_add(ctx->h, s);
  180. for (i = 0; i < 16; i++)
  181. out[i] = ctx->h[i];
  182. mem_clean(ctx, sizeof *ctx);
  183. }