Skip to content

Commit 7cdc0dd

Browse files
Ard Biesheuvelherbertx
authored andcommitted
crypto: aegis128 - add support for SIMD acceleration
Add some plumbing to allow the AEGIS128 code to be built with SIMD routines for acceleration. Reviewed-by: Ondrej Mosnacek <omosnace@redhat.com> Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 521cdde commit 7cdc0dd

3 files changed

Lines changed: 46 additions & 11 deletions

File tree

‎crypto/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ obj-$(CONFIG_CRYPTO_GCM) += gcm.o
9090
obj-$(CONFIG_CRYPTO_CCM) += ccm.o
9191
obj-$(CONFIG_CRYPTO_CHACHA20POLY1305) += chacha20poly1305.o
9292
obj-$(CONFIG_CRYPTO_AEGIS128) += aegis128.o
93+
aegis128-y := aegis128-core.o
9394
obj-$(CONFIG_CRYPTO_PCRYPT) += pcrypt.o
9495
obj-$(CONFIG_CRYPTO_CRYPTD) += cryptd.o
9596
obj-$(CONFIG_CRYPTO_DES) += des_generic.o

‎crypto/aegis.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,23 @@ static const union aegis_block crypto_aegis_const[2] = {
3535
} },
3636
};
3737

38-
static void crypto_aegis_block_xor(union aegis_block *dst,
39-
const union aegis_block *src)
38+
static inline void crypto_aegis_block_xor(union aegis_block *dst,
39+
const union aegis_block *src)
4040
{
4141
dst->words64[0] ^= src->words64[0];
4242
dst->words64[1] ^= src->words64[1];
4343
}
4444

45-
static void crypto_aegis_block_and(union aegis_block *dst,
46-
const union aegis_block *src)
45+
static inline void crypto_aegis_block_and(union aegis_block *dst,
46+
const union aegis_block *src)
4747
{
4848
dst->words64[0] &= src->words64[0];
4949
dst->words64[1] &= src->words64[1];
5050
}
5151

52-
static void crypto_aegis_aesenc(union aegis_block *dst,
53-
const union aegis_block *src,
54-
const union aegis_block *key)
52+
static inline void crypto_aegis_aesenc(union aegis_block *dst,
53+
const union aegis_block *src,
54+
const union aegis_block *key)
5555
{
5656
const u8 *s = src->bytes;
5757
const u32 *t = crypto_ft_tab[0];
Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
#include <crypto/algapi.h>
1010
#include <crypto/internal/aead.h>
11+
#include <crypto/internal/simd.h>
1112
#include <crypto/internal/skcipher.h>
1213
#include <crypto/scatterwalk.h>
1314
#include <linux/err.h>
1415
#include <linux/init.h>
1516
#include <linux/kernel.h>
1617
#include <linux/module.h>
1718
#include <linux/scatterlist.h>
19+
#include <asm/simd.h>
1820

1921
#include "aegis.h"
2022

@@ -40,6 +42,15 @@ struct aegis128_ops {
4042
const u8 *src, unsigned int size);
4143
};
4244

45+
static bool have_simd;
46+
47+
bool crypto_aegis128_have_simd(void);
48+
void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
49+
void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
50+
const u8 *src, unsigned int size);
51+
void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
52+
const u8 *src, unsigned int size);
53+
4354
static void crypto_aegis128_update(struct aegis_state *state)
4455
{
4556
union aegis_block tmp;
@@ -55,12 +66,22 @@ static void crypto_aegis128_update(struct aegis_state *state)
5566
static void crypto_aegis128_update_a(struct aegis_state *state,
5667
const union aegis_block *msg)
5768
{
69+
if (have_simd && crypto_simd_usable()) {
70+
crypto_aegis128_update_simd(state, msg);
71+
return;
72+
}
73+
5874
crypto_aegis128_update(state);
5975
crypto_aegis_block_xor(&state->blocks[0], msg);
6076
}
6177

6278
static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg)
6379
{
80+
if (have_simd && crypto_simd_usable()) {
81+
crypto_aegis128_update_simd(state, msg);
82+
return;
83+
}
84+
6485
crypto_aegis128_update(state);
6586
crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
6687
}
@@ -365,7 +386,7 @@ static void crypto_aegis128_crypt(struct aead_request *req,
365386

366387
static int crypto_aegis128_encrypt(struct aead_request *req)
367388
{
368-
static const struct aegis128_ops ops = {
389+
const struct aegis128_ops *ops = &(struct aegis128_ops){
369390
.skcipher_walk_init = skcipher_walk_aead_encrypt,
370391
.crypt_chunk = crypto_aegis128_encrypt_chunk,
371392
};
@@ -375,7 +396,12 @@ static int crypto_aegis128_encrypt(struct aead_request *req)
375396
unsigned int authsize = crypto_aead_authsize(tfm);
376397
unsigned int cryptlen = req->cryptlen;
377398

378-
crypto_aegis128_crypt(req, &tag, cryptlen, &ops);
399+
if (have_simd && crypto_simd_usable())
400+
ops = &(struct aegis128_ops){
401+
.skcipher_walk_init = skcipher_walk_aead_encrypt,
402+
.crypt_chunk = crypto_aegis128_encrypt_chunk_simd };
403+
404+
crypto_aegis128_crypt(req, &tag, cryptlen, ops);
379405

380406
scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
381407
authsize, 1);
@@ -384,7 +410,7 @@ static int crypto_aegis128_encrypt(struct aead_request *req)
384410

385411
static int crypto_aegis128_decrypt(struct aead_request *req)
386412
{
387-
static const struct aegis128_ops ops = {
413+
const struct aegis128_ops *ops = &(struct aegis128_ops){
388414
.skcipher_walk_init = skcipher_walk_aead_decrypt,
389415
.crypt_chunk = crypto_aegis128_decrypt_chunk,
390416
};
@@ -398,7 +424,12 @@ static int crypto_aegis128_decrypt(struct aead_request *req)
398424
scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
399425
authsize, 0);
400426

401-
crypto_aegis128_crypt(req, &tag, cryptlen, &ops);
427+
if (have_simd && crypto_simd_usable())
428+
ops = &(struct aegis128_ops){
429+
.skcipher_walk_init = skcipher_walk_aead_decrypt,
430+
.crypt_chunk = crypto_aegis128_decrypt_chunk_simd };
431+
432+
crypto_aegis128_crypt(req, &tag, cryptlen, ops);
402433

403434
return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
404435
}
@@ -429,6 +460,9 @@ static struct aead_alg crypto_aegis128_alg = {
429460

430461
static int __init crypto_aegis128_module_init(void)
431462
{
463+
if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD))
464+
have_simd = crypto_aegis128_have_simd();
465+
432466
return crypto_register_aead(&crypto_aegis128_alg);
433467
}
434468

0 commit comments

Comments
 (0)