Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array length Allocation failed issue when Message is huge #255

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
// Clamp excess bits
this.clamp();

// Set array length first to fix array length allocation issue
if (thisWords.length < Math.ceil((thisSigBytes + thatSigBytes) / 4)) {
thisWords.length = Math.ceil((thisSigBytes + thatSigBytes) / 4);
}

// Concat
if (thisSigBytes % 4) {
// Copy one byte at a time
Expand Down Expand Up @@ -406,7 +411,7 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
var hexStrLength = hexStr.length;

// Convert
var words = [];
var words = new Array((hexStrLength + 7) >>> 3);
for (var i = 0; i < hexStrLength; i += 2) {
words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
}
Expand Down Expand Up @@ -465,7 +470,7 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
var latin1StrLength = latin1Str.length;

// Convert
var words = [];
var words = new Array((latin1StrLength + 3) >>> 2);
for (var i = 0; i < latin1StrLength; i++) {
words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
}
Expand Down Expand Up @@ -602,6 +607,10 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {

// Process blocks
if (nWordsReady) {
if (dataWords.length < nWordsReady) {
data.words = dataWords.concat(new Array(nWordsReady - dataWords.length));
dataWords = data.words;
}
for (var offset = 0; offset < nWordsReady; offset += blockSize) {
// Perform concrete-algorithm logic
this._doProcessBlock(dataWords, offset);
Expand Down
2 changes: 1 addition & 1 deletion src/enc-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
};

function parseLoop(base64Str, base64StrLength, reverseMap) {
var words = [];
var words = new Array((base64StrLength - (base64StrLength >>> 2) + 2) >>> 2);
var nBytes = 0;
for (var i = 0; i < base64StrLength; i++) {
if (i % 4) {
Expand Down
2 changes: 1 addition & 1 deletion src/enc-utf16.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
var utf16StrLength = utf16Str.length;

// Convert
var words = [];
var words = new Array((utf16StrLength + 1) >>> 1);
for (var i = 0; i < utf16StrLength; i++) {
words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
}
Expand Down
6 changes: 6 additions & 0 deletions src/md5.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@
var nBitsTotal = this._nDataBytes * 8;
var nBitsLeft = data.sigBytes * 8;

var newLength = Math.max((nBitsLeft >>> 5) + 1, (((nBitsLeft + 64) >>> 9) << 4) + 16);
if (dataWords.length < newLength) {
data.words = dataWords.concat(new Array(newLength - dataWords.length));
dataWords = data.words;
}

// Add padding
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);

Expand Down
6 changes: 6 additions & 0 deletions src/ripemd160.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
var nBitsTotal = this._nDataBytes * 8;
var nBitsLeft = data.sigBytes * 8;

var newLength = Math.max((nBitsLeft >>> 5) + 1, (((nBitsLeft + 64) >>> 9) << 4) + 15);
if (dataWords.length < newLength) {
data.words = dataWords.concat(new Array(newLength - dataWords.length));
dataWords = data.words;
}

// Add padding
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
Expand Down
6 changes: 6 additions & 0 deletions src/sha1.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@
var nBitsTotal = this._nDataBytes * 8;
var nBitsLeft = data.sigBytes * 8;

var newLength = Math.max((nBitsLeft >>> 5) + 1, (((nBitsLeft + 64) >>> 9) << 4) + 16);
if (dataWords.length < newLength) {
data.words = dataWords.concat(new Array(newLength - dataWords.length));
dataWords = data.words;
}

// Add padding
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
Expand Down
6 changes: 6 additions & 0 deletions src/sha256.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@
var nBitsTotal = this._nDataBytes * 8;
var nBitsLeft = data.sigBytes * 8;

var newLength = Math.max((nBitsLeft >>> 5) + 1, (((nBitsLeft + 64) >>> 9) << 4) + 16);
if (dataWords.length < newLength) {
data.words = dataWords.concat(new Array(newLength - dataWords.length));
dataWords = data.words;
}

// Add padding
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
Expand Down
6 changes: 6 additions & 0 deletions src/sha3.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@
var nBitsLeft = data.sigBytes * 8;
var blockSizeBits = this.blockSize * 32;

var newLength = Math.max((nBitsLeft >>> 5) + 1, (Math.ceil((nBitsLeft + 1) / blockSizeBits) * blockSizeBits) >>> 5);
if (dataWords.length < newLength) {
data.words = dataWords.concat(new Array(newLength - dataWords.length));
dataWords = data.words;
}

// Add padding
dataWords[nBitsLeft >>> 5] |= 0x1 << (24 - nBitsLeft % 32);
dataWords[((Math.ceil((nBitsLeft + 1) / blockSizeBits) * blockSizeBits) >>> 5) - 1] |= 0x80;
Expand Down
6 changes: 6 additions & 0 deletions src/sha512.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@
var nBitsTotal = this._nDataBytes * 8;
var nBitsLeft = data.sigBytes * 8;

var newLength = Math.max((nBitsLeft >>> 5) + 1, (((nBitsLeft + 128) >>> 10) << 5) + 32);
if (dataWords.length < newLength) {
data.words = dataWords.concat(new Array(newLength - dataWords.length));
dataWords = data.words;
}

// Add padding
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000);
Expand Down