Re: [PATCH v2] crypto: ecdsa - Reject non-DER X9.62 signatures
From: Stefan Berger
Date: Wed Dec 02 2026 - 16:13:50 EST
On 12/1/26 6:21 AM, Florian Bauckholt via B4 Relay wrote:
From: Florian Bauckholt <florian.bauckholt@xxxxxxxx>
The x962(ecdsa-nist-*) template decodes Ecdsa-Sig-Value with the
generic BER decoder, and ecdsa_get_signature_rs() only strips one
leading zero byte without looking at the INTEGER's sign or encoding.
As a result the following encodings of a valid signature are accepted
as well:
- a SEQUENCE tag without the constructed bit (0x10 instead of 0x30),
as the decoder ignores that bit when matching a constructed tag
- non-minimal length encodings of the SEQUENCE, r or s (long form
where the short form fits, or padded with zero bytes)
- indefinite length for the SEQUENCE
- data following the SEQUENCE (the decoder does not check that the
whole buffer was consumed)
- r or s with the most significant bit set but without the leading
zero byte, i.e. a negative INTEGER read as positive
- r or s with a superfluous leading zero byte
RFC 3279 section 2.2.3 and SEC 1 section C.5 require Ecdsa-Sig-Value to
be DER-encoded, so all of these are invalid and should be rejected.
This is a potential but unlikely behaviour change for users of the x962
template (X.509 certificates, module and IMA signatures,
KEYCTL_PKEY_VERIFY): signatures that are valid ECDSA signatures but are
not DER-encoded used to verify and are now rejected with -EINVAL. The
p1363 template is not affected.
Rejecting trailing data relies on KEYCTL_PKEY_VERIFY passing the
caller's signature length. Between commit cb481e59ea6c ("KEYS: fix
overflow in keyctl_pkey_params_get_2()") and commit 94cd21592349 ("KEYS:
Fix asym key overflow in keyctl_pkey_verify()") it passed max_sig_size
instead, so trees containing the former need the latter before this
patch, or every signature shorter than the maximum is rejected.
Add a test vector for each of these encodings, taken or derived from
Wycheproof's secp256r1/SHA-256 test vectors.
Found by differential testing against other ECDSA implementations.
Fixes: 4e6602916bc6 ("crypto: ecdsa - Add support for ECDSA signature verification")
Signed-off-by: Florian Bauckholt <florian.bauckholt@xxxxxxxx>
Tested-by: Stefan Berger <stefanb@xxxxxxxxxxxxx>