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

add secure equals method to prevent timing attacks #133

Merged
merged 1 commit into from
Oct 26, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/api_auth/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ def signatures_match?(headers, secret_key, options)
header_sig = match_data[3]
calculated_sig = hmac_signature(headers, secret_key, options)

header_sig == calculated_sig
secure_equals?(header_sig, calculated_sig, secret_key)
end

def secure_equals?(m1, m2, key)
sha1_hmac(key, m1) == sha1_hmac(key, m2)
end

def sha1_hmac(key, message)
digest = OpenSSL::Digest.new('sha1')
OpenSSL::HMAC.digest(digest, key, message)
end

def hmac_signature(headers, secret_key, options)
Expand Down