Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crypto.randomBytes entropy sources draining

Tags:

node.js

I tried to generate very big amounts (> 1GB) of pseudo-random data using crypto.randomBytes() method but I could not produce the exception for drained entropy sources to see what is the behaviour of my application in case of this possible exception.

From Node.JS docs:

Note: Will throw error or invoke callback with error, if there is not enough accumulated entropy to generate cryptographically strong data.

My question is:

How to drain all entropy sources to make crypto.randomBytes() to produce an exception?

like image 322
micnic Avatar asked May 04 '14 16:05

micnic


1 Answers

Short answer is - you can't.

Little bit longer answer is - it depends on OS. I assume you use Linux. In theory entropy pool in linux can be easily drained using following script:

#!/bin/bash

while true; do
    # write how much entropy is left
    cat /proc/sys/kernel/random/entropy_avail

    # drain a little bit
    dd if=/dev/random of=/dev/null bs=1 count=1 2> /dev/null
done

Running this script will eventually block operations which uses /dev/random, but not /dev/urandom. Urandom doesn't read directly from entropy pool, it uses PRNG and reseeds it (by default) every 60 seconds using /dev/random. So what happen when entropy pool dries up? Nothing. PRNG will be not reseeded, but it will be still generating new numbers, just less cryptographically strong ones.

The only time when this exception could be throwed, is right after system was booted for the first time. I guess it's rather unlikely... Of course other operating systems can handle this matter differently, but as long you use Linux, you shouldn't have to worry about that.

like image 68
qzb Avatar answered Sep 25 '22 23:09

qzb