Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

288 řádky
7.3 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 was taken from the public domain, neon2 version in
  15. * SUPERCOP by D. J. Bernstein and Peter Schwabe. */
  16. #include <openssl/poly1305.h>
  17. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  18. typedef struct {
  19. uint32_t v[12]; /* for alignment; only using 10 */
  20. } fe1305x2;
  21. #define addmulmod openssl_poly1305_neon2_addmulmod
  22. #define blocks openssl_poly1305_neon2_blocks
  23. extern void addmulmod(fe1305x2 *r, const fe1305x2 *x, const fe1305x2 *y,
  24. const fe1305x2 *c);
  25. extern int blocks(fe1305x2 *h, const fe1305x2 *precomp, const uint8_t *in,
  26. unsigned int inlen);
  27. static void freeze(fe1305x2 *r) {
  28. int i;
  29. uint32_t x0 = r->v[0];
  30. uint32_t x1 = r->v[2];
  31. uint32_t x2 = r->v[4];
  32. uint32_t x3 = r->v[6];
  33. uint32_t x4 = r->v[8];
  34. uint32_t y0;
  35. uint32_t y1;
  36. uint32_t y2;
  37. uint32_t y3;
  38. uint32_t y4;
  39. uint32_t swap;
  40. for (i = 0; i < 3; ++i) {
  41. x1 += x0 >> 26;
  42. x0 &= 0x3ffffff;
  43. x2 += x1 >> 26;
  44. x1 &= 0x3ffffff;
  45. x3 += x2 >> 26;
  46. x2 &= 0x3ffffff;
  47. x4 += x3 >> 26;
  48. x3 &= 0x3ffffff;
  49. x0 += 5 * (x4 >> 26);
  50. x4 &= 0x3ffffff;
  51. }
  52. y0 = x0 + 5;
  53. y1 = x1 + (y0 >> 26);
  54. y0 &= 0x3ffffff;
  55. y2 = x2 + (y1 >> 26);
  56. y1 &= 0x3ffffff;
  57. y3 = x3 + (y2 >> 26);
  58. y2 &= 0x3ffffff;
  59. y4 = x4 + (y3 >> 26);
  60. y3 &= 0x3ffffff;
  61. swap = -(y4 >> 26);
  62. y4 &= 0x3ffffff;
  63. y0 ^= x0;
  64. y1 ^= x1;
  65. y2 ^= x2;
  66. y3 ^= x3;
  67. y4 ^= x4;
  68. y0 &= swap;
  69. y1 &= swap;
  70. y2 &= swap;
  71. y3 &= swap;
  72. y4 &= swap;
  73. y0 ^= x0;
  74. y1 ^= x1;
  75. y2 ^= x2;
  76. y3 ^= x3;
  77. y4 ^= x4;
  78. r->v[0] = y0;
  79. r->v[2] = y1;
  80. r->v[4] = y2;
  81. r->v[6] = y3;
  82. r->v[8] = y4;
  83. }
  84. static void fe1305x2_tobytearray(uint8_t *r, fe1305x2 *x) {
  85. uint32_t x0 = x->v[0];
  86. uint32_t x1 = x->v[2];
  87. uint32_t x2 = x->v[4];
  88. uint32_t x3 = x->v[6];
  89. uint32_t x4 = x->v[8];
  90. x1 += x0 >> 26;
  91. x0 &= 0x3ffffff;
  92. x2 += x1 >> 26;
  93. x1 &= 0x3ffffff;
  94. x3 += x2 >> 26;
  95. x2 &= 0x3ffffff;
  96. x4 += x3 >> 26;
  97. x3 &= 0x3ffffff;
  98. *(uint32_t *)r = x0 + (x1 << 26);
  99. *(uint32_t *)(r + 4) = (x1 >> 6) + (x2 << 20);
  100. *(uint32_t *)(r + 8) = (x2 >> 12) + (x3 << 14);
  101. *(uint32_t *)(r + 12) = (x3 >> 18) + (x4 << 8);
  102. }
  103. /* load32 exists to avoid breaking strict aliasing rules in
  104. * fe1305x2_frombytearray. */
  105. static uint32_t load32(uint8_t *t) {
  106. uint32_t tmp;
  107. memcpy(&tmp, t, sizeof(tmp));
  108. return tmp;
  109. }
  110. static void fe1305x2_frombytearray(fe1305x2 *r, const uint8_t *x,
  111. unsigned long long xlen) {
  112. int i;
  113. uint8_t t[17];
  114. for (i = 0; (i < 16) && (i < xlen); i++)
  115. t[i] = x[i];
  116. xlen -= i;
  117. x += i;
  118. t[i++] = 1;
  119. for (; i < 17; i++)
  120. t[i] = 0;
  121. r->v[0] = 0x3ffffff & load32(t);
  122. r->v[2] = 0x3ffffff & (load32(t + 3) >> 2);
  123. r->v[4] = 0x3ffffff & (load32(t + 6) >> 4);
  124. r->v[6] = 0x3ffffff & (load32(t + 9) >> 6);
  125. r->v[8] = load32(t + 13);
  126. if (xlen) {
  127. for (i = 0; (i < 16) && (i < xlen); i++)
  128. t[i] = x[i];
  129. t[i++] = 1;
  130. for (; i < 17; i++)
  131. t[i] = 0;
  132. r->v[1] = 0x3ffffff & load32(t);
  133. r->v[3] = 0x3ffffff & (load32(t + 3) >> 2);
  134. r->v[5] = 0x3ffffff & (load32(t + 6) >> 4);
  135. r->v[7] = 0x3ffffff & (load32(t + 9) >> 6);
  136. r->v[9] = load32(t + 13);
  137. } else
  138. r->v[1] = r->v[3] = r->v[5] = r->v[7] = r->v[9] = 0;
  139. }
  140. static const fe1305x2 zero __attribute__((aligned(16)));
  141. struct poly1305_state_st {
  142. uint8_t data[sizeof(fe1305x2[5]) + 128];
  143. uint8_t buf[32];
  144. unsigned int buf_used;
  145. uint8_t key[16];
  146. };
  147. void CRYPTO_poly1305_init_neon(poly1305_state *state, const uint8_t key[32]) {
  148. struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
  149. fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
  150. fe1305x2 *const h = r + 1;
  151. fe1305x2 *const c = h + 1;
  152. fe1305x2 *const precomp = c + 1;
  153. unsigned int j;
  154. r->v[1] = r->v[0] = 0x3ffffff & *(uint32_t *)key;
  155. r->v[3] = r->v[2] = 0x3ffff03 & ((*(uint32_t *)(key + 3)) >> 2);
  156. r->v[5] = r->v[4] = 0x3ffc0ff & ((*(uint32_t *)(key + 6)) >> 4);
  157. r->v[7] = r->v[6] = 0x3f03fff & ((*(uint32_t *)(key + 9)) >> 6);
  158. r->v[9] = r->v[8] = 0x00fffff & ((*(uint32_t *)(key + 12)) >> 8);
  159. for (j = 0; j < 10; j++)
  160. h->v[j] = 0; /* XXX: should fast-forward a bit */
  161. addmulmod(precomp, r, r, &zero); /* precompute r^2 */
  162. addmulmod(precomp + 1, precomp, precomp, &zero); /* precompute r^4 */
  163. memcpy(st->key, key + 16, 16);
  164. st->buf_used = 0;
  165. }
  166. void CRYPTO_poly1305_update_neon(poly1305_state *state, const uint8_t *in,
  167. size_t in_len) {
  168. struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
  169. fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
  170. fe1305x2 *const h = r + 1;
  171. fe1305x2 *const c = h + 1;
  172. fe1305x2 *const precomp = c + 1;
  173. unsigned int i;
  174. if (st->buf_used) {
  175. unsigned int todo = 32 - st->buf_used;
  176. if (todo > in_len)
  177. todo = in_len;
  178. for (i = 0; i < todo; i++)
  179. st->buf[st->buf_used + i] = in[i];
  180. st->buf_used += todo;
  181. in_len -= todo;
  182. in += todo;
  183. if (st->buf_used == sizeof(st->buf) && in_len) {
  184. addmulmod(h, h, precomp, &zero);
  185. fe1305x2_frombytearray(c, st->buf, sizeof(st->buf));
  186. for (i = 0; i < 10; i++)
  187. h->v[i] += c->v[i];
  188. st->buf_used = 0;
  189. }
  190. }
  191. while (in_len > 32) {
  192. unsigned int tlen = 1048576;
  193. if (in_len < tlen)
  194. tlen = in_len;
  195. tlen -= blocks(h, precomp, in, tlen);
  196. in_len -= tlen;
  197. in += tlen;
  198. }
  199. if (in_len) {
  200. for (i = 0; i < in_len; i++)
  201. st->buf[i] = in[i];
  202. st->buf_used = in_len;
  203. }
  204. }
  205. void CRYPTO_poly1305_finish_neon(poly1305_state *state, uint8_t mac[16]) {
  206. struct poly1305_state_st *st = (struct poly1305_state_st *)(state);
  207. fe1305x2 *const r = (fe1305x2 *)(st->data + (15 & (-(int)st->data)));
  208. fe1305x2 *const h = r + 1;
  209. fe1305x2 *const c = h + 1;
  210. fe1305x2 *const precomp = c + 1;
  211. addmulmod(h, h, precomp, &zero);
  212. if (st->buf_used > 16) {
  213. fe1305x2_frombytearray(c, st->buf, st->buf_used);
  214. precomp->v[1] = r->v[1];
  215. precomp->v[3] = r->v[3];
  216. precomp->v[5] = r->v[5];
  217. precomp->v[7] = r->v[7];
  218. precomp->v[9] = r->v[9];
  219. addmulmod(h, h, precomp, c);
  220. } else if (st->buf_used > 0) {
  221. fe1305x2_frombytearray(c, st->buf, st->buf_used);
  222. r->v[1] = 1;
  223. r->v[3] = 0;
  224. r->v[5] = 0;
  225. r->v[7] = 0;
  226. r->v[9] = 0;
  227. addmulmod(h, h, r, c);
  228. }
  229. h->v[0] += h->v[1];
  230. h->v[2] += h->v[3];
  231. h->v[4] += h->v[5];
  232. h->v[6] += h->v[7];
  233. h->v[8] += h->v[9];
  234. freeze(h);
  235. fe1305x2_frombytearray(c, st->key, 16);
  236. c->v[8] ^= (1 << 24);
  237. h->v[0] += c->v[0];
  238. h->v[2] += c->v[2];
  239. h->v[4] += c->v[4];
  240. h->v[6] += c->v[6];
  241. h->v[8] += c->v[8];
  242. fe1305x2_tobytearray(mac, h);
  243. }
  244. #endif /* OPENSSL_ARM && !OPENSSL_NO_ASM */