Skip to content

Commit ec808bb

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: arm64/aes-bs - implement non-SIMD fallback for AES-CTR
Of the various chaining modes implemented by the bit sliced AES driver, only CTR is exposed as a synchronous cipher, and requires a fallback in order to remain usable once we update the kernel mode NEON handling logic to disallow nested use. So wire up the existing CTR fallback C code. Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 611d532 commit ec808bb

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

arch/arm64/crypto/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ config CRYPTO_AES_ARM64_BS
8989
depends on KERNEL_MODE_NEON
9090
select CRYPTO_BLKCIPHER
9191
select CRYPTO_AES_ARM64_NEON_BLK
92+
select CRYPTO_AES_ARM64
9293
select CRYPTO_SIMD
9394

9495
endif

arch/arm64/crypto/aes-neonbs-glue.c

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
/*
22
* Bit sliced AES using NEON instructions
33
*
4-
* Copyright (C) 2016 Linaro Ltd <ard.biesheuvel@linaro.org>
4+
* Copyright (C) 2016 - 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
55
*
66
* This program is free software; you can redistribute it and/or modify
77
* it under the terms of the GNU General Public License version 2 as
88
* published by the Free Software Foundation.
99
*/
1010

1111
#include <asm/neon.h>
12+
#include <asm/simd.h>
1213
#include <crypto/aes.h>
1314
#include <crypto/internal/simd.h>
1415
#include <crypto/internal/skcipher.h>
1516
#include <crypto/xts.h>
1617
#include <linux/module.h>
1718

19+
#include "aes-ctr-fallback.h"
20+
1821
MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
1922
MODULE_LICENSE("GPL v2");
2023

@@ -58,6 +61,11 @@ struct aesbs_cbc_ctx {
5861
u32 enc[AES_MAX_KEYLENGTH_U32];
5962
};
6063

64+
struct aesbs_ctr_ctx {
65+
struct aesbs_ctx key; /* must be first member */
66+
struct crypto_aes_ctx fallback;
67+
};
68+
6169
struct aesbs_xts_ctx {
6270
struct aesbs_ctx key;
6371
u32 twkey[AES_MAX_KEYLENGTH_U32];
@@ -196,6 +204,25 @@ static int cbc_decrypt(struct skcipher_request *req)
196204
return err;
197205
}
198206

207+
static int aesbs_ctr_setkey_sync(struct crypto_skcipher *tfm, const u8 *in_key,
208+
unsigned int key_len)
209+
{
210+
struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
211+
int err;
212+
213+
err = crypto_aes_expand_key(&ctx->fallback, in_key, key_len);
214+
if (err)
215+
return err;
216+
217+
ctx->key.rounds = 6 + key_len / 4;
218+
219+
kernel_neon_begin();
220+
aesbs_convert_key(ctx->key.rk, ctx->fallback.key_enc, ctx->key.rounds);
221+
kernel_neon_end();
222+
223+
return 0;
224+
}
225+
199226
static int ctr_encrypt(struct skcipher_request *req)
200227
{
201228
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
@@ -259,6 +286,17 @@ static int aesbs_xts_setkey(struct crypto_skcipher *tfm, const u8 *in_key,
259286
return aesbs_setkey(tfm, in_key, key_len);
260287
}
261288

289+
static int ctr_encrypt_sync(struct skcipher_request *req)
290+
{
291+
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
292+
struct aesbs_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
293+
294+
if (!may_use_simd())
295+
return aes_ctr_encrypt_fallback(&ctx->fallback, req);
296+
297+
return ctr_encrypt(req);
298+
}
299+
262300
static int __xts_crypt(struct skcipher_request *req,
263301
void (*fn)(u8 out[], u8 const in[], u8 const rk[],
264302
int rounds, int blocks, u8 iv[]))
@@ -355,17 +393,17 @@ static struct skcipher_alg aes_algs[] = { {
355393
.base.cra_driver_name = "ctr-aes-neonbs",
356394
.base.cra_priority = 250 - 1,
357395
.base.cra_blocksize = 1,
358-
.base.cra_ctxsize = sizeof(struct aesbs_ctx),
396+
.base.cra_ctxsize = sizeof(struct aesbs_ctr_ctx),
359397
.base.cra_module = THIS_MODULE,
360398

361399
.min_keysize = AES_MIN_KEY_SIZE,
362400
.max_keysize = AES_MAX_KEY_SIZE,
363401
.chunksize = AES_BLOCK_SIZE,
364402
.walksize = 8 * AES_BLOCK_SIZE,
365403
.ivsize = AES_BLOCK_SIZE,
366-
.setkey = aesbs_setkey,
367-
.encrypt = ctr_encrypt,
368-
.decrypt = ctr_encrypt,
404+
.setkey = aesbs_ctr_setkey_sync,
405+
.encrypt = ctr_encrypt_sync,
406+
.decrypt = ctr_encrypt_sync,
369407
}, {
370408
.base.cra_name = "__xts(aes)",
371409
.base.cra_driver_name = "__xts-aes-neonbs",

0 commit comments

Comments
 (0)