Re: [PATCH v2] crypto: hisilicon/hpre - fix ECDH compute_shared_secret dst_len requirement

From: Herbert Xu

Date: Fri Dec 04 2026 - 01:09:55 EST


On Tue, Nov 24, 2026 at 01:01:27PM +0000, Ryan Prinz wrote:
> hpre_ecdh_msg_request_set() requires req->dst_len to be at least
> 2 * ctx->key_sz, regardless of which operation is being performed.
> The generic KPP convention only needs that much for
> generate_public_key(); a shared secret is conventionally just the X
> coordinate, ctx->key_sz bytes.
>
> Both in-tree callers of the generic ecdh-nist-pNNN names call
> compute_shared_secret() with a buffer sized for just the X
> coordinate: net/bluetooth/ecdh_helper.c's compute_ecdh_secret()
> allocates secret[32] for P-256, and drivers/char/tpm/tpm2-sessions.c's
> session salt derivation does kpp_request_set_output(req, d, EC_PT_SZ).
>
> Bluetooth reaches this unconditionally and fails with -EOVERFLOW on
> HPRE hardware before the peer's public key is looked at. TPM2's
> session code would hit the same failure, but on HPRE hardware it
> never gets that far: the same session's earlier generate_public_key()
> call uses a 2-entry destination scatterlist for its own public key,
> which hpre_ecdh_dst_data_init()'s existing sg_is_last() check
> rejects, for a reason unrelated to this patch.
>
> Found by Sashiko's automated review of a related patch, as a
> pre-existing issue that patch doesn't introduce.
>
> Fixed by requiring the curve's own coordinate size for
> compute_shared_secret(), and using a bounce buffer for that case:
> hpre_ecdh_dst_data_init() DMA-maps an internal 2 * key_sz buffer
> instead of the caller's, and hpre_ecdh_hw_data_clr_all() copies the
> X coordinate to the caller's destination. generate_public_key() is
> unaffected; it still requires, and writes directly into, a
> 2 * key_sz-sized caller buffer. areq->dst_len is also now set to
> what was written (curve_sz or 2 * curve_sz) instead of reporting
> 2 * key_sz.
>
> Fixes: 05e7b906aa7c ("crypto: hisilicon/hpre - add 'ECDH' algorithm")
> Signed-off-by: Ryan Prinz <rc4d5a@xxxxxxxxxxxxxx>
> ---
> v2:
> - Fixed a bug Sashiko's automated review found: moving the dst
> compaction/copy-out into hpre_ecdh_hw_data_clr_all() made it run
> from the clear_all error path too, where hpre_send() failed
> before reaching hardware. That path now only frees/unmaps;
> a new copy_out argument, true only from the real completion
> callback, gates the data-touching steps.
> - Fixed a second bug Sashiko's review found: areq->dst_len for
> generate_public_key() still reported ctx->key_sz << 1, which is
> wrong for P-192, where ctx->key_sz is the 32-byte P-256 hardware
> slot width but the compaction only produces 2 * curve_sz = 48
> valid bytes. Both branches are now curve_sz-based.
>
> No HiSilicon HPRE hardware was available to reproduce this. Verified
> by building the modified driver against an unmodified kernel tree,
> confirming the module compiles and links cleanly with no new
> warnings. Verified the first fix above by enumerating every path
> that reaches the clear_all label and confirming copy_out is false
> for all of them, while remaining true only on the hardware-completion
> callback path. Verified the sizing and offset arithmetic in a standalone
> test across all three supported curves (P-192, P-256, P-384): the
> compute_shared_secret() minimum (curve_sz) and generate_public_key()
> minimum (2 * key_sz) come out distinct and correct; every
> read/write offset used by both the bounce-buffer copy-out and the
> direct-buffer compaction stays inside its respective buffer; and
> the corrected generate_public_key() dst_len now matches the
> number of valid bytes the compaction produces for all three curves
> (48/64/96), rather than always reporting key_sz << 1 (64/64/96,
> wrong for P-192).
>
> AI Disclosure: An AI assistant was used to assist with the
> analysis and testing. I have verified the vulnerability, the fix, and
> the test results myself and take responsibility for this contribution,
> per Documentation/process/coding-assistants.rst.
>
> drivers/crypto/hisilicon/hpre/hpre_crypto.c | 82 +++++++++++++++++++++++------
> 1 file changed, 65 insertions(+), 17 deletions(-)

Patch applied. Thanks.
--
Email: Herbert Xu <herbert@xxxxxxxxxxxxxxxxxxx>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt