25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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