This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
test-generator.js
420 lines (355 loc) · 15.8 KB
/
test-generator.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*******************************************************************************
* This file is part of DiscordCrypt (https://gitlab.com/leogx9r/DiscordCrypt).
* Copyright (c) 2019-Present Leonardo Gates
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
class testGenerator {
/**
* @constructor
* @desc Loads required modules for test generation.
*/
constructor() {
this.fs = require( 'fs' );
this.crypto = require( 'crypto' );
this.discordCrypt = require( '../build/discordCrypt.plugin.js' ).discordCrypt;
this.discordCrypt_instance = new ( this.discordCrypt )();
this.cipherModeCount = 25;
this.ciphers = [
{
full_name: 'AES-256',
name: 'aes-256',
block_size: 16,
key_size: 32,
encrypt: this.discordCrypt.__aes256_encrypt,
decrypt: this.discordCrypt.__aes256_decrypt,
},
{
full_name: 'Camellia-256',
name: 'camellia-256',
block_size: 16,
key_size: 32,
encrypt: this.discordCrypt.__camellia256_encrypt,
decrypt: this.discordCrypt.__camellia256_decrypt,
},
{
full_name: 'TripleDES-192',
name: 'tripledes-192',
block_size: 8,
key_size: 24,
encrypt: this.discordCrypt.__tripledes192_encrypt,
decrypt: this.discordCrypt.__tripledes192_decrypt,
},
{
full_name: 'IDEA-128',
name: 'idea-128',
block_size: 8,
key_size: 16,
encrypt: this.discordCrypt.__idea128_encrypt,
decrypt: this.discordCrypt.__idea128_decrypt,
},
{
full_name: 'Blowfish-512',
name: 'blowfish-512',
block_size: 8,
key_size: 64,
encrypt: this.discordCrypt.__blowfish512_encrypt,
decrypt: this.discordCrypt.__blowfish512_decrypt
}
];
this.block_modes = [
'cbc',
'cfb',
'ofb'
];
this.padding_schemes = [
'PKC7',
'ANS2',
'ISO9',
'ISO1',
];
this.discordCrypt.__loadLibraries( this.discordCrypt_instance._libraries );
}
/**
* @desc Generates individual tests for each cipher mode and padding scheme.
* @param {int} num_tests The number of tests to generate.
* @param {Array<Object>} unit_tests The output array to store the generated tests.
* @param {int} i The index of ciphers used for this test.
* @param {int} j The block operation mode of the ciphers.
* @param {int} k The padding scheme for the ciphers.
*/
addCipherTest( num_tests = 25, unit_tests, i, j, k ) {
/* Loop for the number of tests desired. */
for ( let l = 0; l < num_tests; l++ ) {
/* Generate random plaintext. */
let plaintext = this.crypto.randomBytes( ( this.ciphers[ i ].key_size * ( l + 1 ) ) + ( l + k + i ) );
/* Generate a random key and one-time salt. */
let key = this.crypto.randomBytes( this.ciphers[ i ].key_size );
let salt = this.crypto.randomBytes( 8 );
/* Perform a round of encryption. */
let ciphertext = this.ciphers[ i ].encrypt(
plaintext,
key,
unit_tests[ i ].tests[ j ][ k ].mode,
unit_tests[ i ].tests[ j ][ k ].scheme,
true,
false,
salt
);
/* Perform a round of decryption. */
let _plaintext = this.ciphers[ i ].decrypt(
ciphertext,
key,
unit_tests[ i ].tests[ j ][ k ].mode,
unit_tests[ i ].tests[ j ][ k ].scheme,
'hex',
true
);
/* Make sure that the plaintext generated matches the decrypted value. */
if ( _plaintext !== plaintext.toString( 'hex' ) ) {
l--;
console.log( `Invalid test generated for ${this.ciphers[ i ].name}.` );
continue;
}
/* Store the test result. */
unit_tests[ i ].tests[ j ][ k ].r[ l ] = {};
unit_tests[ i ].tests[ j ][ k ].r[ l ].plaintext = plaintext.toString( 'hex' );
unit_tests[ i ].tests[ j ][ k ].r[ l ].ciphertext = ciphertext;
unit_tests[ i ].tests[ j ][ k ].r[ l ].key = key.toString( 'hex' );
unit_tests[ i ].tests[ j ][ k ].r[ l ].salt = salt.toString( 'hex' );
}
}
/**
* @desc Generates cipher test vectors.
* @param {int} num_tests The number of tests to generate PER cipher-combo PER block mode PER padding scheme.
* @param {string} output_path The output path of the JSON file containing the generated test vectors.
*/
generateCipherTests( num_tests = 25, output_path = './tests/vectors/cipher-test-vectors.json' ) {
let unit_tests = [];
for ( let i = 0; i < this.ciphers.length; i++ ) {
unit_tests[ i ] = {};
unit_tests[ i ].full_name = this.ciphers[ i ].full_name;
unit_tests[ i ].name = this.ciphers[ i ].name;
unit_tests[ i ].tests = [];
for ( let j = 0; j < this.block_modes.length; j++ ) {
unit_tests[ i ].tests[ j ] = [];
for ( let k = 0; k < this.padding_schemes.length; k++ ) {
unit_tests[ i ].tests[ j ][ k ] = {};
unit_tests[ i ].tests[ j ][ k ].mode = this.block_modes[ j ];
unit_tests[ i ].tests[ j ][ k ].scheme = this.padding_schemes[ k ];
unit_tests[ i ].tests[ j ][ k ].r = [];
this.addCipherTest( num_tests, unit_tests, i, j, k );
}
}
}
/* Add AES-GCM tests. */
let aes_gcm = {};
/* Keep to the standard format above. */
aes_gcm.full_name = 'AES-256-GCM';
aes_gcm.name = 'aes-256-gcm';
aes_gcm.tests = [];
/* Since GCM is the only mode tested here, use only 1 entry. */
aes_gcm.tests[ 0 ] = [];
/* Loop over all padding schemes. */
for ( let k = 0; k < this.padding_schemes.length; k++ ) {
aes_gcm.tests[ 0 ][ k ] = {};
aes_gcm.tests[ 0 ][ k ].mode = 'gcm';
aes_gcm.tests[ 0 ][ k ].scheme = this.padding_schemes[ k ];
aes_gcm.tests[ 0 ][ k ].r = [];
/* Generate each test. */
for ( let l = 0; l < num_tests; l++ ) {
/* Generate random plaintext. */
let plaintext = this.crypto.randomBytes( ( 32 * ( l + 1 ) ) + ( l + k + this.ciphers.length ) );
/* Generate a random key and one-time salt. */
let key = this.crypto.randomBytes( 32 );
let salt = this.crypto.randomBytes( 8 );
/* Perform a round of encryption. */
let ciphertext = this.discordCrypt.__aes256_encrypt_gcm(
plaintext,
key,
this.padding_schemes[ k ],
true,
false,
undefined,
salt
);
/* Perform a round of decryption. */
let _plaintext = this.discordCrypt.__aes256_decrypt_gcm(
ciphertext,
key,
this.padding_schemes[ k ],
'hex',
true
);
/* Make sure that the plaintext generated matches the decrypted value. */
if ( _plaintext !== plaintext.toString( 'hex' ) ) {
l--;
console.log( `Invalid test generated for ${this.ciphers[ i ].name}.` );
continue;
}
/* Store the test result. */
aes_gcm.tests[ 0 ][ k ].r[ l ] = {};
aes_gcm.tests[ 0 ][ k ].r[ l ].plaintext = plaintext.toString( 'hex' );
aes_gcm.tests[ 0 ][ k ].r[ l ].ciphertext = ciphertext;
aes_gcm.tests[ 0 ][ k ].r[ l ].key = key.toString( 'hex' );
aes_gcm.tests[ 0 ][ k ].r[ l ].salt = salt.toString( 'hex' );
}
}
unit_tests[ this.ciphers.length ] = aes_gcm;
this.fs.writeFileSync( output_path, JSON.stringify( unit_tests, undefined, ' ' ) );
}
/**
* @desc Generate hash test vectors.
* @param {int} num_tests The number of tests to generate.
* @param {string} output_path The output path of the JSON file containing the generated test vectors.
*/
generateHashTests( num_tests = 25, output_path = './tests/vectors/hash-test-vectors.json' ) {
let unit_tests = [];
const hash_list = [
{
name: 'sha3-224',
length: 28,
hash: global.sha3.sha3_224,
},
{
name: 'sha3-256',
length: 32,
hash: global.sha3.sha3_256,
},
{
name: 'sha3-384',
length: 48,
hash: global.sha3.sha3_384,
},
{
name: 'sha3-512',
length: 64,
hash: global.sha3.sha3_512,
}
];
for ( let i = 0; i < hash_list.length; i++ ) {
unit_tests[ i ] = {};
unit_tests[ i ].name = hash_list[ i ].name;
unit_tests[ i ].length = hash_list[ i ].length;
unit_tests[ i ].tests = [];
for ( let j = 0; j < num_tests; j++ ) {
let input = this.crypto.randomBytes( hash_list[ i ].length * ( j + 1 ) + j );
unit_tests[ i ].tests[ j ] = {};
unit_tests[ i ].tests[ j ].input = input.toString( 'hex' );
unit_tests[ i ].tests[ j ].output = hash_list[ i ].hash( input );
}
}
this.fs.writeFileSync( output_path, JSON.stringify( unit_tests, undefined, ' ' ) );
}
/**
* @desc Generate symmetric encryption & decryption tests which includes encoding.
* @param {int} num_tests The number of tests to generate.
* @param {string} output_path The output path of the JSON file containing the generated test vectors.
*/
generateFullEncryptionTests( num_tests = 5, output_path = './tests/vectors/encode-test-vectors.json' ) {
let unit_tests = [];
/* Loop over each dual-encryption type. */
for ( let i = 0; i < this.cipherModeCount; i++ ) {
unit_tests[ i ] = [];
/* Loop over every block encryption mode. */
for ( let j = 0; j < this.block_modes.length; j++ ) {
unit_tests[ i ][ j ] = [];
/* Loop over every padding scheme.*/
for ( let k = 0; k < this.padding_schemes.length; k++ ) {
unit_tests[ i ][ j ][ k ] = {};
unit_tests[ i ][ j ][ k ].mode = this.block_modes[ j ];
unit_tests[ i ][ j ][ k ].scheme = this.padding_schemes[ k ];
unit_tests[ i ][ j ][ k ].r = [];
/* Generate each test. */
for ( let l = 0; l < num_tests; l++ ) {
/* Get some random values. */
let len = parseInt( this.crypto.randomBytes( 1 ).toString( 'hex' ), 16 ) + 32 + i + j + k + l,
primary = this.crypto.randomBytes( parseInt( len / 2 ) ),
secondary = this.crypto.randomBytes( parseInt( len / 3 ) ),
plaintext = this.crypto.randomBytes( len + ( l % 5 ? l + 33 : k + 11 ) );
/* Store the test output. */
unit_tests[ i ][ j ][ k ].r[ l ] = {};
unit_tests[ i ][ j ][ k ].r[ l ].primary_key = primary.toString( 'hex' );
unit_tests[ i ][ j ][ k ].r[ l ].secondary_key = secondary.toString( 'hex' );
unit_tests[ i ][ j ][ k ].r[ l ].plaintext = plaintext.toString( 'hex' );
unit_tests[ i ][ j ][ k ].r[ l ].ciphertext = this.discordCrypt.__symmetricEncrypt(
plaintext,
primary,
secondary,
i,
this.block_modes[ j ],
this.padding_schemes[ k ],
true
);
}
}
}
}
this.fs.writeFileSync( output_path, JSON.stringify( unit_tests, undefined, ' ' ) );
}
/**
* @desc Generates SJCL test vectors used for Up1 encryption via SJCL's library.
* @param {int} num_tests The number of tests to generate.
* @param {string} output_path The output path of the JSON file containing the generated test vectors.
*/
generateSJCLTests( num_tests, output_path = './tests/vectors/sjcl-test-vectors.json' ) {
let unit_tests = [];
/**
* @desc Pads a URL-encoded Base64 string and converts symbols to the required format.
* @param {string} input The input to convert from URL form.
* @return {string}
*/
const url_to_base64 = ( input ) => {
if ( !input.length % 4 )
return input;
let position = input.length;
let padLength = 4 - ( input.length % 4 );
let buffer = Buffer.alloc( input.length + padLength );
buffer.write( input );
while ( padLength-- )
buffer.write( '=', position++ );
return buffer.toString().replace( /-/g, '+' ).replace( /_/g, '/' );
};
/* Loop over every test. */
for( let i = 0; i < num_tests; i++ ) {
/* Create the test & input. */
unit_tests[ i ] = {};
unit_tests[ i ].buffer = this.crypto.randomBytes( 1024 * ( num_tests + 1 ) );
/* Encrypt the buffer. */
this.discordCrypt.__up1EncryptBuffer(
unit_tests[ i ].buffer,
'',
'',
global.sjcl,
( err, output, id, seed ) => {
/* Check for any errors. */
if( err )
throw new Error( err );
/* Store the seed and actual input. We expect no errors here when running the test. */
/* We don't actually store the output due to how SJCL works. */
unit_tests[ i ].seed = url_to_base64( seed );
unit_tests[ i ].buffer = unit_tests[ i ].buffer.toString( 'base64' );
}
);
}
/* Write the output vectors. */
this.fs.writeFileSync( output_path, JSON.stringify( unit_tests, undefined, ' ' ) );
}
}
module.exports = { testGenerator };
let generator = new testGenerator();
generator.generateSJCLTests();
generator.generateSJCLTests()
generator.generateCipherTests();
generator.generateFullEncryptionTests();
generator.generateHashTests();