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.
 
 
 
 
 
 

324 line
9.0 KiB

  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 <openssl/poly1305.h>
  18. #include <string.h>
  19. #include <openssl/cpu.h>
  20. #if defined(OPENSSL_WINDOWS) || !defined(OPENSSL_X86_64)
  21. #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || defined(OPENSSL_ARM)
  22. /* We can assume little-endian. */
  23. static uint32_t U8TO32_LE(const uint8_t *m) {
  24. uint32_t r;
  25. memcpy(&r, m, sizeof(r));
  26. return r;
  27. }
  28. static void U32TO8_LE(uint8_t *m, uint32_t v) { memcpy(m, &v, sizeof(v)); }
  29. #else
  30. static uint32_t U8TO32_LE(const uint8_t *m) {
  31. return (uint32_t)m[0] | (uint32_t)m[1] << 8 | (uint32_t)m[2] << 16 |
  32. (uint32_t)m[3] << 24;
  33. }
  34. static void U32TO8_LE(uint8_t *m, uint32_t v) {
  35. m[0] = v;
  36. m[1] = v >> 8;
  37. m[2] = v >> 16;
  38. m[3] = v >> 24;
  39. }
  40. #endif
  41. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  42. void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]);
  43. void CRYPTO_poly1305_update_neon(poly1305_state *state, const uint8_t *in,
  44. size_t in_len);
  45. void CRYPTO_poly1305_finish_neon(poly1305_state *state, uint8_t mac[16]);
  46. #endif
  47. static uint64_t mul32x32_64(uint32_t a, uint32_t b) { return (uint64_t)a * b; }
  48. struct poly1305_state_st {
  49. uint32_t r0, r1, r2, r3, r4;
  50. uint32_t s1, s2, s3, s4;
  51. uint32_t h0, h1, h2, h3, h4;
  52. uint8_t buf[16];
  53. unsigned int buf_used;
  54. uint8_t key[16];
  55. };
  56. /* poly1305_blocks updates |state| given some amount of input data. This
  57. * function may only be called with a |len| that is not a multiple of 16 at the
  58. * end of the data. Otherwise the input must be buffered into 16 byte blocks. */
  59. static void poly1305_update(struct poly1305_state_st *state, const uint8_t *in,
  60. size_t len) {
  61. uint32_t t0, t1, t2, t3;
  62. uint64_t t[5];
  63. uint32_t b;
  64. uint64_t c;
  65. size_t j;
  66. uint8_t mp[16];
  67. if (len < 16) {
  68. goto poly1305_donna_atmost15bytes;
  69. }
  70. poly1305_donna_16bytes:
  71. t0 = U8TO32_LE(in);
  72. t1 = U8TO32_LE(in + 4);
  73. t2 = U8TO32_LE(in + 8);
  74. t3 = U8TO32_LE(in + 12);
  75. in += 16;
  76. len -= 16;
  77. state->h0 += t0 & 0x3ffffff;
  78. state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
  79. state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
  80. state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
  81. state->h4 += (t3 >> 8) | (1 << 24);
  82. poly1305_donna_mul:
  83. t[0] = mul32x32_64(state->h0, state->r0) + mul32x32_64(state->h1, state->s4) +
  84. mul32x32_64(state->h2, state->s3) + mul32x32_64(state->h3, state->s2) +
  85. mul32x32_64(state->h4, state->s1);
  86. t[1] = mul32x32_64(state->h0, state->r1) + mul32x32_64(state->h1, state->r0) +
  87. mul32x32_64(state->h2, state->s4) + mul32x32_64(state->h3, state->s3) +
  88. mul32x32_64(state->h4, state->s2);
  89. t[2] = mul32x32_64(state->h0, state->r2) + mul32x32_64(state->h1, state->r1) +
  90. mul32x32_64(state->h2, state->r0) + mul32x32_64(state->h3, state->s4) +
  91. mul32x32_64(state->h4, state->s3);
  92. t[3] = mul32x32_64(state->h0, state->r3) + mul32x32_64(state->h1, state->r2) +
  93. mul32x32_64(state->h2, state->r1) + mul32x32_64(state->h3, state->r0) +
  94. mul32x32_64(state->h4, state->s4);
  95. t[4] = mul32x32_64(state->h0, state->r4) + mul32x32_64(state->h1, state->r3) +
  96. mul32x32_64(state->h2, state->r2) + mul32x32_64(state->h3, state->r1) +
  97. mul32x32_64(state->h4, state->r0);
  98. state->h0 = (uint32_t)t[0] & 0x3ffffff;
  99. c = (t[0] >> 26);
  100. t[1] += c;
  101. state->h1 = (uint32_t)t[1] & 0x3ffffff;
  102. b = (uint32_t)(t[1] >> 26);
  103. t[2] += b;
  104. state->h2 = (uint32_t)t[2] & 0x3ffffff;
  105. b = (uint32_t)(t[2] >> 26);
  106. t[3] += b;
  107. state->h3 = (uint32_t)t[3] & 0x3ffffff;
  108. b = (uint32_t)(t[3] >> 26);
  109. t[4] += b;
  110. state->h4 = (uint32_t)t[4] & 0x3ffffff;
  111. b = (uint32_t)(t[4] >> 26);
  112. state->h0 += b * 5;
  113. if (len >= 16)
  114. goto poly1305_donna_16bytes;
  115. /* final bytes */
  116. poly1305_donna_atmost15bytes:
  117. if (!len)
  118. return;
  119. for (j = 0; j < len; j++)
  120. mp[j] = in[j];
  121. mp[j++] = 1;
  122. for (; j < 16; j++)
  123. mp[j] = 0;
  124. len = 0;
  125. t0 = U8TO32_LE(mp + 0);
  126. t1 = U8TO32_LE(mp + 4);
  127. t2 = U8TO32_LE(mp + 8);
  128. t3 = U8TO32_LE(mp + 12);
  129. state->h0 += t0 & 0x3ffffff;
  130. state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
  131. state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
  132. state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
  133. state->h4 += (t3 >> 8);
  134. goto poly1305_donna_mul;
  135. }
  136. void CRYPTO_poly1305_init(poly1305_state *statep, const uint8_t key[32]) {
  137. struct poly1305_state_st *state = (struct poly1305_state_st *)statep;
  138. uint32_t t0, t1, t2, t3;
  139. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  140. if (CRYPTO_is_NEON_functional()) {
  141. CRYPTO_poly1305_init_neon(statep, key);
  142. return;
  143. }
  144. #endif
  145. t0 = U8TO32_LE(key + 0);
  146. t1 = U8TO32_LE(key + 4);
  147. t2 = U8TO32_LE(key + 8);
  148. t3 = U8TO32_LE(key + 12);
  149. /* precompute multipliers */
  150. state->r0 = t0 & 0x3ffffff;
  151. t0 >>= 26;
  152. t0 |= t1 << 6;
  153. state->r1 = t0 & 0x3ffff03;
  154. t1 >>= 20;
  155. t1 |= t2 << 12;
  156. state->r2 = t1 & 0x3ffc0ff;
  157. t2 >>= 14;
  158. t2 |= t3 << 18;
  159. state->r3 = t2 & 0x3f03fff;
  160. t3 >>= 8;
  161. state->r4 = t3 & 0x00fffff;
  162. state->s1 = state->r1 * 5;
  163. state->s2 = state->r2 * 5;
  164. state->s3 = state->r3 * 5;
  165. state->s4 = state->r4 * 5;
  166. /* init state */
  167. state->h0 = 0;
  168. state->h1 = 0;
  169. state->h2 = 0;
  170. state->h3 = 0;
  171. state->h4 = 0;
  172. state->buf_used = 0;
  173. memcpy(state->key, key + 16, sizeof(state->key));
  174. }
  175. void CRYPTO_poly1305_update(poly1305_state *statep, const uint8_t *in,
  176. size_t in_len) {
  177. unsigned int i;
  178. struct poly1305_state_st *state = (struct poly1305_state_st *)statep;
  179. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  180. if (CRYPTO_is_NEON_functional()) {
  181. CRYPTO_poly1305_update_neon(statep, in, in_len);
  182. return;
  183. }
  184. #endif
  185. if (state->buf_used) {
  186. unsigned int todo = 16 - state->buf_used;
  187. if (todo > in_len)
  188. todo = in_len;
  189. for (i = 0; i < todo; i++)
  190. state->buf[state->buf_used + i] = in[i];
  191. state->buf_used += todo;
  192. in_len -= todo;
  193. in += todo;
  194. if (state->buf_used == 16) {
  195. poly1305_update(state, state->buf, 16);
  196. state->buf_used = 0;
  197. }
  198. }
  199. if (in_len >= 16) {
  200. size_t todo = in_len & ~0xf;
  201. poly1305_update(state, in, todo);
  202. in += todo;
  203. in_len &= 0xf;
  204. }
  205. if (in_len) {
  206. for (i = 0; i < in_len; i++)
  207. state->buf[i] = in[i];
  208. state->buf_used = in_len;
  209. }
  210. }
  211. void CRYPTO_poly1305_finish(poly1305_state *statep, uint8_t mac[16]) {
  212. struct poly1305_state_st *state = (struct poly1305_state_st *)statep;
  213. uint64_t f0, f1, f2, f3;
  214. uint32_t g0, g1, g2, g3, g4;
  215. uint32_t b, nb;
  216. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  217. if (CRYPTO_is_NEON_functional()) {
  218. CRYPTO_poly1305_finish_neon(statep, mac);
  219. return;
  220. }
  221. #endif
  222. if (state->buf_used)
  223. poly1305_update(state, state->buf, state->buf_used);
  224. b = state->h0 >> 26;
  225. state->h0 = state->h0 & 0x3ffffff;
  226. state->h1 += b;
  227. b = state->h1 >> 26;
  228. state->h1 = state->h1 & 0x3ffffff;
  229. state->h2 += b;
  230. b = state->h2 >> 26;
  231. state->h2 = state->h2 & 0x3ffffff;
  232. state->h3 += b;
  233. b = state->h3 >> 26;
  234. state->h3 = state->h3 & 0x3ffffff;
  235. state->h4 += b;
  236. b = state->h4 >> 26;
  237. state->h4 = state->h4 & 0x3ffffff;
  238. state->h0 += b * 5;
  239. g0 = state->h0 + 5;
  240. b = g0 >> 26;
  241. g0 &= 0x3ffffff;
  242. g1 = state->h1 + b;
  243. b = g1 >> 26;
  244. g1 &= 0x3ffffff;
  245. g2 = state->h2 + b;
  246. b = g2 >> 26;
  247. g2 &= 0x3ffffff;
  248. g3 = state->h3 + b;
  249. b = g3 >> 26;
  250. g3 &= 0x3ffffff;
  251. g4 = state->h4 + b - (1 << 26);
  252. b = (g4 >> 31) - 1;
  253. nb = ~b;
  254. state->h0 = (state->h0 & nb) | (g0 & b);
  255. state->h1 = (state->h1 & nb) | (g1 & b);
  256. state->h2 = (state->h2 & nb) | (g2 & b);
  257. state->h3 = (state->h3 & nb) | (g3 & b);
  258. state->h4 = (state->h4 & nb) | (g4 & b);
  259. f0 = ((state->h0) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]);
  260. f1 = ((state->h1 >> 6) | (state->h2 << 20)) +
  261. (uint64_t)U8TO32_LE(&state->key[4]);
  262. f2 = ((state->h2 >> 12) | (state->h3 << 14)) +
  263. (uint64_t)U8TO32_LE(&state->key[8]);
  264. f3 = ((state->h3 >> 18) | (state->h4 << 8)) +
  265. (uint64_t)U8TO32_LE(&state->key[12]);
  266. U32TO8_LE(&mac[0], f0);
  267. f1 += (f0 >> 32);
  268. U32TO8_LE(&mac[4], f1);
  269. f2 += (f1 >> 32);
  270. U32TO8_LE(&mac[8], f2);
  271. f3 += (f2 >> 32);
  272. U32TO8_LE(&mac[12], f3);
  273. }
  274. #endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 */