poly1305.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. /* poly1305.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*
  22. DESCRIPTION
  23. This library contains implementation for the Poly1305 authenticator.
  24. Based off the public domain implementations by Andrew Moon
  25. and Daniel J. Bernstein
  26. */
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30. #include <wolfssl/wolfcrypt/settings.h>
  31. #ifdef HAVE_POLY1305
  32. #include <wolfssl/wolfcrypt/poly1305.h>
  33. #include <wolfssl/wolfcrypt/error-crypt.h>
  34. #include <wolfssl/wolfcrypt/logging.h>
  35. #include <wolfssl/wolfcrypt/cpuid.h>
  36. #ifdef NO_INLINE
  37. #include <wolfssl/wolfcrypt/misc.h>
  38. #else
  39. #define WOLFSSL_MISC_INCLUDED
  40. #include <wolfcrypt/src/misc.c>
  41. #endif
  42. #ifdef CHACHA_AEAD_TEST
  43. #include <stdio.h>
  44. #endif
  45. #ifdef _MSC_VER
  46. /* 4127 warning constant while(1) */
  47. #pragma warning(disable: 4127)
  48. #endif
  49. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  50. #include <emmintrin.h>
  51. #include <immintrin.h>
  52. #if defined(__GNUC__) && ((__GNUC__ < 4) || \
  53. (__GNUC__ == 4 && __GNUC_MINOR__ <= 8))
  54. #undef NO_AVX2_SUPPORT
  55. #define NO_AVX2_SUPPORT
  56. #endif
  57. #if defined(__clang__) && ((__clang_major__ < 3) || \
  58. (__clang_major__ == 3 && __clang_minor__ <= 5))
  59. #define NO_AVX2_SUPPORT
  60. #elif defined(__clang__) && defined(NO_AVX2_SUPPORT)
  61. #undef NO_AVX2_SUPPORT
  62. #endif
  63. #define HAVE_INTEL_AVX1
  64. #ifndef NO_AVX2_SUPPORT
  65. #define HAVE_INTEL_AVX2
  66. #endif
  67. #endif
  68. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  69. static word32 intel_flags = 0;
  70. static word32 cpu_flags_set = 0;
  71. #endif
  72. #if (defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)) || \
  73. defined(POLY130564)
  74. #if defined(_MSC_VER)
  75. #define POLY1305_NOINLINE __declspec(noinline)
  76. #elif defined(__GNUC__)
  77. #define POLY1305_NOINLINE __attribute__((noinline))
  78. #else
  79. #define POLY1305_NOINLINE
  80. #endif
  81. #if defined(_MSC_VER)
  82. #include <intrin.h>
  83. typedef struct word128 {
  84. word64 lo;
  85. word64 hi;
  86. } word128;
  87. #define MUL(out, x, y) out.lo = _umul128((x), (y), &out.hi)
  88. #define ADD(out, in) { word64 t = out.lo; out.lo += in.lo; \
  89. out.hi += (out.lo < t) + in.hi; }
  90. #define ADDLO(out, in) { word64 t = out.lo; out.lo += in; \
  91. out.hi += (out.lo < t); }
  92. #define SHR(in, shift) (__shiftright128(in.lo, in.hi, (shift)))
  93. #define LO(in) (in.lo)
  94. #elif defined(__GNUC__)
  95. #if defined(__SIZEOF_INT128__)
  96. PEDANTIC_EXTENSION typedef unsigned __int128 word128;
  97. #else
  98. typedef unsigned word128 __attribute__((mode(TI)));
  99. #endif
  100. #define MUL(out, x, y) out = ((word128)(x) * (y))
  101. #define ADD(out, in) (out) += (in)
  102. #define ADDLO(out, in) (out) += (in)
  103. #define SHR(in, shift) (word64)((in) >> (shift))
  104. #define LO(in) (word64)(in)
  105. #endif
  106. #endif
  107. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  108. #ifdef __cplusplus
  109. extern "C" {
  110. #endif
  111. #ifdef HAVE_INTEL_AVX1
  112. /* Process one block (16 bytes) of data.
  113. *
  114. * ctx Poly1305 context.
  115. * m One block of message data.
  116. */
  117. extern void poly1305_block_avx(Poly1305* ctx, const unsigned char *m);
  118. /* Process multiple blocks (n * 16 bytes) of data.
  119. *
  120. * ctx Poly1305 context.
  121. * m Blocks of message data.
  122. * bytes The number of bytes to process.
  123. */
  124. extern void poly1305_blocks_avx(Poly1305* ctx, const unsigned char* m,
  125. size_t bytes);
  126. /* Set the key to use when processing data.
  127. * Initialize the context.
  128. *
  129. * ctx Poly1305 context.
  130. * key The key data (16 bytes).
  131. */
  132. extern void poly1305_setkey_avx(Poly1305* ctx, const byte* key);
  133. /* Calculate the final result - authentication data.
  134. * Zeros out the private data in the context.
  135. *
  136. * ctx Poly1305 context.
  137. * mac Buffer to hold 16 bytes.
  138. */
  139. extern void poly1305_final_avx(Poly1305* ctx, byte* mac);
  140. #endif
  141. #ifdef HAVE_INTEL_AVX2
  142. /* Process multiple blocks (n * 16 bytes) of data.
  143. *
  144. * ctx Poly1305 context.
  145. * m Blocks of message data.
  146. * bytes The number of bytes to process.
  147. */
  148. extern void poly1305_blocks_avx2(Poly1305* ctx, const unsigned char* m,
  149. size_t bytes);
  150. /* Calculate R^1, R^2, R^3 and R^4 and store them in the context.
  151. *
  152. * ctx Poly1305 context.
  153. */
  154. extern void poly1305_calc_powers_avx2(Poly1305* ctx);
  155. /* Set the key to use when processing data.
  156. * Initialize the context.
  157. * Calls AVX set key function as final function calls AVX code.
  158. *
  159. * ctx Poly1305 context.
  160. * key The key data (16 bytes).
  161. */
  162. extern void poly1305_setkey_avx2(Poly1305* ctx, const byte* key);
  163. /* Calculate the final result - authentication data.
  164. * Zeros out the private data in the context.
  165. * Calls AVX final function to quickly process last blocks.
  166. *
  167. * ctx Poly1305 context.
  168. * mac Buffer to hold 16 bytes - authentication data.
  169. */
  170. extern void poly1305_final_avx2(Poly1305* ctx, byte* mac);
  171. #endif
  172. #ifdef __cplusplus
  173. } /* extern "C" */
  174. #endif
  175. #elif defined(POLY130564)
  176. #ifndef WOLFSSL_ARMASM
  177. static word64 U8TO64(const byte* p)
  178. {
  179. return
  180. (((word64)(p[0] & 0xff) ) |
  181. ((word64)(p[1] & 0xff) << 8) |
  182. ((word64)(p[2] & 0xff) << 16) |
  183. ((word64)(p[3] & 0xff) << 24) |
  184. ((word64)(p[4] & 0xff) << 32) |
  185. ((word64)(p[5] & 0xff) << 40) |
  186. ((word64)(p[6] & 0xff) << 48) |
  187. ((word64)(p[7] & 0xff) << 56));
  188. }
  189. static void U64TO8(byte* p, word64 v) {
  190. p[0] = (byte)v;
  191. p[1] = (byte)(v >> 8);
  192. p[2] = (byte)(v >> 16);
  193. p[3] = (byte)(v >> 24);
  194. p[4] = (byte)(v >> 32);
  195. p[5] = (byte)(v >> 40);
  196. p[6] = (byte)(v >> 48);
  197. p[7] = (byte)(v >> 56);
  198. }
  199. #endif/* WOLFSSL_ARMASM */
  200. #else /* if not 64 bit then use 32 bit */
  201. static word32 U8TO32(const byte *p)
  202. {
  203. return
  204. (((word32)(p[0] & 0xff) ) |
  205. ((word32)(p[1] & 0xff) << 8) |
  206. ((word32)(p[2] & 0xff) << 16) |
  207. ((word32)(p[3] & 0xff) << 24));
  208. }
  209. static void U32TO8(byte *p, word32 v) {
  210. p[0] = (byte)((v ) & 0xff);
  211. p[1] = (byte)((v >> 8) & 0xff);
  212. p[2] = (byte)((v >> 16) & 0xff);
  213. p[3] = (byte)((v >> 24) & 0xff);
  214. }
  215. #endif
  216. /* convert 32-bit unsigned to little endian 64 bit type as byte array */
  217. static WC_INLINE void u32tole64(const word32 inLe32, byte outLe64[8])
  218. {
  219. #ifndef WOLFSSL_X86_64_BUILD
  220. outLe64[0] = (byte)(inLe32 & 0x000000FF);
  221. outLe64[1] = (byte)((inLe32 & 0x0000FF00) >> 8);
  222. outLe64[2] = (byte)((inLe32 & 0x00FF0000) >> 16);
  223. outLe64[3] = (byte)((inLe32 & 0xFF000000) >> 24);
  224. outLe64[4] = 0;
  225. outLe64[5] = 0;
  226. outLe64[6] = 0;
  227. outLe64[7] = 0;
  228. #else
  229. *(word64*)outLe64 = inLe32;
  230. #endif
  231. }
  232. #if !defined(WOLFSSL_ARMASM) || !defined(__aarch64__)
  233. /*
  234. This local function operates on a message with a given number of bytes
  235. with a given ctx pointer to a Poly1305 structure.
  236. */
  237. static int poly1305_blocks(Poly1305* ctx, const unsigned char *m,
  238. size_t bytes)
  239. {
  240. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  241. /* AVX2 is handled in wc_Poly1305Update. */
  242. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  243. poly1305_blocks_avx(ctx, m, bytes);
  244. RESTORE_VECTOR_REGISTERS();
  245. return 0;
  246. #elif defined(POLY130564)
  247. const word64 hibit = (ctx->finished) ? 0 : ((word64)1 << 40); /* 1 << 128 */
  248. word64 r0,r1,r2;
  249. word64 s1,s2;
  250. word64 h0,h1,h2;
  251. word64 c;
  252. word128 d0,d1,d2,d;
  253. r0 = ctx->r[0];
  254. r1 = ctx->r[1];
  255. r2 = ctx->r[2];
  256. h0 = ctx->h[0];
  257. h1 = ctx->h[1];
  258. h2 = ctx->h[2];
  259. s1 = r1 * (5 << 2);
  260. s2 = r2 * (5 << 2);
  261. while (bytes >= POLY1305_BLOCK_SIZE) {
  262. word64 t0,t1;
  263. /* h += m[i] */
  264. t0 = U8TO64(&m[0]);
  265. t1 = U8TO64(&m[8]);
  266. h0 += (( t0 ) & 0xfffffffffff);
  267. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff);
  268. h2 += (((t1 >> 24) ) & 0x3ffffffffff) | hibit;
  269. /* h *= r */
  270. MUL(d0, h0, r0); MUL(d, h1, s2); ADD(d0, d); MUL(d, h2, s1); ADD(d0, d);
  271. MUL(d1, h0, r1); MUL(d, h1, r0); ADD(d1, d); MUL(d, h2, s2); ADD(d1, d);
  272. MUL(d2, h0, r2); MUL(d, h1, r1); ADD(d2, d); MUL(d, h2, r0); ADD(d2, d);
  273. /* (partial) h %= p */
  274. c = SHR(d0, 44); h0 = LO(d0) & 0xfffffffffff;
  275. ADDLO(d1, c); c = SHR(d1, 44); h1 = LO(d1) & 0xfffffffffff;
  276. ADDLO(d2, c); c = SHR(d2, 42); h2 = LO(d2) & 0x3ffffffffff;
  277. h0 += c * 5; c = (h0 >> 44); h0 = h0 & 0xfffffffffff;
  278. h1 += c;
  279. m += POLY1305_BLOCK_SIZE;
  280. bytes -= POLY1305_BLOCK_SIZE;
  281. }
  282. ctx->h[0] = h0;
  283. ctx->h[1] = h1;
  284. ctx->h[2] = h2;
  285. return 0;
  286. #else /* if not 64 bit then use 32 bit */
  287. const word32 hibit = (ctx->finished) ? 0 : ((word32)1 << 24); /* 1 << 128 */
  288. word32 r0,r1,r2,r3,r4;
  289. word32 s1,s2,s3,s4;
  290. word32 h0,h1,h2,h3,h4;
  291. word64 d0,d1,d2,d3,d4;
  292. word32 c;
  293. r0 = ctx->r[0];
  294. r1 = ctx->r[1];
  295. r2 = ctx->r[2];
  296. r3 = ctx->r[3];
  297. r4 = ctx->r[4];
  298. s1 = r1 * 5;
  299. s2 = r2 * 5;
  300. s3 = r3 * 5;
  301. s4 = r4 * 5;
  302. h0 = ctx->h[0];
  303. h1 = ctx->h[1];
  304. h2 = ctx->h[2];
  305. h3 = ctx->h[3];
  306. h4 = ctx->h[4];
  307. while (bytes >= POLY1305_BLOCK_SIZE) {
  308. /* h += m[i] */
  309. h0 += (U8TO32(m+ 0) ) & 0x3ffffff;
  310. h1 += (U8TO32(m+ 3) >> 2) & 0x3ffffff;
  311. h2 += (U8TO32(m+ 6) >> 4) & 0x3ffffff;
  312. h3 += (U8TO32(m+ 9) >> 6) & 0x3ffffff;
  313. h4 += (U8TO32(m+12) >> 8) | hibit;
  314. /* h *= r */
  315. d0 = ((word64)h0 * r0) + ((word64)h1 * s4) + ((word64)h2 * s3) +
  316. ((word64)h3 * s2) + ((word64)h4 * s1);
  317. d1 = ((word64)h0 * r1) + ((word64)h1 * r0) + ((word64)h2 * s4) +
  318. ((word64)h3 * s3) + ((word64)h4 * s2);
  319. d2 = ((word64)h0 * r2) + ((word64)h1 * r1) + ((word64)h2 * r0) +
  320. ((word64)h3 * s4) + ((word64)h4 * s3);
  321. d3 = ((word64)h0 * r3) + ((word64)h1 * r2) + ((word64)h2 * r1) +
  322. ((word64)h3 * r0) + ((word64)h4 * s4);
  323. d4 = ((word64)h0 * r4) + ((word64)h1 * r3) + ((word64)h2 * r2) +
  324. ((word64)h3 * r1) + ((word64)h4 * r0);
  325. /* (partial) h %= p */
  326. c = (word32)(d0 >> 26); h0 = (word32)d0 & 0x3ffffff;
  327. d1 += c; c = (word32)(d1 >> 26); h1 = (word32)d1 & 0x3ffffff;
  328. d2 += c; c = (word32)(d2 >> 26); h2 = (word32)d2 & 0x3ffffff;
  329. d3 += c; c = (word32)(d3 >> 26); h3 = (word32)d3 & 0x3ffffff;
  330. d4 += c; c = (word32)(d4 >> 26); h4 = (word32)d4 & 0x3ffffff;
  331. h0 += c * 5; c = (h0 >> 26); h0 = h0 & 0x3ffffff;
  332. h1 += c;
  333. m += POLY1305_BLOCK_SIZE;
  334. bytes -= POLY1305_BLOCK_SIZE;
  335. }
  336. ctx->h[0] = h0;
  337. ctx->h[1] = h1;
  338. ctx->h[2] = h2;
  339. ctx->h[3] = h3;
  340. ctx->h[4] = h4;
  341. return 0;
  342. #endif /* end of 64 bit cpu blocks or 32 bit cpu */
  343. }
  344. /*
  345. This local function is used for the last call when a message with a given
  346. number of bytes is less than the block size.
  347. */
  348. static int poly1305_block(Poly1305* ctx, const unsigned char *m)
  349. {
  350. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  351. /* No call to poly1305_block when AVX2, AVX2 does 4 blocks at a time. */
  352. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  353. poly1305_block_avx(ctx, m);
  354. RESTORE_VECTOR_REGISTERS();
  355. return 0;
  356. #else
  357. return poly1305_blocks(ctx, m, POLY1305_BLOCK_SIZE);
  358. #endif
  359. }
  360. #endif /* !defined(WOLFSSL_ARMASM) || !defined(__aarch64__) */
  361. #if !defined(WOLFSSL_ARMASM) || !defined(__aarch64__)
  362. int wc_Poly1305SetKey(Poly1305* ctx, const byte* key, word32 keySz)
  363. {
  364. #if defined(POLY130564) && \
  365. !(defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP))
  366. word64 t0,t1;
  367. #endif
  368. if (key == NULL)
  369. return BAD_FUNC_ARG;
  370. #ifdef CHACHA_AEAD_TEST
  371. word32 k;
  372. printf("Poly key used:\n");
  373. for (k = 0; k < keySz; k++) {
  374. printf("%02x", key[k]);
  375. if ((k+1) % 8 == 0)
  376. printf("\n");
  377. }
  378. printf("\n");
  379. #endif
  380. if (keySz != 32 || ctx == NULL)
  381. return BAD_FUNC_ARG;
  382. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  383. if (!cpu_flags_set) {
  384. intel_flags = cpuid_get_flags();
  385. cpu_flags_set = 1;
  386. }
  387. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  388. #ifdef HAVE_INTEL_AVX2
  389. if (IS_INTEL_AVX2(intel_flags))
  390. poly1305_setkey_avx2(ctx, key);
  391. else
  392. #endif
  393. poly1305_setkey_avx(ctx, key);
  394. RESTORE_VECTOR_REGISTERS();
  395. #elif defined(POLY130564)
  396. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  397. t0 = U8TO64(key + 0);
  398. t1 = U8TO64(key + 8);
  399. ctx->r[0] = ( t0 ) & 0xffc0fffffff;
  400. ctx->r[1] = ((t0 >> 44) | (t1 << 20)) & 0xfffffc0ffff;
  401. ctx->r[2] = ((t1 >> 24) ) & 0x00ffffffc0f;
  402. /* h (accumulator) = 0 */
  403. ctx->h[0] = 0;
  404. ctx->h[1] = 0;
  405. ctx->h[2] = 0;
  406. /* save pad for later */
  407. ctx->pad[0] = U8TO64(key + 16);
  408. ctx->pad[1] = U8TO64(key + 24);
  409. ctx->leftover = 0;
  410. ctx->finished = 0;
  411. #else /* if not 64 bit then use 32 bit */
  412. /* r &= 0xffffffc0ffffffc0ffffffc0fffffff */
  413. ctx->r[0] = (U8TO32(key + 0) ) & 0x3ffffff;
  414. ctx->r[1] = (U8TO32(key + 3) >> 2) & 0x3ffff03;
  415. ctx->r[2] = (U8TO32(key + 6) >> 4) & 0x3ffc0ff;
  416. ctx->r[3] = (U8TO32(key + 9) >> 6) & 0x3f03fff;
  417. ctx->r[4] = (U8TO32(key + 12) >> 8) & 0x00fffff;
  418. /* h = 0 */
  419. ctx->h[0] = 0;
  420. ctx->h[1] = 0;
  421. ctx->h[2] = 0;
  422. ctx->h[3] = 0;
  423. ctx->h[4] = 0;
  424. /* save pad for later */
  425. ctx->pad[0] = U8TO32(key + 16);
  426. ctx->pad[1] = U8TO32(key + 20);
  427. ctx->pad[2] = U8TO32(key + 24);
  428. ctx->pad[3] = U8TO32(key + 28);
  429. ctx->leftover = 0;
  430. ctx->finished = 0;
  431. #endif
  432. return 0;
  433. }
  434. int wc_Poly1305Final(Poly1305* ctx, byte* mac)
  435. {
  436. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  437. #elif defined(POLY130564)
  438. word64 h0,h1,h2,c;
  439. word64 g0,g1,g2;
  440. word64 t0,t1;
  441. #else
  442. word32 h0,h1,h2,h3,h4,c;
  443. word32 g0,g1,g2,g3,g4;
  444. word64 f;
  445. word32 mask;
  446. #endif
  447. if (ctx == NULL || mac == NULL)
  448. return BAD_FUNC_ARG;
  449. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  450. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  451. #ifdef HAVE_INTEL_AVX2
  452. if (IS_INTEL_AVX2(intel_flags))
  453. poly1305_final_avx2(ctx, mac);
  454. else
  455. #endif
  456. poly1305_final_avx(ctx, mac);
  457. RESTORE_VECTOR_REGISTERS();
  458. #elif defined(POLY130564)
  459. /* process the remaining block */
  460. if (ctx->leftover) {
  461. size_t i = ctx->leftover;
  462. ctx->buffer[i] = 1;
  463. for (i = i + 1; i < POLY1305_BLOCK_SIZE; i++)
  464. ctx->buffer[i] = 0;
  465. ctx->finished = 1;
  466. poly1305_block(ctx, ctx->buffer);
  467. }
  468. /* fully carry h */
  469. h0 = ctx->h[0];
  470. h1 = ctx->h[1];
  471. h2 = ctx->h[2];
  472. c = (h1 >> 44); h1 &= 0xfffffffffff;
  473. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  474. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  475. h1 += c; c = (h1 >> 44); h1 &= 0xfffffffffff;
  476. h2 += c; c = (h2 >> 42); h2 &= 0x3ffffffffff;
  477. h0 += c * 5; c = (h0 >> 44); h0 &= 0xfffffffffff;
  478. h1 += c;
  479. /* compute h + -p */
  480. g0 = h0 + 5; c = (g0 >> 44); g0 &= 0xfffffffffff;
  481. g1 = h1 + c; c = (g1 >> 44); g1 &= 0xfffffffffff;
  482. g2 = h2 + c - ((word64)1 << 42);
  483. /* select h if h < p, or h + -p if h >= p */
  484. c = (g2 >> ((sizeof(word64) * 8) - 1)) - 1;
  485. g0 &= c;
  486. g1 &= c;
  487. g2 &= c;
  488. c = ~c;
  489. h0 = (h0 & c) | g0;
  490. h1 = (h1 & c) | g1;
  491. h2 = (h2 & c) | g2;
  492. /* h = (h + pad) */
  493. t0 = ctx->pad[0];
  494. t1 = ctx->pad[1];
  495. h0 += (( t0 ) & 0xfffffffffff) ;
  496. c = (h0 >> 44); h0 &= 0xfffffffffff;
  497. h1 += (((t0 >> 44) | (t1 << 20)) & 0xfffffffffff) + c;
  498. c = (h1 >> 44); h1 &= 0xfffffffffff;
  499. h2 += (((t1 >> 24) ) & 0x3ffffffffff) + c;
  500. h2 &= 0x3ffffffffff;
  501. /* mac = h % (2^128) */
  502. h0 = ((h0 ) | (h1 << 44));
  503. h1 = ((h1 >> 20) | (h2 << 24));
  504. U64TO8(mac + 0, h0);
  505. U64TO8(mac + 8, h1);
  506. /* zero out the state */
  507. ctx->h[0] = 0;
  508. ctx->h[1] = 0;
  509. ctx->h[2] = 0;
  510. ctx->r[0] = 0;
  511. ctx->r[1] = 0;
  512. ctx->r[2] = 0;
  513. ctx->pad[0] = 0;
  514. ctx->pad[1] = 0;
  515. #else /* if not 64 bit then use 32 bit */
  516. /* process the remaining block */
  517. if (ctx->leftover) {
  518. size_t i = ctx->leftover;
  519. ctx->buffer[i++] = 1;
  520. for (; i < POLY1305_BLOCK_SIZE; i++)
  521. ctx->buffer[i] = 0;
  522. ctx->finished = 1;
  523. poly1305_block(ctx, ctx->buffer);
  524. }
  525. /* fully carry h */
  526. h0 = ctx->h[0];
  527. h1 = ctx->h[1];
  528. h2 = ctx->h[2];
  529. h3 = ctx->h[3];
  530. h4 = ctx->h[4];
  531. c = h1 >> 26; h1 = h1 & 0x3ffffff;
  532. h2 += c; c = h2 >> 26; h2 = h2 & 0x3ffffff;
  533. h3 += c; c = h3 >> 26; h3 = h3 & 0x3ffffff;
  534. h4 += c; c = h4 >> 26; h4 = h4 & 0x3ffffff;
  535. h0 += c * 5; c = h0 >> 26; h0 = h0 & 0x3ffffff;
  536. h1 += c;
  537. /* compute h + -p */
  538. g0 = h0 + 5; c = g0 >> 26; g0 &= 0x3ffffff;
  539. g1 = h1 + c; c = g1 >> 26; g1 &= 0x3ffffff;
  540. g2 = h2 + c; c = g2 >> 26; g2 &= 0x3ffffff;
  541. g3 = h3 + c; c = g3 >> 26; g3 &= 0x3ffffff;
  542. g4 = h4 + c - ((word32)1 << 26);
  543. /* select h if h < p, or h + -p if h >= p */
  544. mask = ((word32)g4 >> ((sizeof(word32) * 8) - 1)) - 1;
  545. g0 &= mask;
  546. g1 &= mask;
  547. g2 &= mask;
  548. g3 &= mask;
  549. g4 &= mask;
  550. mask = ~mask;
  551. h0 = (h0 & mask) | g0;
  552. h1 = (h1 & mask) | g1;
  553. h2 = (h2 & mask) | g2;
  554. h3 = (h3 & mask) | g3;
  555. h4 = (h4 & mask) | g4;
  556. /* h = h % (2^128) */
  557. h0 = ((h0 ) | (h1 << 26)) & 0xffffffff;
  558. h1 = ((h1 >> 6) | (h2 << 20)) & 0xffffffff;
  559. h2 = ((h2 >> 12) | (h3 << 14)) & 0xffffffff;
  560. h3 = ((h3 >> 18) | (h4 << 8)) & 0xffffffff;
  561. /* mac = (h + pad) % (2^128) */
  562. f = (word64)h0 + ctx->pad[0] ; h0 = (word32)f;
  563. f = (word64)h1 + ctx->pad[1] + (f >> 32); h1 = (word32)f;
  564. f = (word64)h2 + ctx->pad[2] + (f >> 32); h2 = (word32)f;
  565. f = (word64)h3 + ctx->pad[3] + (f >> 32); h3 = (word32)f;
  566. U32TO8(mac + 0, h0);
  567. U32TO8(mac + 4, h1);
  568. U32TO8(mac + 8, h2);
  569. U32TO8(mac + 12, h3);
  570. /* zero out the state */
  571. ctx->h[0] = 0;
  572. ctx->h[1] = 0;
  573. ctx->h[2] = 0;
  574. ctx->h[3] = 0;
  575. ctx->h[4] = 0;
  576. ctx->r[0] = 0;
  577. ctx->r[1] = 0;
  578. ctx->r[2] = 0;
  579. ctx->r[3] = 0;
  580. ctx->r[4] = 0;
  581. ctx->pad[0] = 0;
  582. ctx->pad[1] = 0;
  583. ctx->pad[2] = 0;
  584. ctx->pad[3] = 0;
  585. #endif
  586. return 0;
  587. }
  588. #endif /* !defined(WOLFSSL_ARMASM) || !defined(__aarch64__) */
  589. int wc_Poly1305Update(Poly1305* ctx, const byte* m, word32 bytes)
  590. {
  591. size_t i;
  592. if (ctx == NULL || (m == NULL && bytes > 0))
  593. return BAD_FUNC_ARG;
  594. if (bytes == 0) {
  595. /* valid, but do nothing */
  596. return 0;
  597. }
  598. #ifdef CHACHA_AEAD_TEST
  599. word32 k;
  600. printf("Raw input to poly:\n");
  601. for (k = 0; k < bytes; k++) {
  602. printf("%02x", m[k]);
  603. if ((k+1) % 16 == 0)
  604. printf("\n");
  605. }
  606. printf("\n");
  607. #endif
  608. #if defined(WOLFSSL_X86_64_BUILD) && defined(USE_INTEL_SPEEDUP)
  609. #ifdef HAVE_INTEL_AVX2
  610. if (IS_INTEL_AVX2(intel_flags)) {
  611. SAVE_VECTOR_REGISTERS(return _svr_ret;);
  612. /* handle leftover */
  613. if (ctx->leftover) {
  614. size_t want = sizeof(ctx->buffer) - ctx->leftover;
  615. if (want > bytes)
  616. want = bytes;
  617. for (i = 0; i < want; i++)
  618. ctx->buffer[ctx->leftover + i] = m[i];
  619. bytes -= (word32)want;
  620. m += want;
  621. ctx->leftover += want;
  622. if (ctx->leftover < sizeof(ctx->buffer)) {
  623. RESTORE_VECTOR_REGISTERS();
  624. return 0;
  625. }
  626. if (!ctx->started)
  627. poly1305_calc_powers_avx2(ctx);
  628. poly1305_blocks_avx2(ctx, ctx->buffer, sizeof(ctx->buffer));
  629. ctx->leftover = 0;
  630. }
  631. /* process full blocks */
  632. if (bytes >= sizeof(ctx->buffer)) {
  633. size_t want = bytes & ~(sizeof(ctx->buffer) - 1);
  634. if (!ctx->started)
  635. poly1305_calc_powers_avx2(ctx);
  636. poly1305_blocks_avx2(ctx, m, want);
  637. m += want;
  638. bytes -= (word32)want;
  639. }
  640. /* store leftover */
  641. if (bytes) {
  642. for (i = 0; i < bytes; i++)
  643. ctx->buffer[ctx->leftover + i] = m[i];
  644. ctx->leftover += bytes;
  645. }
  646. RESTORE_VECTOR_REGISTERS();
  647. }
  648. else
  649. #endif
  650. #endif
  651. {
  652. /* handle leftover */
  653. if (ctx->leftover) {
  654. size_t want = (POLY1305_BLOCK_SIZE - ctx->leftover);
  655. if (want > bytes)
  656. want = bytes;
  657. for (i = 0; i < want; i++)
  658. ctx->buffer[ctx->leftover + i] = m[i];
  659. bytes -= (word32)want;
  660. m += want;
  661. ctx->leftover += want;
  662. if (ctx->leftover < POLY1305_BLOCK_SIZE)
  663. return 0;
  664. poly1305_block(ctx, ctx->buffer);
  665. ctx->leftover = 0;
  666. }
  667. /* process full blocks */
  668. if (bytes >= POLY1305_BLOCK_SIZE) {
  669. size_t want = ((size_t)bytes & ~((size_t)POLY1305_BLOCK_SIZE - 1));
  670. #if !defined(WOLFSSL_ARMASM) || !defined(__aarch64__)
  671. int ret;
  672. ret = poly1305_blocks(ctx, m, want);
  673. if (ret != 0)
  674. return ret;
  675. #else
  676. poly1305_blocks(ctx, m, want);
  677. #endif
  678. m += want;
  679. bytes -= (word32)want;
  680. }
  681. /* store leftover */
  682. if (bytes) {
  683. for (i = 0; i < bytes; i++)
  684. ctx->buffer[ctx->leftover + i] = m[i];
  685. ctx->leftover += bytes;
  686. }
  687. }
  688. return 0;
  689. }
  690. /* Takes a Poly1305 struct that has a key loaded and pads the provided length
  691. ctx : Initialized Poly1305 struct to use
  692. lenToPad : Current number of bytes updated that needs padding to 16
  693. */
  694. int wc_Poly1305_Pad(Poly1305* ctx, word32 lenToPad)
  695. {
  696. int ret = 0;
  697. word32 paddingLen;
  698. byte padding[WC_POLY1305_PAD_SZ - 1];
  699. if (ctx == NULL) {
  700. return BAD_FUNC_ARG;
  701. }
  702. if (lenToPad == 0) {
  703. return 0; /* nothing needs to be done */
  704. }
  705. XMEMSET(padding, 0, sizeof(padding));
  706. /* Pad length to 16 bytes */
  707. paddingLen = (-(int)lenToPad) & (WC_POLY1305_PAD_SZ - 1);
  708. if ((paddingLen > 0) && (paddingLen < WC_POLY1305_PAD_SZ)) {
  709. ret = wc_Poly1305Update(ctx, padding, paddingLen);
  710. }
  711. return ret;
  712. }
  713. /* Takes a Poly1305 struct that has a key loaded and adds the AEAD length
  714. encoding in 64-bit little endian
  715. aadSz : Size of the additional authentication data
  716. dataSz : Size of the plaintext or ciphertext
  717. */
  718. int wc_Poly1305_EncodeSizes(Poly1305* ctx, word32 aadSz, word32 dataSz)
  719. {
  720. int ret;
  721. byte little64[16]; /* sizeof(word64) * 2 */
  722. if (ctx == NULL) {
  723. return BAD_FUNC_ARG;
  724. }
  725. XMEMSET(little64, 0, sizeof(little64));
  726. /* size of additional data and input data as little endian 64 bit types */
  727. u32tole64(aadSz, little64);
  728. u32tole64(dataSz, little64 + 8);
  729. ret = wc_Poly1305Update(ctx, little64, sizeof(little64));
  730. return ret;
  731. }
  732. #ifdef WORD64_AVAILABLE
  733. int wc_Poly1305_EncodeSizes64(Poly1305* ctx, word64 aadSz, word64 dataSz)
  734. {
  735. int ret;
  736. word64 little64[2];
  737. if (ctx == NULL) {
  738. return BAD_FUNC_ARG;
  739. }
  740. #ifdef BIG_ENDIAN_ORDER
  741. little64[0] = ByteReverseWord64(aadSz);
  742. little64[1] = ByteReverseWord64(dataSz);
  743. #else
  744. little64[0] = aadSz;
  745. little64[1] = dataSz;
  746. #endif
  747. ret = wc_Poly1305Update(ctx, (byte *)little64, sizeof(little64));
  748. return ret;
  749. }
  750. #endif
  751. /* Takes in an initialized Poly1305 struct that has a key loaded and creates
  752. a MAC (tag) using recent TLS AEAD padding scheme.
  753. ctx : Initialized Poly1305 struct to use
  754. additional : Additional data to use
  755. addSz : Size of additional buffer
  756. input : Input buffer to create tag from
  757. sz : Size of input buffer
  758. tag : Buffer to hold created tag
  759. tagSz : Size of input tag buffer (must be at least
  760. WC_POLY1305_MAC_SZ(16))
  761. */
  762. int wc_Poly1305_MAC(Poly1305* ctx, const byte* additional, word32 addSz,
  763. const byte* input, word32 sz, byte* tag, word32 tagSz)
  764. {
  765. int ret;
  766. /* sanity check on arguments */
  767. if (ctx == NULL || input == NULL || tag == NULL ||
  768. tagSz < WC_POLY1305_MAC_SZ) {
  769. return BAD_FUNC_ARG;
  770. }
  771. /* additional allowed to be 0 */
  772. if (addSz > 0) {
  773. if (additional == NULL)
  774. return BAD_FUNC_ARG;
  775. /* additional data plus padding */
  776. if ((ret = wc_Poly1305Update(ctx, additional, addSz)) != 0) {
  777. return ret;
  778. }
  779. /* pad additional data */
  780. if ((ret = wc_Poly1305_Pad(ctx, addSz)) != 0) {
  781. return ret;
  782. }
  783. }
  784. /* input plus padding */
  785. if ((ret = wc_Poly1305Update(ctx, input, sz)) != 0) {
  786. return ret;
  787. }
  788. /* pad input data */
  789. if ((ret = wc_Poly1305_Pad(ctx, sz)) != 0) {
  790. return ret;
  791. }
  792. /* encode size of AAD and input data as little endian 64 bit types */
  793. if ((ret = wc_Poly1305_EncodeSizes(ctx, addSz, sz)) != 0) {
  794. return ret;
  795. }
  796. /* Finalize the auth tag */
  797. ret = wc_Poly1305Final(ctx, tag);
  798. return ret;
  799. }
  800. #endif /* HAVE_POLY1305 */