Skip to content

Commit 5373306

Browse files
committed
crypto: make generatePrime/checkPrime interruptible
The `generatePrime` and `checkPrime` functions in the `crypto` module are only somewhat interruptible. This change makes it possible to interrupt these more reliably. Note that generating overly large primes can still take a long time and may not be interruptible as this mechanism relies on a callback to check for stopping conditions but OpenSSL may perform a long running operation without calling the callback right away. Fixes: #56449
1 parent 01554f3 commit 5373306

File tree

3 files changed

+61
-11
lines changed

3 files changed

+61
-11
lines changed

doc/api/crypto.md

+14
Original file line numberDiff line numberDiff line change
@@ -3940,6 +3940,13 @@ By default, the prime is encoded as a big-endian sequence of octets
39403940
in an {ArrayBuffer}. If the `bigint` option is `true`, then a {bigint}
39413941
is provided.
39423942

3943+
The `size` of the prime will have a direct impact on how long it takes to
3944+
generate the prime. The larger the size, the longer it will take. Because
3945+
we use OpenSSL's `BN_generate_prime_ex` function, which provides only
3946+
minimal control over our ability to interrupt the generation process,
3947+
it is not recommended to generate overly large primes, as doing so may make
3948+
the process unresponsive.
3949+
39433950
### `crypto.generatePrimeSync(size[, options])`
39443951

39453952
<!-- YAML
@@ -3981,6 +3988,13 @@ By default, the prime is encoded as a big-endian sequence of octets
39813988
in an {ArrayBuffer}. If the `bigint` option is `true`, then a {bigint}
39823989
is provided.
39833990

3991+
The `size` of the prime will have a direct impact on how long it takes to
3992+
generate the prime. The larger the size, the longer it will take. Because
3993+
we use OpenSSL's `BN_generate_prime_ex` function, which provides only
3994+
minimal control over our ability to interrupt the generation process,
3995+
it is not recommended to generate overly large primes, as doing so may make
3996+
the process unresponsive.
3997+
39843998
### `crypto.getCipherInfo(nameOrNid[, options])`
39853999

39864000
<!-- YAML

src/crypto/crypto_random.cc

+31-11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@ using v8::Uint32;
2828
using v8::Value;
2929

3030
namespace crypto {
31+
namespace {
32+
using BNGENCBPointer = DeleteFnPtr<BN_GENCB, BN_GENCB_free>;
33+
34+
BNGENCBPointer getBN_GENCB(Environment* env) {
35+
// The callback is used to check if the operation should be stopped.
36+
// Currently, the only check we perform is if env->is_stopping()
37+
// is true.
38+
BNGENCBPointer cb(BN_GENCB_new());
39+
BN_GENCB_set(
40+
cb.get(),
41+
[](int a, int b, BN_GENCB* cb) -> int {
42+
Environment* env = static_cast<Environment*>(BN_GENCB_get_arg(cb));
43+
return env->is_stopping() ? 0 : 1;
44+
},
45+
env);
46+
return cb;
47+
}
48+
49+
} // namespace
3150
MaybeLocal<Value> RandomBytesTraits::EncodeOutput(
3251
Environment* env, const RandomBytesConfig& params, ByteSource* unused) {
3352
return v8::Undefined(env->isolate());
@@ -150,13 +169,16 @@ bool RandomPrimeTraits::DeriveBits(Environment* env,
150169
// Make sure the CSPRNG is properly seeded.
151170
CHECK(ncrypto::CSPRNG(nullptr, 0));
152171

153-
if (BN_generate_prime_ex(
154-
params.prime.get(),
155-
params.bits,
156-
params.safe ? 1 : 0,
157-
params.add.get(),
158-
params.rem.get(),
159-
nullptr) == 0) {
172+
BNGENCBPointer cb = getBN_GENCB(env);
173+
BignumCtxPointer ctx(BN_CTX_new());
174+
175+
if (BN_generate_prime_ex2(params.prime.get(),
176+
params.bits,
177+
params.safe ? 1 : 0,
178+
params.add.get(),
179+
params.rem.get(),
180+
cb.get(),
181+
ctx.get()) == 0) {
160182
return false;
161183
}
162184

@@ -189,12 +211,10 @@ bool CheckPrimeTraits::DeriveBits(
189211
ByteSource* out) {
190212

191213
BignumCtxPointer ctx(BN_CTX_new());
214+
BNGENCBPointer cb = getBN_GENCB(env);
192215

193216
int ret = BN_is_prime_ex(
194-
params.candidate.get(),
195-
params.checks,
196-
ctx.get(),
197-
nullptr);
217+
params.candidate.get(), params.checks, ctx.get(), cb.get());
198218
if (ret < 0) return false;
199219
ByteSource::Builder buf(1);
200220
buf.data<char>()[0] = ret;

test/parallel/test-crypto-prime.js

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ const {
1414
checkPrimeSync,
1515
} = require('crypto');
1616

17+
const { Worker } = require('worker_threads');
18+
1719
const { promisify } = require('util');
1820
const pgeneratePrime = promisify(generatePrime);
1921
const pCheckPrime = promisify(checkPrime);
@@ -295,3 +297,17 @@ assert.throws(() => {
295297
checkPrime(prime, common.mustSucceed(assert));
296298
}));
297299
}
300+
301+
{
302+
// Verify that generatePrime can be reasonably interrupted.
303+
const worker = new Worker(`
304+
const { generatePrime } = require('crypto');
305+
generatePrime(2048, () => {
306+
throw new Error('should not be called');
307+
});
308+
process.exit(0);
309+
`, { eval: true });
310+
311+
worker.on('error', common.mustNotCall());
312+
worker.on('exit', common.mustCall());
313+
}

0 commit comments

Comments
 (0)