poly1305-donna-32.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. poly1305 implementation using 32 bit * 32 bit = 64 bit multiplication and 64 bit addition
  3. */
  4. #if defined(_MSC_VER)
  5. #define POLY1305_NOINLINE __declspec(noinline)
  6. #elif defined(__GNUC__)
  7. #define POLY1305_NOINLINE __attribute__((noinline))
  8. #else
  9. #define POLY1305_NOINLINE
  10. #endif
  11. #define poly1305_block_size 16
  12. /* 17 + sizeof(size_t) + 14*sizeof(unsigned long) */
  13. typedef struct poly1305_state_internal_t {
  14. unsigned long r[5];
  15. unsigned long h[5];
  16. unsigned long pad[4];
  17. size_t leftover;
  18. unsigned char buffer[poly1305_block_size];
  19. unsigned char final;
  20. } poly1305_state_internal_t;
  21. /* interpret four 8 bit unsigned integers as a 32 bit unsigned integer in little endian */
  22. static unsigned long
  23. U8TO32(const unsigned char *p) {
  24. return
  25. (((unsigned long)(p[0] & 0xff) ) |
  26. ((unsigned long)(p[1] & 0xff) << 8) |
  27. ((unsigned long)(p[2] & 0xff) << 16) |
  28. ((unsigned long)(p[3] & 0xff) << 24));
  29. }
  30. /* store a 32 bit unsigned integer as four 8 bit unsigned integers in little endian */
  31. static void
  32. U32TO8(unsigned char *p, unsigned long v) {
  33. p[0] = (v ) & 0xff;
  34. p[1] = (v >> 8) & 0xff;
  35. p[2] = (v >> 16) & 0xff;
  36. p[3] = (v >> 24) & 0xff;
  37. }
  38. void
  39. poly1305_init(poly1305_context *ctx, const unsigned char key[32]) {
  40. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  41. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  42. st->r[0] = (U8TO32(&key[ 0]) ) & 0x3ffffff;
  43. st->r[1] = (U8TO32(&key[ 3]) >> 2) & 0x3ffff03;
  44. st->r[2] = (U8TO32(&key[ 6]) >> 4) & 0x3ffc0ff;
  45. st->r[3] = (U8TO32(&key[ 9]) >> 6) & 0x3f03fff;
  46. st->r[4] = (U8TO32(&key[12]) >> 8) & 0x00fffff;
  47. /* h = 0 */
  48. st->h[0] = 0;
  49. st->h[1] = 0;
  50. st->h[2] = 0;
  51. st->h[3] = 0;
  52. st->h[4] = 0;
  53. /* save pad for later */
  54. st->pad[0] = U8TO32(&key[16]);
  55. st->pad[1] = U8TO32(&key[20]);
  56. st->pad[2] = U8TO32(&key[24]);
  57. st->pad[3] = U8TO32(&key[28]);
  58. st->leftover = 0;
  59. st->final = 0;
  60. }
  61. static void
  62. poly1305_blocks(poly1305_state_internal_t *st, const unsigned char *m, size_t bytes) {
  63. const unsigned long hibit = (st->final) ? 0 : (1UL << 24); /* 1 << 128 */
  64. unsigned long r0,r1,r2,r3,r4;
  65. unsigned long s1,s2,s3,s4;
  66. unsigned long h0,h1,h2,h3,h4;
  67. unsigned long long d0,d1,d2,d3,d4;
  68. unsigned long c;
  69. r0 = st->r[0];
  70. r1 = st->r[1];
  71. r2 = st->r[2];
  72. r3 = st->r[3];
  73. r4 = st->r[4];
  74. s1 = r1 * 5;
  75. s2 = r2 * 5;
  76. s3 = r3 * 5;
  77. s4 = r4 * 5;
  78. h0 = st->h[0];
  79. h1 = st->h[1];
  80. h2 = st->h[2];
  81. h3 = st->h[3];
  82. h4 = st->h[4];
  83. while (bytes >= poly1305_block_size) {
  84. /* h += m[i] */
  85. h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
  86. h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
  87. h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
  88. h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
  89. h4 += (U8TO32(m+12) >> 8) | hibit;
  90. /* h *= r */
  91. 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);
  92. 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);
  93. 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);
  94. 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);
  95. 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);
  96. /* (partial) h %= p */
  97. c = (unsigned long)(d0 >> 26); h0 = (unsigned long)d0 & 0x3ffffff;
  98. d1 += c; c = (unsigned long)(d1 >> 26); h1 = (unsigned long)d1 & 0x3ffffff;
  99. d2 += c; c = (unsigned long)(d2 >> 26); h2 = (unsigned long)d2 & 0x3ffffff;
  100. d3 += c; c = (unsigned long)(d3 >> 26); h3 = (unsigned long)d3 & 0x3ffffff;
  101. d4 += c; c = (unsigned long)(d4 >> 26); h4 = (unsigned long)d4 & 0x3ffffff;
  102. h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
  103. h1 += c;
  104. m += poly1305_block_size;
  105. bytes -= poly1305_block_size;
  106. }
  107. st->h[0] = h0;
  108. st->h[1] = h1;
  109. st->h[2] = h2;
  110. st->h[3] = h3;
  111. st->h[4] = h4;
  112. }
  113. POLY1305_NOINLINE void
  114. poly1305_finish(poly1305_context *ctx, unsigned char mac[16]) {
  115. poly1305_state_internal_t *st = (poly1305_state_internal_t *)ctx;
  116. unsigned long h0,h1,h2,h3,h4,c;
  117. unsigned long g0,g1,g2,g3,g4;
  118. unsigned long long f;
  119. unsigned long mask;
  120. /* process the remaining block */
  121. if (st->leftover) {
  122. size_t i = st->leftover;
  123. st->buffer[i++] = 1;
  124. for (; i < poly1305_block_size; i++)
  125. st->buffer[i] = 0;
  126. st->final = 1;
  127. poly1305_blocks(st, st->buffer, poly1305_block_size);
  128. }
  129. /* fully carry h */
  130. h0 = st->h[0];
  131. h1 = st->h[1];
  132. h2 = st->h[2];
  133. h3 = st->h[3];
  134. h4 = st->h[4];
  135. c = h1 >> 26; h1 = h1 & 0x3ffffff;
  136. h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
  137. h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
  138. h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
  139. h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
  140. h1 += c;
  141. /* compute h + -p */
  142. g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
  143. g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
  144. g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
  145. g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
  146. g4 = h4 + c - (1UL << 26);
  147. /* select h if h < p, or h + -p if h >= p */
  148. mask = (g4 >> ((sizeof(unsigned long) * 8) - 1)) - 1;
  149. g0 &= mask;
  150. g1 &= mask;
  151. g2 &= mask;
  152. g3 &= mask;
  153. g4 &= mask;
  154. mask = ~mask;
  155. h0 = (h0 & mask) | g0;
  156. h1 = (h1 & mask) | g1;
  157. h2 = (h2 & mask) | g2;
  158. h3 = (h3 & mask) | g3;
  159. h4 = (h4 & mask) | g4;
  160. /* h = h % (2^128) */
  161. h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
  162. h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
  163. h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
  164. h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
  165. /* mac = (h + pad) % (2^128) */
  166. f = (unsigned long long)h0 + st->pad[0] ; h0 = (unsigned long)f;
  167. f = (unsigned long long)h1 + st->pad[1] + (f >> 32); h1 = (unsigned long)f;
  168. f = (unsigned long long)h2 + st->pad[2] + (f >> 32); h2 = (unsigned long)f;
  169. f = (unsigned long long)h3 + st->pad[3] + (f >> 32); h3 = (unsigned long)f;
  170. U32TO8(mac + 0, h0);
  171. U32TO8(mac + 4, h1);
  172. U32TO8(mac + 8, h2);
  173. U32TO8(mac + 12, h3);
  174. /* zero out the state */
  175. st->h[0] = 0;
  176. st->h[1] = 0;
  177. st->h[2] = 0;
  178. st->h[3] = 0;
  179. st->h[4] = 0;
  180. st->r[0] = 0;
  181. st->r[1] = 0;
  182. st->r[2] = 0;
  183. st->r[3] = 0;
  184. st->r[4] = 0;
  185. st->pad[0] = 0;
  186. st->pad[1] = 0;
  187. st->pad[2] = 0;
  188. st->pad[3] = 0;
  189. }