diff options
author | Nick Mathewson <nickm@torproject.org> | 2012-12-03 23:31:07 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2013-01-02 14:11:13 -0500 |
commit | 25c05cb747eece7d720a3f79c172e83a0e79a3a1 (patch) | |
tree | 8aab82509ef888cbc2b385fb3275405ee0381577 /src/common/crypto_curve25519.c | |
parent | 4d36eafd74e9c66a0dc76e5543a2aaabfa11f8b2 (diff) | |
download | tor-25c05cb747eece7d720a3f79c172e83a0e79a3a1.tar.gz tor-25c05cb747eece7d720a3f79c172e83a0e79a3a1.zip |
Refactor strong os-RNG into its own function
Previously, we only used the strong OS entropy source as part of
seeding OpenSSL's RNG. But with curve25519, we'll have occasion to
want to generate some keys using extremely-good entopy, as well as the
means to do so. So let's!
This patch refactors the OS-entropy wrapper into its own
crypto_strongest_rand() function, and makes our new
curve25519_secret_key_generate function try it as appropriate.
Diffstat (limited to 'src/common/crypto_curve25519.c')
-rw-r--r-- | src/common/crypto_curve25519.c | 13 |
1 files changed, 11 insertions, 2 deletions
diff --git a/src/common/crypto_curve25519.c b/src/common/crypto_curve25519.c index 1985e8af2d..ce0cd0d60c 100644 --- a/src/common/crypto_curve25519.c +++ b/src/common/crypto_curve25519.c @@ -59,9 +59,18 @@ void curve25519_secret_key_generate(curve25519_secret_key_t *key_out, int extra_strong) { - (void)extra_strong; + uint8_t k_tmp[CURVE25519_SECKEY_LEN]; - crypto_rand((char*)key_out->secret_key, 32); + crypto_rand((char*)key_out->secret_key, CURVE25519_SECKEY_LEN); + if (extra_strong && !crypto_strongest_rand(k_tmp, CURVE25519_SECKEY_LEN)) { + /* If they asked for extra-strong entropy and we have some, use it as an + * HMAC key to improve not-so-good entopy rather than using it directly, + * just in case the extra-strong entropy is less amazing than we hoped. */ + crypto_hmac_sha256((char *)key_out->secret_key, + (const char *)k_tmp, sizeof(k_tmp), + (const char *)key_out->secret_key, CURVE25519_SECKEY_LEN); + } + memwipe(k_tmp, 0, sizeof(k_tmp)); key_out->secret_key[0] &= 248; key_out->secret_key[31] &= 127; key_out->secret_key[31] |= 64; |