rustrandomopensslrandom-seeddeterministic

How to seed OpenSSL PRNG in Rust?


I use the openssl crate for key generation and crypto functions.

I would like to seed the PRNG (or at least add entropy from application with RAND_ADD()),.

However it seems that it is not possible (yet ?).
cf rand module.

How can I add seed/add random to the OpenSSL PRNG from my rust application ?

Alternatively, is there a way to deterministically generate keys with openSSL (using a DRBG) ?


Solution

  • "Adding a seed" almost universally means mixing in the entropy for DRBG (aka CSPRNG) API. So you would not get a deterministic algorithm that way.

    If you want to deterministically generate keys then please don't use a PRNG. You can use a key derivation method (KDF) instead, e.g. Argon2 for passwords or HKDF for deriving keys from a secret. Note that these derives symmetric keys such as AES keys.

    If you want to generate a key pair for asymmetric cryptography then the question suddenly becomes a lot harder. Take, for instance, a look at this Q/A where I ask if there are any issues with this approach. The problem is that any change in the key pair generation algorithm will affect the generated key; there are multiple mathematically equivalent strategies for generating the required RSA primes. If you want to go this way then Elliptic Curves are more suitable for the purpose. Depending on the use case you could also go for wrapping a private key instead (encryption of a private key with a derived symmetric key).

    Very basically, do not use a PRNG unless you can fully control it. If a DRBG is really required then try and substitute the DRBG with your own. You can e.g. implement one using key derivation followed by a counter mode encryption of a stream of zeros with the derived key (a more modern way would be to use a XOF but that would introduce implementation complexities).