MASWE-0007: Improper Encryption
MAS Requirement
The app properly encrypts sensitive data.
Mappings
MASVS V1: MSTG-CRYPTO-4, MSTG-CRYPTO-5
MASVS V2: MASVS-CRYPTO-1, MASVS-CRYPTO-2
CWE: CWE-208: Observable Timing Discrepancy, CWE-323: Reusing a Nonce, Key Pair in Encryption, CWE-325: Missing Cryptographic Step, CWE-326: Inadequate Encryption Strength, CWE-327: Use of a Broken or Risky Cryptographic Algorithm, CWE-329: Generation of Predictable IV with CBC Mode, CWE-780: Use of RSA Algorithm without OAEP
Android Risks: broken-cryptographic-algorithm
Android Core Quality: Cryptographic_Algorithms
Overview¶
This weakness occurs when encryption is implemented with broken algorithms, insecure modes or parameters, or non-cryptographic techniques, leaving the protected data recoverable or forgeable without the key.
Encryption is only as strong as its weakest component: the algorithm, the mode of operation, the padding scheme, the initialization vectors (IVs) or nonces, and the way keys are used all contribute to the actual security of the ciphertext.
Modes of Introduction¶
- Broken Algorithms: Relying on broken encryption algorithms (i.e., that are deprecated or disallowed by NIST or other standards) such as RC4, DES, or 3DES.
- Predictable or Reused Initialization Vectors (IVs): Using IVs that are hardcoded, null, predictable, or reused in modes like AES-CBC or AES-CTR, or reusing nonces or using authentication tags of insufficient length in AEAD modes like AES-GCM.
- Risky Padding: Using a padding scheme susceptible to padding oracle attacks (e.g. PKCS#7 with unauthenticated AES-CBC, or PKCS#1 v1.5 for RSA) in combination with observable padding-error signals.
- Broken Modes of Operation: Using modes that are considered broken. For example, NIST disallows AES-ECB because it is deterministic, meaning identical plaintext blocks yield identical ciphertext blocks, which leaks data patterns.
- Insufficient Key Length: Using key sizes below current recommendations for the chosen algorithm.
- Insecure or Wrong Key Usage: Reusing a single key for multiple purposes (e.g. encryption and signing) or with an unauthorized algorithm, violating key-separation principles. Per NIST.SP.800-57pt1r5, a single key should be used for only one purpose.
- Non-Cryptographic Operations: Relying on techniques such as XOR, Base64 encoding, or simple obfuscation methods for security purposes. These methods provide no actual encryption.
Impact¶
- Compromise of Sensitive Data: Attackers can decipher protected information or alter it without detection, resulting in unauthorized disclosure or manipulation of sensitive data.
- Authentication or Authorization Bypass: Attackers can forge ciphertexts, tokens, or signatures accepted by the app or its backend, resulting in unauthorized access to protected accounts, data, or functionality.
Mitigations¶
- Use Secure Encryption Modes: Choose secure, authenticated modes (e.g. approved by NIST) such as
AES/GCM/NoPadding. If AES-CBC must be used, apply Encrypt-then-MAC (e.g. append HMAC) and, for RSA, prefer OAEP over PKCS#1 v1.5. - Ensure Proper Initialization Vector Management: Generate IVs using cryptographically secure random number generators (with sufficient entropy) and ensure they are unique for every operation; never hardcode, null, or reuse them. On Android, use
GCMParameterSpec(not the legacyIvParameterSpec) for GCM. - Use Sufficiently Long Keys: Enforce sufficiently long keys such as those approved by NIST, e.g., a minimum of 128 bits for AES.
- Use Each Key for a Single Purpose: Derive or generate separate keys for encryption, authentication, and signing, and only use each key with its authorized algorithm.
- Do Not Expose Cryptographic Errors: Avoid leaking detailed padding or decryption error messages or timing differences that could serve as an oracle.
- Rely on Proper Cryptographic Primitives: Rely on well-vetted cryptographic primitives that have undergone rigorous peer review and formal validation. See Improper Verification of Cryptographic Signature for signature verification.
Knowledge¶
MASTG-KNOW-0011: Security Provider MASTG-KNOW-0012: Key Generation
Tests¶
MASTG-TEST-0309: References to Reused Initialization Vectors in Symmetric Encryption MASTG-TEST-0312: References to Explicit Security Provider in Cryptographic APIs MASTG-TEST-0308: Runtime Use of Asymmetric Key Pairs Used For Multiple Purposes MASTG-TEST-0232: Broken Symmetric Encryption Modes MASTG-TEST-0350: Runtime Use of Broken Symmetric Encryption Modes MASTG-TEST-0221: Broken Symmetric Encryption Algorithms MASTG-TEST-0310: Runtime Use of Reused Initialization Vectors in Symmetric Encryption MASTG-TEST-0307: References to Asymmetric Key Pairs Used For Multiple Purposes MASTG-TEST-0317: Broken Symmetric Encryption Modes MASTG-TEST-0210: Broken Symmetric Encryption Algorithms
Best Practices¶
MASTG-BEST-0020: Update the GMS Security Provider MASTG-BEST-0005: Use Secure Encryption Modes MASTG-BEST-0009: Use Secure Encryption Algorithms