GeezerVison is showing github.com — crypto: improve random synchronous number generation performance · nodejs/node@bb1def2
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/random.js +2-10Lines changed: 2 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -35,6 +35,7 @@ const { | 35 | 35 | CheckPrimeJob, | 36 | 36 | kCryptoJobAsync, | 37 | 37 | kCryptoJobSync, | | 38 | + randomFillSync: randomFillSyncImpl, | 38 | 39 | secureBuffer, | 39 | 40 | } = internalBinding('crypto'); | 40 | 41 |
| @@ -184,16 +185,7 @@ function randomFillSync(buf, offset = 0, size) { | 184 | 185 | if (size === 0) | 185 | 186 | return buf; | 186 | 187 |
| 187 | | - const job = new RandomBytesJob( | 188 | | - kCryptoJobSync, | 189 | | - buf, | 190 | | - offset, | 191 | | - size); | 192 | | - | 193 | | - const err = job.run()[0]; | 194 | | - if (err) | 195 | | - throw err; | 196 | | - | | 188 | + randomFillSyncImpl(buf, offset, size); | 197 | 189 | return buf; | 198 | 190 | } | 199 | 191 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/crypto/crypto_random.cc +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -211,13 +211,46 @@ MaybeLocal<Value> CheckPrimeTraits::EncodeOutput(Environment* env, | 211 | 211 | } | 212 | 212 |
| 213 | 213 | namespace Random { | | 214 | +static void RandomFillSync(const FunctionCallbackInfo<Value>& args) { | | 215 | + Environment* env = Environment::GetCurrent(args); | | 216 | + CHECK(IsAnyBufferSource(args[0])); // Buffer to fill | | 217 | + CHECK(args[1]->IsUint32()); // Offset | | 218 | + CHECK(args[2]->IsUint32()); // Size | | 219 | + | | 220 | + ArrayBufferOrViewContents<unsigned char> in(args[0]); | | 221 | + | | 222 | + const uint32_t byte_offset = args[1].As<Uint32>()->Value(); | | 223 | + const uint32_t size = args[2].As<Uint32>()->Value(); | | 224 | + CHECK_GE(byte_offset + size, byte_offset); // Overflow check. | | 225 | + CHECK_LE(byte_offset + size, in.size()); // Bounds check. | | 226 | + | | 227 | + env->PrintSyncTrace(); | | 228 | + if (ERR_peek_error() != 0) ERR_clear_error(); | | 229 | + if (ncrypto::CSPRNG(in.data() + byte_offset, size)) { | | 230 | + if (ERR_peek_error() != 0) ERR_clear_error(); | | 231 | + return; | | 232 | + } | | 233 | + | | 234 | + CryptoErrorStore errors; | | 235 | + errors.Capture(); | | 236 | + if (errors.Empty()) { | | 237 | + errors.Insert(NodeCryptoError::DERIVING_BITS_FAILED); | | 238 | + } | | 239 | + Local<Value> exception; | | 240 | + if (errors.ToException(env).ToLocal(&exception)) { | | 241 | + env->isolate()->ThrowException(exception); | | 242 | + } | | 243 | +} | | 244 | + | 214 | 245 | void Initialize(Environment* env, Local<Object> target) { | | 246 | + SetMethod(env->context(), target, "randomFillSync", RandomFillSync); | 215 | 247 | RandomBytesJob::Initialize(env, target); | 216 | 248 | RandomPrimeJob::Initialize(env, target); | 217 | 249 | CheckPrimeJob::Initialize(env, target); | 218 | 250 | } | 219 | 251 |
| 220 | 252 | void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | | 253 | + registry->Register(RandomFillSync); | 221 | 254 | RandomBytesJob::RegisterExternalReferences(registry); | 222 | 255 | RandomPrimeJob::RegisterExternalReferences(registry); | 223 | 256 | CheckPrimeJob::RegisterExternalReferences(registry); | |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +if (!common.hasCrypto) | | 4 | + common.skip('missing crypto'); | | 5 | + | | 6 | +const assert = require('assert'); | | 7 | +const { spawnSync } = require('child_process'); | | 8 | + | | 9 | +// randomFillSync() should be reported by --trace-sync-io when it runs after | | 10 | +// the first event loop turn. | | 11 | + | | 12 | +if (process.argv[2] === 'child') { | | 13 | + setImmediate(() => { | | 14 | + require('crypto').randomFillSync(Buffer.alloc(16)); | | 15 | + }); | | 16 | + return; | | 17 | +} | | 18 | + | | 19 | +const { stderr, status } = spawnSync(process.execPath, | | 20 | + ['--trace-sync-io', __filename, 'child'], | | 21 | + { encoding: 'utf8' }); | | 22 | +assert.strictEqual(status, 0); | | 23 | +assert.match(stderr, /WARNING: Detected use of sync API[\s\S]*randomFillSync/); |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/crypto.d.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1002,6 +1002,7 @@ export interface CryptoBinding { | 1002 | 1002 | privateEncrypt: InternalCryptoBinding.PublicKeyCipher; | 1003 | 1003 | publicDecrypt: InternalCryptoBinding.PublicKeyCipher; | 1004 | 1004 | publicEncrypt: InternalCryptoBinding.PublicKeyCipher; | | 1005 | + randomFillSync(buf: ArrayBufferLike | ArrayBufferView, offset: number, size: number): void; | 1005 | 1006 | resetRootCertStore(): void; | 1006 | 1007 | secureBuffer(length: number): Uint8Array | undefined; | 1007 | 1008 | secureHeapUsed(): bigint | undefined; | |
|