-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
BlockCipher.js
322 lines (289 loc) · 8.06 KB
/
BlockCipher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import EnvUtil from '../EnvUtil'
import Encoder from '../Encoder'
import InvalidInputError from '../Error/InvalidInput'
import nodeCrypto from 'crypto'
const meta = {
name: 'block-cipher',
title: 'Block Cipher',
category: 'Modern cryptography',
type: 'encoder'
}
const algorithms = [
{
name: 'aes-128',
label: 'AES-128',
blockSize: 16,
keySize: 16,
browserAlgorithm: 'aes',
nodeAlgorithm: 'aes-128'
},
{
name: 'aes-192',
label: 'AES-192',
blockSize: 16,
keySize: 24,
// Not widely supported in browsers
browserAlgorithm: false,
nodeAlgorithm: 'aes-192'
},
{
name: 'aes-256',
label: 'AES-256',
blockSize: 16,
keySize: 32,
browserAlgorithm: 'aes',
nodeAlgorithm: 'aes-256'
}
]
const modes = [
{
name: 'ecb',
label: 'ECB (Electronic Code Book)',
hasIV: false,
browserMode: false,
nodeMode: true
},
{
name: 'cbc',
label: 'CBC (Cipher Block Chaining)',
hasIV: true,
browserMode: true,
nodeMode: true
},
{
name: 'ctr',
label: 'CTR (Counter)',
hasIV: true,
browserMode: true,
nodeMode: true
}
]
/**
* Encoder brick for block cipher encryption and decryption
*/
export default class BlockCipherEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
const algorithms = BlockCipherEncoder.getAlgorithms()
const defaultAlgorithm = algorithms[0]
const modes = BlockCipherEncoder.getModes()
const paddingAvailable = BlockCipherEncoder.isPaddingAvailable()
this.addSettings([
{
name: 'algorithm',
type: 'enum',
value: defaultAlgorithm.name,
elements: algorithms.map(algorithm => algorithm.name),
labels: algorithms.map(algorithm => algorithm.label),
randomizable: false,
width: paddingAvailable ? 8 : 12
},
{
name: 'padding',
type: 'boolean',
value: false,
randomizable: false,
width: paddingAvailable ? 4 : 12,
visible: paddingAvailable
},
{
name: 'mode',
type: 'enum',
value: 'cbc',
elements: modes.map(mode => mode.name),
labels: modes.map(mode => mode.label),
randomizable: false
},
{
name: 'key',
type: 'bytes',
value: new Uint8Array([
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c ]
),
minSize: defaultAlgorithm.keySize,
maxSize: defaultAlgorithm.keySize
},
{
name: 'iv',
label: 'IV',
type: 'bytes',
value: new Uint8Array([
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F ]
),
minSize: defaultAlgorithm.blockSize,
maxSize: defaultAlgorithm.blockSize
}
])
}
/**
* Triggered when a setting field has changed.
* @protected
* @param {Field} setting Sender setting field
* @param {mixed} value New field value
*/
settingValueDidChange (setting, value) {
switch (setting.getName()) {
case 'algorithm':
const { keySize } = BlockCipherEncoder.getAlgorithm(value)
this.getSetting('key')
.setMinSize(keySize)
.setMaxSize(keySize)
break
case 'mode':
const algorithm = this.getSettingValue('algorithm')
const { blockSize } = BlockCipherEncoder.getAlgorithm(algorithm)
const { hasIV } = BlockCipherEncoder.getMode(value)
this.getSetting('iv')
.setVisible(hasIV)
.setMinSize(blockSize)
.setMaxSize(blockSize)
break
}
}
/**
* Performs encode or decode on given content.
* @protected
* @param {Chain} content
* @param {boolean} isEncode True for encoding, false for decoding
* @return {number[]|string|Uint8Array|Chain|Promise} Resulting content
*/
async performTranslate (content, isEncode) {
const message = content.getBytes()
const { algorithm, mode, key, padding, iv } = this.getSettingValues()
try {
// Try to encrypt or decrypt
return await this.createCipher(
algorithm, mode, key, iv, padding, isEncode, message)
} catch (err) {
// Catch invalid input errors
if (!isEncode) {
throw new InvalidInputError(
`${algorithm} decryption failed, ` +
`this may be due to malformed content`)
} else {
throw new InvalidInputError(`${algorithm} encryption failed`)
}
}
}
/**
* Creates message cipher using given algorithm.
* @protected
* @param {string} name Algorithm name
* @param {Uint8Array} message Message bytes
* @return {Promise}
*/
async createCipher (name, mode, key, iv, padding, isEncode, message) {
const algorithm = BlockCipherEncoder.getAlgorithm(name)
const { hasIV } = BlockCipherEncoder.getMode(mode)
if (!hasIV) {
iv = new Uint8Array([])
}
if (EnvUtil.isNode()) {
const cipherName = algorithm.nodeAlgorithm + '-' + mode
// Node v8.x - convert Uint8Array to Buffer - not needed for v10
iv = global.Buffer.from(iv)
message = global.Buffer.from(message)
// Create message cipher using Node Crypto async
return new Promise((resolve, reject) => {
const cipher = isEncode
? nodeCrypto.createCipheriv(cipherName, key, iv)
: nodeCrypto.createDecipheriv(cipherName, key, iv)
cipher.setAutoPadding(padding)
const resultBuffer = Buffer.concat([
cipher.update(message),
cipher.final()
])
resolve(new Uint8Array(resultBuffer))
})
} else {
const cipherName = algorithm.browserAlgorithm + '-' + mode
// Get crypto subtle instance
const crypto = window.crypto || window.msCrypto
const cryptoSubtle = crypto.subtle || crypto.webkitSubtle
// Create key instance
const cryptoKey = await cryptoSubtle.importKey(
'raw', key, { name: cipherName }, false, ['encrypt', 'decrypt'])
// Create message cipher using Web Crypto API
const algo = {
name: cipherName,
iv,
counter: iv,
length: algorithm.blockSize
}
let result = isEncode
? cryptoSubtle.encrypt(algo, cryptoKey, message)
: cryptoSubtle.decrypt(algo, cryptoKey, message)
// IE11 exception
if (result.oncomplete !== undefined) {
// Wrap IE11 CryptoOperation object in a promise
result = new Promise((resolve, reject) => {
result.oncomplete = resolve.bind(this, result.result)
result.onerror = reject
})
}
return result.then(buffer => new Uint8Array(buffer))
}
}
/**
* Returns wether padding is available in the current environment.
* @protected
* @return {boolean}
*/
static isPaddingAvailable () {
return EnvUtil.isNode()
}
/**
* Returns algorithm for given name.
* @protected
* @param {string} name Algorithm name
* @return {?object} Algorithm object or null, if not found.
*/
static getAlgorithm (name) {
return algorithms.find(algorithm => algorithm.name === name)
}
/**
* Returns algorithm objects available in the current environment.
* @protected
* @return {object[]}
*/
static getAlgorithms () {
const isNode = EnvUtil.isNode()
return algorithms.filter(algorithm =>
(algorithm.browserAlgorithm && !isNode) ||
(algorithm.nodeAlgorithm && isNode)
)
}
/**
* Returns mode for given name.
* @protected
* @param {string} name Mode name
* @return {?object} Mode object or null, if not found.
*/
static getMode (name) {
return modes.find(mode => mode.name === name)
}
/**
* Returns mode objects available in the current environment.
* @protected
* @return {object[]}
*/
static getModes () {
const isNode = EnvUtil.isNode()
return modes.filter(mode =>
(mode.browserMode && !isNode) ||
(mode.nodeMode && isNode)
)
}
}