Skip to content

Commit

Permalink
Merge pull request #51096 from ClickHouse/backport/23.4/51086
Browse files Browse the repository at this point in the history
Backport #51086 to 23.4: Generate safe IVs
  • Loading branch information
alexey-milovidov committed Jun 16, 2023
2 parents dbe6693 + 9ab8c49 commit f06f72e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/IO/FileEncryptionCommon.cpp
Expand Up @@ -8,10 +8,11 @@
#include <Common/SipHash.h>
#include <Common/safe_cast.h>

#include <boost/algorithm/string/predicate.hpp>
#include <cassert>
#include <random>
# include <cassert>
# include <boost/algorithm/string/predicate.hpp>

# include <openssl/err.h>
# include <openssl/rand.h>

namespace DB
{
Expand All @@ -20,6 +21,7 @@ namespace ErrorCodes
{
extern const int BAD_ARGUMENTS;
extern const int DATA_ENCRYPTION_ERROR;
extern const int OPENSSL_ERROR;
}

namespace FileEncryption
Expand Down Expand Up @@ -254,12 +256,11 @@ void InitVector::write(WriteBuffer & out) const

InitVector InitVector::random()
{
std::random_device rd;
std::mt19937 gen{rd()};
std::uniform_int_distribution<UInt128::base_type> dis;
UInt128 counter;
for (auto & i : counter.items)
i = dis(gen);
auto * buf = reinterpret_cast<unsigned char *>(counter.items);
auto ret = RAND_bytes(buf, sizeof(counter.items));
if (ret != 1)
throw Exception(DB::ErrorCodes::OPENSSL_ERROR, "OpenSSL error code: {}", ERR_get_error());
return InitVector{counter};
}

Expand Down

0 comments on commit f06f72e

Please sign in to comment.