== Why is this an issue?

Encryption algorithms can be used with various modes. Some combinations are not secured:


* Electronic Codebook (ECB) mode: Under a given key, any given plaintext block always gets encrypted to the same ciphertext block. Thus, it does not hide data patterns well. In some senses, it doesn't provide serious message confidentiality, and it is not recommended for use in cryptographic protocols at all.
* Cipher Block Chaining (CBC) with PKCS#5 padding (or PKCS#7) is susceptible to padding oracle attacks. CBC + PKCS#7 can be used if combined with an authenticity check (HMAC-SHA256 for example) on the cipher text.

In both cases, Galois/Counter Mode (GCM) with no padding should be preferred. As the .NET framework doesn't provide this natively, the use of a certified third party lib is recommended. 


This rule raises an issue when any of the following CipherMode is detected: ECB, CBC, OFB, CFB, CTS.

=== Noncompliant code example

[source,csharp]
----
AesManaged aes = new AesManaged
{
  KeySize = 128,
  BlockSize = 128,
  Mode = CipherMode.OFB, // Noncompliant
  Padding = PaddingMode.PKCS7
};
----

== Resources

* OWASP - https://owasp.org/www-project-top-ten/2017/A6_2017-Security_Misconfiguration[Top 10 2017 Category A6 - Security Misconfiguration]
* CWE - https://cwe.mitre.org/data/definitions/327[CWE-327 - Use of a Broken or Risky Cryptographic Algorithm]
* https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf[Recommendation for Block Cipher Modes of Operation]
* Derived from FindSecBugs rule https://find-sec-bugs.github.io/bugs.htm#ECB_MODE[ECB_MODE]
* Derived from FindSecBugs rule https://find-sec-bugs.github.io/bugs.htm#PADDING_ORACLE[PADDING_ORACLE]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

=== Message

Use a certified third party lib implementing Galois/Counter Mode (GCM) instead.


=== Highlighting

First: new AesManaged

Second (optional): where the CipherMode is set


'''
== Comments And Links
(visible only on this page)

=== on 5 Apr 2018, 10:23:00 Alexandre Gigleux wrote:
Reference: \https://security.stackexchange.com/questions/52665/which-is-the-best-cipher-mode-and-padding-mode-for-aes-encryption

=== on 12 Apr 2018, 13:51:05 Alexandre Gigleux wrote:
Reference: \http://www.bouncycastle.org/csharp/

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
