Don't use DES/3DES/RC2 weak cipher algorithms
Since 2005 the NIST no longer considers DES and 3DES cipher algorithms secure (3DES was fully withdrawn at the end of 2023). RC2 shares the 64-bit block size that makes them vulnerable to the Sweet32 birthday attack. Rijndael / RijndaelManaged are obsolete in .NET (SYSLIB0022): they only add non-standard block sizes to AES and are not FIPS validated. Sources: https://www.nist.gov/news-events/news/2005/06/nist-withdraws-outdated-data-encryption-standard https://sweet32.info/
This is CWE-327 Use of a Broken or Risky Cryptographic Algorithm.
Flagged Code
This is CWE-327 Use of a Broken or Risky Cryptographic Algorithm.
Flagged Code
static byte[] Encrypt(byte[] plain, byte[] key, byte[] iv) {
using var cipher = TripleDES.Create(); // flagged: 3DES
cipher.Key = key;
cipher.IV = iv;
using var encryptor = cipher.CreateEncryptor();
return encryptor.TransformFinalBlock(plain, 0, plain.Length);
}
Fixed Codestatic byte[] Encrypt(byte[] plain, byte[] key, byte[] iv) {
using var cipher = Aes.Create(); // AES (or AesGcm)
cipher.Key = key;
cipher.IV = iv;
using var encryptor = cipher.CreateEncryptor();
return encryptor.TransformFinalBlock(plain, 0, plain.Length);
}How to fix
Use AES via System.Security.Cryptography.Aes.Create() (or the authenticated AesGcm which also detects tampering). Suppress issues when DES/3DES/RC2 is kept for legacy data compatibility.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Don't use DES/3DES/RC2 weak cipher algorithms</Name>
// <Id>ND3102:DontUseDES3DESWeakCipherAlgorithms</Id>
warnif count > 0
// Third-party members are part of the code model only when the application uses them.
let weakCipherTypes = ThirdParty.Types.WithFullNameIn(
"System.Security.Cryptography.DES",
"System.Security.Cryptography.DESCryptoServiceProvider",
"System.Security.Cryptography.TripleDES",
"System.Security.Cryptography.TripleDESCryptoServiceProvider",
"System.Security.Cryptography.TripleDESCng",
"System.Security.Cryptography.RC2",
"System.Security.Cryptography.RC2CryptoServiceProvider",
// Rijndael is obsolete (SYSLIB0022): AES with non-standard block sizes, not FIPS validated
"System.Security.Cryptography.Rijndael",
"System.Security.Cryptography.RijndaelManaged")
// Constructors and factories: new DESCryptoServiceProvider(), TripleDES.Create(), RC2.Create("RC2")
let weakCipherMembers = weakCipherTypes.ChildMethods()
.Where(m => m.IsConstructor || (m.IsStatic && m.SimpleName == "Create"))
.ToHashSetEx()
where weakCipherMembers.Any()
from m in Application.Methods.UsingAny(weakCipherMembers)
let weakCipherMembersUsed = m.MethodsCalled.Intersect(weakCipherMembers).ToArray()
let onlyRijndael = weakCipherMembersUsed.All(mc => mc.ParentType.SimpleName.StartsWith("Rijndael"))
select new {
m,
weakCipherMembersUsed,
Debt = 1.ToHours().ToDebt(),
Severity = onlyRijndael ? Severity.Medium : Severity.High
}
//<Expl>
// The *{0}* uses the weak or obsolete cipher algorithm(s) *{1}*.
// Use *System.Security.Cryptography.Aes* instead.
//</Expl>
//<Desc>
// Since 2005 the NIST no longer considers DES and 3DES cipher algorithms
// secure (3DES was fully withdrawn at the end of 2023). RC2 shares the
// 64-bit block size that makes them vulnerable to the *Sweet32* birthday
// attack. *Rijndael* / *RijndaelManaged* are obsolete in .NET (SYSLIB0022):
// they only add non-standard block sizes to AES and are not FIPS validated.
// Sources:
// https://www.nist.gov/news-events/news/2005/06/nist-withdraws-outdated-data-encryption-standard
// https://sweet32.info/
//
// This is CWE-327 *Use of a Broken or Risky Cryptographic Algorithm*.
//
// **Flagged Code**
// ```csharp
// static byte[] Encrypt(byte[] plain, byte[] key, byte[] iv) {
// using var cipher = TripleDES.Create(); // flagged: 3DES
// cipher.Key = key;
// cipher.IV = iv;
// using var encryptor = cipher.CreateEncryptor();
// return encryptor.TransformFinalBlock(plain, 0, plain.Length);
// }
// ```
//
// **Fixed Code**
// ```csharp
// static byte[] Encrypt(byte[] plain, byte[] key, byte[] iv) {
// using var cipher = Aes.Create(); // AES (or AesGcm)
// cipher.Key = key;
// cipher.IV = iv;
// using var encryptor = cipher.CreateEncryptor();
// return encryptor.TransformFinalBlock(plain, 0, plain.Length);
// }
// ```
//</Desc>
//<HowToFix>
// Use AES via *System.Security.Cryptography.Aes.Create()* (or the
// authenticated *AesGcm* which also detects tampering).
// Suppress issues when DES/3DES/RC2 is kept for legacy data compatibility.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Security: all 39 items
- Software Composition Analysis (SCA) ND3120
- Transitive Software Composition Analysis (SCA) ND3121
- Don't hard-code secrets in constants ND3114
- Review credentials built from hard-coded values ND3115
- Don't use insecure deserializers ND3110
- Don't use weak hash algorithms (MD5, SHA1) ND3109
- Don't use DES/3DES/RC2 weak cipher algorithms ND3102
- Don't use weak password-based key derivation ND3123
- Don't use the ECB encryption mode ND3132
- Don't disable certificate validation ND3103
- Don't hard-code TLS/SSL protocol versions ND3122
- Review commands vulnerable to SQL injection ND3107
- Review data adapters vulnerable to SQL injection ND3108
- Review raw SQL built from composed strings ND3128
- Review LDAP filters composed from strings ND3137
- Don't enable external XML entity resolution (XXE) ND3111
- Review processes started with a composed arguments string ND3124
- Review web endpoints building file paths from their parameters ND3129
- Review custom archive extraction (Zip Slip) ND3135
- Review assemblies and types loaded from runtime-provided names ND3125
- Review web endpoints without [Authorize] nor [AllowAnonymous] ND3118
- Review state-changing endpoints opened to anonymous callers ND3130
- Review state-changing MVC actions without anti-forgery validation ND3119
- Review HTML-encoding bypasses ND3133
- Review explicitly disabled request validation ND3134
- Review redirects to a URL that is not validated as local ND3126
- Review request-influenced outbound HTTP calls (SSRF) ND3136
- Review insecure ASP.NET Core CORS policies and unguarded developer exception page ND3116
- Review cookies created without HttpOnly and Secure flags ND3117
- Review regular expressions without a match timeout (ReDoS) ND3112
- Review secrets compared with non-constant-time equality ND3113
- Review secrets passed to logging methods ND3127
- Review security operations that log nothing ND3131
- Don't use System.Random for security purposes ND3101
- Don't use CoSetProxyBlanket and CoInitializeSecurity ND3100
- Review publicly visible event handlers ND3104
- Pointers should not be publicly visible ND3105
- Seal methods that satisfy non-public interfaces ND3106
- Restrict sensitive APIs to reviewed components ND3138