Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/default-encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ module.exports = class DefaultEncryption {
const block = b4a.equals(this.key, this.blockKey)
const keys = DefaultEncryption.deriveKeys(this.key, core.key, { block, compat: core.compat })

this.blockKey = keys.blockKey
this.blindingKey = keys.blindingKey
this.compat = core.compat
this.blockKey = keys.block
this.blindingKey = keys.blinding
}
}
43 changes: 42 additions & 1 deletion test/encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const test = require('brittle')
const b4a = require('b4a')
const crypto = require('hypercore-crypto')
const Hypercore = require('..')
const { create, createStorage, replicate } = require('./helpers')
const Verifier = require('../lib/verifier')
const { create, createStorage, createStored, replicate } = require('./helpers')

const fixturesRaw = require('./fixtures/encryption/v11.0.48.cjs')

Expand Down Expand Up @@ -373,6 +374,46 @@ test('encryption backwards compatibility', async function (t) {
}
})

test('encryption reloads keys when manifest flips compat mode at runtime', async function (t) {
const keyPair = crypto.keyPair()
const manifest = Verifier.createManifest({
quorum: 1,
signers: [
{
signature: 'ed25519',
publicKey: keyPair.publicKey
}
]
})

const key = Verifier.manifestHash(manifest)
const open = await createStored(t)

// Create storage with a key-only core so manifest is absent on disk.
const bootstrap = await open(key, { compat: false })
await bootstrap.ready()
await bootstrap.close()

const core = await open(key, { compat: true })
await core.ready()

await core.setEncryptionKey(encryptionKey)
t.is(core.core.compat, true, 'starts in compat mode before manifest is known')

// This sets the manifest on the shared core during session open.
const s = core.session({ manifest })
await s.ready()

t.is(core.core.compat, false, 'compat mode flips after manifest is set')

await core.encryption.decrypt(0, b4a.alloc(core.padding), core)
t.ok(core.encryption.blockKey, 'reload keeps a valid block key')
t.ok(core.encryption.blindingKey, 'reload keeps a valid blinding key')

await s.close()
await core.close()
})

function getBlock(core, index) {
const batch = core.core.storage.read()
const b = batch.getBlock(index)
Expand Down
2 changes: 1 addition & 1 deletion test/replicate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2887,7 +2887,7 @@ test('local writable caught up by remote', async function (t) {
t.is(b.length, a.length)
})

test.solo('local recovering from remote', async function (t) {
test('local recovering from remote', async function (t) {
const a = await create(t)

await a.append(['a', 'b', 'c', 'd', 'e'])
Expand Down