Skip to content

crypto: ecdh - generate public key when setting private key and more

Rodrigo Muino Tomonari requested to merge github/fork/mruddy/ecdh into master

crypto: ecdh updates to support using previously known private keys

  • generate public key when setting private key
  • remove "generated" checks from getters
  • added tests
  • updated docs
  • check the key pair before computing the secret to avoid computing a garbage shared secret
  • added ECDH.validKeyPair
  • marked ECDH.setPublicKey as deprecated

This pull request shares many similarities with #1020.

I decided not to add the public key generation as a separate method like was proposed in #1020. Rather I made it part of setting the private key. I liked this way more because then it leaves two main ways to use the class. Either generate the keys, or set the private key then compute the shared secret.

Originally I started looking at this code because the following error was annoying and unnecessary (and is why I removed the generated_ member from ECDH):

> node -p "require('crypto').createECDH('secp256k1').setPrivateKey(new Buffer('1111111111111111111111111111111111111111111111111111111111111111', 'hex')).getPublicKey()"
  crypto.js:526
    var key = this._handle.getPublicKey(f);
                           ^

  Error: You should generate ECDH keys first
      at Error (native)
      at ECDH.getPublicKey (crypto.js:526:26)

Merge request reports

Loading