Skip to content

Commit

Permalink
Fixes same array in memory was used for generateRandom in web crypto
Browse files Browse the repository at this point in the history
I'm not entirely sure why `generateRandom` was reusing the same
Uint32Array array stored in `blockRandomArray` variable between calls.
It may be the relic of the past, or a bug that was only discovered
recently, since we stopped copying this array for some operations.

This was causing race condition errors in crypto browser tests,
specifically with `multiple_send_*` tests. They generated cipher key
using `Crypto.generateRandomKey()` and thus shared the same array in
memory which was used for iv generation on each message encryption.
This way, cipher key was actually changed in memory after each
`CBCCipher.encrypt` call, and if two messages were encrypted back to
back, then next call to `CBCCipher.decrypt` would fail due to incorrect
cipher key.

This regression was introduced in e14f1bf, when we switched from using
WordArray and `BufferUtils.toWordArray`, which was copying the array, to
ArrayBuffer and `BufferUtils.toArrayBuffer`, which is returning the same
array in memory.

Resolves #1557
  • Loading branch information
VeskeR committed Jan 19, 2024
1 parent 112eab3 commit fccd4ac
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/platform/web/lib/util/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ var createCryptoClass = function (config: IPlatformConfig, bufferUtils: typeof B
if (config.getRandomArrayBuffer) {
generateRandom = config.getRandomArrayBuffer;
} else if (typeof Uint32Array !== 'undefined' && config.getRandomValues) {
var blockRandomArray = new Uint32Array(DEFAULT_BLOCKLENGTH_WORDS);
generateRandom = function (bytes, callback) {
var blockRandomArray = new Uint32Array(DEFAULT_BLOCKLENGTH_WORDS);
var words = bytes / 4,
nativeArray = words == DEFAULT_BLOCKLENGTH_WORDS ? blockRandomArray : new Uint32Array(words);
config.getRandomValues!(nativeArray, function (err) {
Expand Down

0 comments on commit fccd4ac

Please sign in to comment.