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.
 
 
 
 
 
 

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