@@ -3,15 +3,16 @@ const isOptions = require('is-options')
3
3
const crypto = require ( 'hypercore-crypto' )
4
4
const CoreStorage = require ( 'hypercore-storage' )
5
5
const c = require ( 'compact-encoding' )
6
+ const sodium = require ( 'sodium-universal' )
6
7
const b4a = require ( 'b4a' )
7
8
const NoiseSecretStream = require ( '@hyperswarm/secret-stream' )
9
+ const HypercoreEncryption = require ( 'hypercore-encryption' )
8
10
const Protomux = require ( 'protomux' )
9
11
const id = require ( 'hypercore-id-encoding' )
10
12
const safetyCatch = require ( 'safety-catch' )
11
13
const unslab = require ( 'unslab' )
12
14
13
15
const Core = require ( './lib/core' )
14
- const BlockEncryption = require ( './lib/block-encryption' )
15
16
const Info = require ( './lib/info' )
16
17
const Download = require ( './lib/download' )
17
18
const caps = require ( './lib/caps' )
@@ -149,7 +150,7 @@ class Hypercore extends EventEmitter {
149
150
}
150
151
151
152
static blockEncryptionKey ( key , encryptionKey ) {
152
- return BlockEncryption . blockEncryptionKey ( key , encryptionKey )
153
+ return HypercoreEncryption . blockEncryptionKey ( key , encryptionKey )
153
154
}
154
155
155
156
static getProtocolMuxer ( stream ) {
@@ -233,10 +234,25 @@ class Hypercore extends EventEmitter {
233
234
return s
234
235
}
235
236
236
- async setEncryptionKey ( encryptionKey , opts ) {
237
+ setEncryptionKey ( encryptionKey , opts ) {
238
+ const encryption = this . _getLegacyEncryption ( encryptionKey , ! ! ( opts && opts . block ) )
239
+ return this . setEncryption ( encryption , opts )
240
+ }
241
+
242
+ async setEncryption ( encryption , opts ) {
237
243
if ( ! this . opened ) await this . opening
238
244
if ( this . core . unencrypted ) return
239
- this . encryption = encryptionKey ? new BlockEncryption ( encryptionKey , this . key , { compat : this . core . compat , ...opts } ) : null
245
+
246
+ if ( encryption === null ) {
247
+ this . encryption = encryption
248
+ return
249
+ }
250
+
251
+ if ( ! HypercoreEncryption . isHypercoreEncryption ( encryption ) ) {
252
+ throw new Error ( 'Expected hypercore encryption provider' )
253
+ }
254
+
255
+ this . encryption = encryption
240
256
if ( ! this . core . encryption ) this . core . encryption = this . encryption
241
257
}
242
258
@@ -315,7 +331,12 @@ class Hypercore extends EventEmitter {
315
331
316
332
if ( ! this . core . encryption && ! this . core . unencrypted ) {
317
333
const e = getEncryptionOption ( opts )
318
- if ( e ) this . core . encryption = new BlockEncryption ( e . key , this . key , { compat : this . core . compat , ...e } )
334
+
335
+ if ( HypercoreEncryption . isHypercoreEncryption ( e ) ) {
336
+ this . core . encryption = e
337
+ } else if ( e ) {
338
+ this . core . encryption = this . _getLegacyEncryption ( e . key , e . block )
339
+ }
319
340
}
320
341
321
342
const parent = opts . parent || null
@@ -576,10 +597,6 @@ class Hypercore extends EventEmitter {
576
597
return this . opened === false ? [ ] : this . core . replicator . peers
577
598
}
578
599
579
- get encryptionKey ( ) {
580
- return this . encryption && this . encryption . key
581
- }
582
-
583
600
get padding ( ) {
584
601
return this . encryption === null ? 0 : this . encryption . padding
585
602
}
@@ -758,7 +775,7 @@ class Hypercore extends EventEmitter {
758
775
759
776
if ( this . encryption . compat !== this . core . compat ) this . _updateEncryption ( )
760
777
if ( this . core . unencrypted ) this . encryption = null
761
- else this . encryption . decrypt ( index , block )
778
+ else await this . encryption . decrypt ( index , block )
762
779
}
763
780
764
781
return this . _decode ( encoding , block )
@@ -907,6 +924,7 @@ class Hypercore extends EventEmitter {
907
924
blocks = Array . isArray ( blocks ) ? blocks : [ blocks ]
908
925
909
926
const preappend = this . core . unencrypted ? null : ( this . encryption && this . _preappend )
927
+ if ( preappend ) await this . encryption . ready ( )
910
928
911
929
const buffers = this . encodeBatch !== null ? this . encodeBatch ( blocks ) : new Array ( blocks . length )
912
930
@@ -1037,9 +1055,22 @@ class Hypercore extends EventEmitter {
1037
1055
1038
1056
_updateEncryption ( ) {
1039
1057
const e = this . encryption
1040
- this . encryption = new BlockEncryption ( e . key , this . key , { compat : this . core . compat , block : b4a . equals ( e . blockKey , e . key ) } )
1058
+ if ( HypercoreEncryption . isHypercoreEncryption ( e ) ) return
1059
+
1060
+ this . encryption = this . _getLegacyEncryption ( e . key , e . block )
1061
+
1041
1062
if ( e === this . core . encryption ) this . core . encryption = this . encryption
1042
1063
}
1064
+
1065
+ _getLegacyEncryption ( encryptionKey , block ) {
1066
+ if ( ! encryptionKey ) return null
1067
+
1068
+ const blockKey = block
1069
+ ? encryptionKey
1070
+ : getLegacyBlockKey ( this . key , encryptionKey , this . core . compat )
1071
+
1072
+ return HypercoreEncryption . createLegacyProvider ( encryptionKey , blockKey )
1073
+ }
1043
1074
}
1044
1075
1045
1076
module . exports = Hypercore
@@ -1052,14 +1083,14 @@ function toHex (buf) {
1052
1083
return buf && b4a . toString ( buf , 'hex' )
1053
1084
}
1054
1085
1055
- function preappend ( blocks ) {
1086
+ async function preappend ( blocks ) {
1056
1087
const offset = this . state . length
1057
1088
const fork = this . state . encryptionFork
1058
1089
1059
1090
if ( this . encryption . compat !== this . core . compat ) this . _updateEncryption ( )
1060
1091
1061
1092
for ( let i = 0 ; i < blocks . length ; i ++ ) {
1062
- this . encryption . encrypt ( offset + i , blocks [ i ] , fork )
1093
+ await this . encryption . encrypt ( offset + i , blocks [ i ] , fork )
1063
1094
}
1064
1095
}
1065
1096
@@ -1126,3 +1157,12 @@ function getEncryptionOption (opts) {
1126
1157
if ( ! opts . encryption ) return null
1127
1158
return b4a . isBuffer ( opts . encryption ) ? { key : opts . encryption } : opts . encryption
1128
1159
}
1160
+
1161
+ function getLegacyBlockKey ( hypercoreKey , encryptionKey , compat ) {
1162
+ const key = b4a . alloc ( HypercoreEncryption . KEYBYTES )
1163
+
1164
+ if ( compat ) sodium . crypto_generichash_batch ( key , [ encryptionKey ] , hypercoreKey )
1165
+ else sodium . crypto_generichash_batch ( key , [ caps . LEGACY_BLOCK_ENCRYPTION , hypercoreKey , encryptionKey ] )
1166
+
1167
+ return key
1168
+ }
0 commit comments