poly1305_glue.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // SPDX-License-Identifier: GPL-2.0 OR MIT
  2. /*
  3. * Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
  4. */
  5. #include <crypto/algapi.h>
  6. #include <crypto/internal/hash.h>
  7. #include <crypto/internal/poly1305.h>
  8. #include <crypto/internal/simd.h>
  9. #include <linux/crypto.h>
  10. #include <linux/jump_label.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/sizes.h>
  14. #include <asm/intel-family.h>
  15. #include <asm/simd.h>
  16. asmlinkage void poly1305_init_x86_64(void *ctx,
  17. const u8 key[POLY1305_BLOCK_SIZE]);
  18. asmlinkage void poly1305_blocks_x86_64(void *ctx, const u8 *inp,
  19. const size_t len, const u32 padbit);
  20. asmlinkage void poly1305_emit_x86_64(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],
  21. const u32 nonce[4]);
  22. asmlinkage void poly1305_emit_avx(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],
  23. const u32 nonce[4]);
  24. asmlinkage void poly1305_blocks_avx(void *ctx, const u8 *inp, const size_t len,
  25. const u32 padbit);
  26. asmlinkage void poly1305_blocks_avx2(void *ctx, const u8 *inp, const size_t len,
  27. const u32 padbit);
  28. asmlinkage void poly1305_blocks_avx512(void *ctx, const u8 *inp,
  29. const size_t len, const u32 padbit);
  30. static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx);
  31. static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx2);
  32. static __ro_after_init DEFINE_STATIC_KEY_FALSE(poly1305_use_avx512);
  33. struct poly1305_arch_internal {
  34. union {
  35. struct {
  36. u32 h[5];
  37. u32 is_base2_26;
  38. };
  39. u64 hs[3];
  40. };
  41. u64 r[2];
  42. u64 pad;
  43. struct { u32 r2, r1, r4, r3; } rn[9];
  44. };
  45. /* The AVX code uses base 2^26, while the scalar code uses base 2^64. If we hit
  46. * the unfortunate situation of using AVX and then having to go back to scalar
  47. * -- because the user is silly and has called the update function from two
  48. * separate contexts -- then we need to convert back to the original base before
  49. * proceeding. It is possible to reason that the initial reduction below is
  50. * sufficient given the implementation invariants. However, for an avoidance of
  51. * doubt and because this is not performance critical, we do the full reduction
  52. * anyway. Z3 proof of below function: https://xn--4db.cc/ltPtHCKN/py
  53. */
  54. static void convert_to_base2_64(void *ctx)
  55. {
  56. struct poly1305_arch_internal *state = ctx;
  57. u32 cy;
  58. if (!state->is_base2_26)
  59. return;
  60. cy = state->h[0] >> 26; state->h[0] &= 0x3ffffff; state->h[1] += cy;
  61. cy = state->h[1] >> 26; state->h[1] &= 0x3ffffff; state->h[2] += cy;
  62. cy = state->h[2] >> 26; state->h[2] &= 0x3ffffff; state->h[3] += cy;
  63. cy = state->h[3] >> 26; state->h[3] &= 0x3ffffff; state->h[4] += cy;
  64. state->hs[0] = ((u64)state->h[2] << 52) | ((u64)state->h[1] << 26) | state->h[0];
  65. state->hs[1] = ((u64)state->h[4] << 40) | ((u64)state->h[3] << 14) | (state->h[2] >> 12);
  66. state->hs[2] = state->h[4] >> 24;
  67. #define ULT(a, b) ((a ^ ((a ^ b) | ((a - b) ^ b))) >> (sizeof(a) * 8 - 1))
  68. cy = (state->hs[2] >> 2) + (state->hs[2] & ~3ULL);
  69. state->hs[2] &= 3;
  70. state->hs[0] += cy;
  71. state->hs[1] += (cy = ULT(state->hs[0], cy));
  72. state->hs[2] += ULT(state->hs[1], cy);
  73. #undef ULT
  74. state->is_base2_26 = 0;
  75. }
  76. static void poly1305_simd_init(void *ctx, const u8 key[POLY1305_BLOCK_SIZE])
  77. {
  78. poly1305_init_x86_64(ctx, key);
  79. }
  80. static void poly1305_simd_blocks(void *ctx, const u8 *inp, size_t len,
  81. const u32 padbit)
  82. {
  83. struct poly1305_arch_internal *state = ctx;
  84. /* SIMD disables preemption, so relax after processing each page. */
  85. BUILD_BUG_ON(SZ_4K < POLY1305_BLOCK_SIZE ||
  86. SZ_4K % POLY1305_BLOCK_SIZE);
  87. if (!static_branch_likely(&poly1305_use_avx) ||
  88. (len < (POLY1305_BLOCK_SIZE * 18) && !state->is_base2_26) ||
  89. !crypto_simd_usable()) {
  90. convert_to_base2_64(ctx);
  91. poly1305_blocks_x86_64(ctx, inp, len, padbit);
  92. return;
  93. }
  94. do {
  95. const size_t bytes = min_t(size_t, len, SZ_4K);
  96. kernel_fpu_begin();
  97. if (IS_ENABLED(CONFIG_AS_AVX512) && static_branch_likely(&poly1305_use_avx512))
  98. poly1305_blocks_avx512(ctx, inp, bytes, padbit);
  99. else if (static_branch_likely(&poly1305_use_avx2))
  100. poly1305_blocks_avx2(ctx, inp, bytes, padbit);
  101. else
  102. poly1305_blocks_avx(ctx, inp, bytes, padbit);
  103. kernel_fpu_end();
  104. len -= bytes;
  105. inp += bytes;
  106. } while (len);
  107. }
  108. static void poly1305_simd_emit(void *ctx, u8 mac[POLY1305_DIGEST_SIZE],
  109. const u32 nonce[4])
  110. {
  111. if (!static_branch_likely(&poly1305_use_avx))
  112. poly1305_emit_x86_64(ctx, mac, nonce);
  113. else
  114. poly1305_emit_avx(ctx, mac, nonce);
  115. }
  116. void poly1305_init_arch(struct poly1305_desc_ctx *dctx, const u8 key[POLY1305_KEY_SIZE])
  117. {
  118. poly1305_simd_init(&dctx->h, key);
  119. dctx->s[0] = get_unaligned_le32(&key[16]);
  120. dctx->s[1] = get_unaligned_le32(&key[20]);
  121. dctx->s[2] = get_unaligned_le32(&key[24]);
  122. dctx->s[3] = get_unaligned_le32(&key[28]);
  123. dctx->buflen = 0;
  124. dctx->sset = true;
  125. }
  126. EXPORT_SYMBOL(poly1305_init_arch);
  127. static unsigned int crypto_poly1305_setdctxkey(struct poly1305_desc_ctx *dctx,
  128. const u8 *inp, unsigned int len)
  129. {
  130. unsigned int acc = 0;
  131. if (unlikely(!dctx->sset)) {
  132. if (!dctx->rset && len >= POLY1305_BLOCK_SIZE) {
  133. poly1305_simd_init(&dctx->h, inp);
  134. inp += POLY1305_BLOCK_SIZE;
  135. len -= POLY1305_BLOCK_SIZE;
  136. acc += POLY1305_BLOCK_SIZE;
  137. dctx->rset = 1;
  138. }
  139. if (len >= POLY1305_BLOCK_SIZE) {
  140. dctx->s[0] = get_unaligned_le32(&inp[0]);
  141. dctx->s[1] = get_unaligned_le32(&inp[4]);
  142. dctx->s[2] = get_unaligned_le32(&inp[8]);
  143. dctx->s[3] = get_unaligned_le32(&inp[12]);
  144. acc += POLY1305_BLOCK_SIZE;
  145. dctx->sset = true;
  146. }
  147. }
  148. return acc;
  149. }
  150. void poly1305_update_arch(struct poly1305_desc_ctx *dctx, const u8 *src,
  151. unsigned int srclen)
  152. {
  153. unsigned int bytes, used;
  154. if (unlikely(dctx->buflen)) {
  155. bytes = min(srclen, POLY1305_BLOCK_SIZE - dctx->buflen);
  156. memcpy(dctx->buf + dctx->buflen, src, bytes);
  157. src += bytes;
  158. srclen -= bytes;
  159. dctx->buflen += bytes;
  160. if (dctx->buflen == POLY1305_BLOCK_SIZE) {
  161. if (likely(!crypto_poly1305_setdctxkey(dctx, dctx->buf, POLY1305_BLOCK_SIZE)))
  162. poly1305_simd_blocks(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 1);
  163. dctx->buflen = 0;
  164. }
  165. }
  166. if (likely(srclen >= POLY1305_BLOCK_SIZE)) {
  167. bytes = round_down(srclen, POLY1305_BLOCK_SIZE);
  168. srclen -= bytes;
  169. used = crypto_poly1305_setdctxkey(dctx, src, bytes);
  170. if (likely(bytes - used))
  171. poly1305_simd_blocks(&dctx->h, src + used, bytes - used, 1);
  172. src += bytes;
  173. }
  174. if (unlikely(srclen)) {
  175. dctx->buflen = srclen;
  176. memcpy(dctx->buf, src, srclen);
  177. }
  178. }
  179. EXPORT_SYMBOL(poly1305_update_arch);
  180. void poly1305_final_arch(struct poly1305_desc_ctx *dctx, u8 *dst)
  181. {
  182. if (unlikely(dctx->buflen)) {
  183. dctx->buf[dctx->buflen++] = 1;
  184. memset(dctx->buf + dctx->buflen, 0,
  185. POLY1305_BLOCK_SIZE - dctx->buflen);
  186. poly1305_simd_blocks(&dctx->h, dctx->buf, POLY1305_BLOCK_SIZE, 0);
  187. }
  188. poly1305_simd_emit(&dctx->h, dst, dctx->s);
  189. memzero_explicit(dctx, sizeof(*dctx));
  190. }
  191. EXPORT_SYMBOL(poly1305_final_arch);
  192. static int crypto_poly1305_init(struct shash_desc *desc)
  193. {
  194. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  195. *dctx = (struct poly1305_desc_ctx){};
  196. return 0;
  197. }
  198. static int crypto_poly1305_update(struct shash_desc *desc,
  199. const u8 *src, unsigned int srclen)
  200. {
  201. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  202. poly1305_update_arch(dctx, src, srclen);
  203. return 0;
  204. }
  205. static int crypto_poly1305_final(struct shash_desc *desc, u8 *dst)
  206. {
  207. struct poly1305_desc_ctx *dctx = shash_desc_ctx(desc);
  208. if (unlikely(!dctx->sset))
  209. return -ENOKEY;
  210. poly1305_final_arch(dctx, dst);
  211. return 0;
  212. }
  213. static struct shash_alg alg = {
  214. .digestsize = POLY1305_DIGEST_SIZE,
  215. .init = crypto_poly1305_init,
  216. .update = crypto_poly1305_update,
  217. .final = crypto_poly1305_final,
  218. .descsize = sizeof(struct poly1305_desc_ctx),
  219. .base = {
  220. .cra_name = "poly1305",
  221. .cra_driver_name = "poly1305-simd",
  222. .cra_priority = 300,
  223. .cra_blocksize = POLY1305_BLOCK_SIZE,
  224. .cra_module = THIS_MODULE,
  225. },
  226. };
  227. static int __init poly1305_simd_mod_init(void)
  228. {
  229. if (boot_cpu_has(X86_FEATURE_AVX) &&
  230. cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
  231. static_branch_enable(&poly1305_use_avx);
  232. if (boot_cpu_has(X86_FEATURE_AVX) && boot_cpu_has(X86_FEATURE_AVX2) &&
  233. cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, NULL))
  234. static_branch_enable(&poly1305_use_avx2);
  235. if (IS_ENABLED(CONFIG_AS_AVX512) && boot_cpu_has(X86_FEATURE_AVX) &&
  236. boot_cpu_has(X86_FEATURE_AVX2) && boot_cpu_has(X86_FEATURE_AVX512F) &&
  237. cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM | XFEATURE_MASK_AVX512, NULL) &&
  238. /* Skylake downclocks unacceptably much when using zmm, but later generations are fast. */
  239. boot_cpu_data.x86_model != INTEL_FAM6_SKYLAKE_X)
  240. static_branch_enable(&poly1305_use_avx512);
  241. return IS_REACHABLE(CONFIG_CRYPTO_HASH) ? crypto_register_shash(&alg) : 0;
  242. }
  243. static void __exit poly1305_simd_mod_exit(void)
  244. {
  245. if (IS_REACHABLE(CONFIG_CRYPTO_HASH))
  246. crypto_unregister_shash(&alg);
  247. }
  248. module_init(poly1305_simd_mod_init);
  249. module_exit(poly1305_simd_mod_exit);
  250. MODULE_LICENSE("GPL");
  251. MODULE_AUTHOR("Jason A. Donenfeld <[email protected]>");
  252. MODULE_DESCRIPTION("Poly1305 authenticator");
  253. MODULE_ALIAS_CRYPTO("poly1305");
  254. MODULE_ALIAS_CRYPTO("poly1305-simd");