@@ -5,13 +5,16 @@ var ece = require('./ece.js');
55var base64 = require ( 'urlsafe-base64' ) ;
66var assert = require ( 'assert' ) ;
77
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+ }
1518var args = process . argv . slice ( 2 ) ;
1619var minLen = 3 ;
1720var maxLen = 100 ;
@@ -33,6 +36,9 @@ args.forEach(function(arg) {
3336 dumpFile = '../encrypt_data.json' ;
3437 } else if ( arg . substring ( 0 , 5 ) === 'dump=' ) {
3538 dumpFile = arg . substring ( 5 ) ;
39+ } else if ( arg . charAt ( 0 ) === '-' ) {
40+ usage ( ) ;
41+ process . exit ( 2 ) ;
3642 }
3743} ) ;
3844
@@ -78,26 +84,37 @@ function validate() {
7884 } ) ;
7985}
8086
81- function generateInput ( len ) {
87+ function generateInput ( min ) {
8288 var input ;
89+ min = Math . max ( minLen , min || 0 ) ;
8390 if ( plaintext ) {
84- if ( plaintext . length < minLen ) {
91+ if ( plaintext . length < min ) {
8592 throw new Error ( 'Plaintext is too short' ) ;
8693 }
8794 input = plaintext ;
8895 } 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 ) ;
9398 }
9499 logbuf ( 'Input' , input ) ;
95100 return input ;
96101}
97102
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+
98113function encryptDecrypt ( input , encryptParams , decryptParams , keys ) {
99114 // 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+ }
101118 if ( decryptParams . version === 'aes128gcm' ) {
102119 delete decryptParams . rs ;
103120 } else {
@@ -160,7 +177,7 @@ function exactlyOneRecord(version) {
160177 var params = {
161178 version : version ,
162179 key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
163- rs : input . length + 2 // add exactly the padding
180+ rs : input . length + rsoverhead ( version )
164181 } ;
165182 encryptDecrypt ( input , params , params ) ;
166183}
@@ -170,11 +187,14 @@ function detectTruncation(version) {
170187 var params = {
171188 version : version ,
172189 key : base64 . encode ( crypto . randomBytes ( 16 ) ) ,
173- rs : input . length + 1 // so we get two records
190+ rs : input . length + rsoverhead ( version ) - 1
174191 } ;
175192 var headerLen = ( version === 'aes128gcm' ) ? 21 : 0 ;
176193 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+ }
178198 assert . ok ( chunkLen < encrypted . length ) ;
179199 encrypted = encrypted . slice ( 0 , chunkLen ) ;
180200 logbuf ( 'Encrypted' , encrypted ) ;
@@ -282,11 +302,11 @@ function checkExamples() {
282302 key : base64 . decode ( 'BO3ZVPxUlnLORbVGMpbT1Q' ) ,
283303 keyid : 'a1' ,
284304 salt : base64 . decode ( 'uNCkWiNYzKTnBN9ji3-qWA' ) ,
285- rs : 10 ,
305+ rs : 26 ,
286306 pad : 1
287307 } ,
288308 plaintext : Buffer . from ( 'I am the walrus' ) ,
289- ciphertext : base64 . decode ( 'uNCkWiNYzKTnBN9ji3-qWAAAAAoCYTGH ' +
309+ ciphertext : base64 . decode ( 'uNCkWiNYzKTnBN9ji3-qWAAAABoCYTGH ' +
290310 'OqYFz-0in3dpb-VE2GfBngkaPy6bZus_' +
291311 'qLF79s6zQyTSsA0iLOKyd3JqVIwprNzV' +
292312 'atRCWZGUx_qsFbJBCQu62RqQuR2d' )
0 commit comments