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.
 
 
 
 
 
 

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