Skip to content

Commit a8d79d7

Browse files
chunkeeyherbertx
authored andcommitted
crypto: crypto4xx - performance optimizations
This patch provides a cheap 2MiB/s+ (~ 6%) performance improvement over the current code. This is because the compiler can now optimize several endian swap memcpy. Signed-off-by: Christian Lamparter <chunkeey@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 5b0aa25 commit a8d79d7

3 files changed

Lines changed: 34 additions & 26 deletions

File tree

‎drivers/crypto/amcc/crypto4xx_alg.c‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,38 @@ static void set_dynamic_sa_command_1(struct dynamic_sa_ctl *sa, u32 cm,
7474
sa->sa_command_1.bf.copy_hdr = cp_hdr;
7575
}
7676

77-
int crypto4xx_encrypt(struct ablkcipher_request *req)
77+
static inline int crypto4xx_crypt(struct ablkcipher_request *req,
78+
const unsigned int ivlen, bool decrypt)
7879
{
7980
struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
80-
unsigned int ivlen = crypto_ablkcipher_ivsize(
81-
crypto_ablkcipher_reqtfm(req));
8281
__le32 iv[ivlen];
8382

8483
if (ivlen)
8584
crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
8685

8786
return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
88-
req->nbytes, iv, ivlen, ctx->sa_out, ctx->sa_len, 0);
87+
req->nbytes, iv, ivlen, decrypt ? ctx->sa_in : ctx->sa_out,
88+
ctx->sa_len, 0);
8989
}
9090

91-
int crypto4xx_decrypt(struct ablkcipher_request *req)
91+
int crypto4xx_encrypt_noiv(struct ablkcipher_request *req)
9292
{
93-
struct crypto4xx_ctx *ctx = crypto_tfm_ctx(req->base.tfm);
94-
unsigned int ivlen = crypto_ablkcipher_ivsize(
95-
crypto_ablkcipher_reqtfm(req));
96-
__le32 iv[ivlen];
93+
return crypto4xx_crypt(req, 0, false);
94+
}
9795

98-
if (ivlen)
99-
crypto4xx_memcpy_to_le32(iv, req->info, ivlen);
96+
int crypto4xx_encrypt_iv(struct ablkcipher_request *req)
97+
{
98+
return crypto4xx_crypt(req, AES_IV_SIZE, false);
99+
}
100100

101-
return crypto4xx_build_pd(&req->base, ctx, req->src, req->dst,
102-
req->nbytes, iv, ivlen, ctx->sa_in, ctx->sa_len, 0);
101+
int crypto4xx_decrypt_noiv(struct ablkcipher_request *req)
102+
{
103+
return crypto4xx_crypt(req, 0, true);
104+
}
105+
106+
int crypto4xx_decrypt_iv(struct ablkcipher_request *req)
107+
{
108+
return crypto4xx_crypt(req, AES_IV_SIZE, true);
103109
}
104110

105111
/**

‎drivers/crypto/amcc/crypto4xx_core.c‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ static void crypto4xx_aead_done(struct crypto4xx_device *dev,
580580
struct scatterlist *dst = pd_uinfo->dest_va;
581581
size_t cp_len = crypto_aead_authsize(
582582
crypto_aead_reqtfm(aead_req));
583-
u32 icv[cp_len];
583+
u32 icv[AES_BLOCK_SIZE];
584584
int err = 0;
585585

586586
if (pd_uinfo->using_sd) {
@@ -595,7 +595,7 @@ static void crypto4xx_aead_done(struct crypto4xx_device *dev,
595595
if (pd_uinfo->sa_va->sa_command_0.bf.dir == DIR_OUTBOUND) {
596596
/* append icv at the end */
597597
crypto4xx_memcpy_from_le32(icv, pd_uinfo->sr_va->save_digest,
598-
cp_len);
598+
sizeof(icv));
599599

600600
scatterwalk_map_and_copy(icv, dst, aead_req->cryptlen,
601601
cp_len, 1);
@@ -605,7 +605,7 @@ static void crypto4xx_aead_done(struct crypto4xx_device *dev,
605605
aead_req->assoclen + aead_req->cryptlen -
606606
cp_len, cp_len, 0);
607607

608-
crypto4xx_memcpy_from_le32(icv, icv, cp_len);
608+
crypto4xx_memcpy_from_le32(icv, icv, sizeof(icv));
609609

610610
if (crypto_memneq(icv, pd_uinfo->sr_va->save_digest, cp_len))
611611
err = -EBADMSG;
@@ -1122,8 +1122,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
11221122
.max_keysize = AES_MAX_KEY_SIZE,
11231123
.ivsize = AES_IV_SIZE,
11241124
.setkey = crypto4xx_setkey_aes_cbc,
1125-
.encrypt = crypto4xx_encrypt,
1126-
.decrypt = crypto4xx_decrypt,
1125+
.encrypt = crypto4xx_encrypt_iv,
1126+
.decrypt = crypto4xx_decrypt_iv,
11271127
}
11281128
}
11291129
}},
@@ -1146,8 +1146,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
11461146
.max_keysize = AES_MAX_KEY_SIZE,
11471147
.ivsize = AES_IV_SIZE,
11481148
.setkey = crypto4xx_setkey_aes_cfb,
1149-
.encrypt = crypto4xx_encrypt,
1150-
.decrypt = crypto4xx_decrypt,
1149+
.encrypt = crypto4xx_encrypt_iv,
1150+
.decrypt = crypto4xx_decrypt_iv,
11511151
}
11521152
}
11531153
} },
@@ -1195,8 +1195,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
11951195
.min_keysize = AES_MIN_KEY_SIZE,
11961196
.max_keysize = AES_MAX_KEY_SIZE,
11971197
.setkey = crypto4xx_setkey_aes_ecb,
1198-
.encrypt = crypto4xx_encrypt,
1199-
.decrypt = crypto4xx_decrypt,
1198+
.encrypt = crypto4xx_encrypt_noiv,
1199+
.decrypt = crypto4xx_decrypt_noiv,
12001200
}
12011201
}
12021202
} },
@@ -1219,8 +1219,8 @@ static struct crypto4xx_alg_common crypto4xx_alg[] = {
12191219
.max_keysize = AES_MAX_KEY_SIZE,
12201220
.ivsize = AES_IV_SIZE,
12211221
.setkey = crypto4xx_setkey_aes_ofb,
1222-
.encrypt = crypto4xx_encrypt,
1223-
.decrypt = crypto4xx_decrypt,
1222+
.encrypt = crypto4xx_encrypt_iv,
1223+
.decrypt = crypto4xx_decrypt_iv,
12241224
}
12251225
}
12261226
} },

‎drivers/crypto/amcc/crypto4xx_core.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ int crypto4xx_setkey_aes_ofb(struct crypto_ablkcipher *cipher,
168168
const u8 *key, unsigned int keylen);
169169
int crypto4xx_setkey_rfc3686(struct crypto_ablkcipher *cipher,
170170
const u8 *key, unsigned int keylen);
171-
int crypto4xx_encrypt(struct ablkcipher_request *req);
172-
int crypto4xx_decrypt(struct ablkcipher_request *req);
171+
int crypto4xx_encrypt_iv(struct ablkcipher_request *req);
172+
int crypto4xx_decrypt_iv(struct ablkcipher_request *req);
173+
int crypto4xx_encrypt_noiv(struct ablkcipher_request *req);
174+
int crypto4xx_decrypt_noiv(struct ablkcipher_request *req);
173175
int crypto4xx_rfc3686_encrypt(struct ablkcipher_request *req);
174176
int crypto4xx_rfc3686_decrypt(struct ablkcipher_request *req);
175177
int crypto4xx_sha1_alg_init(struct crypto_tfm *tfm);

0 commit comments

Comments
 (0)