poly1305_glue.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Poly1305 authenticator algorithm, RFC7539, SIMD glue code
  3. *
  4. * Copyright (C) 2015 Martin Willi
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <crypto/algapi.h>
  12. #include <crypto/internal/hash.h>
  13. #include <crypto/poly1305.h>
  14. #include <linux/crypto.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <asm/fpu/api.h>
  18. #include <asm/simd.h>
  19. struct poly1305_simd_desc_ctx {
  20. struct poly1305_desc_ctx base;
  21. /* derived key u set? */
  22. bool uset;
  23. #ifdef CONFIG_AS_AVX2
  24. /* derived keys r^3, r^4 set? */
  25. bool wset;
  26. #endif
  27. /* derived Poly1305 key r^2 */
  28. u32 u[5];
  29. /* ... silently appended r^3 and r^4 when using AVX2 */
  30. };
  31. asmlinkage void poly1305_block_sse2(u32 *h, const u8 *src,
  32. const u32 *r, unsigned int blocks);
  33. asmlinkage void poly1305_2block_sse2(u32 *h, const u8 *src, const u32 *r,
  34. unsigned int blocks, const u32 *u);
  35. #ifdef CONFIG_AS_AVX2
  36. asmlinkage void poly1305_4block_avx2(u32 *h, const u8 *src, const u32 *r,
  37. unsigned int blocks, const u32 *u);
  38. static bool poly1305_use_avx2;
  39. #endif
  40. static int poly1305_simd_init(struct shash_desc *desc)
  41. {
  42. struct poly1305_simd_desc_ctx *sctx = shash_desc_ctx(desc);
  43. sctx->uset = false;
  44. #ifdef CONFIG_AS_AVX2
  45. sctx->wset = false;
  46. #endif
  47. return crypto_poly1305_init(desc);
  48. }
  49. static void poly1305_simd_mult(u32 *a, const u32 *b)
  50. {
  51. u8 m[POLY1305_BLOCK_SIZE];
  52. memset(m, 0, sizeof(m));
  53. /* The poly1305 block function adds a hi-bit to the accumulator which
  54. * we don't need for key multiplication; compensate for it. */
  55. a[4] -= 1 << 24;
  56. poly1305_block_sse2(a, m, b, 1);
  57. }
  58. static unsigned int poly1305_simd_blocks(struct poly1305_desc_ctx *dctx,
  59. const u8 *src, unsigned int srclen)
  60. {
  61. struct poly1305_simd_desc_ctx *sctx;
  62. unsigned int blocks, datalen;
  63. BUILD_BUG_ON(offsetof(struct poly1305_simd_desc_ctx, base));
  64. sctx = container_of(dctx, struct poly1305_simd_desc_ctx, base);
  65. if (unlikely(!dctx->sset)) {
  66. datalen = crypto_poly1305_setdesckey(dctx, src, srclen);
  67. src += srclen - datalen;
  68. srclen = datalen;
  69. }
  70. #ifdef CONFIG_AS_AVX2
  71. if (poly1305_use_avx2 && srclen >= POLY1305_BLOCK_SIZE * 4) {
  72. if (unlikely(!sctx->wset)) {
  73. if (!sctx->uset) {
  74. memcpy(sctx->u, dctx->r, sizeof(sctx->u));
  75. poly1305_simd_mult(sctx->u, dctx->r);
  76. sctx->uset = true;
  77. }
  78. memcpy(sctx->u + 5, sctx->u, sizeof(sctx->u));
  79. poly1305_simd_mult(sctx->u + 5, dctx->r);
  80. memcpy(sctx->u + 10, sctx->u + 5, sizeof(sctx->u));
  81. poly1305_simd_mult(sctx->u + 10, dctx->r);
  82. sctx->wset = true;
  83. }
  84. blocks = srclen / (POLY1305_BLOCK_SIZE * 4);
  85. poly1305_4block_avx2(dctx->h, src, dctx->r, blocks, sctx->u);
  86. src += POLY1305_BLOCK_SIZE * 4 * blocks;
  87. srclen -= POLY1305_BLOCK_SIZE * 4 * blocks;
  88. }
  89. #endif
  90. if (likely(srclen >= POLY1305_BLOCK_SIZE * 2)) {
  91. if (unlikely(!sctx->uset)) {
  92. memcpy(sctx->u, dctx->r, sizeof(sctx->u));
  93. poly1305_simd_mult(sctx->u, dctx->r);
  94. sctx->uset = true;
  95. }
  96. blocks = srclen / (POLY1305_BLOCK_SIZE * 2);
  97. poly1305_2block_sse2(dctx->h, src, dctx->r, blocks, sctx->u);
  98. src += POLY1305_BLOCK_SIZE * 2 * blocks;
  99. srclen -= POLY1305_BLOCK_SIZE * 2 * blocks;
  100. }
  101. if (srclen >= POLY1305_BLOCK_SIZE) {
  102. poly1305_block_sse2(dctx->h, src, dctx->r, 1);
  103. srclen -= POLY1305_BLOCK_SIZE;
  104. }
  105. return srclen;
  106. }
  107. static int poly1305_simd_update(struct shash_desc *desc,
  108. const u8 *src, unsigned int srclen)
  109. {
  110. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  111. unsigned int bytes;
  112. /* kernel_fpu_begin/end is costly, use fallback for small updates */
  113. if (srclen <= 288 || !may_use_simd())
  114. return crypto_poly1305_update(desc, src, srclen);
  115. kernel_fpu_begin();
  116. if (unlikely(dctx->buflen)) {
  117. bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
  118. memcpy(dctx->buf + dctx->buflen, src, bytes);
  119. src += bytes;
  120. srclen -= bytes;
  121. dctx->buflen += bytes;
  122. if (dctx->buflen == POLY1305_BLOCK_SIZE) {
  123. poly1305_simd_blocks(dctx, dctx->buf,
  124. POLY1305_BLOCK_SIZE);
  125. dctx->buflen = 0;
  126. }
  127. }
  128. if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
  129. bytes = poly1305_simd_blocks(dctx, src, srclen);
  130. src += srclen - bytes;
  131. srclen = bytes;
  132. }
  133. kernel_fpu_end();
  134. if (unlikely(srclen)) {
  135. dctx->buflen = srclen;
  136. memcpy(dctx->buf, src, srclen);
  137. }
  138. return 0;
  139. }
  140. static struct shash_alg alg = {
  141. .digestsize = POLY1305_DIGEST_SIZE,
  142. .init = poly1305_simd_init,
  143. .update = poly1305_simd_update,
  144. .final = crypto_poly1305_final,
  145. .descsize = sizeof(struct poly1305_simd_desc_ctx),
  146. .base = {
  147. .cra_name = "poly1305",
  148. .cra_driver_name = "poly1305-simd",
  149. .cra_priority = 300,
  150. .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  151. .cra_alignmask = sizeof(u32) - 1,
  152. .cra_blocksize = POLY1305_BLOCK_SIZE,
  153. .cra_module = THIS_MODULE,
  154. },
  155. };
  156. static int __init poly1305_simd_mod_init(void)
  157. {
  158. if (!cpu_has_xmm2)
  159. return -ENODEV;
  160. #ifdef CONFIG_AS_AVX2
  161. poly1305_use_avx2 = cpu_has_avx && cpu_has_avx2 &&
  162. cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL);
  163. alg.descsize = sizeof(struct poly1305_simd_desc_ctx);
  164. if (poly1305_use_avx2)
  165. alg.descsize += 10 * sizeof(u32);
  166. #endif
  167. return crypto_register_shash(&alg);
  168. }
  169. static void __exit poly1305_simd_mod_exit(void)
  170. {
  171. crypto_unregister_shash(&alg);
  172. }
  173. module_init(poly1305_simd_mod_init);
  174. module_exit(poly1305_simd_mod_exit);
  175. MODULE_LICENSE("GPL");
  176. MODULE_AUTHOR("Martin Willi <martin@strongswan.org>");
  177. MODULE_DESCRIPTION("Poly1305 authenticator");
  178. MODULE_ALIAS_CRYPTO("poly1305");
  179. MODULE_ALIAS_CRYPTO("poly1305-simd");