@@ -71,15 +71,15 @@ <h4>Noncompliant code example</h4>
71
71
private_key = dsa.generate_private_key(key_size = 1024, backend = backend) # Noncompliant
72
72
public_key = private_key.public_key()
73
73
</ pre >
74
- < p > Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the algorithm
74
+ < p > Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the elliptic curve
75
75
name:</ p >
76
- < pre data-diff-id ="4 " data-diff-type ="noncompliant ">
76
+ < pre data-diff-id ="3 " data-diff-type ="noncompliant ">
77
77
from cryptography.hazmat.primitives.asymmetric import ec
78
78
from cryptography.hazmat.backends import default_backend
79
79
80
80
backend = default_backend()
81
81
82
- private_key = ec.generate_private_key(curve=ec.SECT163R2, backend=backend) # Noncompliant
82
+ private_key = ec.generate_private_key(curve=ec.SECT163R2() , backend=backend) # Noncompliant
83
83
public_key = private_key.public_key()
84
84
</ pre >
85
85
< h4 > Compliant solution</ h4 >
@@ -89,7 +89,7 @@ <h4>Compliant solution</h4>
89
89
90
90
backend = default_backend()
91
91
92
- private_key = rsa.generate_private_key(key_size = 2048 , backend = backend)
92
+ private_key = rsa.generate_private_key(key_size = 3072 , backend = backend)
93
93
public_key = private_key.public_key()
94
94
</ pre >
95
95
< pre data-diff-id ="2 " data-diff-type ="compliant ">
@@ -98,33 +98,163 @@ <h4>Compliant solution</h4>
98
98
99
99
backend = default_backend()
100
100
101
- private_key = dsa.generate_private_key(key_size = 2048 , backend = backend)
101
+ private_key = dsa.generate_private_key(key_size = 3072 , backend = backend)
102
102
public_key = private_key.public_key()
103
103
</ pre >
104
- < pre data-diff-id ="4 " data-diff-type ="compliant ">
104
+ < pre data-diff-id ="3 " data-diff-type ="compliant ">
105
105
from cryptography.hazmat.primitives.asymmetric import ec
106
106
from cryptography.hazmat.backends import default_backend
107
107
108
108
backend = default_backend()
109
109
110
- private_key = ec.generate_private_key(curve=ec.SECT409R1 , backend=backend)
110
+ private_key = ec.generate_private_key(curve=ec.SECP521R1() , backend=backend)
111
111
public_key = private_key.public_key()
112
112
</ pre >
113
113
< h3 > How does this work?</ h3 >
114
- < p > As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptographic community.</ p >
114
+ < p > As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptography community.</ p >
115
115
< p > The appropriate choices are the following.</ p >
116
116
< h4 > RSA (Rivest-Shamir-Adleman) and DSA (Digital Signature Algorithm)</ h4 >
117
117
< p > The security of these algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.</ p >
118
- < p > In general, a minimum key size of < strong > 2048</ strong > bits is recommended for both.</ p >
118
+ < p > In general, a minimum key size of < strong > 2048</ strong > bits is recommended for both. It provides 112 bits of security. A key length of
119
+ < strong > 3072</ strong > or < strong > 4092</ strong > should be preferred when possible.</ p >
119
120
< h4 > AES (Advanced Encryption Standard)</ h4 >
120
121
< p > AES supports three key sizes: 128 bits, 192 bits and 256 bits. The security of the AES algorithm is based on the computational complexity of trying
121
122
all possible keys.< br > A larger key size increases the number of possible keys and makes exhaustive search attacks computationally infeasible.
122
123
Therefore, a 256-bit key provides a higher level of security than a 128-bit or 192-bit key.</ p >
123
124
< p > Currently, a minimum key size of < strong > 128 bits</ strong > is recommended for AES.</ p >
124
125
< h4 > Elliptic Curve Cryptography (ECC)</ h4 >
125
126
< p > Elliptic curve cryptography is also used in various algorithms, such as ECDSA, ECDH, or ECMQV. The length of keys generated with elliptic curve
126
- algorithms are mentioned directly in their names. For example, < code > secp256k1</ code > generates a 256-bits long private key.</ p >
127
- < p > Currently, a minimum key size of < strong > 224 bits</ strong > is recommended for EC algorithms.</ p >
127
+ algorithms is mentioned directly in their names. For example, < code > secp256k1</ code > generates a 256-bits long private key.</ p >
128
+ < p > Currently, a minimum key size of < strong > 224 bits</ strong > is recommended for EC-based algorithms.</ p >
129
+ < p > Additionally, some curves that theoretically provide sufficiently long keys are still discouraged. This can be because of a flaw in the curve
130
+ parameters, a bad overall design, or poor performance. It is generally advised to use a NIST-approved elliptic curve wherever possible. Such curves
131
+ currently include:</ p >
132
+ < ul >
133
+ < li > NIST P curves with a size of at least 224 bits, e.g. secp256r1. </ li >
134
+ < li > Curve25519, generally known as ed25519 or x25519 depending on its application. </ li >
135
+ < li > Curve448. </ li >
136
+ < li > Brainpool curves with a size of at least 224 bits, e.g. brainpoolP224r1 </ li >
137
+ </ ul >
138
+ < h3 > Going the extra mile</ h3 >
139
+ < h4 > Pre-Quantum Cryptography</ h4 >
140
+ < p > Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.< br > It is important to keep
141
+ in mind that NIST-approved digital signature schemes, key agreement, and key transport may need to be replaced with secure quantum-resistant (or
142
+ "post-quantum") counterpart.</ p >
143
+ < p > Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.</ p >
144
+ < p > < a href ="https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation "> Learn more here</ a > .</ p >
145
+ < h2 > How to fix it in Cryptodome</ h2 >
146
+ < h3 > Code examples</ h3 >
147
+ < p > The following code examples either explicitly or implicitly generate keys. Note that there are differences in the size of the keys depending on the
148
+ algorithm.</ p >
149
+ < p > Due to the mathematical properties of the algorithms, the security requirements for the key size vary depending on the algorithm.< br > For example,
150
+ a 256-bit ECC key provides about the same level of security as a 3072-bit RSA key and a 128-bit symmetric key.</ p >
151
+ < h4 > Noncompliant code example</ h4 >
152
+ < p > Here is an example of a private key generation with RSA:</ p >
153
+ < pre data-diff-id ="6 " data-diff-type ="noncompliant ">
154
+ from Crypto.PublicKey import RSA
155
+
156
+ key_rsa1024 = RSA.generate(1024) # Noncompliant
157
+ </ pre >
158
+ < p > Here is an example of a key generation with the Digital Signature Algorithm (DSA):</ p >
159
+ < pre data-diff-id ="7 " data-diff-type ="noncompliant ">
160
+ from Crypto.PublicKey import DSA
161
+
162
+ key_dsa1024 = DSA.generate(1024) # Noncompliant
163
+ </ pre >
164
+ < p > Here is an example of an Elliptic Curve (EC) initialization. It implicitly generates a private key whose size is indicated in the elliptic curve
165
+ name:</ p >
166
+ < pre data-diff-id ="8 " data-diff-type ="noncompliant ">
167
+ from Crypto.PublicKey import DSA
168
+
169
+ key_p192 = ECC.generate(curve="secp192r1") # Noncompliant
170
+ </ pre >
171
+ < h4 > Compliant solution</ h4 >
172
+ < pre data-diff-id ="6 " data-diff-type ="compliant ">
173
+ from Crypto.PublicKey import RSA
174
+
175
+ key_rsa1024 = RSA.generate(3072)
176
+ </ pre >
177
+ < pre data-diff-id ="7 " data-diff-type ="compliant ">
178
+ from Crypto.PublicKey import DSA
179
+
180
+ key_dsa1024 = DSA.generate(3072)
181
+ </ pre >
182
+ < pre data-diff-id ="8 " data-diff-type ="compliant ">
183
+ from Crypto.PublicKey import DSA
184
+
185
+ key_ed25519 = ECC.generate(curve="ed25519")
186
+ </ pre >
187
+ < h3 > How does this work?</ h3 >
188
+ < p > As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptography community.</ p >
189
+ < p > The appropriate choices are the following.</ p >
190
+ < h4 > RSA (Rivest-Shamir-Adleman) and DSA (Digital Signature Algorithm)</ h4 >
191
+ < p > The security of these algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.</ p >
192
+ < p > In general, a minimum key size of < strong > 2048</ strong > bits is recommended for both. It provides 112 bits of security. A key length of
193
+ < strong > 3072</ strong > or < strong > 4092</ strong > should be preferred when possible.</ p >
194
+ < h4 > AES (Advanced Encryption Standard)</ h4 >
195
+ < p > AES supports three key sizes: 128 bits, 192 bits and 256 bits. The security of the AES algorithm is based on the computational complexity of trying
196
+ all possible keys.< br > A larger key size increases the number of possible keys and makes exhaustive search attacks computationally infeasible.
197
+ Therefore, a 256-bit key provides a higher level of security than a 128-bit or 192-bit key.</ p >
198
+ < p > Currently, a minimum key size of < strong > 128 bits</ strong > is recommended for AES.</ p >
199
+ < h4 > Elliptic Curve Cryptography (ECC)</ h4 >
200
+ < p > Elliptic curve cryptography is also used in various algorithms, such as ECDSA, ECDH, or ECMQV. The length of keys generated with elliptic curve
201
+ algorithms is mentioned directly in their names. For example, < code > secp256k1</ code > generates a 256-bits long private key.</ p >
202
+ < p > Currently, a minimum key size of < strong > 224 bits</ strong > is recommended for EC-based algorithms.</ p >
203
+ < p > Additionally, some curves that theoretically provide sufficiently long keys are still discouraged. This can be because of a flaw in the curve
204
+ parameters, a bad overall design, or poor performance. It is generally advised to use a NIST-approved elliptic curve wherever possible. Such curves
205
+ currently include:</ p >
206
+ < ul >
207
+ < li > NIST P curves with a size of at least 224 bits, e.g. secp256r1. </ li >
208
+ < li > Curve25519, generally known as ed25519 or x25519 depending on its application. </ li >
209
+ < li > Curve448. </ li >
210
+ < li > Brainpool curves with a size of at least 224 bits, e.g. brainpoolP224r1 </ li >
211
+ </ ul >
212
+ < h3 > Going the extra mile</ h3 >
213
+ < h4 > Pre-Quantum Cryptography</ h4 >
214
+ < p > Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.< br > It is important to keep
215
+ in mind that NIST-approved digital signature schemes, key agreement, and key transport may need to be replaced with secure quantum-resistant (or
216
+ "post-quantum") counterpart.</ p >
217
+ < p > Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.</ p >
218
+ < p > < a href ="https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation "> Learn more here</ a > .</ p >
219
+ < h2 > How to fix it in pyOpenSSL</ h2 >
220
+ < h3 > Code examples</ h3 >
221
+ < p > The following code examples either explicitly or implicitly generate keys. Note that there are differences in the size of the keys depending on the
222
+ algorithm.</ p >
223
+ < p > Due to the mathematical properties of the algorithms, the security requirements for the key size vary depending on the algorithm.< br > For example,
224
+ a 256-bit ECC key provides about the same level of security as a 3072-bit RSA key and a 128-bit symmetric key.</ p >
225
+ < h4 > Noncompliant code example</ h4 >
226
+ < p > Here is an example of a private key generation with RSA:</ p >
227
+ < pre data-diff-id ="4 " data-diff-type ="noncompliant ">
228
+ from OpenSSL.crypto import PKey, TYPE_RSA
229
+
230
+ key_rsa1024 = PKey()
231
+ key_rsa1024.generate_key(type=TYPE_RSA, bits=1024) # Noncompliant
232
+ </ pre >
233
+ < p > Here is an example of a key generation with the Digital Signature Algorithm (DSA):</ p >
234
+ < pre data-diff-id ="5 " data-diff-type ="noncompliant ">
235
+ from OpenSSL.crypto import PKey, TYPE_DSA
236
+
237
+ key_dsa1024 = PKey()
238
+ key_dsa1024.generate_key(type=TYPE_DSA, bits=1024) # Noncompliant
239
+ </ pre >
240
+ < h4 > Compliant solution</ h4 >
241
+ < pre data-diff-id ="4 " data-diff-type ="compliant ">
242
+ from OpenSSL.crypto import PKey, TYPE_RSA
243
+
244
+ key_rsa1024 = PKey()
245
+ key_rsa1024.generate_key(type=TYPE_RSA, bits=3072)
246
+ </ pre >
247
+ < pre data-diff-id ="5 " data-diff-type ="compliant ">
248
+ from OpenSSL.crypto import PKey, TYPE_DSA
249
+
250
+ key_dsa1024 = PKey()
251
+ key_dsa1024.generate_key(type=TYPE_DSA, bits=3072)
252
+ </ pre >
253
+ < h3 > How does this work?</ h3 >
254
+ < p > As a rule of thumb, use the cryptographic algorithms and mechanisms that are considered strong by the cryptography community.</ p >
255
+ < p > The security of the RSA and DSA algorithms depends on the difficulty of attacks attempting to solve their underlying mathematical problem.</ p >
256
+ < p > In general, a minimum key size of < strong > 2048</ strong > bits is recommended for both. It provides 112 bits of security. A key length of
257
+ < strong > 3072</ strong > or < strong > 4096</ strong > should be preferred when possible.</ p >
128
258
< h3 > Going the extra mile</ h3 >
129
259
< h4 > Pre-Quantum Cryptography</ h4 >
130
260
< p > Encrypted data and communications recorded today could be decrypted in the future by an attack from a quantum computer.< br > It is important to keep
@@ -133,6 +263,15 @@ <h4>Pre-Quantum Cryptography</h4>
133
263
< p > Thus, if data is to remain secure beyond 2030, proactive measures should be taken now to ensure its safety.</ p >
134
264
< p > < a href ="https://www.enisa.europa.eu/publications/post-quantum-cryptography-current-state-and-quantum-mitigation "> Learn more here</ a > .</ p >
135
265
< h2 > Resources</ h2 >
266
+ < ul >
267
+ < li > Documentation
268
+ < ul >
269
+ < li > NIST Documentation - < a href ="https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-186.pdf "> NIST SP 800-186: Recommendations
270
+ for Discrete Logarithm-based Cryptography: Elliptic Curve Domain Parameters</ a > </ li >
271
+ < li > IETF - < a href ="https://datatracker.ietf.org/doc/html/rfc5639 "> rfc5639: Elliptic Curve Cryptography (ECC) Brainpool Standard Curves and
272
+ Curve Generation</ a > </ li >
273
+ </ ul > </ li >
274
+ </ ul >
136
275
< h3 > Articles & blog posts</ h3 >
137
276
< ul >
138
277
< li > < a href ="https://learn.microsoft.com/en-us/dotnet/standard/security/vulnerabilities-cbc-mode "> Microsoft, Timing vulnerabilities with CBC-mode
0 commit comments