poly1305.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. // This implementation of poly1305 is by Andrew Moon
  15. // (https://github.com/floodyberry/poly1305-donna) and released as public
  16. // domain.
  17. #include <GFp/poly1305.h>
  18. #include "internal.h"
  19. #include "../internal.h"
  20. #if !defined(BORINGSSL_HAS_UINT128) || !defined(OPENSSL_X86_64)
  21. #if defined(__GNUC__)
  22. #pragma GCC diagnostic ignored "-Wsign-conversion"
  23. #pragma GCC diagnostic ignored "-Wconversion"
  24. #endif
  25. // We can assume little-endian.
  26. static uint32_t U8TO32_LE(const uint8_t *m) {
  27. uint32_t r;
  28. GFp_memcpy(&r, m, sizeof(r));
  29. return r;
  30. }
  31. static void U32TO8_LE(uint8_t *m, uint32_t v) {
  32. GFp_memcpy(m, &v, sizeof(v));
  33. }
  34. static uint64_t mul32x32_64(uint32_t a, uint32_t b) { return (uint64_t)a * b; }
  35. struct poly1305_state_st {
  36. uint32_t r0, r1, r2, r3, r4;
  37. uint32_t s1, s2, s3, s4;
  38. uint32_t h0, h1, h2, h3, h4;
  39. uint8_t buf[16];
  40. size_t buf_used;
  41. uint8_t key[16];
  42. };
  43. OPENSSL_STATIC_ASSERT(sizeof(struct poly1305_state_st) <= sizeof(poly1305_state),
  44. "poly1305_state isn't large enough to hold aligned poly1305_state_st");
  45. static inline struct poly1305_state_st *poly1305_aligned_state(
  46. poly1305_state *state) {
  47. dev_assert_secret(((uintptr_t)state & 63) == 0);
  48. return (struct poly1305_state_st *)(((uintptr_t)state + 63) & ~63);
  49. }
  50. // poly1305_blocks updates |state| given some amount of input data. This
  51. // function may only be called with a |len| that is not a multiple of 16 at the
  52. // end of the data. Otherwise the input must be buffered into 16 byte blocks.
  53. static void poly1305_update(struct poly1305_state_st *state, const uint8_t *in,
  54. size_t len) {
  55. uint32_t t0, t1, t2, t3;
  56. uint64_t t[5];
  57. uint32_t b;
  58. uint64_t c;
  59. size_t j;
  60. uint8_t mp[16];
  61. if (len < 16) {
  62. goto poly1305_donna_atmost15bytes;
  63. }
  64. poly1305_donna_16bytes:
  65. t0 = U8TO32_LE(in);
  66. t1 = U8TO32_LE(in + 4);
  67. t2 = U8TO32_LE(in + 8);
  68. t3 = U8TO32_LE(in + 12);
  69. in += 16;
  70. len -= 16;
  71. state->h0 += t0 & 0x3ffffff;
  72. state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
  73. state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
  74. state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
  75. state->h4 += (t3 >> 8) | (1 << 24);
  76. poly1305_donna_mul:
  77. t[0] = mul32x32_64(state->h0, state->r0) + mul32x32_64(state->h1, state->s4) +
  78. mul32x32_64(state->h2, state->s3) + mul32x32_64(state->h3, state->s2) +
  79. mul32x32_64(state->h4, state->s1);
  80. t[1] = mul32x32_64(state->h0, state->r1) + mul32x32_64(state->h1, state->r0) +
  81. mul32x32_64(state->h2, state->s4) + mul32x32_64(state->h3, state->s3) +
  82. mul32x32_64(state->h4, state->s2);
  83. t[2] = mul32x32_64(state->h0, state->r2) + mul32x32_64(state->h1, state->r1) +
  84. mul32x32_64(state->h2, state->r0) + mul32x32_64(state->h3, state->s4) +
  85. mul32x32_64(state->h4, state->s3);
  86. t[3] = mul32x32_64(state->h0, state->r3) + mul32x32_64(state->h1, state->r2) +
  87. mul32x32_64(state->h2, state->r1) + mul32x32_64(state->h3, state->r0) +
  88. mul32x32_64(state->h4, state->s4);
  89. t[4] = mul32x32_64(state->h0, state->r4) + mul32x32_64(state->h1, state->r3) +
  90. mul32x32_64(state->h2, state->r2) + mul32x32_64(state->h3, state->r1) +
  91. mul32x32_64(state->h4, state->r0);
  92. state->h0 = (uint32_t)t[0] & 0x3ffffff;
  93. c = (t[0] >> 26);
  94. t[1] += c;
  95. state->h1 = (uint32_t)t[1] & 0x3ffffff;
  96. b = (uint32_t)(t[1] >> 26);
  97. t[2] += b;
  98. state->h2 = (uint32_t)t[2] & 0x3ffffff;
  99. b = (uint32_t)(t[2] >> 26);
  100. t[3] += b;
  101. state->h3 = (uint32_t)t[3] & 0x3ffffff;
  102. b = (uint32_t)(t[3] >> 26);
  103. t[4] += b;
  104. state->h4 = (uint32_t)t[4] & 0x3ffffff;
  105. b = (uint32_t)(t[4] >> 26);
  106. state->h0 += b * 5;
  107. if (len >= 16) {
  108. goto poly1305_donna_16bytes;
  109. }
  110. // final bytes
  111. poly1305_donna_atmost15bytes:
  112. if (!len) {
  113. return;
  114. }
  115. for (j = 0; j < len; j++) {
  116. mp[j] = in[j];
  117. }
  118. mp[j++] = 1;
  119. for (; j < 16; j++) {
  120. mp[j] = 0;
  121. }
  122. len = 0;
  123. t0 = U8TO32_LE(mp + 0);
  124. t1 = U8TO32_LE(mp + 4);
  125. t2 = U8TO32_LE(mp + 8);
  126. t3 = U8TO32_LE(mp + 12);
  127. state->h0 += t0 & 0x3ffffff;
  128. state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
  129. state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
  130. state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
  131. state->h4 += (t3 >> 8);
  132. goto poly1305_donna_mul;
  133. }
  134. void GFp_poly1305_init(poly1305_state *statep, const uint8_t key[32]) {
  135. struct poly1305_state_st *state = poly1305_aligned_state(statep);
  136. uint32_t t0, t1, t2, t3;
  137. t0 = U8TO32_LE(key + 0);
  138. t1 = U8TO32_LE(key + 4);
  139. t2 = U8TO32_LE(key + 8);
  140. t3 = U8TO32_LE(key + 12);
  141. // precompute multipliers
  142. state->r0 = t0 & 0x3ffffff;
  143. t0 >>= 26;
  144. t0 |= t1 << 6;
  145. state->r1 = t0 & 0x3ffff03;
  146. t1 >>= 20;
  147. t1 |= t2 << 12;
  148. state->r2 = t1 & 0x3ffc0ff;
  149. t2 >>= 14;
  150. t2 |= t3 << 18;
  151. state->r3 = t2 & 0x3f03fff;
  152. t3 >>= 8;
  153. state->r4 = t3 & 0x00fffff;
  154. state->s1 = state->r1 * 5;
  155. state->s2 = state->r2 * 5;
  156. state->s3 = state->r3 * 5;
  157. state->s4 = state->r4 * 5;
  158. // init state
  159. state->h0 = 0;
  160. state->h1 = 0;
  161. state->h2 = 0;
  162. state->h3 = 0;
  163. state->h4 = 0;
  164. state->buf_used = 0;
  165. GFp_memcpy(state->key, key + 16, sizeof(state->key));
  166. }
  167. void GFp_poly1305_update(poly1305_state *statep, const uint8_t *in,
  168. size_t in_len) {
  169. struct poly1305_state_st *state = poly1305_aligned_state(statep);
  170. if (state->buf_used) {
  171. size_t todo = 16 - state->buf_used;
  172. if (todo > in_len) {
  173. todo = in_len;
  174. }
  175. for (size_t i = 0; i < todo; i++) {
  176. state->buf[state->buf_used + i] = in[i];
  177. }
  178. state->buf_used += todo;
  179. in_len -= todo;
  180. in += todo;
  181. if (state->buf_used == 16) {
  182. poly1305_update(state, state->buf, 16);
  183. state->buf_used = 0;
  184. }
  185. }
  186. if (in_len >= 16) {
  187. size_t todo = in_len & ~0xf;
  188. poly1305_update(state, in, todo);
  189. in += todo;
  190. in_len &= 0xf;
  191. }
  192. if (in_len) {
  193. for (size_t i = 0; i < in_len; i++) {
  194. state->buf[i] = in[i];
  195. }
  196. state->buf_used = in_len;
  197. }
  198. }
  199. void GFp_poly1305_finish(poly1305_state *statep, uint8_t mac[16]) {
  200. struct poly1305_state_st *state = poly1305_aligned_state(statep);
  201. uint64_t f0, f1, f2, f3;
  202. uint32_t g0, g1, g2, g3, g4;
  203. uint32_t b, nb;
  204. if (state->buf_used) {
  205. poly1305_update(state, state->buf, state->buf_used);
  206. }
  207. b = state->h0 >> 26;
  208. state->h0 = state->h0 & 0x3ffffff;
  209. state->h1 += b;
  210. b = state->h1 >> 26;
  211. state->h1 = state->h1 & 0x3ffffff;
  212. state->h2 += b;
  213. b = state->h2 >> 26;
  214. state->h2 = state->h2 & 0x3ffffff;
  215. state->h3 += b;
  216. b = state->h3 >> 26;
  217. state->h3 = state->h3 & 0x3ffffff;
  218. state->h4 += b;
  219. b = state->h4 >> 26;
  220. state->h4 = state->h4 & 0x3ffffff;
  221. state->h0 += b * 5;
  222. g0 = state->h0 + 5;
  223. b = g0 >> 26;
  224. g0 &= 0x3ffffff;
  225. g1 = state->h1 + b;
  226. b = g1 >> 26;
  227. g1 &= 0x3ffffff;
  228. g2 = state->h2 + b;
  229. b = g2 >> 26;
  230. g2 &= 0x3ffffff;
  231. g3 = state->h3 + b;
  232. b = g3 >> 26;
  233. g3 &= 0x3ffffff;
  234. g4 = state->h4 + b - (1 << 26);
  235. b = (g4 >> 31) - 1;
  236. nb = ~b;
  237. state->h0 = (state->h0 & nb) | (g0 & b);
  238. state->h1 = (state->h1 & nb) | (g1 & b);
  239. state->h2 = (state->h2 & nb) | (g2 & b);
  240. state->h3 = (state->h3 & nb) | (g3 & b);
  241. state->h4 = (state->h4 & nb) | (g4 & b);
  242. f0 = ((state->h0) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]);
  243. f1 = ((state->h1 >> 6) | (state->h2 << 20)) +
  244. (uint64_t)U8TO32_LE(&state->key[4]);
  245. f2 = ((state->h2 >> 12) | (state->h3 << 14)) +
  246. (uint64_t)U8TO32_LE(&state->key[8]);
  247. f3 = ((state->h3 >> 18) | (state->h4 << 8)) +
  248. (uint64_t)U8TO32_LE(&state->key[12]);
  249. U32TO8_LE(&mac[0], (uint32_t)f0);
  250. f1 += (f0 >> 32);
  251. U32TO8_LE(&mac[4], (uint32_t)f1);
  252. f2 += (f1 >> 32);
  253. U32TO8_LE(&mac[8], (uint32_t)f2);
  254. f3 += (f2 >> 32);
  255. U32TO8_LE(&mac[12], (uint32_t)f3);
  256. }
  257. #endif // !BORINGSSL_HAS_UINT128 || !OPENSSL_X86_64