@@ -51,17 +51,18 @@ <h3>Code examples</h3>
5151< h4 > Noncompliant code example</ h4 >
5252< pre data-diff-id ="11 " data-diff-type ="noncompliant ">
5353from cryptography.hazmat.primitives.ciphers import Cipher, algorithms
54- from cryptography.hazmat.backends import default_backend
5554
56- cipher = Cipher(algorithms.TripleDES(key), mode=None, backend=default_backend()) # Noncompliant
55+ cipher = Cipher(algorithms.TripleDES(key), mode=None) # Noncompliant
5756</ pre >
5857< h4 > Compliant solution</ h4 >
5958< pre data-diff-id ="11 " data-diff-type ="compliant ">
6059from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
61- from cryptography.hazmat.backends import default_backend
6260
63- cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend( ))
61+ cipher = Cipher(algorithms.AES(key), modes.CTR(nonce ))
6462</ pre >
63+ < p > In this example, the counter mode (CTR) of AES is used. This mode takes a cryptographic nonce (a < strong > n</ strong > umber used only
64+ < strong > once</ strong > ) as its initialization vector (IV). This value must never be reused, as doing so allows attackers to decrypt any message
65+ encrypted with the same key.</ p >
6566< h3 > How does this work?</ h3 >
6667< h4 > Use a secure algorithm</ h4 >
6768< p > It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
@@ -74,15 +75,15 @@ <h4>Noncompliant code example</h4>
7475< pre data-diff-id ="21 " data-diff-type ="noncompliant ">
7576from Crypto.Cipher import DES
7677
77- cipher = DES.new(key) # Noncompliant
78+ cipher = DES.new(key, DES.MODE_OFB ) # Noncompliant
7879</ pre >
7980< h4 > Compliant solution</ h4 >
80- < p > PyCrypto is deprecated, thus it is recommended to use another library like pyca .</ p >
81+ < p > PyCrypto is deprecated, thus it is recommended to use another library like PyCryptodome .</ p >
8182< pre data-diff-id ="21 " data-diff-type ="compliant ">
82- from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
83- from cryptography.hazmat.backends import default_backend
83+ from Crypto.Cipher import AES # pycryptodome
84+ from Cryptodome.Cipher import AES # pycryptodomex
8485
85- cipher = Cipher(algorithms. AES(key), modes.CBC(iv), backend=default_backend() )
86+ cipher = AES.new (key, AES.MODE_CCM )
8687</ pre >
8788< h3 > How does this work?</ h3 >
8889< h4 > Use a secure algorithm</ h4 >
@@ -99,13 +100,15 @@ <h4>Noncompliant code example</h4>
99100cipher = pyDes.des(key) # Noncompliant
100101</ pre >
101102< h4 > Compliant solution</ h4 >
102- < p > Since pyDes only provides DES, it is recommended to use another library like pyca.</ p >
103+ < p > Since pyDes only provides DES, it is recommended to use another library like < code > pyca/cryptography </ code > .</ p >
103104< pre data-diff-id ="31 " data-diff-type ="compliant ">
104105from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
105- from cryptography.hazmat.backends import default_backend
106106
107- cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend( ))
107+ cipher = Cipher(algorithms.AES(key), modes.CTR(nonce ))
108108</ pre >
109+ < p > In this example, the counter mode (CTR) of AES is used. This mode takes a cryptographic nonce (a < strong > n</ strong > umber used only
110+ < strong > once</ strong > ) as its initialization vector (IV). This value must never be reused, as doing so allows attackers to decrypt any message
111+ encrypted with the same key.</ p >
109112< h3 > How does this work?</ h3 >
110113< h4 > Use a secure algorithm</ h4 >
111114< p > It is highly recommended to use an algorithm that is currently considered secure by the cryptographic community. A common choice for such an
@@ -129,7 +132,38 @@ <h4>Compliant solution</h4>
129132ctx = ssl.create_default_context()
130133</ pre >
131134< h3 > How does this work?</ h3 >
132- < p > It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time.</ p >
135+ < p > It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time. If
136+ specific cipher suites need to be enabled or disabled, then this is also possible by adding them after the < code > DEFAULT</ code > cipher suite
137+ string.</ p >
138+ < p > For example, < code > DEFAULT:!RSA:!SHA</ code > enables all default cipher suites except those using RSA and SHA1. < code > DEFAULT:HIGH+AESGCM</ code >
139+ enables all default cipher suites, as well as all high encryption cipher suites that use AES-GCM.</ p >
140+ < p > More information about possible options can be found in the < a
141+ href ="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT "> OpenSSL documentation</ a > .</ p >
142+ < h2 > How to fix it in OpenSSL</ h2 >
143+ < h3 > Code examples</ h3 >
144+ < p > The following code contains examples of algorithms that are not considered highly resistant to cryptanalysis and thus should be avoided.</ p >
145+ < h4 > Noncompliant code example</ h4 >
146+ < pre data-diff-id ="51 " data-diff-type ="noncompliant ">
147+ from OpenSSL import SSL
148+
149+ ciphers = b"DEFAULT:RC4-SHA:RC4-MD5"
150+ ctx = SSL.Context(SSL.TLS1_3_VERSION)
151+ ctx.set_cipher_list(ciphers) # Noncompliant
152+ </ pre >
153+ < h4 > Compliant solution</ h4 >
154+ < pre data-diff-id ="51 " data-diff-type ="compliant ">
155+ from OpenSSL import SSL
156+
157+ ctx = SSL.Context(SSL.TLS1_3_VERSION)
158+ </ pre >
159+ < h3 > How does this work?</ h3 >
160+ < p > It is recommended to not override the ciphers but instead, use the secure default ciphers of the module, as they might change over time. If
161+ specific cipher suites need to be enabled or disabled, then this is also possible by adding them after the < code > DEFAULT</ code > cipher suite
162+ string.</ p >
163+ < p > For example, < code > DEFAULT:!RSA:!SHA</ code > enables all default cipher suites except those using RSA and SHA1. < code > DEFAULT:HIGH+AESGCM</ code >
164+ enables all default cipher suites, as well as all high encryption cipher suites that use AES-GCM.</ p >
165+ < p > More information about possible options can be found in the < a
166+ href ="https://www.openssl.org/docs/man1.1.1/man1/ciphers.html#CIPHER-LIST-FORMAT "> OpenSSL documentation</ a > .</ p >
133167< h2 > Resources</ h2 >
134168< h3 > Standards</ h3 >
135169< ul >
0 commit comments