1 | var aes = require('./aes');
|
2 | var Transform = require('./cipherBase');
|
3 | var inherits = require('inherits');
|
4 | var modes = require('./modes');
|
5 | var ebtk = require('./EVP_BytesToKey');
|
6 | var StreamCipher = require('./streamCipher');
|
7 | var AuthCipher = require('./authCipher');
|
8 | inherits(Cipher, Transform);
|
9 | function Cipher(mode, key, iv) {
|
10 | if (!(this instanceof Cipher)) {
|
11 | return new Cipher(mode, key, iv);
|
12 | }
|
13 | Transform.call(this);
|
14 | this._cache = new Splitter();
|
15 | this._cipher = new aes.AES(key);
|
16 | this._prev = new Buffer(iv.length);
|
17 | iv.copy(this._prev);
|
18 | this._mode = mode;
|
19 | }
|
20 | Cipher.prototype._transform = function (data, _, next) {
|
21 | this._cache.add(data);
|
22 | var chunk;
|
23 | var thing;
|
24 | while ((chunk = this._cache.get())) {
|
25 | thing = this._mode.encrypt(this, chunk);
|
26 | this.push(thing);
|
27 | }
|
28 | next();
|
29 | };
|
30 | Cipher.prototype._flush = function (next) {
|
31 | var chunk = this._cache.flush();
|
32 | this.push(this._mode.encrypt(this, chunk));
|
33 | this._cipher.scrub();
|
34 | next();
|
35 | };
|
36 |
|
37 |
|
38 | function Splitter() {
|
39 | if (!(this instanceof Splitter)) {
|
40 | return new Splitter();
|
41 | }
|
42 | this.cache = new Buffer('');
|
43 | }
|
44 | Splitter.prototype.add = function (data) {
|
45 | this.cache = Buffer.concat([this.cache, data]);
|
46 | };
|
47 |
|
48 | Splitter.prototype.get = function () {
|
49 | if (this.cache.length > 15) {
|
50 | var out = this.cache.slice(0, 16);
|
51 | this.cache = this.cache.slice(16);
|
52 | return out;
|
53 | }
|
54 | return null;
|
55 | };
|
56 | Splitter.prototype.flush = function () {
|
57 | var len = 16 - this.cache.length;
|
58 | var padBuff = new Buffer(len);
|
59 |
|
60 | var i = -1;
|
61 | while (++i < len) {
|
62 | padBuff.writeUInt8(len, i);
|
63 | }
|
64 | var out = Buffer.concat([this.cache, padBuff]);
|
65 | return out;
|
66 | };
|
67 | var modelist = {
|
68 | ECB: require('./modes/ecb'),
|
69 | CBC: require('./modes/cbc'),
|
70 | CFB: require('./modes/cfb'),
|
71 | CFB8: require('./modes/cfb8'),
|
72 | CFB1: require('./modes/cfb1'),
|
73 | OFB: require('./modes/ofb'),
|
74 | CTR: require('./modes/ctr'),
|
75 | GCM: require('./modes/ctr')
|
76 | };
|
77 | module.exports = function (crypto) {
|
78 | function createCipheriv(suite, password, iv) {
|
79 | var config = modes[suite];
|
80 | if (!config) {
|
81 | throw new TypeError('invalid suite type');
|
82 | }
|
83 | if (typeof iv === 'string') {
|
84 | iv = new Buffer(iv);
|
85 | }
|
86 | if (typeof password === 'string') {
|
87 | password = new Buffer(password);
|
88 | }
|
89 | if (password.length !== config.key/8) {
|
90 | throw new TypeError('invalid key length ' + password.length);
|
91 | }
|
92 | if (iv.length !== config.iv) {
|
93 | throw new TypeError('invalid iv length ' + iv.length);
|
94 | }
|
95 | if (config.type === 'stream') {
|
96 | return new StreamCipher(modelist[config.mode], password, iv);
|
97 | } else if (config.type === 'auth') {
|
98 | return new AuthCipher(modelist[config.mode], password, iv);
|
99 | }
|
100 | return new Cipher(modelist[config.mode], password, iv);
|
101 | }
|
102 | function createCipher (suite, password) {
|
103 | var config = modes[suite];
|
104 | if (!config) {
|
105 | throw new TypeError('invalid suite type');
|
106 | }
|
107 | var keys = ebtk(crypto, password, config.key, config.iv);
|
108 | return createCipheriv(suite, keys.key, keys.iv);
|
109 | }
|
110 | return {
|
111 | createCipher: createCipher,
|
112 | createCipheriv: createCipheriv
|
113 | };
|
114 | };
|