Skip to content

ssh-key: support for undersized ECDSA/P-521 keys #351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 19, 2025

Conversation

zaczkows
Copy link
Contributor

Comment on lines 52 to 56
if len < SIZE {
reader.read(&mut bytes[0..len])?;
} else {
reader.read(&mut bytes)?;
}
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this could just be:

Suggested change
if len < SIZE {
reader.read(&mut bytes[0..len])?;
} else {
reader.read(&mut bytes)?;
}
reader.read(&mut bytes[..len])?;

...but also it would be good to also check that len <= SIZE? (unless that check is happening elsewhere)

It would also be good to enforce a minimum size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what I'm trying to check and how much smaller the key can be.

@zaczkows zaczkows marked this pull request as ready for review April 18, 2025 18:43
@zaczkows zaczkows changed the title PoC: decode ecdsa key when encoded key size is smaller than expected … Decode ecdsa P-521 key when encoded key size is smaller than 66 bytes Apr 18, 2025
@zaczkows zaczkows changed the title Decode ecdsa P-521 key when encoded key size is smaller than 66 bytes Decode ecdsa P-521 key when encoded keys size is smaller than 66 bytes Apr 18, 2025
@tarcieri tarcieri changed the title Decode ecdsa P-521 key when encoded keys size is smaller than 66 bytes ssh-key: support for undersized P-521 keys Apr 19, 2025
@tarcieri tarcieri changed the title ssh-key: support for undersized P-521 keys ssh-key: support for undersized ECDSA/P-521 keys Apr 19, 2025
@tarcieri tarcieri merged commit bf080c9 into RustCrypto:master Apr 19, 2025
14 checks passed
tarcieri added a commit that referenced this pull request Apr 19, 2025
tarcieri added a commit that referenced this pull request Apr 19, 2025
tarcieri added a commit that referenced this pull request Apr 19, 2025
// https://stackoverflow.com/questions/50002149/why-p-521-public-key-x-y-some-time-is-65-bytes-some-time-is-66-bytes
// although lower keys than 64 are vanishingly possible, but lets stop here
if len > 63 {
reader.read(&mut bytes[..core::cmp::min(len, SIZE)])?;
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, upon further reflection this seems wrong: the key is supposed to be big endian, so the array should contain leading zeros, not trailing zeros, in the event the input document's key is too short.

It would also be good to add a test that the key is decoded correctly.

I'll try to take care of this in #356

tarcieri added a commit that referenced this pull request Apr 19, 2025
Followup to #351

Also includes a fix and test to ensure that short keys zero-pad MSB
rather than LSB, since they're encoded as big endian.
tarcieri added a commit that referenced this pull request Apr 19, 2025
Followup to #351

Also includes a fix and test to ensure that short keys zero-pad MSB
rather than LSB, since they're encoded as big endian.
tarcieri added a commit that referenced this pull request Apr 19, 2025
Followup to #351

Also includes a fix and test to ensure that short keys zero-pad MSB
rather than LSB, since they're encoded as big endian.
@zaczkows zaczkows deleted the ecdsa_fix branch April 26, 2025 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to process python's paramiko ecdsa key
2 participants