Repository navigation
Conversation
When converting a generated ed25519 key pair, every leading zero byte
was stripped from the public key's BIT STRING contents. Only the first
byte (the count of unused bits) should be removed: the key itself can
start with zero bytes, which happens for about 1 in 256 keys.
For those keys a 31 byte public key was written, producing private and
public key files that neither parseKey() nor OpenSSH can read
("Malformed OpenSSH private key").
Co-Authored-By: Claude Fable 5.2 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
utils.generateKeyPair('ed25519')/generateKeyPairSync('ed25519')occasionally return a key pair that cannot be read back, either byutils.parseKey()or by OpenSSH.Reproduction
parseKey()fails withMalformed OpenSSH private key(orMalformed OpenSSH public keyfor the public half), andssh-keygen -y -frejects the same private key file.Cause
When converting the generated key pair, the public key is read from the SPKI
BIT STRINGand then all leading zero bytes are stripped:The first byte of the
BIT STRINGcontents is the number of unused bits (always0x00here), and that is the only byte that should be removed. An ed25519 public key is 32 arbitrary bytes, so roughly 1 in 256 keys starts with0x00, and for those the loop removes part of the key as well. The result is a 31 byte public key in both the public key blob and the private key file.The same loop exists in the ECDSA branch, but it is harmless there because the uncompressed point always starts with
0x04. I left it untouched to keep this change focused.Fix
Remove exactly one byte, and reject anything that is not the expected 1 + 32 bytes.
Test
Added a case to
test/test-keygen.jsthat generates 4000 ed25519 key pairs, checks that every public key is 32 bytes and round-trips throughparseKey(), and asserts that at least one of them had a leading zero byte (so the case is known to have been exercised; the chance of not seeing one in 4000 keys is about 1 in 6 million). It fails on the currentmasterand passes with this change.🤖 Generated with Claude Code