@@ -56,19 +56,15 @@ export default class P2PEncryptor {
56
56
57
57
const p2pKeyWA = CryptoJS . enc . Base64 . parse ( keyBase64 ) ;
58
58
this . p2pKey = wordArrayToUint8Array ( p2pKeyWA ) ;
59
+ this . mode = CryptoJS . mode . CTR ;
60
+ this . padding = CryptoJS . pad . NoPadding ;
59
61
}
60
62
61
63
encryptToBase64 ( str ) {
62
64
if ( P2P_ENCRYPTION ) {
63
65
const enc = new TextEncoder ( ) ;
64
66
const arr = enc . encode ( str ) ;
65
67
66
- // const base64 = btoa(str);
67
- // const inputWA = CryptoJS.enc.Base64.parse(base64);
68
- // const input8Arr = wordArrayToUint8Array(inputWA);
69
-
70
- // const packet = this.encryptRawPacket(input8Arr);
71
- // console.log('[arr] ', arr, input8Arr);
72
68
const packet = this . encryptRawPacket ( new Uint8Array ( arr ) ) ;
73
69
74
70
const { bytes } = packet ;
@@ -115,21 +111,15 @@ export default class P2PEncryptor {
115
111
const x = ( this . isOutgoing ? 0 : 8 ) + ( this . type === 'Signaling' ? 128 : 0 ) ;
116
112
const key = this . p2pKey ;
117
113
118
- // console.log('[encryptor][p2p] encryptPrepared (x, key)', x, key);
119
114
const msgKeyLarge = this . concatSHA256 ( [ key . subarray ( x + 88 , x + 88 + 32 ) , buffer ] ) ;
120
115
const msgKey = result . bytes ;
121
116
for ( let i = 0 ; i < 16 ; i ++ ) {
122
117
msgKey [ i ] = msgKeyLarge [ i + 8 ] ;
123
118
}
124
- // console.log('[encryptor][p2p] encryptPrepared msgKeyLarge', msgKeyLarge, msgKey);
125
119
126
- // console.log('[encryptor][p2p] encryptPrepared prepareAesKeyIv start', key, msgKey, x);
127
120
const aesKeyIv = this . prepareAesKeyIv ( key , msgKey , x ) ;
128
- // console.log('[encryptor][p2p] encryptPrepared prepareAesKeyIv stop', aesKeyIv);
129
121
130
- // console.log('[encryptor][p2p] encryptPrepared aesProcessCtr start', buffer, buffer.length, aesKeyIv);
131
122
const bytes = this . aesProcessCtr ( buffer , buffer . length , aesKeyIv , true ) ;
132
- // console.log('[encryptor][p2p] encryptPrepared aesProcessCtr stop', bytes);
133
123
134
124
result . bytes = new Uint8Array ( [ ...result . bytes . subarray ( 0 , 16 ) , ...bytes ] ) ;
135
125
@@ -158,10 +148,7 @@ export default class P2PEncryptor {
158
148
159
149
const result = new Uint8Array ( [ ...new Uint8Array ( arr ) , ...buffer ] ) ;
160
150
161
- // console.log('[encryptor][p2p] encryptRawPacker buffer', result);
162
- const encryptedPacket = this . encryptPrepared ( result ) ;
163
-
164
- return encryptedPacket ;
151
+ return this . encryptPrepared ( result ) ;
165
152
}
166
153
167
154
prepareAesKeyIv ( key , msgKey , x ) {
@@ -192,33 +179,26 @@ export default class P2PEncryptor {
192
179
aesProcessCtr ( encryptedData , dataSize , aesKeyIv , encrypt = true ) {
193
180
const key = uint8ArrayToWordArray ( aesKeyIv . key ) ;
194
181
const iv = uint8ArrayToWordArray ( aesKeyIv . iv ) ;
195
-
196
182
const str = uint8ArrayToWordArray ( encryptedData ) ;
197
- // console.log('[encryptor][p2p] aesProcessCtr (aesKey, aesIv, encrypt)', { key, iv, encrypt, encryptedData });
183
+
184
+ const { mode, padding } = this ;
198
185
199
186
if ( encrypt ) {
200
187
const encrypted = CryptoJS . AES . encrypt ( str , key , {
201
- mode : CryptoJS . mode . CTR ,
188
+ mode,
202
189
iv,
203
- padding : CryptoJS . pad . NoPadding
190
+ padding
204
191
} ) ;
205
192
206
- const result = wordArrayToUint8Array ( encrypted . ciphertext ) ;
207
-
208
- // console.log('[encryptor][p2p] aesProcessCtr (result)', { result, ciphertext: encrypted.ciphertext });
209
-
210
- return result ;
193
+ return wordArrayToUint8Array ( encrypted . ciphertext ) ;
211
194
} else {
212
195
const decrypted = CryptoJS . AES . decrypt ( { ciphertext : str } , key , {
213
- mode : CryptoJS . mode . CTR ,
196
+ mode,
214
197
iv,
215
- padding : CryptoJS . pad . NoPadding
198
+ padding
216
199
} ) ;
217
200
218
- const result = wordArrayToUint8Array ( decrypted ) ;
219
-
220
- // console.log('[encryptor][p2p] aesProcessCtr (result)', { result, text: decrypted });
221
- return result ;
201
+ return wordArrayToUint8Array ( decrypted ) ;
222
202
}
223
203
}
224
204
@@ -232,6 +212,17 @@ export default class P2PEncryptor {
232
212
return JSON . parse ( dec . decode ( decrypted ) )
233
213
}
234
214
215
+ constTimeIsDifferent ( a , b , count ) {
216
+ let msgKeyEquals = true ;
217
+ for ( let i = 0 ; i < count ; i ++ ) {
218
+ if ( a [ i ] !== b [ i ] ) {
219
+ msgKeyEquals = false ;
220
+ }
221
+ }
222
+
223
+ return ! msgKeyEquals ;
224
+ }
225
+
235
226
decryptRawPacket ( buffer ) {
236
227
if ( buffer . length < 21 || buffer . length > kMaxIncomingPacketSize ) {
237
228
return null ;
@@ -241,39 +232,35 @@ export default class P2PEncryptor {
241
232
242
233
const x = ( isOutgoing ? 8 : 0 ) + ( type === 'Signaling' ? 128 : 0 ) ;
243
234
const key = this . p2pKey ;
244
- // console.log('[encryptor][p2p] decryptRawPacket (x, key)', x, key);
245
235
246
236
const msgKey = buffer . subarray ( 0 , 16 ) ;
247
237
const encryptedData = buffer . subarray ( 16 ) ;
248
238
const encryptedDataSize = buffer . length - 16 ;
249
239
250
- // console.log('[encryptor][p2p] decryptRawPacket prepareAesKeyIv start', { key, msgKey, x });
251
240
const aesKeyIv = this . prepareAesKeyIv ( key , msgKey , x ) ;
252
- // console.log('[encryptor][p2p] decryptRawPacket prepareAesKeyIv stop', aesKeyIv);
253
241
254
- // console.log('[encryptor][p2p] decryptRawPacket aesProcessCtr start', encryptedData, dataSize, aesKeyIv);
255
242
const decryptionBuffer = this . aesProcessCtr ( encryptedData , encryptedDataSize , aesKeyIv , false ) ;
256
- // console.log('[encryptor][p2p] decryptRawPacket aesProcessCtr stop', decryptionBuffer);
257
243
258
244
const msgKeyLarge = this . concatSHA256 ( [
259
245
key . subarray ( 88 + x , 88 + x + 32 ) ,
260
246
decryptionBuffer
261
247
] ) ;
262
248
263
- let msgKeyEquals = true ;
264
- for ( let i = 0 ; i < 16 ; i ++ ) {
265
- if ( msgKey [ i ] !== msgKeyLarge [ i + 8 ] ) {
266
- msgKeyEquals = false ;
267
- }
268
- }
269
- console . log ( '[msgKey]' , msgKey , msgKeyLarge , msgKeyEquals ) ;
270
- if ( ! msgKeyEquals ) {
249
+ if ( this . constTimeIsDifferent ( msgKeyLarge . subarray ( 8 ) , msgKey , 16 ) ) {
271
250
return null ;
272
251
}
273
252
274
- console . log ( '[base64] decryptionBuffer' , decryptionBuffer ) ;
275
- const resultBuffer = decryptionBuffer . slice ( 4 ) ;
276
-
277
- return resultBuffer ;
253
+ // let msgKeyEquals = true;
254
+ // for (let i = 0; i < 16; i++) {
255
+ // if (msgKey[i] !== msgKeyLarge[i + 8]) {
256
+ // msgKeyEquals = false;
257
+ // }
258
+ // }
259
+ //
260
+ // if (!msgKeyEquals) {
261
+ // return null;
262
+ // }
263
+
264
+ return decryptionBuffer . slice ( 4 ) ;
278
265
}
279
266
} ;
0 commit comments