Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

randomBytes vs pseudoRandomBytes

In what situations is it acceptable (from a security standpoint) to use node's crypto.pseudoRandomBytes instead of the cryptographically-strong crypto.randomBytes?

I assume pseudoRandomBytes performs better at the expense of being more predictable (incorrect), but the docs don't really have much to say about how less-strong it is.

Specifically, I'm wondering if I'm ok using pseudoRandomBytes to generate a CSRF token.

like image 940
josh3736 Avatar asked Aug 08 '13 15:08

josh3736


People also ask

What is randomBytes?

randomBytes() method is used to generate a cryptographically well-built artificial random data and the number of bytes to be generated in the written code. Syntax: crypto.randomBytes( size, callback )

Is crypto built in Nodejs?

It includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. crypto is built into Node. js, so it doesn't require rigorous implementation process and configurations. Unlike other modules, you don't need to install Crypto before you use it in your Node.

How does node generate random bytes?

randomBytes() generates cyprtographically strong pseudo-random data. This method will not be completed until there is sufficient entropy in the bytes created. But even after this it does not takes more than a few milliseconds. This method basically creates a few random bytes which are further used.

How would you convert random bytes to hexadecimal in node JS?

GenerateRandomBytesToHex function returns you a hash that is X Byte long inside of a String where each Byte is displayed in hexadecimal value. The hexidecimal value of the number 42 is 0x2A . You can see that one Byte (from 0 to 254) is displayed using 2 character in hexadecimal.


1 Answers

As it turns out, with the default OpenSSL (which is bundled with node, but if you've built your own, it is possible to configure different engines), the algorithm to generate random data is exactly the same for both randomBytes (RAND_bytes) and pseudoRandomBytes (RAND_pseudo_bytes).

The one and only difference between the two calls depends on the version of node you're using:

  • In node v0.12 and prior, randomBytes returns an error if the entropy pool has not yet been seeded with enough data. pseudoRandomBytes will always return bytes, even if the entropy pool has not been properly seeded.
  • In node v4 and later, randomBytes does not return until the entropy pool has enough data. This should take only a few milliseconds (unless the system has just booted).

Once the the entropy pool has been seeded with enough data, it will never "run out," so there is absolutely no effective difference between randomBytes and pseudoRandomBytes once the entropy pool is full.

Because the exact same algorithm is used to generate randrom data, there is no difference in performance between the two calls (one-time entropy pool seeding notwithstanding).

like image 187
josh3736 Avatar answered Sep 21 '22 08:09

josh3736