Skip to content

Commit cd912b3

Browse files
author
Eric Biggers
committed
crypto: adiantum - Use scatter_walk API instead of sg_miter
Make adiantum_hash_message() use the scatter_walk API instead of sg_miter. scatter_walk is a bit simpler and also more efficient. For example, unlike sg_miter, scatter_walk doesn't require that the number of scatterlist entries be calculated up-front. Link: https://lore.kernel.org/r/20251211011846.8179-8-ebiggers@kernel.org Signed-off-by: Eric Biggers <ebiggers@kernel.org>
1 parent 7698747 commit cd912b3

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

crypto/adiantum.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -369,26 +369,23 @@ static void nhpoly1305_final(struct nhpoly1305_ctx *ctx,
369369
* property; NH is used for performance since it's much faster than Poly1305.
370370
*/
371371
static void adiantum_hash_message(struct skcipher_request *req,
372-
struct scatterlist *sgl, unsigned int nents,
373-
le128 *out)
372+
struct scatterlist *sgl, le128 *out)
374373
{
375374
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
376375
const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
377376
struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
378-
const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
379-
struct sg_mapping_iter miter;
380-
unsigned int i, n;
377+
unsigned int len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
378+
struct scatter_walk walk;
381379

382380
nhpoly1305_init(&rctx->u.hash_ctx);
381+
scatterwalk_start(&walk, sgl);
382+
while (len) {
383+
unsigned int n = scatterwalk_next(&walk, len);
383384

384-
sg_miter_start(&miter, sgl, nents, SG_MITER_FROM_SG | SG_MITER_ATOMIC);
385-
for (i = 0; i < bulk_len; i += n) {
386-
sg_miter_next(&miter);
387-
n = min_t(unsigned int, miter.length, bulk_len - i);
388-
nhpoly1305_update(&rctx->u.hash_ctx, tctx, miter.addr, n);
385+
nhpoly1305_update(&rctx->u.hash_ctx, tctx, walk.addr, n);
386+
scatterwalk_done_src(&walk, n);
387+
len -= n;
389388
}
390-
sg_miter_stop(&miter);
391-
392389
nhpoly1305_final(&rctx->u.hash_ctx, tctx, out);
393390
}
394391

@@ -400,7 +397,6 @@ static int adiantum_finish(struct skcipher_request *req)
400397
struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
401398
const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
402399
struct scatterlist *dst = req->dst;
403-
const unsigned int dst_nents = sg_nents(dst);
404400
le128 digest;
405401

406402
/* If decrypting, decrypt C_M with the block cipher to get P_M */
@@ -414,7 +410,8 @@ static int adiantum_finish(struct skcipher_request *req)
414410
* dec: P_R = P_M - H_{K_H}(T, P_L)
415411
*/
416412
le128_sub(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &rctx->header_hash);
417-
if (dst_nents == 1 && dst->offset + req->cryptlen <= PAGE_SIZE) {
413+
if (dst->length >= req->cryptlen &&
414+
dst->offset + req->cryptlen <= PAGE_SIZE) {
418415
/* Fast path for single-page destination */
419416
struct page *page = sg_page(dst);
420417
void *virt = kmap_local_page(page) + dst->offset;
@@ -428,7 +425,7 @@ static int adiantum_finish(struct skcipher_request *req)
428425
kunmap_local(virt);
429426
} else {
430427
/* Slow path that works for any destination scatterlist */
431-
adiantum_hash_message(req, dst, dst_nents, &digest);
428+
adiantum_hash_message(req, dst, &digest);
432429
le128_sub(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
433430
scatterwalk_map_and_copy(&rctx->rbuf.bignum, dst,
434431
bulk_len, sizeof(le128), 1);
@@ -453,7 +450,6 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
453450
struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
454451
const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
455452
struct scatterlist *src = req->src;
456-
const unsigned int src_nents = sg_nents(src);
457453
unsigned int stream_len;
458454
le128 digest;
459455

@@ -468,7 +464,8 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
468464
* dec: C_M = C_R + H_{K_H}(T, C_L)
469465
*/
470466
adiantum_hash_header(req);
471-
if (src_nents == 1 && src->offset + req->cryptlen <= PAGE_SIZE) {
467+
if (src->length >= req->cryptlen &&
468+
src->offset + req->cryptlen <= PAGE_SIZE) {
472469
/* Fast path for single-page source */
473470
void *virt = kmap_local_page(sg_page(src)) + src->offset;
474471

@@ -479,7 +476,7 @@ static int adiantum_crypt(struct skcipher_request *req, bool enc)
479476
kunmap_local(virt);
480477
} else {
481478
/* Slow path that works for any source scatterlist */
482-
adiantum_hash_message(req, src, src_nents, &digest);
479+
adiantum_hash_message(req, src, &digest);
483480
scatterwalk_map_and_copy(&rctx->rbuf.bignum, src,
484481
bulk_len, sizeof(le128), 0);
485482
}

0 commit comments

Comments
 (0)