Re: [PATCH v3] crypto: rsassa-pkcs1 - Align DMA buffer to CRYPTO_DMA_ALIGN

From: Lukas Wunner

Date: Wed Jul 29 2026 - 15:28:25 EST


On Wed, Jul 29, 2026 at 10:41:56AM +1000, Changwei Zou wrote:
> +++ b/crypto/rsassa-pkcs1.c
> @@ -237,12 +237,13 @@ static int rsassa_pkcs1_verify(struct crypto_sig *tfm,
> return -EINVAL;
>
> /* RFC 8017 sec 8.2.2 step 2 - RSA verification */
> - child_req = kmalloc(sizeof(*child_req) + child_reqsize + ctx->key_size,
> - GFP_KERNEL);
> + child_req = kmalloc(sizeof(*child_req) + child_reqsize +
> + ctx->key_size + CRYPTO_DMA_ALIGN - 1, GFP_KERNEL);
> if (!child_req)
> return -ENOMEM;
>
> - out_buf = (u8 *)(child_req + 1) + child_reqsize;
> + out_buf = PTR_ALIGN((u8 *)(child_req + 1) + child_reqsize,
> + CRYPTO_DMA_ALIGN);
> memcpy(out_buf, src, slen);
>
> crypto_init_wait(&cwait);

Thank, this all looks good to me.

However after reading around a bit in crypto code, I'm under the
impression that the "native" way of doing this is to have struct
caam_akcipher_alg[] in drivers/crypto/caam/caampkc.c define
".cra_alignmask = CRYPTO_DMA_ALIGN - 1," and then have
rsassa_pkcs1_verify() call crypto_akcipher_alignmask()
to determine the amount of padding needed and the alignment.

That crypto_akcipher_alignmask() doesn't exist yet, but can
easily be copy-pasted from crypto_skcipher_alignmask()
except it invokes crypto_akcipher_tfm() instead of
crypto_skcipher_tfm().

Finally the kernel-doc of struct crypto_alg would have to be
amended because the documentation of the cra_alignmask member
does not mention akcipher yet.

Now this may all sound like a roundabout way of doing things
but it seems to fit neatly into the existing OO-like approach
of the crypto subsystem and so I strongly suspect that's what
Herbert would want. I apologize for not having steered you
in that direction right away, I feel like a novice myself
sometimes when navigating that code.

You can find some pre-existing use cases with
"git grep alignmask -- crypto/". E.g. in crypto/xctr.c,
alignmask is determined with crypto_cipher_alignmask()
and that (plus 1) is then fed to PTR_ALIGN().

Basically the idea is that whether padding is added at all
and how much depends on the child request, and that child
request knows exactly whether it needs any padding at all.

I think you can do all of that in a single patch and thus
keep this easily backportable to stable kernels.

Thanks!

Lukas