UNPKG

3.31 kBJavaScriptView Raw
1var aes = require('./aes')
2var Transform = require('cipher-base')
3var inherits = require('inherits')
4var modes = require('./modes')
5var ebtk = require('evp_bytestokey')
6var StreamCipher = require('./streamCipher')
7var AuthCipher = require('./authCipher')
8inherits(Cipher, Transform)
9function 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}
21Cipher.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}
32Cipher.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}
43Cipher.prototype.setAutoPadding = function (setTo) {
44 this._autopadding = !!setTo
45 return this
46}
47
48function Splitter () {
49 if (!(this instanceof Splitter)) {
50 return new Splitter()
51 }
52 this.cache = new Buffer('')
53}
54Splitter.prototype.add = function (data) {
55 this.cache = Buffer.concat([this.cache, data])
56}
57
58Splitter.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}
66Splitter.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}
77var 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
88function 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}
112function 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
121exports.createCipheriv = createCipheriv
122exports.createCipher = createCipher