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+
4354static 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)
5566static 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
6278static 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
366387static 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
385411static 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
430461static 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