In preparation of duplicating the sync ctr(aes) functionality to modules
under arch/arm, move the helper function from a inline .h file to the
AES library, which is already depended upon by the drivers that use this
fallback.

Signed-off-by: Ard Biesheuvel <ard.biesheu...@linaro.org>
---
 arch/arm64/crypto/aes-ctr-fallback.h | 53 --------------------
 arch/arm64/crypto/aes-glue.c         | 22 ++++++--
 arch/arm64/crypto/aes-neonbs-glue.c  | 21 ++++++--
 3 files changed, 33 insertions(+), 63 deletions(-)

diff --git a/arch/arm64/crypto/aes-ctr-fallback.h 
b/arch/arm64/crypto/aes-ctr-fallback.h
deleted file mode 100644
index c9285717b6b5..000000000000
--- a/arch/arm64/crypto/aes-ctr-fallback.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Fallback for sync aes(ctr) in contexts where kernel mode NEON
- * is not allowed
- *
- * Copyright (C) 2017 Linaro Ltd <ard.biesheu...@linaro.org>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- */
-
-#include <crypto/aes.h>
-#include <crypto/internal/skcipher.h>
-
-asmlinkage void __aes_arm64_encrypt(u32 *rk, u8 *out, const u8 *in, int 
rounds);
-
-static inline int aes_ctr_encrypt_fallback(struct crypto_aes_ctx *ctx,
-                                          struct skcipher_request *req)
-{
-       struct skcipher_walk walk;
-       u8 buf[AES_BLOCK_SIZE];
-       int err;
-
-       err = skcipher_walk_virt(&walk, req, true);
-
-       while (walk.nbytes > 0) {
-               u8 *dst = walk.dst.virt.addr;
-               u8 *src = walk.src.virt.addr;
-               int nbytes = walk.nbytes;
-               int tail = 0;
-
-               if (nbytes < walk.total) {
-                       nbytes = round_down(nbytes, AES_BLOCK_SIZE);
-                       tail = walk.nbytes % AES_BLOCK_SIZE;
-               }
-
-               do {
-                       int bsize = min(nbytes, AES_BLOCK_SIZE);
-
-                       __aes_arm64_encrypt(ctx->key_enc, buf, walk.iv,
-                                           6 + ctx->key_length / 4);
-                       crypto_xor_cpy(dst, src, buf, bsize);
-                       crypto_inc(walk.iv, AES_BLOCK_SIZE);
-
-                       dst += AES_BLOCK_SIZE;
-                       src += AES_BLOCK_SIZE;
-                       nbytes -= AES_BLOCK_SIZE;
-               } while (nbytes > 0);
-
-               err = skcipher_walk_done(&walk, tail);
-       }
-       return err;
-}
diff --git a/arch/arm64/crypto/aes-glue.c b/arch/arm64/crypto/aes-glue.c
index 3c80345d914f..6dc90557282d 100644
--- a/arch/arm64/crypto/aes-glue.c
+++ b/arch/arm64/crypto/aes-glue.c
@@ -12,6 +12,7 @@
 #include <asm/hwcap.h>
 #include <asm/simd.h>
 #include <crypto/aes.h>
+#include <crypto/ctr.h>
 #include <crypto/internal/hash.h>
 #include <crypto/internal/simd.h>
 #include <crypto/internal/skcipher.h>
@@ -21,7 +22,6 @@
 #include <crypto/xts.h>
 
 #include "aes-ce-setkey.h"
-#include "aes-ctr-fallback.h"
 
 #ifdef USE_V8_CRYPTO_EXTENSIONS
 #define MODE                   "ce"
@@ -404,13 +404,25 @@ static int ctr_encrypt(struct skcipher_request *req)
        return err;
 }
 
-static int ctr_encrypt_sync(struct skcipher_request *req)
+static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 
*dst)
 {
-       struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
-       struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
+       const struct crypto_aes_ctx *ctx = crypto_skcipher_ctx(tfm);
+       unsigned long flags;
+
+       /*
+        * Temporarily disable interrupts to avoid races where
+        * evicted when the CPU is interrupted to do something
+        * else.
+        */
+       local_irq_save(flags);
+       aes_encrypt(ctx, dst, src);
+       local_irq_restore(flags);
+}
 
+static int ctr_encrypt_sync(struct skcipher_request *req)
+{
        if (!crypto_simd_usable())
-               return aes_ctr_encrypt_fallback(ctx, req);
+               return crypto_ctr_encrypt_walk(req, ctr_encrypt_one);
 
        return ctr_encrypt(req);
 }
diff --git a/arch/arm64/crypto/aes-neonbs-glue.c 
b/arch/arm64/crypto/aes-neonbs-glue.c
index cb8d90f795a0..933ce70a2504 100644
--- a/arch/arm64/crypto/aes-neonbs-glue.c
+++ b/arch/arm64/crypto/aes-neonbs-glue.c
@@ -11,13 +11,12 @@
 #include <asm/neon.h>
 #include <asm/simd.h>
 #include <crypto/aes.h>
+#include <crypto/ctr.h>
 #include <crypto/internal/simd.h>
 #include <crypto/internal/skcipher.h>
 #include <crypto/xts.h>
 #include <linux/module.h>
 
-#include "aes-ctr-fallback.h"
-
 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheu...@linaro.org>");
 MODULE_LICENSE("GPL v2");
 
@@ -283,13 +282,25 @@ static int aesbs_xts_setkey(struct crypto_skcipher *tfm, 
const u8 *in_key,
        return aesbs_setkey(tfm, in_key, key_len);
 }
 
-static int ctr_encrypt_sync(struct skcipher_request *req)
+static void ctr_encrypt_one(struct crypto_skcipher *tfm, const u8 *src, u8 
*dst)
 {
-       struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
        struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
+       unsigned long flags;
+
+       /*
+        * Temporarily disable interrupts to avoid races where
+        * evicted when the CPU is interrupted to do something
+        * else.
+        */
+       local_irq_save(flags);
+       aes_encrypt(&ctx->fallback, dst, src);
+       local_irq_restore(flags);
+}
 
+static int ctr_encrypt_sync(struct skcipher_request *req)
+{
        if (!crypto_simd_usable())
-               return aes_ctr_encrypt_fallback(&ctx->fallback, req);
+               return crypto_ctr_encrypt_walk(req, ctr_encrypt_one);
 
        return ctr_encrypt(req);
 }
-- 
2.20.1

Reply via email to