You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ref-64.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. poly1305 implementation using 64 bit * 64 bit = 128 bit multiplication and 128 bit addition
  3. assumes the existence of uint64_t and uint128_t
  4. */
  5. #include "config.h"
  6. #include "poly1305.h"
  7. #include "poly1305_internal.h"
  8. #define POLY1305_BLOCK_SIZE 16
  9. typedef struct poly1305_state_ref_t {
  10. uint64_t r[3];
  11. uint64_t h[3];
  12. uint64_t pad[2];
  13. unsigned char final;
  14. } poly1305_state_ref_t;
  15. /* interpret eight 8 bit unsigned integers as a 64 bit unsigned integer in little endian */
  16. static uint64_t
  17. U8TO64(const unsigned char *p) {
  18. return
  19. ((uint64_t)p[0] ) |
  20. ((uint64_t)p[1] << 8) |
  21. ((uint64_t)p[2] << 16) |
  22. ((uint64_t)p[3] << 24) |
  23. ((uint64_t)p[4] << 32) |
  24. ((uint64_t)p[5] << 40) |
  25. ((uint64_t)p[6] << 48) |
  26. ((uint64_t)p[7] << 56);
  27. }
  28. /* store a 64 bit unsigned integer as eight 8 bit unsigned integers in little endian */
  29. static void
  30. U64TO8(unsigned char *p, uint64_t v) {
  31. p[0] = (unsigned char)(v ) & 0xff;
  32. p[1] = (unsigned char)(v >> 8) & 0xff;
  33. p[2] = (unsigned char)(v >> 16) & 0xff;
  34. p[3] = (unsigned char)(v >> 24) & 0xff;
  35. p[4] = (unsigned char)(v >> 32) & 0xff;
  36. p[5] = (unsigned char)(v >> 40) & 0xff;
  37. p[6] = (unsigned char)(v >> 48) & 0xff;
  38. p[7] = (unsigned char)(v >> 56) & 0xff;
  39. }
  40. size_t
  41. poly1305_block_size_ref(void) {
  42. return POLY1305_BLOCK_SIZE;
  43. }
  44. void
  45. poly1305_init_ext_ref(void *state, const poly1305_key *key, size_t bytes_hint) {
  46. poly1305_state_ref_t *st = (poly1305_state_ref_t *)state;
  47. uint64_t t0, t1;
  48. /* bytes_hint not used */
  49. (void)bytes_hint;
  50. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  51. t0 = U8TO64(&key->b[0]);
  52. t1 = U8TO64(&key->b[8]);
  53. st->r[0] = ( t0 ) & 0xffc0fffffff;
  54. st->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
  55. st->r[2] = ((t1 >> 24) ) & 0x00ffffffc0f;
  56. /* h = 0 */
  57. st->h[0] = 0;
  58. st->h[1] = 0;
  59. st->h[2] = 0;
  60. /* save pad for later */
  61. st->pad[0] = U8TO64(&key->b[16]);
  62. st->pad[1] = U8TO64(&key->b[24]);
  63. st->final = 0;
  64. }
  65. void
  66. poly1305_blocks_ref(void *state, const unsigned char *in, size_t inlen) {
  67. poly1305_state_ref_t *st = (poly1305_state_ref_t *)state;
  68. const uint64_t hibit = (st->final) ? 0 : ((uint64_t)1 << 40); /* 1 << 128 */
  69. uint64_t r0,r1,r2;
  70. uint64_t s1,s2;
  71. uint64_t h0,h1,h2;
  72. uint64_t c;
  73. uint128_t d0,d1,d2;
  74. r0 = st->r[0];
  75. r1 = st->r[1];
  76. r2 = st->r[2];
  77. s1 = r1 * (5 << 2);
  78. s2 = r2 * (5 << 2);
  79. h0 = st->h[0];
  80. h1 = st->h[1];
  81. h2 = st->h[2];
  82. while (inlen >= POLY1305_BLOCK_SIZE) {
  83. uint64_t t0, t1;
  84. /* h += in[i] */
  85. t0 = U8TO64(in + 0);
  86. t1 = U8TO64(in + 8);
  87. h0 += (( t0 ) & 0xfffffffffff);
  88. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
  89. h2 += (((t1 >> 24) ) & 0x3ffffffffff) | hibit;
  90. /* h *= r */
  91. d0 = ((uint128_t)h0 * r0) + ((uint128_t)h1 * s2) + ((uint128_t)h2 * s1);
  92. d1 = ((uint128_t)h0 * r1) + ((uint128_t)h1 * r0) + ((uint128_t)h2 * s2);
  93. d2 = ((uint128_t)h0 * r2) + ((uint128_t)h1 * r1) + ((uint128_t)h2 * r0);
  94. /* (partial) h %= p */
  95. c = (uint64_t)(d0 >> 44); h0 = (uint64_t)d0 & 0xfffffffffff;
  96. d1 += c; c = (uint64_t)(d1 >> 44); h1 = (uint64_t)d1 & 0xfffffffffff;
  97. d2 += c; c = (uint64_t)(d2 >> 42); h2 = (uint64_t)d2 & 0x3ffffffffff;
  98. h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffff;
  99. h1 += c;
  100. in += POLY1305_BLOCK_SIZE;
  101. inlen -= POLY1305_BLOCK_SIZE;
  102. }
  103. st->h[0] = h0;
  104. st->h[1] = h1;
  105. st->h[2] = h2;
  106. }
  107. void
  108. poly1305_finish_ext_ref(void *state, const unsigned char *in, size_t remaining, unsigned char mac[16]) {
  109. poly1305_state_ref_t *st = (poly1305_state_ref_t *)state;
  110. uint64_t h0, h1, h2, c;
  111. uint64_t g0, g1, g2;
  112. uint64_t t0, t1;
  113. /* process the remaining block */
  114. if (remaining) {
  115. unsigned char final[POLY1305_BLOCK_SIZE] = {0};
  116. size_t i;
  117. for (i = 0; i < remaining; i++)
  118. final[i] = in[i];
  119. final[remaining] = 1;
  120. st->final = 1;
  121. poly1305_blocks_ref(st, final, POLY1305_BLOCK_SIZE);
  122. }
  123. /* fully carry h */
  124. h0 = st->h[0];
  125. h1 = st->h[1];
  126. h2 = st->h[2];
  127. c = (h1 >> 44); h1 &= 0xfffffffffff;
  128. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  129. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  130. h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  131. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  132. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  133. h1 += c;
  134. /* compute h + -p */
  135. g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
  136. g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
  137. g2 = h2 + c - ((uint64_t)1 << 42);
  138. /* select h if h < p, or h + -p if h >= p */
  139. c = (g2 >> 63) - 1;
  140. h0 = (h0 & ~c) | (g0 & c);
  141. h1 = (h1 & ~c) | (g1 & c);
  142. h2 = (h2 & ~c) | (g2 & c);
  143. /* h = (h + pad) */
  144. t0 = st->pad[0];
  145. t1 = st->pad[1];
  146. h0 += (( t0 ) & 0xfffffffffff) ; c = (h0 >> 44); h0 &= 0xfffffffffff;
  147. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  148. h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c; h2 &= 0x3ffffffffff;
  149. /* mac = h % (2^128) */
  150. h0 = ((h0 ) | (h1 << 44));
  151. h1 = ((h1 >> 20) | (h2 << 24));
  152. U64TO8(&mac[0], h0);
  153. U64TO8(&mac[8], h1);
  154. /* zero out the state */
  155. st->h[0] = 0;
  156. st->h[1] = 0;
  157. st->h[2] = 0;
  158. st->r[0] = 0;
  159. st->r[1] = 0;
  160. st->r[2] = 0;
  161. st->pad[0] = 0;
  162. st->pad[1] = 0;
  163. }
  164. void
  165. poly1305_auth_ref(unsigned char mac[16], const unsigned char *in, size_t inlen, const poly1305_key *key) {
  166. poly1305_state_ref_t st;
  167. size_t blocks;
  168. poly1305_init_ext_ref(&st, key, inlen);
  169. blocks = (inlen & ~(POLY1305_BLOCK_SIZE - 1));
  170. if (blocks) {
  171. poly1305_blocks_ref(&st, in, blocks);
  172. in += blocks;
  173. inlen -= blocks;
  174. }
  175. poly1305_finish_ext_ref(&st, in, inlen, mac);
  176. }