org.sonar.l10n.py.rules.python.S3329.html Maven / Gradle / Ivy
This vulnerability exposes encrypted data to a number of 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 the mode Cipher Block Chaining (CBC), each block is used as cryptographic input for the next block. For this reason, the first block requires an
initialization vector (IV), also called a "starting variable" (SV).
If the same IV is used for multiple encryption sessions or messages, each new encryption of the same plaintext input would always produce the same
ciphertext output. This may allow an attacker to detect patterns in the ciphertext.
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, a 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 pyca
Code examples
Noncompliant code example
from cryptography.hazmat.primitives.ciphers import (
Cipher,
algorithms,
modes,
)
iv = b"exampleIV1234567"
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
cipher.encryptor() # Noncompliant
Compliant solution
In this example, the code explicitly uses a number generator that is considered strong.
from os import urandom
from cryptography.hazmat.primitives.ciphers import (
Cipher,
algorithms,
modes,
)
iv = urandom(16)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
cipher.encryptor()
How does this work?
Use unique IVs
To ensure high security, initialization vectors must meet two important criteria:
- IVs must be unique for each encryption operation.
- For CBC and CFB modes, a secure FIPS-compliant random number generator should be used to generate unpredictable IVs.
The IV does not need be secret, so the IV or information sufficient to determine the IV may be transmitted along with the ciphertext.
In the previous non-compliant example, the problem is not that the IV is hard-coded.
It is that the same IV is used for multiple encryption
attempts.
How to fix it in Cryptodome
Code examples
Noncompliant code example
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad
iv = b"exampleIV1234567"
cipher = AES.new(key, AES.MODE_CBC, iv)
cipher.encrypt(pad(data, AES.block_size)) # Noncompliant
Compliant solution
In this example, the code explicitly uses a number generator that is considered strong.
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad
iv = get_random_bytes(AES.block_size)
cipher = AES.new(key, AES.MODE_CBC, iv)
cipher.encrypt(pad(data, AES.block_size))
How does this work?
Use unique IVs
To ensure high security, initialization vectors must meet two important criteria:
- IVs must be unique for each encryption operation.
- For CBC and CFB modes, a secure FIPS-compliant random number generator should be used to generate unpredictable IVs.
The IV does not need be secret, so the IV or information sufficient to determine the IV may be transmitted along with the ciphertext.
In the previous non-compliant example, the problem is not that the IV is hard-coded.
It is that the same IV is used for multiple encryption
attempts.
Resources
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
- CWE - CWE-329 - Not Using an Unpredictable IV with CBC Mode
- CWE - CWE-780 - Use of RSA Algorithm without OAEP
- NIST, SP-800-38A - Recommendation for Block Cipher
Modes of Operation