2019-05-29 21:18:13 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-02-06 23:58:31 +07:00
|
|
|
/**
|
|
|
|
* AES CTR routines supporting VMX instructions on the Power 8
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 International Business Machines Inc.
|
|
|
|
*
|
|
|
|
* Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
|
|
|
|
*/
|
|
|
|
|
2019-04-13 12:33:12 +07:00
|
|
|
#include <asm/simd.h>
|
2015-02-06 23:58:31 +07:00
|
|
|
#include <asm/switch_to.h>
|
|
|
|
#include <crypto/aes.h>
|
2019-04-13 12:33:12 +07:00
|
|
|
#include <crypto/internal/simd.h>
|
2019-05-20 23:44:48 +07:00
|
|
|
#include <crypto/internal/skcipher.h>
|
2017-10-17 05:54:19 +07:00
|
|
|
|
2015-02-06 23:58:31 +07:00
|
|
|
#include "aesp8-ppc.h"
|
|
|
|
|
|
|
|
struct p8_aes_ctr_ctx {
|
2019-05-20 23:44:48 +07:00
|
|
|
struct crypto_skcipher *fallback;
|
2015-06-15 15:55:46 +07:00
|
|
|
struct aes_key enc_key;
|
2015-02-06 23:58:31 +07:00
|
|
|
};
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
static int p8_aes_ctr_init(struct crypto_skcipher *tfm)
|
2015-02-06 23:58:31 +07:00
|
|
|
{
|
2019-05-20 23:44:48 +07:00
|
|
|
struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct crypto_skcipher *fallback;
|
2015-06-15 15:55:46 +07:00
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
fallback = crypto_alloc_skcipher("ctr(aes)", 0,
|
|
|
|
CRYPTO_ALG_NEED_FALLBACK |
|
|
|
|
CRYPTO_ALG_ASYNC);
|
2015-06-15 15:55:46 +07:00
|
|
|
if (IS_ERR(fallback)) {
|
2019-05-20 23:44:48 +07:00
|
|
|
pr_err("Failed to allocate ctr(aes) fallback: %ld\n",
|
|
|
|
PTR_ERR(fallback));
|
2015-06-15 15:55:46 +07:00
|
|
|
return PTR_ERR(fallback);
|
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
|
|
|
|
crypto_skcipher_reqsize(fallback));
|
2015-06-15 15:55:46 +07:00
|
|
|
ctx->fallback = fallback;
|
|
|
|
return 0;
|
2015-02-06 23:58:31 +07:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
static void p8_aes_ctr_exit(struct crypto_skcipher *tfm)
|
2015-02-06 23:58:31 +07:00
|
|
|
{
|
2019-05-20 23:44:48 +07:00
|
|
|
struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2015-02-06 23:58:31 +07:00
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
crypto_free_skcipher(ctx->fallback);
|
2015-02-06 23:58:31 +07:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
static int p8_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key,
|
2015-06-15 15:55:46 +07:00
|
|
|
unsigned int keylen)
|
2015-02-06 23:58:31 +07:00
|
|
|
{
|
2019-05-20 23:44:48 +07:00
|
|
|
struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
|
2015-06-15 15:55:46 +07:00
|
|
|
int ret;
|
2015-02-06 23:58:31 +07:00
|
|
|
|
2017-01-20 15:35:33 +07:00
|
|
|
preempt_disable();
|
2015-06-15 15:55:46 +07:00
|
|
|
pagefault_disable();
|
2015-07-13 23:51:39 +07:00
|
|
|
enable_kernel_vsx();
|
2015-06-15 15:55:46 +07:00
|
|
|
ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
|
2015-10-29 07:44:05 +07:00
|
|
|
disable_kernel_vsx();
|
2015-06-15 15:55:46 +07:00
|
|
|
pagefault_enable();
|
2017-01-20 15:35:33 +07:00
|
|
|
preempt_enable();
|
2015-02-06 23:58:31 +07:00
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
ret |= crypto_skcipher_setkey(ctx->fallback, key, keylen);
|
2019-04-10 13:46:35 +07:00
|
|
|
|
|
|
|
return ret ? -EINVAL : 0;
|
2015-02-06 23:58:31 +07:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
static void p8_aes_ctr_final(const struct p8_aes_ctr_ctx *ctx,
|
|
|
|
struct skcipher_walk *walk)
|
2015-02-06 23:58:31 +07:00
|
|
|
{
|
2015-06-15 15:55:46 +07:00
|
|
|
u8 *ctrblk = walk->iv;
|
|
|
|
u8 keystream[AES_BLOCK_SIZE];
|
|
|
|
u8 *src = walk->src.virt.addr;
|
|
|
|
u8 *dst = walk->dst.virt.addr;
|
|
|
|
unsigned int nbytes = walk->nbytes;
|
|
|
|
|
2017-01-20 15:35:33 +07:00
|
|
|
preempt_disable();
|
2015-06-15 15:55:46 +07:00
|
|
|
pagefault_disable();
|
2015-07-13 23:51:39 +07:00
|
|
|
enable_kernel_vsx();
|
2015-06-15 15:55:46 +07:00
|
|
|
aes_p8_encrypt(ctrblk, keystream, &ctx->enc_key);
|
2015-10-29 07:44:05 +07:00
|
|
|
disable_kernel_vsx();
|
2015-06-15 15:55:46 +07:00
|
|
|
pagefault_enable();
|
2017-01-20 15:35:33 +07:00
|
|
|
preempt_enable();
|
2015-06-15 15:55:46 +07:00
|
|
|
|
crypto: algapi - make crypto_xor() take separate dst and src arguments
There are quite a number of occurrences in the kernel of the pattern
if (dst != src)
memcpy(dst, src, walk.total % AES_BLOCK_SIZE);
crypto_xor(dst, final, walk.total % AES_BLOCK_SIZE);
or
crypto_xor(keystream, src, nbytes);
memcpy(dst, keystream, nbytes);
where crypto_xor() is preceded or followed by a memcpy() invocation
that is only there because crypto_xor() uses its output parameter as
one of the inputs. To avoid having to add new instances of this pattern
in the arm64 code, which will be refactored to implement non-SIMD
fallbacks, add an alternative implementation called crypto_xor_cpy(),
taking separate input and output arguments. This removes the need for
the separate memcpy().
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2017-07-24 17:28:04 +07:00
|
|
|
crypto_xor_cpy(dst, keystream, src, nbytes);
|
2015-06-15 15:55:46 +07:00
|
|
|
crypto_inc(ctrblk, AES_BLOCK_SIZE);
|
2015-02-06 23:58:31 +07:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
static int p8_aes_ctr_crypt(struct skcipher_request *req)
|
2015-02-06 23:58:31 +07:00
|
|
|
{
|
2019-05-20 23:44:48 +07:00
|
|
|
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
|
|
|
|
const struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
|
|
|
|
struct skcipher_walk walk;
|
|
|
|
unsigned int nbytes;
|
2015-06-15 15:55:46 +07:00
|
|
|
int ret;
|
|
|
|
|
2019-04-13 12:33:12 +07:00
|
|
|
if (!crypto_simd_usable()) {
|
2019-05-20 23:44:48 +07:00
|
|
|
struct skcipher_request *subreq = skcipher_request_ctx(req);
|
|
|
|
|
|
|
|
*subreq = *req;
|
|
|
|
skcipher_request_set_tfm(subreq, ctx->fallback);
|
|
|
|
return crypto_skcipher_encrypt(subreq);
|
2015-06-15 15:55:46 +07:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
ret = skcipher_walk_virt(&walk, req, false);
|
|
|
|
while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
|
|
|
|
preempt_disable();
|
|
|
|
pagefault_disable();
|
|
|
|
enable_kernel_vsx();
|
|
|
|
aes_p8_ctr32_encrypt_blocks(walk.src.virt.addr,
|
|
|
|
walk.dst.virt.addr,
|
|
|
|
nbytes / AES_BLOCK_SIZE,
|
|
|
|
&ctx->enc_key, walk.iv);
|
|
|
|
disable_kernel_vsx();
|
|
|
|
pagefault_enable();
|
|
|
|
preempt_enable();
|
|
|
|
|
|
|
|
do {
|
|
|
|
crypto_inc(walk.iv, AES_BLOCK_SIZE);
|
|
|
|
} while ((nbytes -= AES_BLOCK_SIZE) >= AES_BLOCK_SIZE);
|
|
|
|
|
|
|
|
ret = skcipher_walk_done(&walk, nbytes);
|
|
|
|
}
|
|
|
|
if (nbytes) {
|
|
|
|
p8_aes_ctr_final(ctx, &walk);
|
|
|
|
ret = skcipher_walk_done(&walk, 0);
|
|
|
|
}
|
2015-06-15 15:55:46 +07:00
|
|
|
return ret;
|
2015-02-06 23:58:31 +07:00
|
|
|
}
|
|
|
|
|
2019-05-20 23:44:48 +07:00
|
|
|
struct skcipher_alg p8_aes_ctr_alg = {
|
|
|
|
.base.cra_name = "ctr(aes)",
|
|
|
|
.base.cra_driver_name = "p8_aes_ctr",
|
|
|
|
.base.cra_module = THIS_MODULE,
|
|
|
|
.base.cra_priority = 2000,
|
|
|
|
.base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
|
|
|
|
.base.cra_blocksize = 1,
|
|
|
|
.base.cra_ctxsize = sizeof(struct p8_aes_ctr_ctx),
|
|
|
|
.setkey = p8_aes_ctr_setkey,
|
|
|
|
.encrypt = p8_aes_ctr_crypt,
|
|
|
|
.decrypt = p8_aes_ctr_crypt,
|
|
|
|
.init = p8_aes_ctr_init,
|
|
|
|
.exit = p8_aes_ctr_exit,
|
|
|
|
.min_keysize = AES_MIN_KEY_SIZE,
|
|
|
|
.max_keysize = AES_MAX_KEY_SIZE,
|
|
|
|
.ivsize = AES_BLOCK_SIZE,
|
|
|
|
.chunksize = AES_BLOCK_SIZE,
|
2015-02-06 23:58:31 +07:00
|
|
|
};
|