+EncryptedKVStorage
This commit is contained in:
1 parent
776f4e75ff
commit
fe6190eb8d
4 files changed
+161
-5
No files matched your search
@@ -0,0 +1,86 @@
|
||||
package net.sergeych.crypto2
|
||||
|
||||
import net.sergeych.bintools.KVStorage
|
||||
import net.sergeych.bintools.MemoryKVStorage
|
||||
import net.sergeych.bintools.optStored
|
||||
import net.sergeych.synctools.ProtectedOp
|
||||
import net.sergeych.synctools.invoke
|
||||
import kotlin.random.Random
|
||||
import kotlin.random.nextUBytes
|
||||
|
||||
/**
|
||||
* Encrypted variant of [KVStorage]; the storage is encrypted with the given key
|
||||
* in a given [plainStore] [KVStorage]. It is threadsafe where
|
||||
* applicable. Also, it supports in-place key change [reEncrypt].
|
||||
*
|
||||
* Keys are stored encrypted and used hashed so it is not possible to
|
||||
* retrieve them without knowing the encryption key.
|
||||
*/
|
||||
class EncryptedKVStorage(
|
||||
private val plainStore: KVStorage,
|
||||
private var encryptionKey: SymmetricKey,
|
||||
private val prefix: String = "EKVS_"
|
||||
) : KVStorage {
|
||||
private val op = ProtectedOp()
|
||||
|
||||
private val prefix2 = prefix + ":"
|
||||
|
||||
val seed: UByteArray
|
||||
|
||||
init {
|
||||
var encryptedSeed by plainStore.optStored<UByteArray>("$prefix#seed")
|
||||
seed = encryptedSeed?.let { encryptionKey.decrypt(it) }
|
||||
?: Random.nextUBytes(32).also {
|
||||
encryptedSeed = encryptionKey.encrypt(it)
|
||||
}
|
||||
}
|
||||
|
||||
private fun mkkey(key: String): String =
|
||||
blake2b(key.encodeToByteArray().asUByteArray() + seed).encodeToBase64Url()
|
||||
|
||||
override val keys: Set<String>
|
||||
get() = op.invoke {
|
||||
plainStore.keys.mapNotNull {
|
||||
if (it.startsWith(prefix2))
|
||||
plainStore[it]?.let { encrypted ->
|
||||
encryptionKey.decrypt(encrypted.asUByteArray()).asByteArray().decodeToString()
|
||||
}
|
||||
else null
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
override fun get(key: String): ByteArray? = op {
|
||||
val k0 = mkkey(key)
|
||||
val k = prefix + k0
|
||||
plainStore[k]?.let { encryptionKey.decrypt(it.asUByteArray()).asByteArray() }
|
||||
?.also {
|
||||
val k2 = prefix2 + k0
|
||||
if (k2 !in plainStore)
|
||||
plainStore[k2] = encryptionKey.encrypt(key).asByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
override fun set(key: String, value: ByteArray?) {
|
||||
op {
|
||||
val k1 = mkkey(key)
|
||||
plainStore[prefix + k1] = value?.let {
|
||||
encryptionKey.encrypt(it.asUByteArray()).asByteArray()
|
||||
}
|
||||
plainStore[prefix2 + k1] = encryptionKey.encrypt(key).asByteArray()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-encrypts the entire storage in-place with the given key; it is threadsafe where
|
||||
* applicable.
|
||||
*
|
||||
* This method re-encrypts every data item so it is cryptographically secure.
|
||||
*/
|
||||
fun reEncrypt(newKey: SymmetricKey) {
|
||||
op {
|
||||
val copy = MemoryKVStorage().also { it.addAll(this) }
|
||||
encryptionKey = newKey
|
||||
addAll(copy)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user