1 | var aes = require('./aes')
|
2 | var Transform = require('cipher-base')
|
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 | this._autopadding = true
|
20 | }
|
21 | Cipher.prototype._update = function (data) {
|
22 | this._cache.add(data)
|
23 | var chunk
|
24 | var thing
|
25 | var out = []
|
26 | while ((chunk = this._cache.get())) {
|
27 | thing = this._mode.encrypt(this, chunk)
|
28 | out.push(thing)
|
29 | }
|
30 | return Buffer.concat(out)
|
31 | }
|
32 | Cipher.prototype._final = function () {
|
33 | var chunk = this._cache.flush()
|
34 | if (this._autopadding) {
|
35 | chunk = this._mode.encrypt(this, chunk)
|
36 | this._cipher.scrub()
|
37 | return chunk
|
38 | } else if (chunk.toString('hex') !== '10101010101010101010101010101010') {
|
39 | this._cipher.scrub()
|
40 | throw new Error('data not multiple of block length')
|
41 | }
|
42 | }
|
43 | Cipher.prototype.setAutoPadding = function (setTo) {
|
44 | this._autopadding = !!setTo
|
45 | return this
|
46 | }
|
47 |
|
48 | function Splitter () {
|
49 | if (!(this instanceof Splitter)) {
|
50 | return new Splitter()
|
51 | }
|
52 | this.cache = new Buffer('')
|
53 | }
|
54 | Splitter.prototype.add = function (data) {
|
55 | this.cache = Buffer.concat([this.cache, data])
|
56 | }
|
57 |
|
58 | Splitter.prototype.get = function () {
|
59 | if (this.cache.length > 15) {
|
60 | var out = this.cache.slice(0, 16)
|
61 | this.cache = this.cache.slice(16)
|
62 | return out
|
63 | }
|
64 | return null
|
65 | }
|
66 | Splitter.prototype.flush = function () {
|
67 | var len = 16 - this.cache.length
|
68 | var padBuff = new Buffer(len)
|
69 |
|
70 | var i = -1
|
71 | while (++i < len) {
|
72 | padBuff.writeUInt8(len, i)
|
73 | }
|
74 | var out = Buffer.concat([this.cache, padBuff])
|
75 | return out
|
76 | }
|
77 | var modelist = {
|
78 | ECB: require('./modes/ecb'),
|
79 | CBC: require('./modes/cbc'),
|
80 | CFB: require('./modes/cfb'),
|
81 | CFB8: require('./modes/cfb8'),
|
82 | CFB1: require('./modes/cfb1'),
|
83 | OFB: require('./modes/ofb'),
|
84 | CTR: require('./modes/ctr'),
|
85 | GCM: require('./modes/ctr')
|
86 | }
|
87 |
|
88 | function createCipheriv (suite, password, iv) {
|
89 | var config = modes[suite.toLowerCase()]
|
90 | if (!config) {
|
91 | throw new TypeError('invalid suite type')
|
92 | }
|
93 | if (typeof iv === 'string') {
|
94 | iv = new Buffer(iv)
|
95 | }
|
96 | if (typeof password === 'string') {
|
97 | password = new Buffer(password)
|
98 | }
|
99 | if (password.length !== config.key / 8) {
|
100 | throw new TypeError('invalid key length ' + password.length)
|
101 | }
|
102 | if (iv.length !== config.iv) {
|
103 | throw new TypeError('invalid iv length ' + iv.length)
|
104 | }
|
105 | if (config.type === 'stream') {
|
106 | return new StreamCipher(modelist[config.mode], password, iv)
|
107 | } else if (config.type === 'auth') {
|
108 | return new AuthCipher(modelist[config.mode], password, iv)
|
109 | }
|
110 | return new Cipher(modelist[config.mode], password, iv)
|
111 | }
|
112 | function createCipher (suite, password) {
|
113 | var config = modes[suite.toLowerCase()]
|
114 | if (!config) {
|
115 | throw new TypeError('invalid suite type')
|
116 | }
|
117 | var keys = ebtk(password, false, config.key, config.iv)
|
118 | return createCipheriv(suite, keys.key, keys.iv)
|
119 | }
|
120 |
|
121 | exports.createCipheriv = createCipheriv
|
122 | exports.createCipher = createCipher
|