From 2a5a23aa10c37e233041e76d1ff9f57f8bcc7f0a Mon Sep 17 00:00:00 2001 From: zboris12 Date: Sun, 28 Apr 2024 15:52:18 +0900 Subject: [PATCH] Added support to perform encryption/decryption from the middle of a message by specifying a counter value in CTR mode. --- lib/cipherModes.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/cipherModes.js b/lib/cipherModes.js index 339915cc1..67277efc4 100644 --- a/lib/cipherModes.js +++ b/lib/cipherModes.js @@ -446,6 +446,9 @@ modes.ctr.prototype.start = function(options) { // use IV as first input this._iv = transformIV(options.iv, this.blockSize); this._inBlock = this._iv.slice(0); + if(options.counter) { + inc32(this._inBlock, options.counter); + } this._partialBytes = 0; }; @@ -988,9 +991,9 @@ function transformIV(iv, blockSize) { return iv; } -function inc32(block) { +function inc32(block, count) { // increment last 32 bits of block only - block[block.length - 1] = (block[block.length - 1] + 1) & 0xFFFFFFFF; + block[block.length - 1] = (block[block.length - 1] + (count || 1)) & 0xFFFFFFFF; } function from64To32(num) {