If I were to write a web application using openpgp.js (http://openpgpjs.org/) for creating PGP encrypted/signed messages and if I stored the user's private key in localStorage, would this be vulnerable to the same complaints about JavaScript cryptography on Matasano's webpage (http://www.matasano.com/articles/javascript-cryptography/)?

As far as I can tell, the main points outline by Matasano are:

  • "Secure delivery of Javascript to browsers is a chicken-egg problem." (This could be fixed by using HTTPS)
  • JavaScript isn't well suited to cryptography
  • Browsers are too complex for cryptography since they have the potential to contain multiple attack vectors.

If localStorage isn't the best place to store something sensitive like a private key, what would be a better place?

2 Answers 2


HTTPS is still an absolute requirement, and Matasano's main point is that JavaScript can never replace HTTPS. A MITM attack could deliver a custom JavaScript payload that could read any secrets in localstorage, and an XSS payload could also compromise this data.

Matasano is wrong in regards to random number generators in JavaScript. Most browsers provide a very secure random number generator window.crypto.getRandomValues(). I guess Matasano hasn't read the w3c Cryptography API for JavaScript. This API contains a random number generator that uses the operating system's entropy pool.

  • 1
    Didn't the Matasano article come out before the W3C crypto spec?
    – Polynomial
    Jun 30, 2013 at 17:07
  • 2
    Key management is also a problem, there is nothing in javascript to make it possible to deliberately write to a specific section of memory, meaning keys may remain in active memory long after they are no longer required.
    – ewanm89
    Jun 30, 2013 at 17:54
  • @Polynomial Not sure, the Matasano article isn't dated. In any case, they should update it.
    – rook
    Jun 30, 2013 at 17:56
  • @ewanm89 that is a good point, you can't disable swap like you can in C/C++.
    – rook
    Jun 30, 2013 at 17:57
  • 2
    archive.org visited the article first time on 2011-09-25, which about a year before the W3C recommendation.
    – Jens Erat
    Jun 30, 2013 at 21:34

If you do decide to store the secret in localStorage, you will want to make sure you never store the cleartext session key there. I'm not familiar with openpgpjs, but it probably facilitates secure storage of keys (some form of keyring class I guess).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .