Skip to content
Open
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
14 changes: 14 additions & 0 deletions src/mode-ctr.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ CryptoJS.mode.CTR = (function () {
if (iv) {
counter = this._counter = iv.slice(0);

// Zero-pad a short IV (e.g. a 12-byte/3-word nonce) up to
// the cipher's full block size. Without this, counter[blockSize - 1]
// is `undefined` after the first block, and `undefined + 1 | 0`
// evaluates to 0 - the same value implicitly used (via bitwise
// coercion) as the missing word during that first block's
// keystream generation. That makes the counter appear
// unchanged for one extra block, so blocks 1 and 2 are
// encrypted with an identical keystream (a critical CTR-mode
// keystream-reuse break) before incrementing correctly from
// block 3 onward.
for (var i = counter.length; i < blockSize; i++) {
counter[i] = 0;
}

// Remove IV for subsequent blocks
this._iv = undefined;
}
Expand Down