Skip to content

Commit 50dd4b4

Browse files
ebiggersherbertx
authored andcommitted
crypto: x86/aes-xts - make the fast path 64-bit specific
Remove 32-bit support from the fast path in xts_crypt(). Then optimize it for 64-bit, and simplify the code, by switching to sg_virt() and removing the now-unnecessary checks for crossing a page boundary. The result is simpler code that is slightly smaller and faster in the case that actually matters (64-bit). Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 1d19058 commit 50dd4b4

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

arch/x86/crypto/aesni-intel_glue.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -581,35 +581,25 @@ xts_crypt(struct skcipher_request *req, xts_encrypt_iv_func encrypt_iv,
581581
{
582582
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
583583
const struct aesni_xts_ctx *ctx = aes_xts_ctx(tfm);
584-
const unsigned int cryptlen = req->cryptlen;
585-
struct scatterlist *src = req->src;
586-
struct scatterlist *dst = req->dst;
587584

588-
if (unlikely(cryptlen < AES_BLOCK_SIZE))
585+
if (unlikely(req->cryptlen < AES_BLOCK_SIZE))
589586
return -EINVAL;
590587

591588
kernel_fpu_begin();
592589
(*encrypt_iv)(&ctx->tweak_ctx, req->iv);
593590

594591
/*
595592
* In practice, virtually all XTS plaintexts and ciphertexts are either
596-
* 512 or 4096 bytes, aligned such that they don't span page boundaries.
597-
* To optimize the performance of these cases, and also any other case
598-
* where no page boundary is spanned, the below fast-path handles
599-
* single-page sources and destinations as efficiently as possible.
593+
* 512 or 4096 bytes and do not use multiple scatterlist elements. To
594+
* optimize the performance of these cases, the below fast-path handles
595+
* single-scatterlist-element messages as efficiently as possible. The
596+
* code is 64-bit specific, as it assumes no page mapping is needed.
600597
*/
601-
if (likely(src->length >= cryptlen && dst->length >= cryptlen &&
602-
src->offset + cryptlen <= PAGE_SIZE &&
603-
dst->offset + cryptlen <= PAGE_SIZE)) {
604-
struct page *src_page = sg_page(src);
605-
struct page *dst_page = sg_page(dst);
606-
void *src_virt = kmap_local_page(src_page) + src->offset;
607-
void *dst_virt = kmap_local_page(dst_page) + dst->offset;
608-
609-
(*crypt_func)(&ctx->crypt_ctx, src_virt, dst_virt, cryptlen,
610-
req->iv);
611-
kunmap_local(dst_virt);
612-
kunmap_local(src_virt);
598+
if (IS_ENABLED(CONFIG_X86_64) &&
599+
likely(req->src->length >= req->cryptlen &&
600+
req->dst->length >= req->cryptlen)) {
601+
(*crypt_func)(&ctx->crypt_ctx, sg_virt(req->src),
602+
sg_virt(req->dst), req->cryptlen, req->iv);
613603
kernel_fpu_end();
614604
return 0;
615605
}

0 commit comments

Comments
 (0)