From fef9432ae480cd9a288eb6d74b95fd5bc485b58c Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 13 Nov 2024 13:21:38 -0800 Subject: [PATCH] Update randomPrime to use BufferSource --- src/workerd/api/crypto/prime.c++ | 7 +++++-- src/workerd/api/crypto/prime.h | 8 +++++++- src/workerd/api/node/crypto.c++ | 5 +++-- src/workerd/api/node/crypto.h | 3 ++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/workerd/api/crypto/prime.c++ b/src/workerd/api/crypto/prime.c++ index bb2ff0eff92..8d2d652e578 100644 --- a/src/workerd/api/crypto/prime.c++ +++ b/src/workerd/api/crypto/prime.c++ @@ -1,12 +1,14 @@ #include "prime.h" #include +#include #include namespace workerd::api { -kj::Array randomPrime(uint32_t size, +jsg::BufferSource randomPrime(jsg::Lock& js, + uint32_t size, bool safe, kj::Maybe> add_buf, kj::Maybe> rem_buf) { @@ -85,7 +87,8 @@ kj::Array randomPrime(uint32_t size, int ret = BN_generate_prime_ex(prime.get(), bits, safe ? 1 : 0, add, rem, nullptr); JSG_REQUIRE(ret == 1, Error, "Error while generating prime"); - return JSG_REQUIRE_NONNULL(bignumToArrayPadded(*prime), Error, "Error while generating prime"); + return JSG_REQUIRE_NONNULL( + bignumToArrayPadded(js, *prime), Error, "Error while generating prime"); } bool checkPrime(kj::ArrayPtr bufferView, uint32_t num_checks) { diff --git a/src/workerd/api/crypto/prime.h b/src/workerd/api/crypto/prime.h index ceafc97781a..319616cce45 100644 --- a/src/workerd/api/crypto/prime.h +++ b/src/workerd/api/crypto/prime.h @@ -4,10 +4,16 @@ #include +namespace workerd::jsg { +class Lock; +class BufferSource; +} // namespace workerd::jsg + namespace workerd::api { // Generate a random prime number -kj::Array randomPrime(uint32_t size, +jsg::BufferSource randomPrime(jsg::Lock& js, + uint32_t size, bool safe, kj::Maybe> add_buf, kj::Maybe> rem_buf); diff --git a/src/workerd/api/node/crypto.c++ b/src/workerd/api/node/crypto.c++ index 9f5b04b94c7..336ed9924ac 100644 --- a/src/workerd/api/node/crypto.c++ +++ b/src/workerd/api/node/crypto.c++ @@ -103,11 +103,12 @@ kj::Maybe CryptoImpl::exportChallenge( return workerd::api::exportChallenge(js, input); } -kj::Array CryptoImpl::randomPrime(uint32_t size, +jsg::BufferSource CryptoImpl::randomPrime(jsg::Lock& js, + uint32_t size, bool safe, jsg::Optional> add_buf, jsg::Optional> rem_buf) { - return workerd::api::randomPrime(size, safe, + return workerd::api::randomPrime(js, size, safe, add_buf.map([](kj::Array& buf) { return buf.asPtr(); }), rem_buf.map([](kj::Array& buf) { return buf.asPtr(); })); } diff --git a/src/workerd/api/node/crypto.h b/src/workerd/api/node/crypto.h index 290d4abb7c4..d9881580aea 100644 --- a/src/workerd/api/node/crypto.h +++ b/src/workerd/api/node/crypto.h @@ -52,7 +52,8 @@ class CryptoImpl final: public jsg::Object { jsg::Ref DiffieHellmanGroupHandle(kj::String name); // Primes - kj::Array randomPrime(uint32_t size, + jsg::BufferSource randomPrime(jsg::Lock& js, + uint32_t size, bool safe, jsg::Optional> add, jsg::Optional> rem);