Skip to content

Commit 21070b0

Browse files
rpaul48KinanBab
authored andcommitted
Rocksdb Values are encrypted using a different random nonce
1 parent 3d8fd58 commit 21070b0

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

k9db/sql/rocksdb/encryption_on.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,12 @@ EncryptedKey EncryptionManager::EncryptKey(RocksdbSequence &&k) const {
195195
}
196196
EncryptedValue EncryptionManager::EncryptValue(const std::string &shard_name,
197197
RocksdbSequence &&v) {
198-
const unsigned char *nonce = this->global_nonce_.get();
198+
unsigned char *nonce = new unsigned char[NONCE_SIZE];
199+
randombytes_buf(nonce, NONCE_SIZE);
199200
const unsigned char *key = this->GetOrCreateUserKey(shard_name);
200-
return Cipher(Encrypt(v.Data(), nonce, key));
201+
std::string nonce_str(reinterpret_cast<char *>(nonce), NONCE_SIZE);
202+
std::string cipher_str = Encrypt(v.Data(), nonce, key);
203+
return Cipher(nonce_str + cipher_str);
201204
}
202205

203206
// Decryption of records.
@@ -212,9 +215,12 @@ RocksdbSequence EncryptionManager::DecryptKey(EncryptedKey &&k) const {
212215
}
213216
RocksdbSequence EncryptionManager::DecryptValue(const std::string &shard_name,
214217
EncryptedValue &&v) const {
215-
const unsigned char *nonce = this->global_nonce_.get();
218+
std::string str = v.Release();
219+
const char *ptr = str.data();
220+
const unsigned char *nonce = reinterpret_cast<const unsigned char *>(ptr);
216221
const unsigned char *key = this->GetUserKey(shard_name);
217-
return RocksdbSequence(Decrypt(v.Data(), nonce, key));
222+
rocksdb::Slice cipher(ptr + NONCE_SIZE, str.size() - NONCE_SIZE);
223+
return RocksdbSequence(Decrypt(cipher, nonce, key));
218224
}
219225

220226
// Encrypts a key for use with rocksdb Seek.

0 commit comments

Comments
 (0)