Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-CRYPTO-SEC-021 --project .About This Rule
Understanding the vulnerability and how it is detected
Detects DSA key generation using `dsa.generate_private_key()` from the `cryptography` library where the `key_size` argument is less than 2048 bits. This rule uses a `.where("key_size", lt(2048))` predicate and fires only on provably undersized keys; it does not flag 2048-bit or larger keys. DSA with small keys is vulnerable to discrete logarithm attacks: with a 1024-bit modulus an attacker can recover the private key and then forge arbitrary signatures on any message. NIST SP 800-131A formally deprecated 1024-bit DSA in 2013 and requires 2048-bit minimum. Note that DSA itself is being phased out in favour of ECDSA — even at 2048 bits, DSA requires careful nonce management (a reused nonce leaks the private key as demonstrated in Sony PlayStation 3 key extraction). For new systems prefer ECDSA with SECP256R1 or EdDSA.
How to Fix
Recommended remediation steps
- 1Use key_size=2048 as the absolute minimum; use key_size=3072 for systems expected to operate beyond 2030.
- 2Consider replacing DSA with ECDSA (SECP256R1) or EdDSA for new applications — smaller keys, faster operations, and no nonce-reuse catastrophe.
- 3If DSA must be used, ensure the per-signature nonce (k) is generated with a cryptographically secure random number generator; a reused nonce leaks the private key.
- 4Audit all DSA key material in your infrastructure and schedule rotation of any keys below 2048 bits.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
Insufficient RSA Key Size (cryptography lib)
RSA key size is less than 2048 bits. NIST minimum is 2048 bits; 3072+ recommended for new systems.
EC Key Generation Audit (cryptography lib)
Audit all EC key generation calls — verify the curve is SECP256R1, SECP384R1, or stronger. Weak curves like SECP192R1 must not be used.
Insufficient DSA Key Size (PyCryptodome)
DSA key size is less than 2048 bits in PyCryptodome. Use DSA.generate(2048) or higher.
Frequently Asked Questions
Common questions about Insufficient DSA Key Size (cryptography lib)
New feature
Get these findings posted directly on your GitHub pull requests
The Insufficient DSA Key Size (cryptography lib) rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.