Skip to content

Commit 5752ad2

Browse files
Merge pull request ClickHouse#51086 from aiven-sal/aiven-sal/ctriv
Generate safe IVs
2 parents df1ea0b + 6b49968 commit 5752ad2

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/IO/FileEncryptionCommon.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
#include <Common/SipHash.h>
99
#include <Common/safe_cast.h>
1010

11-
#include <boost/algorithm/string/predicate.hpp>
12-
#include <cassert>
13-
#include <random>
11+
# include <cassert>
12+
# include <boost/algorithm/string/predicate.hpp>
1413

14+
# include <openssl/err.h>
15+
# include <openssl/rand.h>
1516

1617
namespace DB
1718
{
@@ -20,6 +21,7 @@ namespace ErrorCodes
2021
{
2122
extern const int BAD_ARGUMENTS;
2223
extern const int DATA_ENCRYPTION_ERROR;
24+
extern const int OPENSSL_ERROR;
2325
}
2426

2527
namespace FileEncryption
@@ -260,12 +262,11 @@ void InitVector::write(WriteBuffer & out) const
260262

261263
InitVector InitVector::random()
262264
{
263-
std::random_device rd;
264-
std::mt19937 gen{rd()};
265-
std::uniform_int_distribution<UInt128::base_type> dis;
266265
UInt128 counter;
267-
for (auto & i : counter.items)
268-
i = dis(gen);
266+
auto * buf = reinterpret_cast<unsigned char *>(counter.items);
267+
auto ret = RAND_bytes(buf, sizeof(counter.items));
268+
if (ret != 1)
269+
throw Exception(DB::ErrorCodes::OPENSSL_ERROR, "OpenSSL error code: {}", ERR_get_error());
269270
return InitVector{counter};
270271
}
271272

0 commit comments

Comments
 (0)