UNPKG

3.57 kBJavaScriptView Raw
1var aes = require('./aes')
2var Transform = require('./cipherBase')
3var inherits = require('inherits')
4var modes = require('./modes')
5var StreamCipher = require('./streamCipher')
6var AuthCipher = require('./authCipher')
7var ebtk = require('./EVP_BytesToKey')
8
9inherits(Decipher, Transform)
10function Decipher (mode, key, iv) {
11 if (!(this instanceof Decipher)) {
12 return new Decipher(mode, key, iv)
13 }
14 Transform.call(this)
15 this._cache = new Splitter()
16 this._last = void 0
17 this._cipher = new aes.AES(key)
18 this._prev = new Buffer(iv.length)
19 iv.copy(this._prev)
20 this._mode = mode
21 this._autopadding = true
22}
23Decipher.prototype._update = function (data) {
24 this._cache.add(data)
25 var chunk
26 var thing
27 var out = []
28 while ((chunk = this._cache.get(this._autopadding))) {
29 thing = this._mode.decrypt(this, chunk)
30 out.push(thing)
31 }
32 return Buffer.concat(out)
33}
34Decipher.prototype._final = function () {
35 var chunk = this._cache.flush()
36 if (this._autopadding) {
37 return unpad(this._mode.decrypt(this, chunk))
38 } else if (chunk) {
39 throw new Error('data not multiple of block length')
40 }
41}
42Decipher.prototype.setAutoPadding = function (setTo) {
43 this._autopadding = !!setTo
44}
45function Splitter () {
46 if (!(this instanceof Splitter)) {
47 return new Splitter()
48 }
49 this.cache = new Buffer('')
50}
51Splitter.prototype.add = function (data) {
52 this.cache = Buffer.concat([this.cache, data])
53}
54
55Splitter.prototype.get = function (autoPadding) {
56 var out
57 if (autoPadding) {
58 if (this.cache.length > 16) {
59 out = this.cache.slice(0, 16)
60 this.cache = this.cache.slice(16)
61 return out
62 }
63 } else {
64 if (this.cache.length >= 16) {
65 out = this.cache.slice(0, 16)
66 this.cache = this.cache.slice(16)
67 return out
68 }
69 }
70 return null
71}
72Splitter.prototype.flush = function () {
73 if (this.cache.length) {
74 return this.cache
75 }
76}
77function unpad (last) {
78 var padded = last[15]
79 var i = -1
80 while (++i < padded) {
81 if (last[(i + (16 - padded))] !== padded) {
82 throw new Error('unable to decrypt data')
83 }
84 }
85 if (padded === 16) {
86 return
87 }
88 return last.slice(0, 16 - padded)
89}
90
91var modelist = {
92 ECB: require('./modes/ecb'),
93 CBC: require('./modes/cbc'),
94 CFB: require('./modes/cfb'),
95 CFB8: require('./modes/cfb8'),
96 CFB1: require('./modes/cfb1'),
97 OFB: require('./modes/ofb'),
98 CTR: require('./modes/ctr'),
99 GCM: require('./modes/ctr')
100}
101
102function createDecipheriv (suite, password, iv) {
103 var config = modes[suite.toLowerCase()]
104 if (!config) {
105 throw new TypeError('invalid suite type')
106 }
107 if (typeof iv === 'string') {
108 iv = new Buffer(iv)
109 }
110 if (typeof password === 'string') {
111 password = new Buffer(password)
112 }
113 if (password.length !== config.key / 8) {
114 throw new TypeError('invalid key length ' + password.length)
115 }
116 if (iv.length !== config.iv) {
117 throw new TypeError('invalid iv length ' + iv.length)
118 }
119 if (config.type === 'stream') {
120 return new StreamCipher(modelist[config.mode], password, iv, true)
121 } else if (config.type === 'auth') {
122 return new AuthCipher(modelist[config.mode], password, iv, true)
123 }
124 return new Decipher(modelist[config.mode], password, iv)
125}
126
127function createDecipher (suite, password) {
128 var config = modes[suite.toLowerCase()]
129 if (!config) {
130 throw new TypeError('invalid suite type')
131 }
132 var keys = ebtk(password, config.key, config.iv)
133 return createDecipheriv(suite, keys.key, keys.iv)
134}
135exports.createDecipher = createDecipher
136exports.createDecipheriv = createDecipheriv