2017-12-10 21:51:33 +01:00
|
|
|
var MODES = require('./modes')
|
|
|
|
var AuthCipher = require('./authCipher')
|
|
|
|
var Buffer = require('safe-buffer').Buffer
|
|
|
|
var StreamCipher = require('./streamCipher')
|
2017-05-03 15:35:00 +02:00
|
|
|
var Transform = require('cipher-base')
|
2017-12-10 21:51:33 +01:00
|
|
|
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 Cipher (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._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(Cipher, Transform)
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Cipher.prototype._update = function (data) {
|
|
|
|
this._cache.add(data)
|
|
|
|
var chunk
|
|
|
|
var thing
|
|
|
|
var out = []
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
while ((chunk = this._cache.get())) {
|
|
|
|
thing = this._mode.encrypt(this, chunk)
|
|
|
|
out.push(thing)
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
return Buffer.concat(out)
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
|
|
|
var PADDING = Buffer.alloc(16, 0x10)
|
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Cipher.prototype._final = function () {
|
|
|
|
var chunk = this._cache.flush()
|
|
|
|
if (this._autopadding) {
|
|
|
|
chunk = this._mode.encrypt(this, chunk)
|
|
|
|
this._cipher.scrub()
|
|
|
|
return chunk
|
2017-12-10 21:51:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!chunk.equals(PADDING)) {
|
2017-05-03 15:35:00 +02:00
|
|
|
this._cipher.scrub()
|
|
|
|
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
|
|
|
Cipher.prototype.setAutoPadding = function (setTo) {
|
|
|
|
this._autopadding = !!setTo
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
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 () {
|
|
|
|
if (this.cache.length > 15) {
|
|
|
|
var out = this.cache.slice(0, 16)
|
|
|
|
this.cache = this.cache.slice(16)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
2017-05-03 15:35:00 +02:00
|
|
|
Splitter.prototype.flush = function () {
|
|
|
|
var len = 16 - this.cache.length
|
2017-12-10 21:51:33 +01:00
|
|
|
var padBuff = Buffer.allocUnsafe(len)
|
2017-05-03 15:35:00 +02:00
|
|
|
|
|
|
|
var i = -1
|
|
|
|
while (++i < len) {
|
|
|
|
padBuff.writeUInt8(len, i)
|
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
|
|
|
return Buffer.concat([this.cache, padBuff])
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function createCipheriv (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 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)
|
|
|
|
|
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)
|
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)
|
2017-05-03 15:35:00 +02:00
|
|
|
}
|
2017-12-10 21:51:33 +01:00
|
|
|
|
|
|
|
return new Cipher(config.module, password, iv)
|
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 createCipher (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 createCipheriv(suite, keys.key, keys.iv)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.createCipheriv = createCipheriv
|
|
|
|
exports.createCipher = createCipher
|