sm-crypto: Predictable SM2 key generation in Node.js: default RNG uses Math.random + wall clock
The npm package sm-crypto version 0.4.0 and earlier generates SM2 private keys and signing ephemeral scalars using a predictable random number generator in Node.js environments. The library relies on jsbn's SecureRandom, which attempts to seed from window.crypto.getRandomValues in browsers but falls back to non-cryptographic sources (Math.random and wall clock time) in Node.js because window is undefined. This fallback results in keys and ephemeral values that are predictable by attackers who can observe some outputs of Math.random and estimate the generation time. This vulnerability affects the default key generation path without requiring special configuration.
AI Analysis
Technical Summary
[email protected] uses jsbn's SecureRandom for SM2 key generation. In browsers, SecureRandom seeds an ARC4 stream from window.crypto.getRandomValues, providing cryptographically secure randomness. However, in Node.js, window is undefined, so the secure seeding branch is skipped. Instead, the RNG pool is seeded from Math.random (a recoverable xorshift128+ PRNG) and Date.getTime(), both of which are non-cryptographic and attacker-estimable. Consequently, SM2 private keys and signing ephemeral scalars generated by the default no-argument call path are predictable. A proof-of-concept demonstrates that pinning Math.random and the clock before loading sm-crypto results in deterministic key generation, confirming the vulnerability. This issue affects sm-crypto versions before 0.5.0 and is not mitigated by any existing fixes or configuration options.
Potential Impact
An attacker who can observe some outputs of Math.random and estimate the approximate time of key generation can predict SM2 private keys and signing ephemeral scalars generated by sm-crypto in Node.js. This compromises the confidentiality and integrity of cryptographic operations relying on these keys, enabling key recovery and signature forgery. The vulnerability affects all users of the default key generation path in sm-crypto versions prior to 0.5.0 running in Node.js environments.
Mitigation Recommendations
No official fix or patch is currently available for this vulnerability in [email protected]. Users should avoid using the default key generation in Node.js environments until a patched version is released. Monitor the vendor or repository for updates addressing this issue. Since the vulnerability arises from the fallback to non-cryptographic RNG sources, a potential mitigation is to patch or override the RNG initialization to use Node.js's globalThis.crypto.getRandomValues or another cryptographically secure RNG before loading sm-crypto, but this requires custom intervention. Patch status is not yet confirmed — check the vendor advisory for current remediation guidance.
sm-crypto: Predictable SM2 key generation in Node.js: default RNG uses Math.random + wall clock
Description
The npm package sm-crypto version 0.4.0 and earlier generates SM2 private keys and signing ephemeral scalars using a predictable random number generator in Node.js environments. The library relies on jsbn's SecureRandom, which attempts to seed from window.crypto.getRandomValues in browsers but falls back to non-cryptographic sources (Math.random and wall clock time) in Node.js because window is undefined. This fallback results in keys and ephemeral values that are predictable by attackers who can observe some outputs of Math.random and estimate the generation time. This vulnerability affects the default key generation path without requiring special configuration.
CVSS v3.1
Affected software
Run on your own infrastructure? Check whether these packages are installed with threat-finder — our free open-source scanner.
Weaknesses
AI-Powered Analysis
Machine-generated threat intelligence
Technical Analysis
[email protected] uses jsbn's SecureRandom for SM2 key generation. In browsers, SecureRandom seeds an ARC4 stream from window.crypto.getRandomValues, providing cryptographically secure randomness. However, in Node.js, window is undefined, so the secure seeding branch is skipped. Instead, the RNG pool is seeded from Math.random (a recoverable xorshift128+ PRNG) and Date.getTime(), both of which are non-cryptographic and attacker-estimable. Consequently, SM2 private keys and signing ephemeral scalars generated by the default no-argument call path are predictable. A proof-of-concept demonstrates that pinning Math.random and the clock before loading sm-crypto results in deterministic key generation, confirming the vulnerability. This issue affects sm-crypto versions before 0.5.0 and is not mitigated by any existing fixes or configuration options.
Potential Impact
An attacker who can observe some outputs of Math.random and estimate the approximate time of key generation can predict SM2 private keys and signing ephemeral scalars generated by sm-crypto in Node.js. This compromises the confidentiality and integrity of cryptographic operations relying on these keys, enabling key recovery and signature forgery. The vulnerability affects all users of the default key generation path in sm-crypto versions prior to 0.5.0 running in Node.js environments.
Mitigation Recommendations
No official fix or patch is currently available for this vulnerability in [email protected]. Users should avoid using the default key generation in Node.js environments until a patched version is released. Monitor the vendor or repository for updates addressing this issue. Since the vulnerability arises from the fallback to non-cryptographic RNG sources, a potential mitigation is to patch or override the RNG initialization to use Node.js's globalThis.crypto.getRandomValues or another cryptographically secure RNG before loading sm-crypto, but this requires custom intervention. Patch status is not yet confirmed — check the vendor advisory for current remediation guidance.
Technical Details
- Gcve Source
- db.gcve.eu
- Osv Id
- GHSA-vh45-f885-3848
- Osv Schema Version
- 1.4.0
- Aliases
- []
- Ecosystems
- ["npm"]
- Database Specific Severity
- CRITICAL
- Cvss Version
- 3.1
Threat ID: 6a65421f9c2644c7f808819c
Added to database: 07/25/2026, 23:09:19 UTC
Last enriched: 07/25/2026, 23:51:39 UTC
Last updated: 07/26/2026, 03:46:02 UTC
Views: 4
Community Reviews
0 reviewsCrowdsource mitigation strategies, share intel context, and vote on the most helpful responses. Sign in to add your voice and help keep defenders ahead.
Want to contribute mitigation steps or threat intel context? Sign in or create an account to join the community discussion.
Actions
Updates to AI analysis require Pro Console access. Upgrade inside Console → Billing.
External Links
Need more coverage?
Upgrade to Pro Console for AI refresh and higher limits.
For incident response and remediation, OffSeq services can help resolve threats faster.
Latest Threats
Check if your credentials are on the dark web
Instant breach scanning across billions of leaked records. Free tier available.