[ruby-core:64121] [ruby-trunk - Feature #10098] [PATCH] Timing-safe string comparison for OpenSSL::HMAC

From: cremno@...
Date: 2014-07-29 19:10:55 UTC
List: ruby-core #64121
Issue #10098 has been updated by cremno phobia.


I don't like the proposed method name.

`tsafe`? It should be `timingsafe`. Saving some keystrokes isn't worth it t=
o have a less obvious name. Even C programmers chose a longer name!

I'm also not happy about `eql?` either. Then people expect it to work like =
`String#eql?`. Same argument requirements/conversion, same result, and it's=
 even mentioned in the proposed docs (=E2=80=9Csimilarly=E2=80=9D is vague)=
! But it doesn't. For example:

```ruby
a =3D "\xFF".force_encoding(Encoding::CP437)
b =3D "\xFF".force_encoding(Encoding::CP850)
p a.eql?(b)  # =3D> false
p a.tsafe_eql?(b)  # =3D> true
```

If something is going to be added to String, then encodings need to be cons=
idered. Or:

```ruby
require 'fiddle'
a =3D "\xFF"
(b =3D Fiddle::Pointer.malloc(1))[0] =3D 0xff
p a.eql?(b)  # =3D> false
p a.tsafe_eql?(b)  # =3D> true
```


----------------------------------------
Feature #10098: [PATCH] Timing-safe string comparison for OpenSSL::HMAC
https://bugs.ruby-lang.org/issues/10098#change-48130

* Author: Matt U
* Status: Open
* Priority: Normal
* Assignee:=20
* Category: ext/openssl
* Target version: next minor
----------------------------------------
I could be totally wrong, but it seems the standard library doesn't provide=
 a reliable way of comparing hashes in constant-time.

* The docs for `OpenSSL::HMAC` encourage the use of `Digest#to_s` (see: htt=
p://ruby-doc.org/stdlib-2.1.0/libdoc/openssl/rdoc/OpenSSL/HMAC.html#method-=
c-new )
* Ruby's string comparison uses memcmp, which isn't timing safe (see: http:=
//rxr.whitequark.org/mri/source/string.c#2382 )

With this patch I propose to add an additional method, `OpenSSL::HMAC#verif=
y`, which takes a binary string with a digest and compares it against the c=
omputed hash.


---Files--------------------------------
hmac-timing.patch (2.5 KB)
hmac-timing.patch (2.48 KB)
tsafe_eql.patch (2.48 KB)
tsafe_inline.patch (3.51 KB)


--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next