Skip to content

Commit

Permalink
Update randomPrime to use BufferSource
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Nov 13, 2024
1 parent c51e9f3 commit fef9432
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/workerd/api/crypto/prime.c++
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include "prime.h"

#include <workerd/api/crypto/impl.h>
#include <workerd/jsg/jsg.h>

#include <openssl/bn.h>

namespace workerd::api {

kj::Array<kj::byte> randomPrime(uint32_t size,
jsg::BufferSource randomPrime(jsg::Lock& js,
uint32_t size,
bool safe,
kj::Maybe<kj::ArrayPtr<kj::byte>> add_buf,
kj::Maybe<kj::ArrayPtr<kj::byte>> rem_buf) {
Expand Down Expand Up @@ -85,7 +87,8 @@ kj::Array<kj::byte> 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<kj::byte> bufferView, uint32_t num_checks) {
Expand Down
8 changes: 7 additions & 1 deletion src/workerd/api/crypto/prime.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

#include <cstdint>

namespace workerd::jsg {
class Lock;
class BufferSource;
} // namespace workerd::jsg

namespace workerd::api {

// Generate a random prime number
kj::Array<kj::byte> randomPrime(uint32_t size,
jsg::BufferSource randomPrime(jsg::Lock& js,
uint32_t size,
bool safe,
kj::Maybe<kj::ArrayPtr<kj::byte>> add_buf,
kj::Maybe<kj::ArrayPtr<kj::byte>> rem_buf);
Expand Down
5 changes: 3 additions & 2 deletions src/workerd/api/node/crypto.c++
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ kj::Maybe<jsg::BufferSource> CryptoImpl::exportChallenge(
return workerd::api::exportChallenge(js, input);
}

kj::Array<kj::byte> CryptoImpl::randomPrime(uint32_t size,
jsg::BufferSource CryptoImpl::randomPrime(jsg::Lock& js,
uint32_t size,
bool safe,
jsg::Optional<kj::Array<kj::byte>> add_buf,
jsg::Optional<kj::Array<kj::byte>> rem_buf) {
return workerd::api::randomPrime(size, safe,
return workerd::api::randomPrime(js, size, safe,
add_buf.map([](kj::Array<kj::byte>& buf) { return buf.asPtr(); }),
rem_buf.map([](kj::Array<kj::byte>& buf) { return buf.asPtr(); }));
}
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/api/node/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class CryptoImpl final: public jsg::Object {
jsg::Ref<DiffieHellmanHandle> DiffieHellmanGroupHandle(kj::String name);

// Primes
kj::Array<kj::byte> randomPrime(uint32_t size,
jsg::BufferSource randomPrime(jsg::Lock& js,
uint32_t size,
bool safe,
jsg::Optional<kj::Array<kj::byte>> add,
jsg::Optional<kj::Array<kj::byte>> rem);
Expand Down

0 comments on commit fef9432

Please sign in to comment.