All rules
CA5388Security Enabled by default: No

Ensure sufficient iteration count when using weak key derivation function

Ensure sufficient iteration count when using weak key derivation function

Microsoft docs

Description

This rule checks if a cryptographic key was generated by System.Security.Cryptography.Rfc2898DeriveBytes with an iteration count that may be less than 100,000. A higher iteration count can help mitigate against dictionary attacks that try to guess the generated cryptographic key.

This rule is similar to CA5387, but analysis can't determine if the iteration count is less than 100,000.

Cause

Iteration count may be smaller than 100,000 when deriving cryptographic key by System.Security.Cryptography.Rfc2898DeriveBytes.GetBytes.

By default, this rule analyzes the entire codebase, but this is configurable.

How to fix violations

Set the iteration count greater than or equal with 100k before calling System.Security.Cryptography.Rfc2898DeriveBytes.GetBytes explicitly.

Example

using System;
using System.Security.Cryptography;

class ExampleClass
{
    public void ExampleMethod(string password, byte[] salt, int cb)
    {
        var iterations = 100;
        Random r = new Random();

        if (r.Next(6) == 4)
        {
            iterations = 100000;
        }

        var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt, iterations);
        rfc2898DeriveBytes.GetBytes(cb);
    }
}

using System.Security.Cryptography;

class ExampleClass
{
    public void ExampleMethod(string password, byte[] salt, int cb)
    {
        var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, salt);
        rfc2898DeriveBytes.IterationCount = 100000;
        rfc2898DeriveBytes.GetBytes(cb);
    }
}

When to suppress

It's safe to suppress warnings from this rule if:

  • You need to use a smaller iteration count for compatibility with existing data.
  • You're sure that the iteration count is set above 100,000.
Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0