1 | var aes = require('./aes');
|
2 | var Transform = require('./cipherBase');
|
3 | var inherits = require('inherits');
|
4 | var modes = require('./modes');
|
5 | var StreamCipher = require('./streamCipher');
|
6 | var AuthCipher = require('./authCipher');
|
7 | var ebtk = require('./EVP_BytesToKey');
|
8 |
|
9 | inherits(Decipher, Transform);
|
10 | function 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 | }
|
23 | Decipher.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 | };
|
34 | Decipher.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 | };
|
42 | Decipher.prototype.setAutoPadding = function (setTo) {
|
43 | this._autopadding = !!setTo;
|
44 | };
|
45 | function Splitter() {
|
46 | if (!(this instanceof Splitter)) {
|
47 | return new Splitter();
|
48 | }
|
49 | this.cache = new Buffer('');
|
50 | }
|
51 | Splitter.prototype.add = function (data) {
|
52 | this.cache = Buffer.concat([this.cache, data]);
|
53 | };
|
54 |
|
55 | Splitter.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 | };
|
72 | Splitter.prototype.flush = function () {
|
73 | if (this.cache.length) {
|
74 | return this.cache;
|
75 | }
|
76 | };
|
77 | function 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 |
|
91 | var 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 |
|
102 | module.exports = function (crypto) {
|
103 | function createDecipheriv(suite, password, iv) {
|
104 | var config = modes[suite];
|
105 | if (!config) {
|
106 | throw new TypeError('invalid suite type');
|
107 | }
|
108 | if (typeof iv === 'string') {
|
109 | iv = new Buffer(iv);
|
110 | }
|
111 | if (typeof password === 'string') {
|
112 | password = new Buffer(password);
|
113 | }
|
114 | if (password.length !== config.key/8) {
|
115 | throw new TypeError('invalid key length ' + password.length);
|
116 | }
|
117 | if (iv.length !== config.iv) {
|
118 | throw new TypeError('invalid iv length ' + iv.length);
|
119 | }
|
120 | if (config.type === 'stream') {
|
121 | return new StreamCipher(modelist[config.mode], password, iv, true);
|
122 | } else if (config.type === 'auth') {
|
123 | return new AuthCipher(modelist[config.mode], password, iv, true);
|
124 | }
|
125 | return new Decipher(modelist[config.mode], password, iv);
|
126 | }
|
127 |
|
128 | function createDecipher (suite, password) {
|
129 | var config = modes[suite];
|
130 | if (!config) {
|
131 | throw new TypeError('invalid suite type');
|
132 | }
|
133 | var keys = ebtk(crypto, password, config.key, config.iv);
|
134 | return createDecipheriv(suite, keys.key, keys.iv);
|
135 | }
|
136 | return {
|
137 | createDecipher: createDecipher,
|
138 | createDecipheriv: createDecipheriv
|
139 | };
|
140 | };
|