This proposes the addition of a user-addressable function that can be used to fill the
portion of an ArrayBuffer
associated with a TypedArray
with cryptographically secure pseudo-random number values.
Portions of this proposal are derived from the Web Cryptography API
Stage: 1
Champion: Ron Buckton (@rbuckton)
For detailed status of this proposal see TODO, below.
- Ron Buckton (@rbuckton)
One of the key issues for https://github.com/tc39/proposal-uuid is the lack of a "source of truth" for cryptographically secure pseudo-random numbers within the ECMAScript language. While hosts such as browsers and NodeJS provide implementations of CSPRNGs (Cryptographically Secure Pseudo-Random Number Generators), the ECMAScript language itself has no mechanism for supplying a CSPRNG that can be used by proposed APIs such as UUID.
- Provide a single "source of truth" for generating cryptographically secure pseudo-random values within the language.
- Provide a single location for mocking the CSPRNG, vs a method on each
TypedArray
prototype. - If introducing a new
crypto
global namespace for cryptography-related APIs in ECMA-262, we should ensure that the Web cryptography APIs could be layered on top.
- Web Cryptography API:
crypto.getRandomValues
- NodeJS:
crypto.randomFillSync
We are still investigating the API surface area for this proposal. The intended API would be a user-addressable function that when called executes the FillRandomValues abstract operation, below.
The Stage 0 API proposal exposed this as a static ArrayBuffer.fillRandom
method, although we are continuing to investigate this space.
When abstract operation FillRandomValues is called with argument view, the following steps are taken:
- Perform ? RequireInternalSlot(view, [[TypedArrayName]]).
- Assert: view has the [[ViewedArrayBuffer]], [[ByteLength]], and [[ByteOffset]] internal slots.
- If view.[[TypedArrayName]] is not one of
"Int8Array"
,"Uint8Array"
,"Uint8ClampedArray"
,"Int16Array"
,"Uint16Array"
,"Int32Array"
,"Uint32Array"
,"BigInt64Array"
, or"BigUint64Array"
, throw a TypeError exception. - Let buffer be view.[[ViewedArrayBuffer]].
- If ! IsDetachedBuffer(buffer) is true, throw a TypeError exception.
- Let byteLength be view.[[ByteLength]].
- If byteLength is greater than 65536, throw a RangeError exception.
- Let byteOffset be view.[[ByteOffset]].
- Let byteEndOffset be byteOffset + byteLength.
- Overwrite the elements of buffer from index byteOffset (inclusive) through index byteEndOffset (exclusive) with cryptographically secure random values.
- Return view.
Note
Implementations should generate cryptographically secure random values using well-established cryptographic pseudo-random number generators seeded with high-quality entropy, such as from an operating-system entropy source (e.g., "/dev/urandom"). This specification provides no lower-bound on the information theoretic entropy present in cryptographically secure random values, but implementations should make a best effort to provide as much entropy as practicable.
Note
This interface defines a synchronous method for obtaining cryptographically secure random values. While some devices and implementations may support truly random cryptographic number generators or provide interfaces that block when there is insufficient entropy, implementations are discouraged from using these sources when implementing getRandomValues, both for performance and to avoid depleting the system of entropy. Instead, these sources should be used to seed a cryptographic pseudo-random number generator that can then return suitable values efficiently.
- Separate proposal for CSPRNG "source of truth": tc39/proposal-uuid#37
The following is a high-level list of tasks to progress through each stage of the TC39 proposal process:
- Identified a "champion" who will advance the addition.
- Prose outlining the problem or need and the general shape of a solution.
- Illustrative examples of usage.
- High-level API.
- Initial specification text.
- Transpiler support (Optional).
- Complete specification text.
- Designated reviewers have signed off on the current spec text.
- The ECMAScript editor has signed off on the current spec text.
- Test262 acceptance tests have been written for mainline usage scenarios and merged.
- Two compatible implementations which pass the acceptance tests: [1], [2].
- A pull request has been sent to tc39/ecma262 with the integrated spec text.
- The ECMAScript editor has signed off on the pull request.