Skip to content
This repository was archived by the owner on Feb 1, 2026. It is now read-only.

Use timing-safe hash comparision#20

Merged
emarref merged 1 commit into
masterfrom
feature-timing-attack
Sep 5, 2016
Merged

Use timing-safe hash comparision#20
emarref merged 1 commit into
masterfrom
feature-timing-attack

Conversation

@emarref
Copy link
Copy Markdown
Owner

@emarref emarref commented Sep 5, 2016

This branch addresses a timing attack raised by Dennis Detering from [rub.de] when comparing hashes for symmetric encryption verification. Details are below.

Description

The PHP jwt library by Malcolm Fell version <= 1.0.2 is vulnerable to a timing attack on hash comparison in the symmetric encryption component resulting in crafting a valid signature for arbitrary content.

Details

The verification of the HMAC hash in the verify() function in Symmetric.php is vulnerable to a timing attack. No timing safe equal function, like e.g. hash_equals() (PHP >= 5.6.0 and PHP 7), is used.

This allows an attacker to craft a valid signature for an arbitrary content.

Recommendation

It is recommended to use a timing safe equal function for comparison. In PHP >= 5.6.0 and PHP 7, the hash_equals() function has been implemented.

For unsupported versions, the following example function might be used (taken from here - also recommended for further details of timing attacks on equals comparison):

/**
 * A timing safe equals comparison
 *
 * @param string $safe The internal (safe) value to be checked
 * @param string $user The user submitted (unsafe) value
 *
 * @return boolean True if the two strings are identical.
 */
function timingSafeEquals($safe, $user) {
    $safeLen = strlen($safe);
    $userLen = strlen($user);

    if ($userLen != $safeLen) {
        return false;
    }

    $result = 0;

    for ($i = 0; $i < $userLen; $i++) {
        $result |= (ord($safe[$i]) ^ ord($user[$i]));
    }

    // They are only identical strings if $result is exactly 0...
    return $result === 0;
}
@emarref emarref merged commit 79f5637 into master Sep 5, 2016
emarref added a commit that referenced this pull request Sep 5, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

1 participant