Skip to content

tests: cover rejection of excessive 0xFF padding in DER integers - #1989

Open
amartya1523 wants to merge 1 commit into
bitcoin-core:masterfrom
amartya1523:tests-der-ff-padding
Open

amartya1523 wants to merge 1 commit into
bitcoin-core:masterfrom
amartya1523:tests-der-ff-padding

Conversation

@amartya1523

Copy link
Copy Markdown
Contributor

secp256k1_der_parse_integer rejects a negative integer whose leading 0xFF byte is redundant (X.690 8.3.2), but nothing in the test suite checks it. random_ber_signature can produce such encodings, but doesn't mark them as certainly_not_der, so run_ecdsa_der_parse stays green if the check is removed, or if either of its two conditions is dropped (I tried all three; a coverage build also shows 0 hits on that branch).

This adds a small deterministic test with the padded encoding that must be rejected, plus the two neighbouring negative encodings that must still parse (as an overflow to zero): a lone 0xFF, and 0xFF 0x7F. It follows the same pattern as the recent long-form length test (#1924).

secp256k1_der_parse_integer rejects a negative integer whose leading
0xFF byte is redundant, but no test checks this. random_ber_signature
can produce such an encoding, yet it does not mark it as
certainly_not_der, so run_ecdsa_der_parse passes even if the check is
removed or either of its conditions is dropped.

Add a padded encoding that must be rejected, along with the two
negative encodings next to it that must still be accepted: a single
0xFF byte, and a 0xFF byte followed by a byte without its top bit set.
@pinheadmz

Copy link
Copy Markdown

@amartya1523 you just opened 5 PRs in one hour. Please stop there, give us time to digest. In the meantime, please review our AI Policy and close any of the PRs you don't fully understand, or think are actually meaningful to the project, review resources are extremely limited.

@amartya1523

Copy link
Copy Markdown
Contributor Author

Thanks @pinheadmz , and sorry for the noise. Opening five PRs at once was too much and I’ve closed the other four.

I kept this one because the excessive 0xFF padding check in secp256k1_der_parse_integer had no test: when I disabled it (or dropped either of its two conditions), the whole test suite still passed. E.g. FF 80 must be rejected because 80 alone already encodes -128, while FF (-1) and FF 7F (-129) are valid and should still parse (as overflow → zero). The new test checks exactly those three cases.

I’ve read the AI policy. Going forward I’ll keep it to one PR at a time and wait for review. Happy to close this one too if it isn’t useful.

@real-or-random real-or-random left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK

Comment thread src/tests.c
Comment on lines +7558 to +7560
* A negative R or S is not rejected: secp256k1_der_parse_integer flags it as
* overflowing and, as for any overflow, sets the scalar to zero. A leading
* 0xFF byte is not allowed if the next byte has its top bit set, though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nicer to move the descriptions of the exact checked conditions above the correspondling CHECK line.

Comment thread src/tests.c
Comment on lines +7561 to +7562
* random_ber_signature can emit such padding, but does not mark those
* signatures as certainly_not_der, so run_ecdsa_der_parse would not notice if

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can drop this comment. It's hard to maintain because it creates a coupling with random_ber_signature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants