From: "Dmitry S. Kravtsov" Date: 2012-05-29T14:19:23+09:00 Subject: Re: does this leak more than the size of the string via timing side channels --047d7b33cb802ab48004c125fae3 Content-Type: text/plain; charset=UTF-8 In your example you simply testing two strings for equality, with exception that when strings have different lengths you return true. So your example can be written as this: def message_authentic?(ciphertext, key, presented_mac) computed_mac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, OpenSSL::Digest::SHA256.new("mac" + key).to_s, ciphertext) computed_mac.length != presented_mac.length or computed_mac == presented_mac end 2012/5/29 rooby shoez > yuck the first example has issues I just realized, how about this way: > > > def message_authentic?(ciphertext, key, presented_mac) > computed_mac = OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new, > OpenSSL::Digest::SHA256.new("mac" + key).to_s, ciphertext) > a = [true] > if computed_mac.length == presented_mac.length then > computer_mac.length.times do |i| > a += [computed_mac[i] == presented_mac[i]] > end > mac_verifies = a.inject(:==) > end > > -- > Posted via http://www.ruby-forum.com/. > > -- Dmitry S. Kravtsov --047d7b33cb802ab48004c125fae3 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable In your example you simply testing two strings for equality, with exception= that when strings have different lengths you return true.
So your exam= ple can be written as this:
def message_authentic?(ci= phertext, key, presented_mac)
computed_mac =3D OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new,
OpenSSL::Digest::SHA256.= new("mac" + key).to_s, ciphertext)

computed_mac.length !=3D presented_mac.length or comput= ed_mac =3D=3D presented_mac
end

2012/5/29 rooby shoez <lists@ruby-forum.com= >
yuck the first example has issues I just rea= lized, how about this way:


def message_authentic?(ciphertext, key, presented_mac)
computed_mac =3D OpenSSL::HMAC.digest(OpenSSL::Digest::SHA256.new,
OpenSSL::Digest::SHA256.new("mac" + key).to_s, ciphertext)
a =3D [true]
if computed_mac.length =3D=3D presented_mac.length then
computer_mac.length.times do |i|
a +=3D [computed_mac[i] =3D=3D presented_mac[i]]
end
mac_verifies =3D a.inject(:=3D=3D)
end



--
= Dmitry S. Kravtsov
--047d7b33cb802ab48004c125fae3--