react / wstein / node_modules / browserify / node_modules / crypto-browserify / node_modules / browserify-aes / ghash.js
80540 viewsvar zeros = new Buffer(16)1zeros.fill(0)2module.exports = GHASH3function GHASH (key) {4this.h = key5this.state = new Buffer(16)6this.state.fill(0)7this.cache = new Buffer('')8}9// from http://bitwiseshiftleft.github.io/sjcl/doc/symbols/src/core_gcm.js.html10// by Juho Vähä-Herttua11GHASH.prototype.ghash = function (block) {12var i = -113while (++i < block.length) {14this.state[i] ^= block[i]15}16this._multiply()17}1819GHASH.prototype._multiply = function () {20var Vi = toArray(this.h)21var Zi = [0, 0, 0, 0]22var j, xi, lsb_Vi23var i = -124while (++i < 128) {25xi = (this.state[~~(i / 8)] & (1 << (7 - i % 8))) !== 026if (xi) {27// Z_i+1 = Z_i ^ V_i28Zi = xor(Zi, Vi)29}3031// Store the value of LSB(V_i)32lsb_Vi = (Vi[3] & 1) !== 03334// V_i+1 = V_i >> 135for (j = 3; j > 0; j--) {36Vi[j] = (Vi[j] >>> 1) | ((Vi[j - 1] & 1) << 31)37}38Vi[0] = Vi[0] >>> 13940// If LSB(V_i) is 1, V_i+1 = (V_i >> 1) ^ R41if (lsb_Vi) {42Vi[0] = Vi[0] ^ (0xe1 << 24)43}44}45this.state = fromArray(Zi)46}47GHASH.prototype.update = function (buf) {48this.cache = Buffer.concat([this.cache, buf])49var chunk50while (this.cache.length >= 16) {51chunk = this.cache.slice(0, 16)52this.cache = this.cache.slice(16)53this.ghash(chunk)54}55}56GHASH.prototype.final = function (abl, bl) {57if (this.cache.length) {58this.ghash(Buffer.concat([this.cache, zeros], 16))59}60this.ghash(fromArray([610, abl,620, bl63]))64return this.state65}6667function toArray (buf) {68return [69buf.readUInt32BE(0),70buf.readUInt32BE(4),71buf.readUInt32BE(8),72buf.readUInt32BE(12)73]74}75function fromArray (out) {76out = out.map(fixup_uint32)77var buf = new Buffer(16)78buf.writeUInt32BE(out[0], 0)79buf.writeUInt32BE(out[1], 4)80buf.writeUInt32BE(out[2], 8)81buf.writeUInt32BE(out[3], 12)82return buf83}84var uint_max = Math.pow(2, 32)85function fixup_uint32 (x) {86var ret, x_pos87ret = x > uint_max || x < 0 ? (x_pos = Math.abs(x) % uint_max, x < 0 ? uint_max - x_pos : x_pos) : x88return ret89}90function xor (a, b) {91return [92a[0] ^ b[0],93a[1] ^ b[1],94a[2] ^ b[2],95a[3] ^ b[3]96]97}9899100