Skip to content

Commit e4f8748

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: arm64/gcm - use inline helper to suppress indirect calls
Introduce an inline wrapper for ghash_do_update() that incorporates the indirect call to the asm routine that is passed as an argument, and keep the non-SIMD fallback code out of line. This ensures that all references to the function pointer are inlined where the address is taken, removing the need for any indirect calls to begin with. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 17d0fb1 commit e4f8748

1 file changed

Lines changed: 46 additions & 39 deletions

File tree

‎arch/arm64/crypto/ghash-ce-glue.c‎

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,43 @@ static int ghash_init(struct shash_desc *desc)
6969
}
7070

7171
static void ghash_do_update(int blocks, u64 dg[], const char *src,
72-
struct ghash_key *key, const char *head,
73-
void (*simd_update)(int blocks, u64 dg[],
74-
const char *src,
75-
u64 const h[][2],
76-
const char *head))
72+
struct ghash_key *key, const char *head)
7773
{
78-
if (likely(crypto_simd_usable() && simd_update)) {
74+
be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
75+
76+
do {
77+
const u8 *in = src;
78+
79+
if (head) {
80+
in = head;
81+
blocks++;
82+
head = NULL;
83+
} else {
84+
src += GHASH_BLOCK_SIZE;
85+
}
86+
87+
crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
88+
gf128mul_lle(&dst, &key->k);
89+
} while (--blocks);
90+
91+
dg[0] = be64_to_cpu(dst.b);
92+
dg[1] = be64_to_cpu(dst.a);
93+
}
94+
95+
static __always_inline
96+
void ghash_do_simd_update(int blocks, u64 dg[], const char *src,
97+
struct ghash_key *key, const char *head,
98+
void (*simd_update)(int blocks, u64 dg[],
99+
const char *src,
100+
u64 const h[][2],
101+
const char *head))
102+
{
103+
if (likely(crypto_simd_usable())) {
79104
kernel_neon_begin();
80105
simd_update(blocks, dg, src, key->h, head);
81106
kernel_neon_end();
82107
} else {
83-
be128 dst = { cpu_to_be64(dg[1]), cpu_to_be64(dg[0]) };
84-
85-
do {
86-
const u8 *in = src;
87-
88-
if (head) {
89-
in = head;
90-
blocks++;
91-
head = NULL;
92-
} else {
93-
src += GHASH_BLOCK_SIZE;
94-
}
95-
96-
crypto_xor((u8 *)&dst, in, GHASH_BLOCK_SIZE);
97-
gf128mul_lle(&dst, &key->k);
98-
} while (--blocks);
99-
100-
dg[0] = be64_to_cpu(dst.b);
101-
dg[1] = be64_to_cpu(dst.a);
108+
ghash_do_update(blocks, dg, src, key, head);
102109
}
103110
}
104111

@@ -131,9 +138,9 @@ static int ghash_update(struct shash_desc *desc, const u8 *src,
131138
do {
132139
int chunk = min(blocks, MAX_BLOCKS);
133140

134-
ghash_do_update(chunk, ctx->digest, src, key,
135-
partial ? ctx->buf : NULL,
136-
pmull_ghash_update_p8);
141+
ghash_do_simd_update(chunk, ctx->digest, src, key,
142+
partial ? ctx->buf : NULL,
143+
pmull_ghash_update_p8);
137144

138145
blocks -= chunk;
139146
src += chunk * GHASH_BLOCK_SIZE;
@@ -155,8 +162,8 @@ static int ghash_final(struct shash_desc *desc, u8 *dst)
155162

156163
memset(ctx->buf + partial, 0, GHASH_BLOCK_SIZE - partial);
157164

158-
ghash_do_update(1, ctx->digest, ctx->buf, key, NULL,
159-
pmull_ghash_update_p8);
165+
ghash_do_simd_update(1, ctx->digest, ctx->buf, key, NULL,
166+
pmull_ghash_update_p8);
160167
}
161168
put_unaligned_be64(ctx->digest[1], dst);
162169
put_unaligned_be64(ctx->digest[0], dst + 8);
@@ -280,9 +287,9 @@ static void gcm_update_mac(u64 dg[], const u8 *src, int count, u8 buf[],
280287
if (count >= GHASH_BLOCK_SIZE || *buf_count == GHASH_BLOCK_SIZE) {
281288
int blocks = count / GHASH_BLOCK_SIZE;
282289

283-
ghash_do_update(blocks, dg, src, &ctx->ghash_key,
284-
*buf_count ? buf : NULL,
285-
pmull_ghash_update_p64);
290+
ghash_do_simd_update(blocks, dg, src, &ctx->ghash_key,
291+
*buf_count ? buf : NULL,
292+
pmull_ghash_update_p64);
286293

287294
src += blocks * GHASH_BLOCK_SIZE;
288295
count %= GHASH_BLOCK_SIZE;
@@ -326,8 +333,8 @@ static void gcm_calculate_auth_mac(struct aead_request *req, u64 dg[])
326333

327334
if (buf_count) {
328335
memset(&buf[buf_count], 0, GHASH_BLOCK_SIZE - buf_count);
329-
ghash_do_update(1, dg, buf, &ctx->ghash_key, NULL,
330-
pmull_ghash_update_p64);
336+
ghash_do_simd_update(1, dg, buf, &ctx->ghash_key, NULL,
337+
pmull_ghash_update_p64);
331338
}
332339
}
333340

@@ -403,7 +410,7 @@ static int gcm_encrypt(struct aead_request *req)
403410
} while (--remaining > 0);
404411

405412
ghash_do_update(blocks, dg, walk.dst.virt.addr,
406-
&ctx->ghash_key, NULL, NULL);
413+
&ctx->ghash_key, NULL);
407414

408415
err = skcipher_walk_done(&walk,
409416
walk.nbytes % AES_BLOCK_SIZE);
@@ -422,7 +429,7 @@ static int gcm_encrypt(struct aead_request *req)
422429

423430
tag = (u8 *)&lengths;
424431
ghash_do_update(1, dg, tag, &ctx->ghash_key,
425-
walk.nbytes ? buf : NULL, NULL);
432+
walk.nbytes ? buf : NULL);
426433

427434
if (walk.nbytes)
428435
err = skcipher_walk_done(&walk, 0);
@@ -507,7 +514,7 @@ static int gcm_decrypt(struct aead_request *req)
507514
u8 *dst = walk.dst.virt.addr;
508515

509516
ghash_do_update(blocks, dg, walk.src.virt.addr,
510-
&ctx->ghash_key, NULL, NULL);
517+
&ctx->ghash_key, NULL);
511518

512519
do {
513520
aes_encrypt(&ctx->aes_key, buf, iv);
@@ -530,7 +537,7 @@ static int gcm_decrypt(struct aead_request *req)
530537

531538
tag = (u8 *)&lengths;
532539
ghash_do_update(1, dg, tag, &ctx->ghash_key,
533-
walk.nbytes ? buf : NULL, NULL);
540+
walk.nbytes ? buf : NULL);
534541

535542
if (walk.nbytes) {
536543
aes_encrypt(&ctx->aes_key, buf, iv);

0 commit comments

Comments
 (0)