Secret compared with a non-constant-time operation

ID

vbnet.cryptography.constant_time_comparison

Severity

low

Remediation Complexity

medium

Remediation Risk

medium

Remediation Effort

medium

Resource

Cryptography

Language

VB.NET

Description

A secret value (password, token, HMAC, signature, hash, MAC or API key) is compared using a non-constant-time comparison (String.Equals, .Equals or the equality operators). The time taken by such comparisons depends on how many leading bytes match, which leaks information about the secret through a timing side channel. Use CryptographicOperations.FixedTimeEquals for comparing security-sensitive values.

Rationale

A secret value (password, token, HMAC, signature, hash, MAC or API key) is compared using a non-constant-time comparison (String.Equals, .Equals or the equality operators). The time taken by such comparisons depends on how many leading bytes match, which leaks information about the secret through a timing side channel. Use CryptographicOperations.FixedTimeEquals for comparing security-sensitive values.

The following code illustrates a vulnerable pattern detected by this rule:

Public Function CompareEquals(userToken As String, input As String) As Boolean
    Dim storedToken As String = LoadToken()
    Dim passwordHash As String = LoadHash()

    ' VULNERABLE: Secret compared with a non-constant-time operation
    If storedToken.Equals(userToken) Then
        Return True
    End If

    ' VULNERABLE: Secret compared with a non-constant-time operation
    If userToken.Equals(storedToken) Then
        Return True
    End If

    ' VULNERABLE: Secret compared with a non-constant-time operation
    If String.Equals(passwordHash, input) Then
        Return True
    End If

    ' VULNERABLE: Secret compared with a non-constant-time operation
    If String.Equals(input, passwordHash) Then
        Return True
    End If

    ' VULNERABLE: Secret compared with a non-constant-time operation
    If storedToken <> input Then
        Return True
    End If

Remediation

Follow secure coding practices and review the references below for detailed remediation guidance.

Configuration

This detector does not need any configuration.