Skip to content
Merged
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
12 changes: 8 additions & 4 deletions src/workerd/api/node/crypto.c++
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,7 @@ jsg::Ref<CryptoImpl::DiffieHellmanHandle> CryptoImpl::DiffieHellmanHandle::const
return js.alloc<DiffieHellmanHandle>(DiffieHellman(sizeOrKeyParam, generatorParam));
}

CryptoImpl::DiffieHellmanHandle::DiffieHellmanHandle(DiffieHellman dh): dh(kj::mv(dh)) {
verifyError = JSG_REQUIRE_NONNULL(this->dh.check(), Error, "DiffieHellman init failed");
};
CryptoImpl::DiffieHellmanHandle::DiffieHellmanHandle(DiffieHellman dh): dh(kj::mv(dh)) {}

void CryptoImpl::DiffieHellmanHandle::setPrivateKey(jsg::Lock& js, jsg::JsBufferSource key) {
dh.setPrivateKey(nonNullBytes(key.asArrayPtr()));
Expand Down Expand Up @@ -298,7 +296,13 @@ jsg::JsUint8Array CryptoImpl::DiffieHellmanHandle::generateKeys(jsg::Lock& js) {
}

int CryptoImpl::DiffieHellmanHandle::getVerifyError() {
return verifyError;
KJ_IF_SOME(codes, verifyError) {
return codes;
}
// The prime and generator never change after construction, and DH_check() reads nothing else.
int codes = JSG_REQUIRE_NONNULL(dh.check(), Error, "DiffieHellman check failed");
verifyError = codes;
return codes;
}
#pragma endregion // DiffieHellman

Expand Down
4 changes: 3 additions & 1 deletion src/workerd/api/node/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class CryptoImpl final: public jsg::Object {

private:
DiffieHellman dh;
int verifyError;
// DH_check() runs primality tests, which are expensive for primes as large as the standard
// groups', so it runs only when the script first reads verifyError.
kj::Maybe<int> verifyError;
};

jsg::Ref<DiffieHellmanHandle> DiffieHellmanGroupHandle(jsg::Lock& js, kj::String name);
Expand Down
1 change: 0 additions & 1 deletion src/workerd/api/node/tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ wd_test(
)

wd_test(
size = "enormous",
src = "crypto_dh-test.wd-test",
args = ["--experimental"],
data = ["crypto_dh-test.js"],
Expand Down
12 changes: 7 additions & 5 deletions src/workerd/api/node/tests/crypto_dh-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,10 @@ export const dh_padding_test = {

export const dhKeygenTest = {
test() {
// FIPS-mode BoringSSL mandates keys of at least 1024 bits. RFC 8270 recommends that sizes of
// at least 2048 bits should be used, 1024-bit primes are sufficient for these tests though.
const size = 1024;
// The time to generate a safe prime grows steeply with its size and varies widely from one
// run to the next, so the test generates a small one. Real use needs at least 2048 bits
// (RFC 8270).
const size = 512;
const dh1 = crypto.createDiffieHellman(size);
const p1 = dh1.getPrime('buffer');
const dh2 = crypto.createDiffieHellman(p1, 'buffer');
Expand Down Expand Up @@ -573,9 +574,10 @@ export const DhLargeGeneratorParam = {
// Test that invalid public keys are rejected by setPublicKey and computeSecret.
export const dhPubKeyValidation = {
test() {
const dh = crypto.createDiffieHellman(1024);
// A fixed prime, since dhKeygenTest already covers generating one.
const prime = crypto.getDiffieHellman('modp14').getPrime();
const dh = crypto.createDiffieHellman(prime);
dh.generateKeys();
const prime = dh.getPrime();

// Public key of 0 should be rejected (too small)
const dh2 = crypto.createDiffieHellman(prime);
Expand Down
Loading