@@ -5,13 +5,16 @@ var ece = require('./ece.js');
5
5
var base64 = require ( 'urlsafe-base64' ) ;
6
6
var assert = require ( 'assert' ) ;
7
7
8
- // Usage: node test.js [args]
9
- // If args contains a version (e.g., aes128gcm), filter on versions.
10
- // If args contains a test function, filter on test functions.
11
- // If args contains 'verbose' show logs.
12
- // If args contains 'text=...' set the input string to the UTF-8 encoding of that string.
13
- // If args contains 'max=<n>' set the maximum input size to that value.
14
- // If args contains 'dump[=file]' log info to ../encrypt_data.json or the specified file.
8
+ function usage ( ) {
9
+ console . log ( 'Usage: node test.js [args]' ) ;
10
+ console . log ( ' <version> - test only the specified version(s)' ) ;
11
+ console . log ( ' Supported: [aes128gcm,aesgcm,aesgcm128]' ) ;
12
+ console . log ( ' <test function> - test only the specified function(s)' ) ;
13
+ console . log ( ' "verbose" enable logging for tests (export ECE_KEYLOG=1 for more)' ) ;
14
+ console . log ( ' "text=..." sets the input string' ) ;
15
+ console . log ( ' "max=<n>" sets the maximum input size' ) ;
16
+ console . log ( ' "dump[=file]" log info to ../encrypt_data.json or the specified file' ) ;
17
+ }
15
18
var args = process . argv . slice ( 2 ) ;
16
19
var minLen = 3 ;
17
20
var maxLen = 100 ;
@@ -33,6 +36,9 @@ args.forEach(function(arg) {
33
36
dumpFile = '../encrypt_data.json' ;
34
37
} else if ( arg . substring ( 0 , 5 ) === 'dump=' ) {
35
38
dumpFile = arg . substring ( 5 ) ;
39
+ } else if ( arg . charAt ( 0 ) === '-' ) {
40
+ usage ( ) ;
41
+ process . exit ( 2 ) ;
36
42
}
37
43
} ) ;
38
44
@@ -78,26 +84,37 @@ function validate() {
78
84
} ) ;
79
85
}
80
86
81
- function generateInput ( len ) {
87
+ function generateInput ( min ) {
82
88
var input ;
89
+ min = Math . max ( minLen , min || 0 ) ;
83
90
if ( plaintext ) {
84
- if ( plaintext . length < minLen ) {
91
+ if ( plaintext . length < min ) {
85
92
throw new Error ( 'Plaintext is too short' ) ;
86
93
}
87
94
input = plaintext ;
88
95
} else {
89
- if ( typeof len === 'undefined' ) {
90
- len = Math . floor ( ( Math . random ( ) * ( maxLen - minLen ) + minLen ) ) ;
91
- }
92
- input = crypto . randomBytes ( Math . max ( minLen , Math . min ( len , maxLen ) ) ) ;
96
+ var len = Math . floor ( ( Math . random ( ) * ( maxLen - min ) + min ) ) ;
97
+ input = crypto . randomBytes ( len ) ;
93
98
}
94
99
logbuf ( 'Input' , input ) ;
95
100
return input ;
96
101
}
97
102
103
+ function rsoverhead ( version ) {
104
+ if ( version === 'aesgcm128' ) {
105
+ return 1 ;
106
+ }
107
+ if ( version === 'aesgcm' ) {
108
+ return 2 ;
109
+ }
110
+ return 18 ;
111
+ }
112
+
98
113
function encryptDecrypt ( input , encryptParams , decryptParams , keys ) {
99
114
// Fill out a default rs.
100
- encryptParams . rs = encryptParams . rs || ( input . length + minLen ) ;
115
+ if ( ! encryptParams . rs ) {
116
+ encryptParams . rs = input . length + rsoverhead ( encryptParams . version ) + 1 ;
117
+ }
101
118
if ( decryptParams . version === 'aes128gcm' ) {
102
119
delete decryptParams . rs ;
103
120
} else {
@@ -160,7 +177,7 @@ function exactlyOneRecord(version) {
160
177
var params = {
161
178
version : version ,
162
179
key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
163
- rs : input . length + 2 // add exactly the padding
180
+ rs : input . length + rsoverhead ( version )
164
181
} ;
165
182
encryptDecrypt ( input , params , params ) ;
166
183
}
@@ -170,11 +187,14 @@ function detectTruncation(version) {
170
187
var params = {
171
188
version : version ,
172
189
key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
173
- rs : input . length + 1 // so we get two records
190
+ rs : input . length + rsoverhead ( version ) - 1
174
191
} ;
175
192
var headerLen = ( version === 'aes128gcm' ) ? 21 : 0 ;
176
193
var encrypted = ece . encrypt ( input , params ) ;
177
- var chunkLen = headerLen + params . rs + 16 ;
194
+ var chunkLen = headerLen + params . rs ;
195
+ if ( version != 'aes128gcm' ) {
196
+ chunkLen += 16 ;
197
+ }
178
198
assert . ok ( chunkLen < encrypted . length ) ;
179
199
encrypted = encrypted . slice ( 0 , chunkLen ) ;
180
200
logbuf ( 'Encrypted' , encrypted ) ;
@@ -282,11 +302,11 @@ function checkExamples() {
282
302
key : base64 . decode ( 'BO3ZVPxUlnLORbVGMpbT1Q' ) ,
283
303
keyid : 'a1' ,
284
304
salt : base64 . decode ( 'uNCkWiNYzKTnBN9ji3-qWA' ) ,
285
- rs : 10 ,
305
+ rs : 26 ,
286
306
pad : 1
287
307
} ,
288
308
plaintext : Buffer . from ( 'I am the walrus' ) ,
289
- ciphertext : base64 . decode ( 'uNCkWiNYzKTnBN9ji3-qWAAAAAoCYTGH ' +
309
+ ciphertext : base64 . decode ( 'uNCkWiNYzKTnBN9ji3-qWAAAABoCYTGH ' +
290
310
'OqYFz-0in3dpb-VE2GfBngkaPy6bZus_' +
291
311
'qLF79s6zQyTSsA0iLOKyd3JqVIwprNzV' +
292
312
'atRCWZGUx_qsFbJBCQu62RqQuR2d' )
0 commit comments