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 checkglobalThis.crypto in addition to window.crypto.
Affected versions
- npm
sm-crypto0.4.0 (latest, published 2026-01-20). Depends onjsbn ^1.1.0([email protected], whoseindex.jsRNG 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).