2017-05-03 15:35:00 +02:00
|
|
|
var AuthCipher = require('./authCipher')
|
2017-12-10 21:51:33 +01:00
|
|
|
var Buffer = require('safe-buffer').Buffer
|
|
|
|
var MODES = require('./modes')
|
|
|
|
var StreamCipher = require('./streamCipher')
|
|
|
|
var Transform = require('cipher-base')
|
|
|
|
var aes = require('./aes')
|
2017-05-03 15:35:00 +02:00
|
|
|
var ebtk = require('evp_bytestokey')
|
2017-12-10 21:51:33 +01:00
|
|
|
var inherits = require('inherits')
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
function Decipher (mode, key, iv) {
|
|
|
|
Transform.call(this)
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
this._cache = new Splitter()
|
|
|
|
this._last = void 0
|
|
|
|
this._cipher = new aes.AES(key)
|
2017-12-10 21:51:33 +01:00
|
|
|
this._prev = Buffer.from(iv)
|
2017-05-03 15:35:00 +02:00
|
|
|
this._mode = mode
|
|
|
|
this._autopadding = true
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
|
|
|
inherits(Decipher, Transform)
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Decipher.prototype._update = function (data) {
|
|
|
|
this._cache.add(data)
|
|
|
|
var chunk
|
|
|
|
var thing
|
|
|
|
var out = []
|
|
|
|
while ((chunk = this._cache.get(this._autopadding))) {
|
|
|
|
thing = this._mode.decrypt(this, chunk)
|
|
|
|
out.push(thing)
|
|
|
|
}
|
|
|
|
return Buffer.concat(out)
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Decipher.prototype._final = function () {
|
|
|
|
var chunk = this._cache.flush()
|
|
|
|
if (this._autopadding) {
|
|
|
|
return unpad(this._mode.decrypt(this, chunk))
|
|
|
|
} else if (chunk) {
|
|
|
|
throw new Error('data not multiple of block length')
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Decipher.prototype.setAutoPadding = function (setTo) {
|
|
|
|
this._autopadding = !!setTo
|
|
|
|
return this
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
function Splitter () {
|
2017-12-10 21:51:33 +01:00
|
|
|
this.cache = Buffer.allocUnsafe(0)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Splitter.prototype.add = function (data) {
|
|
|
|
this.cache = Buffer.concat([this.cache, data])
|
|
|
|
}
|
|
|
|
|
|
|
|
Splitter.prototype.get = function (autoPadding) {
|
|
|
|
var out
|
|
|
|
if (autoPadding) {
|
|
|
|
if (this.cache.length > 16) {
|
|
|
|
out = this.cache.slice(0, 16)
|
|
|
|
this.cache = this.cache.slice(16)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (this.cache.length >= 16) {
|
|
|
|
out = this.cache.slice(0, 16)
|
|
|
|
this.cache = this.cache.slice(16)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
return null
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Splitter.prototype.flush = function () {
|
2017-12-10 21:51:33 +01:00
|
|
|
if (this.cache.length) return this.cache
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
function unpad (last) {
|
|
|
|
var padded = last[15]
|
|
|
|
var i = -1
|
|
|
|
while (++i < padded) {
|
|
|
|
if (last[(i + (16 - padded))] !== padded) {
|
|
|
|
throw new Error('unable to decrypt data')
|
|
|
|
}
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
if (padded === 16) return
|
2017-05-03 15:35:00 +02:00
|
|
|
|
2017-12-10 21:51:33 +01:00
|
|
|
return last.slice(0, 16 - padded)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDecipheriv (suite, password, iv) {
|
2017-12-10 21:51:33 +01:00
|
|
|
var config = MODES[suite.toLowerCase()]
|
|
|
|
if (!config) throw new TypeError('invalid suite type')
|
|
|
|
|
|
|
|
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 (typeof password === 'string') password = Buffer.from(password)
|
|
|
|
if (password.length !== config.key / 8) throw new TypeError('invalid key length ' + password.length)
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
if (config.type === 'stream') {
|
2017-12-10 21:51:33 +01:00
|
|
|
return new StreamCipher(config.module, password, iv, true)
|
2017-05-03 15:35:00 +02:00
|
|
|
} else if (config.type === 'auth') {
|
2017-12-10 21:51:33 +01:00
|
|
|
return new AuthCipher(config.module, password, iv, true)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
|
|
|
return new Decipher(config.module, password, iv)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDecipher (suite, password) {
|
2017-12-10 21:51:33 +01:00
|
|
|
var config = MODES[suite.toLowerCase()]
|
|
|
|
if (!config) throw new TypeError('invalid suite type')
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
var keys = ebtk(password, false, config.key, config.iv)
|
|
|
|
return createDecipheriv(suite, keys.key, keys.iv)
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
exports.createDecipher = createDecipher
|
|
|
|
exports.createDecipheriv = createDecipheriv
|