Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate safe IVs #51086

Merged
merged 1 commit into from Jun 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -260,12 +262,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