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

reduce buffer creation for ctr mode #48

Merged
merged 1 commit into from
Aug 18, 2017
Merged
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
8 changes: 6 additions & 2 deletions aes.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,13 @@ AES.prototype._reset = function () {
this._invKeySchedule = invKeySchedule
}

AES.prototype.encryptBlock = function (M) {
AES.prototype.encryptBlockRaw = function (M) {
M = asUInt32Array(M)
var out = cryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX, this._nRounds)
return cryptBlock(M, this._keySchedule, G.SUB_MIX, G.SBOX, this._nRounds)
}

AES.prototype.encryptBlock = function (M) {
var out = this.encryptBlockRaw(M)
var buf = Buffer.allocUnsafe(16)
buf.writeUInt32BE(out[0], 0)
buf.writeUInt32BE(out[1], 4)
Expand Down
2 changes: 1 addition & 1 deletion bench/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let key = Buffer.alloc(16, 0xff)
let iv = Buffer.alloc(16, 0x01)

function test (mod, message) {
let cipher = mod.createCipheriv('aes-128-cbc', key, iv)
let cipher = mod.createCipheriv('aes-128-ctr', key, iv)
let b = cipher.update(message)
return Buffer.concat([b, cipher.final()])
}
Expand Down
18 changes: 15 additions & 3 deletions modes/ctr.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,26 @@ function incr32 (iv) {
}

function getBlock (self) {
var out = self._cipher.encryptBlock(self._prev)
var out = self._cipher.encryptBlockRaw(self._prev)
incr32(self._prev)
return out
}

var blockSize = 16
exports.encrypt = function (self, chunk) {
while (self._cache.length < chunk.length) {
self._cache = Buffer.concat([self._cache, getBlock(self)])
var chunkNum = Math.ceil(chunk.length / blockSize)
var start = self._cache.length
self._cache = Buffer.concat([
self._cache,
Buffer.allocUnsafe(chunkNum * blockSize)
])
for (var i = 0; i < chunkNum; i++) {
var out = getBlock(self)
var offset = start + i * blockSize
self._cache.writeUInt32BE(out[0], offset + 0)
self._cache.writeUInt32BE(out[1], offset + 4)
self._cache.writeUInt32BE(out[2], offset + 8)
self._cache.writeUInt32BE(out[3], offset + 12)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really better than a copy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The writeUint is not better, but the reason I am doing this is that it avoids creating intermediary buffers. Before encryptBlock creates a new buffer, fills it with those four values, just so that the buffer can be copied in here. By using the array directly there is now one less buffer allocation in the loop and together with the preallocation, no buffer allocations are in the loop.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, my mistake, I thought out was a Buffer.
ACK :)

}
var pad = self._cache.slice(0, chunk.length)
self._cache = self._cache.slice(chunk.length)
Expand Down