Advisory

GHSA-vh45-f885-3848

Details, part 2 of 2. The advisory

Details

Suggested fix

Seed the RNG from a CSPRNG in Node. The simplest fix in sm-crypto is to
replace the jsbn ARC4 instance with Web Crypto / crypto.randomBytes:

// src/sm2/utils.js
const nodeCrypto = (typeof require === 'function') ? require('crypto') : null;
function csrandBytes(n) {
  if (nodeCrypto) return nodeCrypto.randomBytes(n);          // Node
  if (globalThis.crypto) {                                   // Web Crypto (browser/Node ≥ 19)
    const b = new Uint8Array(n); globalThis.crypto.getRandomValues(b); return b;
  }
  throw new Error('no CSPRNG available');
}

and use it to generate the private key / ephemeral directly, or to reseed the
jsbn pool. A separate (upstream) fix belongs in jsbn to check
globalThis.crypto in addition to window.crypto.

Affected versions

  • npm sm-crypto 0.4.0 (latest, published 2026-01-20). Depends on jsbn ^1.1.0 ([email protected], whose index.js RNG is the root cause).
  • Runtime: Node.js (the primary runtime; in a browser the jsbn CSPRNG branch is taken).

Credit

Reported by the diff/ambidiff security research effort (afldl).

Sources & licences