Skip to content

crypto.timingSafeEqual is not really time safe? #17178

Description

@elaygl

After trying to use crypto.timingSafeEqual with two buffers that have different length I've got an exception.

I read the docs and realized that crypto.timingSafeEqual is supporting only buffers with the same length which is contradicting the concept of timing safe compare.

The comparison code:

void TimingSafeEqual(const FunctionCallbackInfo<Value>& args) {

Maybe it's worth to implement it like that: https://github.com/vadimdemedes/secure-compare/blob/master/index.js#L12

Activity

  1. added
    cryptoIssues and PRs related to the crypto subsystem.
    questionIssues asking questions about Node.js.
    on Nov 21, 2017
  2. bnoordhuis commented on Nov 21, 2017

    @bnoordhuis
    Member

    See #3043, #3073 and #8040. The current behavior is the product of a stupefying amount of discussion. I think it's unlikely we'll revisit that.

    As well, your suggestion is a subtle change in behavior. Too subtle, IMO - it might end up breaking applications in ways that go undetected until it's too late. That risk alone practically rules it out.

  3. ryangreenberg commented on Dec 3, 2017

    @ryangreenberg

    Generally a comparison function that is resistant to timing attacks is used to compare signatures or hashes, which should be the same length by virtue of being generated by a hash function. If you are accepting a user-provided signature and want to compare it in a safe way, it's OK to check the length first and return early, e.g. return a.length === b.length && timingSafeEqual(new Buffer(a), new Buffer(b)). This doesn't reveal any information about the contents that would aid a typical attack.

    See https://codahale.com/a-lesson-in-timing-attacks/ for more details (which also does a length equality check).

  4. jaylattice commented on Apr 3, 2020

    @jaylattice

    Length checks are timing safe, because they do not depend on time.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions