@@ -14,6 +14,15 @@ var SHA_256_LENGTH = 32;
1414var MODE_ENCRYPT = 'encrypt' ;
1515var MODE_DECRYPT = 'decrypt' ;
1616
17+ var keylog ;
18+ if ( process . env . ECE_KEYLOG === '1' ) {
19+ keylog = function ( m , k ) {
20+ console . warn ( m + ' [' + k . length + ']: ' + base64 . encode ( k ) ) ;
21+ } ;
22+ } else {
23+ keylog = function ( ) { } ;
24+ }
25+
1726function HMAC_hash ( key , input ) {
1827 var hmac = crypto . createHmac ( 'sha256' , key ) ;
1928 hmac . update ( input ) ;
@@ -113,9 +122,12 @@ function extractSecretAndContext(params, mode) {
113122 if ( ! result . secret ) {
114123 throw new Error ( 'Unable to determine key' ) ;
115124 }
125+ keylog ( 'secret' , result . secret ) ;
126+ keylog ( 'context' , result . context ) ;
116127 if ( params . authSecret ) {
117128 result . secret = HKDF ( base64 . decode ( params . authSecret ) , result . secret ,
118- info ( 'auth' , new Buffer ( 0 ) ) , SHA_256_LENGTH ) ;
129+ info ( 'auth' , new Buffer ( 0 ) ) , SHA_256_LENGTH ) ;
130+ keylog ( 'authsecret' , result . secret ) ;
119131 }
120132 return result ;
121133}
@@ -128,6 +140,8 @@ function deriveKeyAndNonce(params, mode) {
128140 key : HKDF_expand ( prk , info ( 'aesgcm128' , s . context ) , KEY_LENGTH ) ,
129141 nonce : HKDF_expand ( prk , info ( 'nonce' , s . context ) , NONCE_LENGTH )
130142 } ;
143+ keylog ( 'key' , result . key ) ;
144+ keylog ( 'nonce base' , result . nonce ) ;
131145 return result ;
132146}
133147
@@ -149,15 +163,18 @@ function generateNonce(base, counter) {
149163 var x = ( ( m ^ counter ) & 0xffffff ) +
150164 ( ( ( ( m / 0x1000000 ) ^ ( counter / 0x1000000 ) ) & 0xffffff ) * 0x1000000 ) ;
151165 nonce . writeUIntBE ( x , nonce . length - 6 , 6 ) ;
166+ keylog ( 'nonce' + counter , nonce ) ;
152167 return nonce ;
153168}
154169
155170function decryptRecord ( key , counter , buffer , padSize ) {
171+ keylog ( 'decrypt' , buffer ) ;
156172 var nonce = generateNonce ( key . nonce , counter ) ;
157173 var gcm = crypto . createDecipheriv ( AES_GCM , key . key , nonce ) ;
158174 gcm . setAuthTag ( buffer . slice ( buffer . length - TAG_LENGTH ) ) ;
159175 var data = gcm . update ( buffer . slice ( 0 , buffer . length - TAG_LENGTH ) ) ;
160176 data = Buffer . concat ( [ data , gcm . final ( ) ] ) ;
177+ keylog ( 'decrypted' , data ) ;
161178 padSize = padSize || PAD_SIZE
162179 var pad = data . readUIntBE ( 0 , padSize ) ;
163180 if ( pad + padSize > data . length ) {
@@ -206,6 +223,7 @@ function decrypt(buffer, params) {
206223}
207224
208225function encryptRecord ( key , counter , buffer , pad , padSize ) {
226+ keylog ( 'encrypt' , buffer ) ;
209227 pad = pad || 0 ;
210228 var nonce = generateNonce ( key . nonce , counter ) ;
211229 var gcm = crypto . createCipheriv ( AES_GCM , key . key , nonce ) ;
@@ -220,7 +238,9 @@ function encryptRecord(key, counter, buffer, pad, padSize) {
220238 if ( tag . length !== TAG_LENGTH ) {
221239 throw new Error ( 'invalid tag generated' ) ;
222240 }
223- return Buffer . concat ( [ epadding , ebuffer , tag ] ) ;
241+ var encrypted = Buffer . concat ( [ epadding , ebuffer , tag ] ) ;
242+ keylog ( 'encrypted' , encrypted ) ;
243+ return encrypted ;
224244}
225245
226246/**
0 commit comments