Why is this an issue?
Encryption algorithms should use secure modes and padding schemes where appropriate to guarantee data confidentiality and integrity.
- For block cipher encryption algorithms (like AES):
- The ECB (Electronic Codebook) cipher mode doesn’t provide serious message confidentiality: under a given key any given plaintext block
always gets encrypted to the same ciphertext block. This mode should never be used.
- The CBC (Cipher Block Chaining) mode by itself provides only data confidentiality. This cipher mode is also vulnerable to padding oracle attacks when used with padding. Using CBC along with Message
Authentication Code can provide data integrity and should prevent such attacks. In practice the implementation has many pitfalls and it’s
recommended to avoid CBC with padding completely.
- The GCM (Galois Counter Mode) mode which works
internally with zero/no padding scheme, is recommended, as it is designed to provide both data authenticity (integrity) and confidentiality.
Other similar modes are CCM, CWC, EAX, IAPM and OCB.
- For RSA encryption algorithm, the recommended padding scheme is OAEP.
Noncompliant code example
AesManaged object with
insecure mode:
Dim aes4 = New AesManaged With {
.KeySize = 128,
.BlockSize = 128,
.Mode = CipherMode.ECB, ' Noncompliant
.Padding = PaddingMode.PKCS7
}
RSACryptoServiceProvider
object without OAEP padding:
Dim RSA1 = New RSACryptoServiceProvider()
Dim encryptedData = RSA1.Encrypt(dataToEncrypt, False) ' Noncompliant: OAEP Padding is not used (second parameter set to false)
Compliant solution
AES with GCM mode with bouncycastle library:
Dim blockCipher As GcmBlockCipher = New GcmBlockCipher(New AesFastEngine()) ' Compliant
blockCipher.Init(True, New AeadParameters(New KeyParameter(secretKey), 128, iv, Nothing))
AES with GCM mode with AesGcm
object:
Dim aesGcm = New AesGcm(key) ' Compliant
RSA with OAEP padding with RSACryptoServiceProvider
object:
Dim RSA2 = New RSACryptoServiceProvider()
Dim encryptedData = RSA2.Encrypt(dataToEncrypt, True) ' Compliant: OAEP Padding is used (second parameter set to true)
Resources