Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

332 Zeilen
9.1 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. }
  116. /* final bytes */
  117. poly1305_donna_atmost15bytes:
  118. if (!len) {
  119. return;
  120. }
  121. for (j = 0; j < len; j++) {
  122. mp[j] = in[j];
  123. }
  124. mp[j++] = 1;
  125. for (; j < 16; j++) {
  126. mp[j] = 0;
  127. }
  128. len = 0;
  129. t0 = U8TO32_LE(mp + 0);
  130. t1 = U8TO32_LE(mp + 4);
  131. t2 = U8TO32_LE(mp + 8);
  132. t3 = U8TO32_LE(mp + 12);
  133. state->h0 += t0 & 0x3ffffff;
  134. state->h1 += ((((uint64_t)t1 << 32) | t0) >> 26) & 0x3ffffff;
  135. state->h2 += ((((uint64_t)t2 << 32) | t1) >> 20) & 0x3ffffff;
  136. state->h3 += ((((uint64_t)t3 << 32) | t2) >> 14) & 0x3ffffff;
  137. state->h4 += (t3 >> 8);
  138. goto poly1305_donna_mul;
  139. }
  140. void CRYPTO_poly1305_init(poly1305_state *statep, const uint8_t key[32]) {
  141. struct poly1305_state_st *state = (struct poly1305_state_st *)statep;
  142. uint32_t t0, t1, t2, t3;
  143. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  144. if (CRYPTO_is_NEON_functional()) {
  145. CRYPTO_poly1305_init_neon(statep, key);
  146. return;
  147. }
  148. #endif
  149. t0 = U8TO32_LE(key + 0);
  150. t1 = U8TO32_LE(key + 4);
  151. t2 = U8TO32_LE(key + 8);
  152. t3 = U8TO32_LE(key + 12);
  153. /* precompute multipliers */
  154. state->r0 = t0 & 0x3ffffff;
  155. t0 >>= 26;
  156. t0 |= t1 << 6;
  157. state->r1 = t0 & 0x3ffff03;
  158. t1 >>= 20;
  159. t1 |= t2 << 12;
  160. state->r2 = t1 & 0x3ffc0ff;
  161. t2 >>= 14;
  162. t2 |= t3 << 18;
  163. state->r3 = t2 & 0x3f03fff;
  164. t3 >>= 8;
  165. state->r4 = t3 & 0x00fffff;
  166. state->s1 = state->r1 * 5;
  167. state->s2 = state->r2 * 5;
  168. state->s3 = state->r3 * 5;
  169. state->s4 = state->r4 * 5;
  170. /* init state */
  171. state->h0 = 0;
  172. state->h1 = 0;
  173. state->h2 = 0;
  174. state->h3 = 0;
  175. state->h4 = 0;
  176. state->buf_used = 0;
  177. memcpy(state->key, key + 16, sizeof(state->key));
  178. }
  179. void CRYPTO_poly1305_update(poly1305_state *statep, const uint8_t *in,
  180. size_t in_len) {
  181. unsigned int i;
  182. struct poly1305_state_st *state = (struct poly1305_state_st *)statep;
  183. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  184. if (CRYPTO_is_NEON_functional()) {
  185. CRYPTO_poly1305_update_neon(statep, in, in_len);
  186. return;
  187. }
  188. #endif
  189. if (state->buf_used) {
  190. unsigned int todo = 16 - state->buf_used;
  191. if (todo > in_len) {
  192. todo = in_len;
  193. }
  194. for (i = 0; i < todo; i++) {
  195. state->buf[state->buf_used + i] = in[i];
  196. }
  197. state->buf_used += todo;
  198. in_len -= todo;
  199. in += todo;
  200. if (state->buf_used == 16) {
  201. poly1305_update(state, state->buf, 16);
  202. state->buf_used = 0;
  203. }
  204. }
  205. if (in_len >= 16) {
  206. size_t todo = in_len & ~0xf;
  207. poly1305_update(state, in, todo);
  208. in += todo;
  209. in_len &= 0xf;
  210. }
  211. if (in_len) {
  212. for (i = 0; i < in_len; i++) {
  213. state->buf[i] = in[i];
  214. }
  215. state->buf_used = in_len;
  216. }
  217. }
  218. void CRYPTO_poly1305_finish(poly1305_state *statep, uint8_t mac[16]) {
  219. struct poly1305_state_st *state = (struct poly1305_state_st *)statep;
  220. uint64_t f0, f1, f2, f3;
  221. uint32_t g0, g1, g2, g3, g4;
  222. uint32_t b, nb;
  223. #if defined(OPENSSL_ARM) && !defined(OPENSSL_NO_ASM)
  224. if (CRYPTO_is_NEON_functional()) {
  225. CRYPTO_poly1305_finish_neon(statep, mac);
  226. return;
  227. }
  228. #endif
  229. if (state->buf_used) {
  230. poly1305_update(state, state->buf, state->buf_used);
  231. }
  232. b = state->h0 >> 26;
  233. state->h0 = state->h0 & 0x3ffffff;
  234. state->h1 += b;
  235. b = state->h1 >> 26;
  236. state->h1 = state->h1 & 0x3ffffff;
  237. state->h2 += b;
  238. b = state->h2 >> 26;
  239. state->h2 = state->h2 & 0x3ffffff;
  240. state->h3 += b;
  241. b = state->h3 >> 26;
  242. state->h3 = state->h3 & 0x3ffffff;
  243. state->h4 += b;
  244. b = state->h4 >> 26;
  245. state->h4 = state->h4 & 0x3ffffff;
  246. state->h0 += b * 5;
  247. g0 = state->h0 + 5;
  248. b = g0 >> 26;
  249. g0 &= 0x3ffffff;
  250. g1 = state->h1 + b;
  251. b = g1 >> 26;
  252. g1 &= 0x3ffffff;
  253. g2 = state->h2 + b;
  254. b = g2 >> 26;
  255. g2 &= 0x3ffffff;
  256. g3 = state->h3 + b;
  257. b = g3 >> 26;
  258. g3 &= 0x3ffffff;
  259. g4 = state->h4 + b - (1 << 26);
  260. b = (g4 >> 31) - 1;
  261. nb = ~b;
  262. state->h0 = (state->h0 & nb) | (g0 & b);
  263. state->h1 = (state->h1 & nb) | (g1 & b);
  264. state->h2 = (state->h2 & nb) | (g2 & b);
  265. state->h3 = (state->h3 & nb) | (g3 & b);
  266. state->h4 = (state->h4 & nb) | (g4 & b);
  267. f0 = ((state->h0) | (state->h1 << 26)) + (uint64_t)U8TO32_LE(&state->key[0]);
  268. f1 = ((state->h1 >> 6) | (state->h2 << 20)) +
  269. (uint64_t)U8TO32_LE(&state->key[4]);
  270. f2 = ((state->h2 >> 12) | (state->h3 << 14)) +
  271. (uint64_t)U8TO32_LE(&state->key[8]);
  272. f3 = ((state->h3 >> 18) | (state->h4 << 8)) +
  273. (uint64_t)U8TO32_LE(&state->key[12]);
  274. U32TO8_LE(&mac[0], f0);
  275. f1 += (f0 >> 32);
  276. U32TO8_LE(&mac[4], f1);
  277. f2 += (f1 >> 32);
  278. U32TO8_LE(&mac[8], f2);
  279. f3 += (f2 >> 32);
  280. U32TO8_LE(&mac[12], f3);
  281. }
  282. #endif /* OPENSSL_WINDOWS || !OPENSSL_X86_64 */