UNPKG

3.29 kBJavaScriptView Raw
1var aes = require('./aes')
2var Transform = require('./cipherBase')
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}
46
47function Splitter () {
48 if (!(this instanceof Splitter)) {
49 return new Splitter()
50 }
51 this.cache = new Buffer('')
52}
53Splitter.prototype.add = function (data) {
54 this.cache = Buffer.concat([this.cache, data])
55}
56
57Splitter.prototype.get = function () {
58 if (this.cache.length > 15) {
59 var out = this.cache.slice(0, 16)
60 this.cache = this.cache.slice(16)
61 return out
62 }
63 return null
64}
65Splitter.prototype.flush = function () {
66 var len = 16 - this.cache.length
67 var padBuff = new Buffer(len)
68
69 var i = -1
70 while (++i < len) {
71 padBuff.writeUInt8(len, i)
72 }
73 var out = Buffer.concat([this.cache, padBuff])
74 return out
75}
76var modelist = {
77 ECB: require('./modes/ecb'),
78 CBC: require('./modes/cbc'),
79 CFB: require('./modes/cfb'),
80 CFB8: require('./modes/cfb8'),
81 CFB1: require('./modes/cfb1'),
82 OFB: require('./modes/ofb'),
83 CTR: require('./modes/ctr'),
84 GCM: require('./modes/ctr')
85}
86
87function createCipheriv (suite, password, iv) {
88 var config = modes[suite.toLowerCase()]
89 if (!config) {
90 throw new TypeError('invalid suite type')
91 }
92 if (typeof iv === 'string') {
93 iv = new Buffer(iv)
94 }
95 if (typeof password === 'string') {
96 password = new Buffer(password)
97 }
98 if (password.length !== config.key / 8) {
99 throw new TypeError('invalid key length ' + password.length)
100 }
101 if (iv.length !== config.iv) {
102 throw new TypeError('invalid iv length ' + iv.length)
103 }
104 if (config.type === 'stream') {
105 return new StreamCipher(modelist[config.mode], password, iv)
106 } else if (config.type === 'auth') {
107 return new AuthCipher(modelist[config.mode], password, iv)
108 }
109 return new Cipher(modelist[config.mode], password, iv)
110}
111function createCipher (suite, password) {
112 var config = modes[suite.toLowerCase()]
113 if (!config) {
114 throw new TypeError('invalid suite type')
115 }
116 var keys = ebtk(password, config.key, config.iv)
117 return createCipheriv(suite, keys.key, keys.iv)
118}
119
120exports.createCipheriv = createCipheriv
121exports.createCipher = createCipher