Skip to content

[SECURITY] Timing leaks in lib/native/bn.js #57

@soatok

Description

@soatok
  • Branching timing leaks
  • powm leaks the value of e
    • bcrypto/lib/native/bn.js

      Lines 3869 to 3897 in b73dbc6

      function powm(x, e, m) {
      assert(m > 0n);
      if (e < 0n) {
      x = invert(x, m);
      e = -e;
      } else {
      x = mod(x, m);
      }
      if (e <= U32_MAX)
      return rtl(x, e, m);
      return slide(x, e, m);
      }
      function rtl(x, e, m) {
      let r = 1n;
      while (e > 0n) {
      if ((e & 1n) === 1n)
      r = (r * x) % m;
      x = (x * x) % m;
      e >>= 1n;
      }
      return r;
      }
    • bcrypto/lib/native/bn.js

      Lines 3889 to 3893 in b73dbc6

      if ((e & 1n) === 1n)
      r = (r * x) % m;
      x = (x * x) % m;
      e >>= 1n;
    • (This also breaks any modular inversion that relies on fermat())

I wrote a library that implements constant-time algorithms in TypeScript if you want to mitigate these risks.

Further reading: [1] [2]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions