Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use secure comparison function for HMAC verification. #217

Open
4 of 5 tasks
pirogoeth opened this issue Dec 1, 2016 · 1 comment
Open
4 of 5 tasks

Use secure comparison function for HMAC verification. #217

pirogoeth opened this issue Dec 1, 2016 · 1 comment
Assignees

Comments

@pirogoeth
Copy link
Contributor

pirogoeth commented Dec 1, 2016

Similar to PR #196, but for other languages that need a simple, standalone, constant-time secure comparison function.


secureCompare for Ruby:

# Run a constant-time comparison against two strings to determine equality.
# Useful for performing cryptographic comparison / avoiding timing attacks.
#
# @param [String] a
# @param [String] b
# @return [Boolean]
def secureCompare(a, b)
  if a.length != b.length then
    return false
  end

  result = 0

  cmp = Hash[[a.bytes, b.bytes].transpose]
  cmp.each do |x, y|
    result |= x ^ y
  end

  return result == 0
end

secureCompare for NodeJS

var bufferEq = require('buffer-equal-constant-time');

function secureCompare(a, b) {
  bufA = new Buffer(a);
  bufB = new Buffer(b);

  return bufferEq(bufA, bufB);
}

secureCompare for PHP

<?php

function secureCompare($a, $b) {
    $bytes_a = unpack("C*", $a);
    $bytes_b = unpack("C*", $b);

    $cmplen = count($bytes_a);

    if ($cmplen !== count($bytes_b)) {
        return false;
    }

    $result = 0;

    // The result from unpack() is 1-indexed instead of 0-indexed.
    for ($i = 1; $i <= $cmplen; $i++) {
        $result |= $bytes_a[$i] ^ $bytes_b[$i];
    }

    return $result === 0;
}

?>

TODO:

Resources:

@phayes
Copy link

phayes commented Sep 24, 2019

This is still an issue. Not only that, but a critical one that can compromise the security of an API token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants