I am trying to use the RAND_bytes API of OpenSSL, but I want to try it with various Random Number Generating Engines.
Is there a recommended way of generating Random bytes and adding entropy in OpenSSL? Where can I get other Engine implementations, and how can I swap them in?
Yes. See the OpenSSL wiki on Random Numbers. It takes you through adding entropy for a seed, and extracting bytes for use in keying and other secret material.
Adding entropy for seeding is covered under Random Numbers and Seeds. Extracting bytes for use in keysing and other secret material is covered at Random Numbers and Generation.
OpenSSL comes with a few engines related to random numbers. The default is a software based PRNG engine,
md_rand
. You can find its source code at<openssl src>/crypto/rand/md_rand.c
. Another is Intel'sRDRAND
engine. You can find the source at<openssl src>/crypto/engine/eng_rdrand.c
.You can also use hardware based RNGs if you have the hardware. You can even write your own engine that provides a SHA-512 HMAC. Or even one that combines (XORs) an SHA-512 HMAC with with
RDRAND
. The Mersenne Twister is popular, and you could even write an engine for it, too.Here's how you swap in an engine to use for random numbers. Its taken from the OpenSSL wiki, and it swaps-in the Intel
RDRAND
engine:You never do anything other than use
RAND_bytes
,RAND_add
and friends as normal. How you useRAND_bytes
,RAND_add
and friends never changes.If you do this, then you might consider posting the source code for others to use. I would suggest creating a page on OpenSSL's wiki, explain the Mersenne Twister engine, explain how to use it, and provide a patch for it.
The other choice is to submit it to the RT system (the bug tracker) as a feature/enhancement. But its been my observation that most things wither and die once they enter RT.