Skip to content

keygen: fix ed25519 public keys that start with a zero byte - #1533

Open
dmnlk wants to merge 1 commit into
mscdex:masterfrom
dmnlk:fix-ed25519-keygen-leading-zero
Open

dmnlk wants to merge 1 commit into
mscdex:masterfrom
dmnlk:fix-ed25519-keygen-leading-zero

Conversation

@dmnlk

@dmnlk dmnlk commented Nov 25, 2026

Copy link
Copy Markdown

utils.generateKeyPair('ed25519') / generateKeyPairSync('ed25519') occasionally return a key pair that cannot be read back, either by utils.parseKey() or by OpenSSH.

Reproduction

const { utils: { generateKeyPairSync, parseKey } } = require('ssh2');

let bad = 0;
for (let i = 0; i < 4000; ++i) {
  const keys = generateKeyPairSync('ed25519');
  if (parseKey(keys.private) instanceof Error)
    ++bad;
}
console.log(bad); // ~15, i.e. about 1 in 256

parseKey() fails with Malformed OpenSSH private key (or Malformed OpenSSH public key for the public half), and ssh-keygen -y -f rejects the same private key file.

Cause

When converting the generated key pair, the public key is read from the SPKI BIT STRING and then all leading zero bytes are stripped:

let pubBin = reader.readString(Ber.BitString, true);
{
  // Remove leading zero bytes
  let i = 0;
  for (; i < pubBin.length && pubBin[i] === 0x00; ++i);
  if (i > 0)
    pubBin = pubBin.slice(i);
}

The first byte of the BIT STRING contents is the number of unused bits (always 0x00 here), 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 with 0x00, 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.js that generates 4000 ed25519 key pairs, checks that every public key is 32 bytes and round-trips through parseKey(), 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 current master and passes with this change.

🤖 Generated with Claude Code

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]>
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.

1 participant