Description
Reusing an IV or nonce with the same key can reveal repeated plaintext prefixes in CBC and compromise the confidentiality of different messages in CTR and GCM. In GCM, reuse also weakens authentication-tag protection.
Potential impact
- Exposure of repeated plaintext patterns
- Increased opportunity for ciphertext analysis
- Greater risk of data recovery with the same key and weakened GCM authentication
Remediation
For CBC, generate a fresh, unpredictable IV with a cryptographically secure random generator. For CTR or GCM, use a unique nonce for each message under the same key. IVs and nonces can accompany the ciphertext. Prefer an API that generates the nonce, such as CryptoKit's AES-GCM.
Examples
Before
swift
let aes = try AES(
key: key,
blockMode: CBC(iv: Array<UInt8>(repeating: 0, count: AES.blockSize)))
After
swift
import Foundation
import CryptoKit
func encryptWithFreshNonce(
data: Data,
key: SymmetricKey
) throws -> AES.GCM.SealedBox {
return try AES.GCM.seal(data, using: key)
}
Calling AES.GCM.seal without a nonce makes CryptoKit generate a fresh random one for this message.