Skip to content

Commit cf3d41a

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 8083b1b commit cf3d41a

2 files changed

Lines changed: 49 additions & 4 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
Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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>
@@ -16,6 +17,8 @@
1617
#include <linux/module.h>
1718
#include <linux/scatterlist.h>
1819

20+
#include <asm/simd.h>
21+
1922
#include "aegis.h"
2023

2124
#define AEGIS128_NONCE_SIZE 16
@@ -40,6 +43,24 @@ struct aegis128_ops {
4043
const u8 *src, unsigned int size);
4144
};
4245

46+
static bool have_simd;
47+
48+
static bool aegis128_do_simd(void)
49+
{
50+
#ifdef CONFIG_CRYPTO_AEGIS128_SIMD
51+
if (have_simd)
52+
return crypto_simd_usable();
53+
#endif
54+
return false;
55+
}
56+
57+
bool crypto_aegis128_have_simd(void);
58+
void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
59+
void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
60+
const u8 *src, unsigned int size);
61+
void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
62+
const u8 *src, unsigned int size);
63+
4364
static void crypto_aegis128_update(struct aegis_state *state)
4465
{
4566
union aegis_block tmp;
@@ -55,12 +76,22 @@ static void crypto_aegis128_update(struct aegis_state *state)
5576
static void crypto_aegis128_update_a(struct aegis_state *state,
5677
const union aegis_block *msg)
5778
{
79+
if (aegis128_do_simd()) {
80+
crypto_aegis128_update_simd(state, msg);
81+
return;
82+
}
83+
5884
crypto_aegis128_update(state);
5985
crypto_aegis_block_xor(&state->blocks[0], msg);
6086
}
6187

6288
static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg)
6389
{
90+
if (aegis128_do_simd()) {
91+
crypto_aegis128_update_simd(state, msg);
92+
return;
93+
}
94+
6495
crypto_aegis128_update(state);
6596
crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
6697
}
@@ -365,7 +396,7 @@ static void crypto_aegis128_crypt(struct aead_request *req,
365396

366397
static int crypto_aegis128_encrypt(struct aead_request *req)
367398
{
368-
static const struct aegis128_ops ops = {
399+
const struct aegis128_ops *ops = &(struct aegis128_ops){
369400
.skcipher_walk_init = skcipher_walk_aead_encrypt,
370401
.crypt_chunk = crypto_aegis128_encrypt_chunk,
371402
};
@@ -375,7 +406,12 @@ static int crypto_aegis128_encrypt(struct aead_request *req)
375406
unsigned int authsize = crypto_aead_authsize(tfm);
376407
unsigned int cryptlen = req->cryptlen;
377408

378-
crypto_aegis128_crypt(req, &tag, cryptlen, &ops);
409+
if (aegis128_do_simd())
410+
ops = &(struct aegis128_ops){
411+
.skcipher_walk_init = skcipher_walk_aead_encrypt,
412+
.crypt_chunk = crypto_aegis128_encrypt_chunk_simd };
413+
414+
crypto_aegis128_crypt(req, &tag, cryptlen, ops);
379415

380416
scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
381417
authsize, 1);
@@ -384,7 +420,7 @@ static int crypto_aegis128_encrypt(struct aead_request *req)
384420

385421
static int crypto_aegis128_decrypt(struct aead_request *req)
386422
{
387-
static const struct aegis128_ops ops = {
423+
const struct aegis128_ops *ops = &(struct aegis128_ops){
388424
.skcipher_walk_init = skcipher_walk_aead_decrypt,
389425
.crypt_chunk = crypto_aegis128_decrypt_chunk,
390426
};
@@ -398,7 +434,12 @@ static int crypto_aegis128_decrypt(struct aead_request *req)
398434
scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
399435
authsize, 0);
400436

401-
crypto_aegis128_crypt(req, &tag, cryptlen, &ops);
437+
if (aegis128_do_simd())
438+
ops = &(struct aegis128_ops){
439+
.skcipher_walk_init = skcipher_walk_aead_decrypt,
440+
.crypt_chunk = crypto_aegis128_decrypt_chunk_simd };
441+
442+
crypto_aegis128_crypt(req, &tag, cryptlen, ops);
402443

403444
return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
404445
}
@@ -429,6 +470,9 @@ static struct aead_alg crypto_aegis128_alg = {
429470

430471
static int __init crypto_aegis128_module_init(void)
431472
{
473+
if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD))
474+
have_simd = crypto_aegis128_have_simd();
475+
432476
return crypto_register_aead(&crypto_aegis128_alg);
433477
}
434478

0 commit comments

Comments
 (0)