diff --git a/lib/crypto.js b/lib/crypto.js index bed7d7764e3ad5..5dacb4014f0bec 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -11,6 +11,7 @@ try { var getCiphers = binding.getCiphers; var getHashes = binding.getHashes; var getCurves = binding.getCurves; + var addEntropy = binding.addEntropy; } catch (e) { throw new Error('Node.js is not compiled with openssl crypto support'); } @@ -621,6 +622,8 @@ exports.randomBytes = exports.pseudoRandomBytes = randomBytes; exports.rng = exports.prng = randomBytes; +exports.addEntropy = addEntropy; + exports.getCiphers = function() { return filterDuplicates(getCiphers()); }; diff --git a/src/node_crypto.cc b/src/node_crypto.cc index bd7314c9db902c..f0d2a8d9558148 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5354,6 +5354,25 @@ void GetCurves(const FunctionCallbackInfo& args) { } +void AddEntropy(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + + if (args.Length() == 0) { + // Delegate entropy generation to OpenSSL, which will add + // entropy from system sources + RAND_poll(); + return; + } + // Make sure we got a buffer from the user and use it to + // seed OpenSSL. + THROW_AND_RETURN_IF_NOT_BUFFER(args[0]); + Local bufObj = args[0]->ToObject(); + const void* buf = Buffer::Data(bufObj); + size_t bufLength = Buffer::Length(bufObj); + RAND_seed(buf, bufLength); +} + + void Certificate::Initialize(Environment* env, Local target) { HandleScope scope(env->isolate()); @@ -5648,6 +5667,7 @@ void InitCrypto(Local target, env->SetMethod(target, "getCiphers", GetCiphers); env->SetMethod(target, "getHashes", GetHashes); env->SetMethod(target, "getCurves", GetCurves); + env->SetMethod(target, "addEntropy", AddEntropy); env->SetMethod(target, "publicEncrypt", PublicKeyCipher::Cipher