org.sonar.l10n.javascript.rules.javascript.S4426.html Maven / Gradle / Ivy
This vulnerability exposes encrypted data to attacks whose goal is to recover the plaintext.
Why is this an issue?
Encryption algorithms are essential for protecting sensitive information and ensuring secure communications in a variety of domains. They are used
for several important reasons:
- Confidentiality, privacy, and intellectual property protection
- Security during transmission or on storage devices
- Data integrity, general trust, and authentication
When selecting encryption algorithms, tools, or combinations, you should also consider two things:
- No encryption is unbreakable.
- The strength of an encryption algorithm is usually measured by the effort required to crack it within a reasonable time frame.
In today’s cryptography, the length of the key directly affects the security level of cryptographic algorithms.
Note that depending on the algorithm, the term key refers to a different mathematical property. For example:
- For RSA, the key is the product of two large prime numbers, also called the modulus.
- For AES and Elliptic Curve Cryptography (ECC), the key is only a sequence of randomly generated bytes.
- In some cases, AES keys are derived from a master key or a passphrase using a Key Derivation Function (KDF) like PBKDF2 (Password-Based Key
Derivation Function 2)
If an application uses a key that is considered short and insecure, the encrypted data is exposed to attacks aimed at getting at
the plaintext.
In general, it is best practice to expect a breach: that a user or organization with malicious intent will perform cryptographic attacks on this
data after obtaining it by other means.
What is the potential impact?
After retrieving encrypted data and performing cryptographic attacks on it on a given timeframe, attackers can recover the plaintext that
encryption was supposed to protect.
Depending on the recovered data, the impact may vary.
Below are some real-world scenarios that illustrate the potential impact of an attacker exploiting the vulnerability.
Additional attack surface
By modifying the plaintext of the encrypted message, an attacker may be able to trigger additional vulnerabilities in the code. An attacker can
further exploit a system to obtain more information.
Encrypted values are often considered trustworthy because it would not be possible for a
third party to modify them under normal circumstances.
Breach of confidentiality and privacy
When encrypted data contains personal or sensitive information, its retrieval by an attacker can lead to privacy violations, identity theft,
financial loss, reputational damage, or unauthorized access to confidential systems.
In this scenario, the company, its employees, users, and partners could be seriously affected.
The impact is twofold, as data breaches and exposure of encrypted data can undermine trust in the organization, as customers, clients and
stakeholders may lose confidence in the organization’s ability to protect their sensitive data.
Legal and compliance issues
In many industries and locations, there are legal and compliance requirements to protect sensitive data. If encrypted data is compromised and the
plaintext can be recovered, companies face legal consequences, penalties, or violations of privacy laws.
How to fix it in Node.js
Code examples
The following code examples either explicitly or implicitly generate keys. Note that there are differences in the size of the keys depending on the
algorithm.
Due to the mathematical properties of the algorithms, the security requirements for the key size vary depending on the algorithm.
For example,
a 256-bit ECC key provides about the same level of security as a 3072-bit RSA key and a 128-bit symmetric key.
Noncompliant code example
Here is an example of a private key generation with RSA:
const crypto = require('crypto');
function callback(err, pub, priv) {}
var { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 1024, // Noncompliant
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
},
callback);
Here is an example of a key generation with the Digital Signature Algorithm (DSA):
const crypto = require('crypto');
function callback(err, pub, priv) {}
var { privateKey, publicKey } = crypto.generateKeyPairSync('dsa', {
modulusLength: 1024, // Noncompliant
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
},
callback);
Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the algorithm
name:
const crypto = require('crypto');
function callback(err, pub, priv) {}
var { privateKey, publicKey } = crypto.generateKeyPair('ec', {
namedCurve: 'secp112r2', // Noncompliant
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
},
callback);
Compliant solution
Here is an example of a private key generation with RSA:
const crypto = require('crypto');
function callback(err, pub, priv) {}
var { privateKey, publicKey } = crypto.generateKeyPairSync('rsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
},
callback);
Here is an example of a key generation with the Digital Signature Algorithm (DSA):
const crypto = require('crypto');
function callback(err, pub, priv) {}
var { privateKey, publicKey } = crypto.generateKeyPairSync('dsa', {
modulusLength: 2048,
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
},
callback);
Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the algorithm
name:
const crypto = require('crypto');
function callback(err, pub, priv) {}
var { privateKey, publicKey } = crypto.generateKeyPair('ec', {
namedCurve: 'secp224k1',
publicKeyEncoding: { type: 'spki', format: 'pem' },
privateKeyEncoding: { type: 'pkcs8', format: 'pem' }
},
callback);
How does this work?
As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptographic community.
The appropriate choices are the following.
RSA (Rivest-Shamir-Adleman) and DSA (Digital Signature Algorithm)
The security of these algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.
In general, a minimum key size of 2048 bits is recommended for both.
AES (Advanced Encryption Standard)
AES supports three key sizes: 128 bits, 192 bits and 256 bits. The security of the AES algorithm is based on the computational complexity of trying
all possible keys.
A larger key size increases the number of possible keys and makes exhaustive search attacks computationally infeasible.
Therefore, a 256-bit key provides a higher level of security than a 128-bit or 192-bit key.
Currently, a minimum key size of 128 bits is recommended for AES.
Elliptic Curve Cryptography (ECC)
Elliptic curve cryptography is also used in various algorithms, such as ECDSA, ECDH, or ECMQV. The length of keys generated with elliptic curve
algorithms are mentioned directly in their names. For example, secp256k1
generates a 256-bits long private key.
Currently, a minimum key size of 224 bits is recommended for EC algorithms.
Going the extra mile
Pre-Quantum Cryptography
Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.
It is important to keep
in mind that NIST-approved digital signature schemes, key agreement, and key transport may need to be replaced with secure quantum-resistant (or
"post-quantum") counterpart.
Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.
Resources
Articles & blog posts
- Microsoft, Timing vulnerabilities with CBC-mode
symmetric decryption using padding
- Wikipedia, Padding Oracle Attack
- Wikipedia, Chosen-Ciphertext Attack
- Wikipedia, Chosen-Plaintext Attack
- Wikipedia, Semantically Secure Cryptosystems
- Wikipedia, OAEP
- Wikipedia, Galois/Counter Mode
Standards
- OWASP Top 10 2021 Category A2 - Cryptographic Failures
- OWASP Top 10 2017 Category A3 - Sensitive Data
Exposure
- OWASP Top 10 2017 Category A6 - Security
Misconfiguration
- Mobile AppSec
Verification Standard - Cryptography Requirements
- OWASP Mobile Top 10 2016 Category M5 -
Insufficient Cryptography
- NIST 800-131A - Recommendation for Transitioning the
Use of Cryptographic Algorithms and Key Lengths* MITRE, CWE-326 - Inadequate Encryption
Strength
- MITRE, CWE-327 - Use of a Broken or Risky Cryptographic Algorithm
- CERT, MSC61-J. - Do not use insecure or weak cryptographic algorithms