coinbin
coinbin copied to clipboard
Consider changing coinjs.newPrivkey() function to use a CSPRNG to generate random values for private keys
Looking at the coinjs.newPrivkey() function (coin.js, line 46), it appears that this function generates random values for private keys using several wrapper functions that rely on Math.random(), and several (low entropy) inputs from the user’s environment, such as screen width, screen height, timezone offset, etc.
At https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random, it warns:
Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.
Whereas at https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues it reads:
The Crypto.getRandomValues() method lets you get cryptographically strong random values.
Perhaps it might be worth considering re-writing the coinjs.newPrivkey() function to use Crypto.getRandomValues(). The following would be a drop-in replacement for the current function:
coinjs.newPrivkey = function(){
var randombytes=new Uint8Array(32);
window.crypto.getRandomValues(randombytes);
r=Uint8ArrayToHexString(randombytes);
return r;
}
function Uint8ArrayToHexString(ui8array) {
var hexstring='', h;
for(var i=0; i<ui8array.length; i++) {
h=ui8array[i].toString(16);
if(h.length==1) { h='0'+h; }
hexstring+=h;
}
return hexstring;
}