Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: make pseudoRandomBytes an alias for randomBytes #557

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions doc/api/crypto.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -647,16 +647,9 @@ Generates cryptographically strong pseudo-random data. Usage:
// most likely, entropy sources are drained
}

NOTE: Will throw error or invoke callback with error, if there is not enough
accumulated entropy to generate cryptographically strong data. In other words,
`crypto.randomBytes` without callback will not block even if all entropy sources
are drained.

## crypto.pseudoRandomBytes(size[, callback])

Identical to `crypto.randomBytes` except that, instead of throwing an error when
there is not enough accumulated entropy to generate cryptographically strong
data, it will silently return **non**-cryptographically strong data.
NOTE: This will block if there is insufficient entropy it should normally never
take longer than a few milliseconds. The only time when this may conceivably
block is right after boot, when the whole system is still low on entropy.

## Class: Certificate

Expand Down
8 changes: 2 additions & 6 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ exports.DEFAULT_ENCODING = 'buffer';
try {
var binding = process.binding('crypto');
var randomBytes = binding.randomBytes;
var pseudoRandomBytes = binding.pseudoRandomBytes;
var getCiphers = binding.getCiphers;
var getHashes = binding.getHashes;
} catch (e) {
Expand Down Expand Up @@ -636,12 +635,9 @@ exports.setEngine = function setEngine(id, flags) {
return binding.setEngine(id, flags);
};

exports.randomBytes = randomBytes;
exports.pseudoRandomBytes = pseudoRandomBytes;

exports.rng = randomBytes;
exports.prng = pseudoRandomBytes;
exports.randomBytes = exports.pseudoRandomBytes = randomBytes;

exports.rng = exports.prng = randomBytes;

exports.getCiphers = function() {
return filterDuplicates(getCiphers.call(null, arguments));
Expand Down
23 changes: 7 additions & 16 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4589,25 +4589,18 @@ class RandomBytesRequest : public AsyncWrap {
};


template <bool pseudoRandom>
void RandomBytesWork(uv_work_t* work_req) {
RandomBytesRequest* req =
ContainerOf(&RandomBytesRequest::work_req_, work_req);
int r;

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

if (pseudoRandom == true) {
r = RAND_pseudo_bytes(reinterpret_cast<unsigned char*>(req->data()),
req->size());
} else {
r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data()), req->size());
}
const int r = RAND_bytes(reinterpret_cast<unsigned char*>(req->data()),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't hate me but it's > 80 columns now.

EDIT: Preempted!

req->size());

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


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

Expand All @@ -4675,12 +4667,12 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
obj->Set(env->domain_string(), env->domain_array()->Get(0));
uv_queue_work(env->event_loop(),
req->work_req(),
RandomBytesWork<pseudoRandom>,
RandomBytesWork,
RandomBytesAfter);
args.GetReturnValue().Set(obj);
} else {
Local<Value> argv[2];
RandomBytesWork<pseudoRandom>(req->work_req());
RandomBytesWork(req->work_req());
RandomBytesCheck(req, argv);
delete req;

Expand Down Expand Up @@ -5041,8 +5033,7 @@ void InitCrypto(Handle<Object> target,
env->SetMethod(target, "setEngine", SetEngine);
#endif // !OPENSSL_NO_ENGINE
env->SetMethod(target, "PBKDF2", PBKDF2);
env->SetMethod(target, "randomBytes", RandomBytes<false>);
env->SetMethod(target, "pseudoRandomBytes", RandomBytes<true>);
env->SetMethod(target, "randomBytes", RandomBytes);
env->SetMethod(target, "getSSLCiphers", GetSSLCiphers);
env->SetMethod(target, "getCiphers", GetCiphers);
env->SetMethod(target, "getHashes", GetHashes);
Expand Down