Predictable Key Generation Vulnerability in sm-crypto Package
Summary
GHSA-vh45-f885-3848: sm-crypto: Predictable SM2 key generation in Node.js: default RNG uses Math.random + wall clock. Affects sm-crypto versions < 0.5.0. Fixed in 0.5.0. How to fix ↓
Check if this vulnerability affects your application
Upload a manifest or SBOM and we'll show whether you're affected, plus every other risky package in the same scan.
No signup required · Results in about 60 seconds
Background
The sm-crypto package is an npm library designed for cryptographic operations, specifically implementing the SM2 algorithm for public key cryptography. It is widely used in applications requiring secure key generation and signing. However, a critical vulnerability exists in its key generation process when used in Node.js environments, where it relies on non-cryptographic sources for randomness, making it susceptible to attacks.
Vulnerability Detail
The vulnerability arises from the sm-crypto package's use of a predictable random number generator (RNG) for generating SM2 private keys and signing ephemeral scalars. In Node.js, the library defaults to using Math.random() and the current time (Date.now()) to seed its RNG, which is non-cryptographic and can be estimated by an attacker. This predictability allows an attacker who can observe a few outputs of Math.random() and estimate the generation time to recover private keys and forge signatures, leading to severe security implications.
How To Fix
Latest published version: 0.5.7 as of 23 Oct 2026
npm install sm-crypto@0.5.7
Upgrading to it may include breaking changes; the version below is the minimum fixed release for this vulnerability.
If you need the minimum change
0.5.0
This is the first release that contains the fix.
npm install sm-crypto@0.5.0
Tip — Not sure which version your app runs? Vulert can match this fix to it. Scan free →
Workaround
As a temporary workaround, you can implement your own secure RNG or use Node.js's built-in crypto module to generate secure random bytes. Replace the RNG initialization in sm-crypto with the following code:
const crypto = require('crypto');
function csrandBytes(n) {
return crypto.randomBytes(n);
}
Conclusion
The sm-crypto package contains a critical vulnerability due to its reliance on predictable RNG for key generation in Node.js. It is essential to update to the latest version to ensure cryptographic security. Users are encouraged to utilize tools like Vulert to monitor for vulnerabilities in their applications.
See every vulnerability recorded for sm-crypto →
FAQ
The CVSS score of this vulnerability is 9.1, indicating a critical severity level.
You can use Vulert, which monitors and alerts for open-source vulnerabilities in real-time. You can check your application using the Vulert playground without any signup.
The consequences include private key recovery and signature forgery, which can lead to unauthorized access and data breaches.
Yes, a patch is available in version 0.5.0 of the sm-crypto package. It is recommended to update to this version.
If immediate updating is not possible, consider implementing a secure RNG as a workaround to mitigate the risk until you can upgrade.
References
Related vulnerabilities
Recent vulnerabilities recorded for sm-crypto.