Skip to content

Commit e5e5980

Browse files
calvinmetcalfbnoordhuis
authored andcommitted
lib,src: make pseudoRandomBytes alias randomBytes
Previously pseudoRandomBytes worked similarly to randomBytes but in the event of insufficient entropy would silently return non-secure values. As of f68a116, the entropy pool blocks if there is insufficient entropy instead of giving an error so there is now no longer a case where pseudoRandomBytes would act differently than randomBytes. Docs are updated to remove pseudoRandomBytes and to clarify that randomBytes now does block instead of erring when entropy is low. PR-URL: #557 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Fedor Indutny <[email protected]>
1 parent c6cd460 commit e5e5980

File tree

3 files changed

+13
-32
lines changed

3 files changed

+13
-32
lines changed

doc/api/crypto.markdown

+4-10
Original file line numberDiff line numberDiff line change
@@ -647,16 +647,10 @@ Generates cryptographically strong pseudo-random data. Usage:
647647
// most likely, entropy sources are drained
648648
}
649649

650-
NOTE: Will throw error or invoke callback with error, if there is not enough
651-
accumulated entropy to generate cryptographically strong data. In other words,
652-
`crypto.randomBytes` without callback will not block even if all entropy sources
653-
are drained.
654-
655-
## crypto.pseudoRandomBytes(size[, callback])
656-
657-
Identical to `crypto.randomBytes` except that, instead of throwing an error when
658-
there is not enough accumulated entropy to generate cryptographically strong
659-
data, it will silently return **non**-cryptographically strong data.
650+
NOTE: This will block if there is insufficient entropy, although it should
651+
normally never take longer than a few milliseconds. The only time when this
652+
may conceivably block is right after boot, when the whole system is still
653+
low on entropy.
660654

661655
## Class: Certificate
662656

lib/crypto.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ exports.DEFAULT_ENCODING = 'buffer';
88
try {
99
var binding = process.binding('crypto');
1010
var randomBytes = binding.randomBytes;
11-
var pseudoRandomBytes = binding.pseudoRandomBytes;
1211
var getCiphers = binding.getCiphers;
1312
var getHashes = binding.getHashes;
1413
} catch (e) {
@@ -636,12 +635,9 @@ exports.setEngine = function setEngine(id, flags) {
636635
return binding.setEngine(id, flags);
637636
};
638637

639-
exports.randomBytes = randomBytes;
640-
exports.pseudoRandomBytes = pseudoRandomBytes;
641-
642-
exports.rng = randomBytes;
643-
exports.prng = pseudoRandomBytes;
638+
exports.randomBytes = exports.pseudoRandomBytes = randomBytes;
644639

640+
exports.rng = exports.prng = randomBytes;
645641

646642
exports.getCiphers = function() {
647643
return filterDuplicates(getCiphers.call(null, arguments));

src/node_crypto.cc

+7-16
Original file line numberDiff line numberDiff line change
@@ -4589,25 +4589,18 @@ class RandomBytesRequest : public AsyncWrap {
45894589
};
45904590

45914591

4592-
template <bool pseudoRandom>
45934592
void RandomBytesWork(uv_work_t* work_req) {
45944593
RandomBytesRequest* req =
45954594
ContainerOf(&RandomBytesRequest::work_req_, work_req);
4596-
int r;
45974595

45984596
// Ensure that OpenSSL's PRNG is properly seeded.
45994597
CheckEntropy();
46004598

4601-
if (pseudoRandom == true) {
4602-
r = RAND_pseudo_bytes(reinterpret_cast<unsigned char*>(req->data()),
4603-
req->size());
4604-
} else {
4605-
r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data()), req->size());
4606-
}
4599+
const int r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data()),
4600+
req->size());
46074601

4608-
// RAND_bytes() returns 0 on error. RAND_pseudo_bytes() returns 0 when the
4609-
// result is not cryptographically strong - but that's not an error.
4610-
if (r == 0 && pseudoRandom == false) {
4602+
// RAND_bytes() returns 0 on error.
4603+
if (r == 0) {
46114604
req->set_error(ERR_get_error());
46124605
} else if (r == -1) {
46134606
req->set_error(static_cast<unsigned long>(-1));
@@ -4650,7 +4643,6 @@ void RandomBytesAfter(uv_work_t* work_req, int status) {
46504643
}
46514644

46524645

4653-
template <bool pseudoRandom>
46544646
void RandomBytes(const FunctionCallbackInfo<Value>& args) {
46554647
Environment* env = Environment::GetCurrent(args);
46564648

@@ -4675,12 +4667,12 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
46754667
obj->Set(env->domain_string(), env->domain_array()->Get(0));
46764668
uv_queue_work(env->event_loop(),
46774669
req->work_req(),
4678-
RandomBytesWork<pseudoRandom>,
4670+
RandomBytesWork,
46794671
RandomBytesAfter);
46804672
args.GetReturnValue().Set(obj);
46814673
} else {
46824674
Local<Value> argv[2];
4683-
RandomBytesWork<pseudoRandom>(req->work_req());
4675+
RandomBytesWork(req->work_req());
46844676
RandomBytesCheck(req, argv);
46854677
delete req;
46864678

@@ -5041,8 +5033,7 @@ void InitCrypto(Handle<Object> target,
50415033
env->SetMethod(target, "setEngine", SetEngine);
50425034
#endif // !OPENSSL_NO_ENGINE
50435035
env->SetMethod(target, "PBKDF2", PBKDF2);
5044-
env->SetMethod(target, "randomBytes", RandomBytes<false>);
5045-
env->SetMethod(target, "pseudoRandomBytes", RandomBytes<true>);
5036+
env->SetMethod(target, "randomBytes", RandomBytes);
50465037
env->SetMethod(target, "getSSLCiphers", GetSSLCiphers);
50475038
env->SetMethod(target, "getCiphers", GetCiphers);
50485039
env->SetMethod(target, "getHashes", GetHashes);

0 commit comments

Comments
 (0)