Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider changing coinjs.newPrivkey() function to use a CSPRNG to generate random values for private keys #245

Open
meixler opened this issue Dec 15, 2021 · 0 comments · May be fixed by #246

Comments

@meixler
Copy link

meixler commented Dec 15, 2021

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;
}
@junderw junderw linked a pull request Dec 15, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant