Apply Mobile Cryptography
Use platform-provided cryptographic APIs with approved algorithms (AES-256-GCM, ChaCha20-Poly1305, RSA-2048+) and hardware-backed key storage — never rolling custom crypto or hardcoding keys.
Why This Is Best Practice
Adopted by: OWASP Mobile Top 10 2024 M10 (Insufficient Cryptography). OWASP MASVS MSTG-CRYPTO-1 through MSTG-CRYPTO-6 specify mobile cryptography requirements. Apple's CryptoKit and Android's Keystore API are the official platform cryptography interfaces. NIST SP 800-175B defines approved algorithms. PCI DSS v4.0 Requirement 3.5 mandates strong cryptography for stored cardholder data.
Impact: OWASP's Mobile Security Testing Guide (MSTG) consistently finds weak cryptography in the top 3 mobile app vulnerabilities during assessments. Common findings: AES-ECB mode encryption (identical blocks → visible patterns), hardcoded keys in source code (recovered via static analysis), MD5/SHA-1 for security purposes (collision-vulnerable), and custom XOR encryption (trivially reversible). Mobile app binary analysis tools (jadx, Hopper, frida) make hardcoded keys trivially recoverable.
Why best: Custom cryptography implementations (rolling your own cipher, key derivation, or protocol) are the alternative — they are routinely broken because subtle implementation errors invalidate mathematical security proofs. Platform-provided cryptographic primitives are reviewed, maintained, and hardware-accelerated.
Sources: OWASP Mobile Top 10 2024 M10; OWASP MASVS MSTG-CRYPTO; NIST SP 800-175B; Apple CryptoKit documentation; Android Keystore system documentation
Steps
iOS — use Apple CryptoKit for all cryptographic operations:
import CryptoKit
// Symmetric encryption: AES-GCM
func encrypt(data: Data, key: SymmetricKey) throws -> Data {
let sealedBox = try AES.GCM.seal(data, using: key)
return sealedBox.combined! // nonce + ciphertext + tag
}
func decrypt(combined: Data, key: SymmetricKey) throws -> Data {
let sealedBox = try AES.GCM.SealedBox(combined: combined)
return try AES.GCM.open(sealedBox, using: key)
}
// Key generation: 256-bit key
let key = SymmetricKey(size: .bits256)
// Store key in Keychain (see apply-secure-mobile-storage)
// Never hardcode or persist in UserDefaults
Android — use Android Keystore for key generation and storage:
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
fun generateKeyInKeystore(alias: String): SecretKey {
val keyGenerator = KeyGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"
)
keyGenerator.init(
KeyGenParameterSpec.Builder(alias,
KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.setKeySize(256)
.setUserAuthenticationRequired(true) // require biometrics/PIN
.build()
)
return keyGenerator.generateKey()
}
fun encrypt(plaintext: ByteArray, key: SecretKey): Pair<ByteArray, ByteArray> {
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.ENCRYPT_MODE, key)
val ciphertext = cipher.doFinal(plaintext)
return Pair(cipher.iv, ciphertext) // store IV alongside ciphertext
}
Algorithm selection guide for mobile:
| Use case |
Recommended |
Avoid |
| Symmetric encryption |
AES-256-GCM, ChaCha20-Poly1305 |
AES-ECB, AES-CBC without MAC, 3DES |
| Key exchange |
ECDH P-256 or P-384 |
RSA key exchange (performance), DH <2048 |
| Digital signatures |
ECDSA P-256, Ed25519 |
RSA <2048, SHA-1 |
| Hashing (integrity) |
SHA-256, SHA-3 |
MD5, SHA-1 |
| Password hashing |
Argon2id, bcrypt |
SHA-256(password), MD5 |
| Random numbers |
SecRandomCopyBytes (iOS), KeyGenerator (Android) |
Math.random(), java.util.Random |
Never hardcode cryptographic keys:
// BAD — key visible in binary analysis
let hardcodedKey = "0123456789abcdef0123456789abcdef"
// GOOD — generate per-device key in Secure Enclave
func getOrCreateKey() -> SecureEnclave.P256.Signing.PrivateKey {
if let existing = loadKeyFromKeychain(tag: "app.signing.key") {
return existing
}
let newKey = try! SecureEnclave.P256.Signing.PrivateKey()
saveToKeychain(key: newKey, tag: "app.signing.key")
return newKey
}
Use platform-provided TLS, not custom certificate validation:
// iOS — do NOT override certificate validation
// BAD
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge,
completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
// Accepts any certificate — MitM vulnerability
}
// GOOD — use default URLSession (validates certificates automatically)
let session = URLSession.shared
Generate random values with platform CSPRNG:
// iOS
import CryptoKit
let randomBytes = SymmetricKey(size: .bits256).withUnsafeBytes { Data($0) }
// Or using SecRandomCopyBytes
var bytes = [UInt8](repeating: 0, count: 32)
SecRandomCopyBytes(kSecRandomDefault, 32, &bytes)
// Android
import java.security.SecureRandom
val random = SecureRandom()
val bytes = ByteArray(32)
random.nextBytes(bytes)
Rules
- AES-ECB mode is never acceptable for any data longer than 16 bytes — identical input blocks produce identical ciphertext, revealing data patterns.
- IVs/nonces must be unique per encryption operation — reusing an IV with GCM mode completely breaks confidentiality.
- Keys derived from
SecRandomCopyBytes or SecureRandom are CSPRNG-generated; keys derived from Date().timeIntervalSince1970 or device IMEI are not.
- The Secure Enclave (iOS) and StrongBox (Android) cannot export keys — operations happen inside the hardware module. Use them for the highest-value keys.
Common Mistakes
- Using
AES/ECB/PKCS5Padding because it's in the Android documentation examples — older documentation used ECB; always use GCM.
- Deriving an encryption key from the user's password with SHA-256 — SHA-256 is a hash, not a KDF; use Argon2id, scrypt, or PBKDF2.
- Catching certificate validation exceptions and proceeding — "just for testing" certificates in development code often reach production.
- Storing IVs in a predictable location — IVs can be public, but must be random and stored alongside the ciphertext.
1---2name: apply-mobile-cryptography3description: Use when implementing cryptographic operations in a mobile app — encrypting data, hashing passwords, generating keys, signing data, or establishing secure communication channels between the app and backend.4source: 'OWASP Mobile Top 10 2024 M10 (owasp.org/www-project-mobile-top-10/); OWASP MASVS MSTG-CRYPTO; NIST SP 800-175B; Apple CryptoKit documentation; Android Keystore documentation'5tags: [security, owasp, mobile, cryptography, encryption, key-management, ios, android]6metadata:7 source: https://skillmd.com/skills/jeffreytse/apply-mobile-cryptography8---910# Apply Mobile Cryptography1112Use platform-provided cryptographic APIs with approved algorithms (AES-256-GCM, ChaCha20-Poly1305, RSA-2048+) and hardware-backed key storage — never rolling custom crypto or hardcoding keys.1314## Why This Is Best Practice1516**Adopted by:** OWASP Mobile Top 10 2024 M10 (Insufficient Cryptography). OWASP MASVS MSTG-CRYPTO-1 through MSTG-CRYPTO-6 specify mobile cryptography requirements. Apple's CryptoKit and Android's Keystore API are the official platform cryptography interfaces. NIST SP 800-175B defines approved algorithms. PCI DSS v4.0 Requirement 3.5 mandates strong cryptography for stored cardholder data.17**Impact:** OWASP's Mobile Security Testing Guide (MSTG) consistently finds weak cryptography in the top 3 mobile app vulnerabilities during assessments. Common findings: AES-ECB mode encryption (identical blocks → visible patterns), hardcoded keys in source code (recovered via static analysis), MD5/SHA-1 for security purposes (collision-vulnerable), and custom XOR encryption (trivially reversible). Mobile app binary analysis tools (jadx, Hopper, frida) make hardcoded keys trivially recoverable.18**Why best:** Custom cryptography implementations (rolling your own cipher, key derivation, or protocol) are the alternative — they are routinely broken because subtle implementation errors invalidate mathematical security proofs. Platform-provided cryptographic primitives are reviewed, maintained, and hardware-accelerated.1920Sources: OWASP Mobile Top 10 2024 M10; OWASP MASVS MSTG-CRYPTO; NIST SP 800-175B; Apple CryptoKit documentation; Android Keystore system documentation2122## Steps23241. **iOS — use Apple CryptoKit for all cryptographic operations**:2526 ```swift27 import CryptoKit2829 // Symmetric encryption: AES-GCM30 func encrypt(data: Data, key: SymmetricKey) throws -> Data {31 let sealedBox = try AES.GCM.seal(data, using: key)32 return sealedBox.combined! // nonce + ciphertext + tag33 }3435 func decrypt(combined: Data, key: SymmetricKey) throws -> Data {36 let sealedBox = try AES.GCM.SealedBox(combined: combined)37 return try AES.GCM.open(sealedBox, using: key)38 }3940 // Key generation: 256-bit key41 let key = SymmetricKey(size: .bits256)4243 // Store key in Keychain (see apply-secure-mobile-storage)44 // Never hardcode or persist in UserDefaults45 ```46472. **Android — use Android Keystore for key generation and storage**:4849 ```kotlin50 import android.security.keystore.KeyGenParameterSpec51 import android.security.keystore.KeyProperties52 import javax.crypto.Cipher53 import javax.crypto.KeyGenerator54 import javax.crypto.SecretKey5556 fun generateKeyInKeystore(alias: String): SecretKey {57 val keyGenerator = KeyGenerator.getInstance(58 KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore"59 )60 keyGenerator.init(61 KeyGenParameterSpec.Builder(alias,62 KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)63 .setBlockModes(KeyProperties.BLOCK_MODE_GCM)64 .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)65 .setKeySize(256)66 .setUserAuthenticationRequired(true) // require biometrics/PIN67 .build()68 )69 return keyGenerator.generateKey()70 }7172 fun encrypt(plaintext: ByteArray, key: SecretKey): Pair<ByteArray, ByteArray> {73 val cipher = Cipher.getInstance("AES/GCM/NoPadding")74 cipher.init(Cipher.ENCRYPT_MODE, key)75 val ciphertext = cipher.doFinal(plaintext)76 return Pair(cipher.iv, ciphertext) // store IV alongside ciphertext77 }78 ```79803. **Algorithm selection guide for mobile**:8182 | Use case | Recommended | Avoid |83 |---|---|---|84 | Symmetric encryption | AES-256-GCM, ChaCha20-Poly1305 | AES-ECB, AES-CBC without MAC, 3DES |85 | Key exchange | ECDH P-256 or P-384 | RSA key exchange (performance), DH <2048 |86 | Digital signatures | ECDSA P-256, Ed25519 | RSA <2048, SHA-1 |87 | Hashing (integrity) | SHA-256, SHA-3 | MD5, SHA-1 |88 | Password hashing | Argon2id, bcrypt | SHA-256(password), MD5 |89 | Random numbers | `SecRandomCopyBytes` (iOS), `KeyGenerator` (Android) | `Math.random()`, `java.util.Random` |90914. **Never hardcode cryptographic keys**:9293 ```swift94 // BAD — key visible in binary analysis95 let hardcodedKey = "0123456789abcdef0123456789abcdef"9697 // GOOD — generate per-device key in Secure Enclave98 func getOrCreateKey() -> SecureEnclave.P256.Signing.PrivateKey {99 if let existing = loadKeyFromKeychain(tag: "app.signing.key") {100 return existing101 }102 let newKey = try! SecureEnclave.P256.Signing.PrivateKey()103 saveToKeychain(key: newKey, tag: "app.signing.key")104 return newKey105 }106 ```1071085. **Use platform-provided TLS, not custom certificate validation**:109110 ```swift111 // iOS — do NOT override certificate validation112 // BAD113 func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge,114 completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {115 completionHandler(.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))116 // Accepts any certificate — MitM vulnerability117 }118119 // GOOD — use default URLSession (validates certificates automatically)120 let session = URLSession.shared121 ```1221236. **Generate random values with platform CSPRNG**:124125 ```swift126 // iOS127 import CryptoKit128 let randomBytes = SymmetricKey(size: .bits256).withUnsafeBytes { Data($0) }129130 // Or using SecRandomCopyBytes131 var bytes = [UInt8](repeating: 0, count: 32)132 SecRandomCopyBytes(kSecRandomDefault, 32, &bytes)133 ```134135 ```kotlin136 // Android137 import java.security.SecureRandom138 val random = SecureRandom()139 val bytes = ByteArray(32)140 random.nextBytes(bytes)141 ```142143## Rules144145- AES-ECB mode is never acceptable for any data longer than 16 bytes — identical input blocks produce identical ciphertext, revealing data patterns.146- IVs/nonces must be unique per encryption operation — reusing an IV with GCM mode completely breaks confidentiality.147- Keys derived from `SecRandomCopyBytes` or `SecureRandom` are CSPRNG-generated; keys derived from `Date().timeIntervalSince1970` or device IMEI are not.148- The Secure Enclave (iOS) and StrongBox (Android) cannot export keys — operations happen inside the hardware module. Use them for the highest-value keys.149150## Common Mistakes151152- **Using `AES/ECB/PKCS5Padding` because it's in the Android documentation examples** — older documentation used ECB; always use GCM.153- **Deriving an encryption key from the user's password with SHA-256** — SHA-256 is a hash, not a KDF; use Argon2id, scrypt, or PBKDF2.154- **Catching certificate validation exceptions and proceeding** — "just for testing" certificates in development code often reach production.155- **Storing IVs in a predictable location** — IVs can be public, but must be random and stored alongside the ciphertext.