w3c / w3c/webcrypto

support SharedArrayBuffers with crypto.getRandomValues

Open
#421 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs implementor interest needs tests
Dominant language
HTML
Stars
294
Forks
78
PR merge metrics
No merged PRs in 30d

Description

(forking off of #213)

trying to use crypto.getRandomValues on TypedArrays backed by a SharedArrayBuffer fails. example from Chrome JS console:

// works fine
> ab = new ArrayBuffer(10); u8 = new Uint8Array(ab); crypto.getRandomValues(u8);
Uint8Array(10) [134, 194, 41, 240, 231, 151, 173, 248, 154, 113, buffer: ArrayBuffer(10), byteLength: 10, byteOffset: 0, length: 10, Symbol(Symbol.toStringTag): 'Uint8Array']

// fails
> ab = new SharedArrayBuffer(10); u8 = new Uint8Array(ab); crypto.getRandomValues(u8);
VM253:1 Uncaught TypeError: Failed to execute 'getRandomValues' on 'Crypto': The provided ArrayBufferView value must not be shared.

supporting SharedArrayBuffers is important for me for 2 reasons:

  • using threads with WASI requires the WASM module be backed by a SharedArrayBuffer
  • message passing between main thread & Worker thread has lower overhead with SharedArrayBuffer

it can obviously be worked around with creating a temporary ArrayBuffer and then copying between them, but seems better to not waste the CPU cycles.

ab = new ArrayBuffer(10)
sab = new SharedArrayBuffer(10)
u8 = new Uint8Array(ab)
su8 = new Uint8Array(sab)
crypto.getRandomValues(u8)
su8.set(u8)

I'm mostly interested in crypto.getRandomValues, but if there's other APIs that would benefit, throw them in the pile I guess.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the Chrome console reproduction from the issue, comparing getRandomValues with ArrayBuffer- and SharedArrayBuffer-backed TypedArrays. Start by reviewing the Web Crypto getRandomValues API behavior and the related issue #213; done means SharedArrayBuffer-backed views are supported without the temporary-copy workaround, with any additional affected APIs identified.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, wasm
Domain
api, cryptography
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.