diff options
Diffstat (limited to 'node_modules/browserify-aes/encrypter.js')
-rw-r--r-- | node_modules/browserify-aes/encrypter.js | 96 |
1 files changed, 44 insertions, 52 deletions
diff --git a/node_modules/browserify-aes/encrypter.js b/node_modules/browserify-aes/encrypter.js index 3d3f561e3..0c4c58bc0 100644 --- a/node_modules/browserify-aes/encrypter.js +++ b/node_modules/browserify-aes/encrypter.js @@ -1,56 +1,63 @@ -var aes = require('./aes') +var MODES = require('./modes') +var AuthCipher = require('./authCipher') +var Buffer = require('safe-buffer').Buffer +var StreamCipher = require('./streamCipher') var Transform = require('cipher-base') -var inherits = require('inherits') -var modes = require('./modes') +var aes = require('./aes') var ebtk = require('evp_bytestokey') -var StreamCipher = require('./streamCipher') -var AuthCipher = require('./authCipher') -inherits(Cipher, Transform) +var inherits = require('inherits') + function Cipher (mode, key, iv) { - if (!(this instanceof Cipher)) { - return new Cipher(mode, key, iv) - } Transform.call(this) + this._cache = new Splitter() this._cipher = new aes.AES(key) - this._prev = new Buffer(iv.length) - iv.copy(this._prev) + this._prev = Buffer.from(iv) this._mode = mode this._autopadding = true } + +inherits(Cipher, Transform) + Cipher.prototype._update = function (data) { this._cache.add(data) var chunk var thing var out = [] + while ((chunk = this._cache.get())) { thing = this._mode.encrypt(this, chunk) out.push(thing) } + return Buffer.concat(out) } + +var PADDING = Buffer.alloc(16, 0x10) + Cipher.prototype._final = function () { var chunk = this._cache.flush() if (this._autopadding) { chunk = this._mode.encrypt(this, chunk) this._cipher.scrub() return chunk - } else if (chunk.toString('hex') !== '10101010101010101010101010101010') { + } + + if (!chunk.equals(PADDING)) { this._cipher.scrub() throw new Error('data not multiple of block length') } } + Cipher.prototype.setAutoPadding = function (setTo) { this._autopadding = !!setTo return this } function Splitter () { - if (!(this instanceof Splitter)) { - return new Splitter() - } - this.cache = new Buffer('') + this.cache = Buffer.allocUnsafe(0) } + Splitter.prototype.add = function (data) { this.cache = Buffer.concat([this.cache, data]) } @@ -63,57 +70,42 @@ Splitter.prototype.get = function () { } return null } + Splitter.prototype.flush = function () { var len = 16 - this.cache.length - var padBuff = new Buffer(len) + var padBuff = Buffer.allocUnsafe(len) var i = -1 while (++i < len) { padBuff.writeUInt8(len, i) } - var out = Buffer.concat([this.cache, padBuff]) - return out -} -var modelist = { - ECB: require('./modes/ecb'), - CBC: require('./modes/cbc'), - CFB: require('./modes/cfb'), - CFB8: require('./modes/cfb8'), - CFB1: require('./modes/cfb1'), - OFB: require('./modes/ofb'), - CTR: require('./modes/ctr'), - GCM: require('./modes/ctr') + + return Buffer.concat([this.cache, padBuff]) } function createCipheriv (suite, password, iv) { - var config = modes[suite.toLowerCase()] - if (!config) { - throw new TypeError('invalid suite type') - } - if (typeof iv === 'string') { - iv = new Buffer(iv) - } - if (typeof password === 'string') { - password = new Buffer(password) - } - if (password.length !== config.key / 8) { - throw new TypeError('invalid key length ' + password.length) - } - if (iv.length !== config.iv) { - throw new TypeError('invalid iv length ' + iv.length) - } + var config = MODES[suite.toLowerCase()] + if (!config) throw new TypeError('invalid suite type') + + if (typeof password === 'string') password = Buffer.from(password) + if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length) + + if (typeof iv === 'string') iv = Buffer.from(iv) + if (config.mode !== 'GCM' && iv.length !== config.iv) throw new TypeError('invalid iv length ' + iv.length) + if (config.type === 'stream') { - return new StreamCipher(modelist[config.mode], password, iv) + return new StreamCipher(config.module, password, iv) } else if (config.type === 'auth') { - return new AuthCipher(modelist[config.mode], password, iv) + return new AuthCipher(config.module, password, iv) } - return new Cipher(modelist[config.mode], password, iv) + + return new Cipher(config.module, password, iv) } + function createCipher (suite, password) { - var config = modes[suite.toLowerCase()] - if (!config) { - throw new TypeError('invalid suite type') - } + var config = MODES[suite.toLowerCase()] + if (!config) throw new TypeError('invalid suite type') + var keys = ebtk(password, false, config.key, config.iv) return createCipheriv(suite, keys.key, keys.iv) } |