1010
1111#include <asm/neon.h>
1212#include <asm/hwcap.h>
13+ #include <asm/simd.h>
1314#include <crypto/aes.h>
1415#include <crypto/internal/hash.h>
1516#include <crypto/internal/simd.h>
1920#include <crypto/xts.h>
2021
2122#include "aes-ce-setkey.h"
23+ #include "aes-ctr-fallback.h"
2224
2325#ifdef USE_V8_CRYPTO_EXTENSIONS
2426#define MODE "ce"
@@ -249,6 +251,17 @@ static int ctr_encrypt(struct skcipher_request *req)
249251 return err ;
250252}
251253
254+ static int ctr_encrypt_sync (struct skcipher_request * req )
255+ {
256+ struct crypto_skcipher * tfm = crypto_skcipher_reqtfm (req );
257+ struct crypto_aes_ctx * ctx = crypto_skcipher_ctx (tfm );
258+
259+ if (!may_use_simd ())
260+ return aes_ctr_encrypt_fallback (ctx , req );
261+
262+ return ctr_encrypt (req );
263+ }
264+
252265static int xts_encrypt (struct skcipher_request * req )
253266{
254267 struct crypto_skcipher * tfm = crypto_skcipher_reqtfm (req );
@@ -355,8 +368,8 @@ static struct skcipher_alg aes_algs[] = { {
355368 .ivsize = AES_BLOCK_SIZE ,
356369 .chunksize = AES_BLOCK_SIZE ,
357370 .setkey = skcipher_aes_setkey ,
358- .encrypt = ctr_encrypt ,
359- .decrypt = ctr_encrypt ,
371+ .encrypt = ctr_encrypt_sync ,
372+ .decrypt = ctr_encrypt_sync ,
360373}, {
361374 .base = {
362375 .cra_name = "__xts(aes)" ,
@@ -458,11 +471,35 @@ static int mac_init(struct shash_desc *desc)
458471 return 0 ;
459472}
460473
474+ static void mac_do_update (struct crypto_aes_ctx * ctx , u8 const in [], int blocks ,
475+ u8 dg [], int enc_before , int enc_after )
476+ {
477+ int rounds = 6 + ctx -> key_length / 4 ;
478+
479+ if (may_use_simd ()) {
480+ kernel_neon_begin ();
481+ aes_mac_update (in , ctx -> key_enc , rounds , blocks , dg , enc_before ,
482+ enc_after );
483+ kernel_neon_end ();
484+ } else {
485+ if (enc_before )
486+ __aes_arm64_encrypt (ctx -> key_enc , dg , dg , rounds );
487+
488+ while (blocks -- ) {
489+ crypto_xor (dg , in , AES_BLOCK_SIZE );
490+ in += AES_BLOCK_SIZE ;
491+
492+ if (blocks || enc_after )
493+ __aes_arm64_encrypt (ctx -> key_enc , dg , dg ,
494+ rounds );
495+ }
496+ }
497+ }
498+
461499static int mac_update (struct shash_desc * desc , const u8 * p , unsigned int len )
462500{
463501 struct mac_tfm_ctx * tctx = crypto_shash_ctx (desc -> tfm );
464502 struct mac_desc_ctx * ctx = shash_desc_ctx (desc );
465- int rounds = 6 + tctx -> key .key_length / 4 ;
466503
467504 while (len > 0 ) {
468505 unsigned int l ;
@@ -474,10 +511,8 @@ static int mac_update(struct shash_desc *desc, const u8 *p, unsigned int len)
474511
475512 len %= AES_BLOCK_SIZE ;
476513
477- kernel_neon_begin ();
478- aes_mac_update (p , tctx -> key .key_enc , rounds , blocks ,
479- ctx -> dg , (ctx -> len != 0 ), (len != 0 ));
480- kernel_neon_end ();
514+ mac_do_update (& tctx -> key , p , blocks , ctx -> dg ,
515+ (ctx -> len != 0 ), (len != 0 ));
481516
482517 p += blocks * AES_BLOCK_SIZE ;
483518
@@ -505,11 +540,8 @@ static int cbcmac_final(struct shash_desc *desc, u8 *out)
505540{
506541 struct mac_tfm_ctx * tctx = crypto_shash_ctx (desc -> tfm );
507542 struct mac_desc_ctx * ctx = shash_desc_ctx (desc );
508- int rounds = 6 + tctx -> key .key_length / 4 ;
509543
510- kernel_neon_begin ();
511- aes_mac_update (NULL , tctx -> key .key_enc , rounds , 0 , ctx -> dg , 1 , 0 );
512- kernel_neon_end ();
544+ mac_do_update (& tctx -> key , NULL , 0 , ctx -> dg , 1 , 0 );
513545
514546 memcpy (out , ctx -> dg , AES_BLOCK_SIZE );
515547
@@ -520,17 +552,14 @@ static int cmac_final(struct shash_desc *desc, u8 *out)
520552{
521553 struct mac_tfm_ctx * tctx = crypto_shash_ctx (desc -> tfm );
522554 struct mac_desc_ctx * ctx = shash_desc_ctx (desc );
523- int rounds = 6 + tctx -> key .key_length / 4 ;
524555 u8 * consts = tctx -> consts ;
525556
526557 if (ctx -> len != AES_BLOCK_SIZE ) {
527558 ctx -> dg [ctx -> len ] ^= 0x80 ;
528559 consts += AES_BLOCK_SIZE ;
529560 }
530561
531- kernel_neon_begin ();
532- aes_mac_update (consts , tctx -> key .key_enc , rounds , 1 , ctx -> dg , 0 , 1 );
533- kernel_neon_end ();
562+ mac_do_update (& tctx -> key , consts , 1 , ctx -> dg , 0 , 1 );
534563
535564 memcpy (out , ctx -> dg , AES_BLOCK_SIZE );
536565
0 commit comments