diff options
Diffstat (limited to 'node_modules/browserify-aes/ghash.js')
-rw-r--r-- | node_modules/browserify-aes/ghash.js | 85 |
1 files changed, 38 insertions, 47 deletions
diff --git a/node_modules/browserify-aes/ghash.js b/node_modules/browserify-aes/ghash.js index 0ca143cd7..26bfeddd2 100644 --- a/node_modules/browserify-aes/ghash.js +++ b/node_modules/browserify-aes/ghash.js @@ -1,12 +1,30 @@ -var zeros = new Buffer(16) -zeros.fill(0) -module.exports = GHASH +var Buffer = require('safe-buffer').Buffer +var ZEROES = Buffer.alloc(16, 0) + +function toArray (buf) { + return [ + buf.readUInt32BE(0), + buf.readUInt32BE(4), + buf.readUInt32BE(8), + buf.readUInt32BE(12) + ] +} + +function fromArray (out) { + var buf = Buffer.allocUnsafe(16) + buf.writeUInt32BE(out[0] >>> 0, 0) + buf.writeUInt32BE(out[1] >>> 0, 4) + buf.writeUInt32BE(out[2] >>> 0, 8) + buf.writeUInt32BE(out[3] >>> 0, 12) + return buf +} + function GHASH (key) { this.h = key - this.state = new Buffer(16) - this.state.fill(0) - this.cache = new Buffer('') + this.state = Buffer.alloc(16, 0) + this.cache = Buffer.allocUnsafe(0) } + // from http://bitwiseshiftleft.github.io/sjcl/doc/symbols/src/core_gcm.js.html // by Juho Vähä-Herttua GHASH.prototype.ghash = function (block) { @@ -20,17 +38,20 @@ GHASH.prototype.ghash = function (block) { GHASH.prototype._multiply = function () { var Vi = toArray(this.h) var Zi = [0, 0, 0, 0] - var j, xi, lsb_Vi + var j, xi, lsbVi var i = -1 while (++i < 128) { - xi = (this.state[~~(i / 8)] & (1 << (7 - i % 8))) !== 0 + xi = (this.state[~~(i / 8)] & (1 << (7 - (i % 8)))) !== 0 if (xi) { // Z_i+1 = Z_i ^ V_i - Zi = xor(Zi, Vi) + Zi[0] ^= Vi[0] + Zi[1] ^= Vi[1] + Zi[2] ^= Vi[2] + Zi[3] ^= Vi[3] } // Store the value of LSB(V_i) - lsb_Vi = (Vi[3] & 1) !== 0 + lsbVi = (Vi[3] & 1) !== 0 // V_i+1 = V_i >> 1 for (j = 3; j > 0; j--) { @@ -39,12 +60,13 @@ GHASH.prototype._multiply = function () { Vi[0] = Vi[0] >>> 1 // If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R - if (lsb_Vi) { + if (lsbVi) { Vi[0] = Vi[0] ^ (0xe1 << 24) } } this.state = fromArray(Zi) } + GHASH.prototype.update = function (buf) { this.cache = Buffer.concat([this.cache, buf]) var chunk @@ -54,45 +76,14 @@ GHASH.prototype.update = function (buf) { this.ghash(chunk) } } + GHASH.prototype.final = function (abl, bl) { if (this.cache.length) { - this.ghash(Buffer.concat([this.cache, zeros], 16)) + this.ghash(Buffer.concat([this.cache, ZEROES], 16)) } - this.ghash(fromArray([ - 0, abl, - 0, bl - ])) + + this.ghash(fromArray([0, abl, 0, bl])) return this.state } -function toArray (buf) { - return [ - buf.readUInt32BE(0), - buf.readUInt32BE(4), - buf.readUInt32BE(8), - buf.readUInt32BE(12) - ] -} -function fromArray (out) { - out = out.map(fixup_uint32) - var buf = new Buffer(16) - buf.writeUInt32BE(out[0], 0) - buf.writeUInt32BE(out[1], 4) - buf.writeUInt32BE(out[2], 8) - buf.writeUInt32BE(out[3], 12) - return buf -} -var uint_max = Math.pow(2, 32) -function fixup_uint32 (x) { - var ret, x_pos - ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x - return ret -} -function xor (a, b) { - return [ - a[0] ^ b[0], - a[1] ^ b[1], - a[2] ^ b[2], - a[3] ^ b[3] - ] -} +module.exports = GHASH |