Place generateRandomData() under {I|Deterministic}Random

This commit is contained in:
Junhyun Shim 2022-07-20 12:31:19 +02:00
parent d1dc82629e
commit 0874e553cd
16 changed files with 46 additions and 36 deletions

View File

@ -70,7 +70,7 @@ TEST_CASE("/fdbclient/TenantMapEntry/Serialization") {
int subspaceLength = deterministicRandom()->randomInt(0, 20); int subspaceLength = deterministicRandom()->randomInt(0, 20);
Standalone<StringRef> subspace = makeString(subspaceLength); Standalone<StringRef> subspace = makeString(subspaceLength);
generateRandomData(mutateString(subspace), subspaceLength); deterministicRandom()->randomBytes(mutateString(subspace), subspaceLength);
TenantMapEntry entry(id, subspace); TenantMapEntry entry(id, subspace);
int64_t bigEndianId = bigEndian64(id); int64_t bigEndianId = bigEndian64(id);

View File

@ -258,7 +258,7 @@ Optional<Standalone<StringRef>> AsyncFileEncrypted::RandomCache::get(uint32_t bl
TEST_CASE("fdbrpc/AsyncFileEncrypted") { TEST_CASE("fdbrpc/AsyncFileEncrypted") {
state const int bytes = FLOW_KNOBS->ENCRYPTION_BLOCK_SIZE * deterministicRandom()->randomInt(0, 1000); state const int bytes = FLOW_KNOBS->ENCRYPTION_BLOCK_SIZE * deterministicRandom()->randomInt(0, 1000);
state std::vector<unsigned char> writeBuffer(bytes, 0); state std::vector<unsigned char> writeBuffer(bytes, 0);
generateRandomData(&writeBuffer.front(), bytes); deterministicRandom()->randomBytes(&writeBuffer.front(), bytes);
state std::vector<unsigned char> readBuffer(bytes, 0); state std::vector<unsigned char> readBuffer(bytes, 0);
ASSERT(g_network->isSimulated()); ASSERT(g_network->isSimulated());
StreamCipherKey::initializeGlobalRandomTestKey(); StreamCipherKey::initializeGlobalRandomTestKey();

View File

@ -880,7 +880,7 @@ std::shared_ptr<platform::TmpFile> prepareTokenFile(const uint8_t* buff, const i
std::shared_ptr<platform::TmpFile> prepareTokenFile(const int tokenLen) { std::shared_ptr<platform::TmpFile> prepareTokenFile(const int tokenLen) {
Standalone<StringRef> buff = makeString(tokenLen); Standalone<StringRef> buff = makeString(tokenLen);
generateRandomData(mutateString(buff), tokenLen); deterministicRandom()->randomBytes(mutateString(buff), tokenLen);
return prepareTokenFile(buff.begin(), tokenLen); return prepareTokenFile(buff.begin(), tokenLen);
} }
@ -941,7 +941,7 @@ ACTOR Future<Void> testValidationFileTokenPayloadTooLarge(Reference<RESTKmsConne
SERVER_KNOBS->REST_KMS_CONNECTOR_VALIDATION_TOKEN_MAX_SIZE + SERVER_KNOBS->REST_KMS_CONNECTOR_VALIDATION_TOKEN_MAX_SIZE +
2; 2;
Standalone<StringRef> buff = makeString(tokenLen); Standalone<StringRef> buff = makeString(tokenLen);
generateRandomData(mutateString(buff), tokenLen); deterministicRandom()->randomBytes(mutateString(buff), tokenLen);
std::string details; std::string details;
state std::vector<std::shared_ptr<platform::TmpFile>> tokenfiles; state std::vector<std::shared_ptr<platform::TmpFile>> tokenfiles;
@ -972,7 +972,7 @@ ACTOR Future<Void> testMultiValidationFileTokenFiles(Reference<RESTKmsConnectorC
state std::unordered_map<std::string, std::string> tokenNameValueMap; state std::unordered_map<std::string, std::string> tokenNameValueMap;
state std::string tokenDetailsStr; state std::string tokenDetailsStr;
generateRandomData(mutateString(buff), tokenLen); deterministicRandom()->randomBytes(mutateString(buff), tokenLen);
for (int i = 1; i <= numFiles; i++) { for (int i = 1; i <= numFiles; i++) {
std::string tokenName = std::to_string(i); std::string tokenName = std::to_string(i);
@ -1350,7 +1350,7 @@ TEST_CASE("/KmsConnector/REST/ParseKmsDiscoveryUrls") {
state Arena arena; state Arena arena;
// initialize cipher key used for testing // initialize cipher key used for testing
generateRandomData(&BASE_CIPHER_KEY_TEST[0], 32); deterministicRandom()->randomBytes(&BASE_CIPHER_KEY_TEST[0], 32);
wait(testParseDiscoverKmsUrlFileNotFound(ctx)); wait(testParseDiscoverKmsUrlFileNotFound(ctx));
wait(testParseDiscoverKmsUrlFile(ctx)); wait(testParseDiscoverKmsUrlFile(ctx));
@ -1363,7 +1363,7 @@ TEST_CASE("/KmsConnector/REST/ParseValidationTokenFile") {
state Arena arena; state Arena arena;
// initialize cipher key used for testing // initialize cipher key used for testing
generateRandomData(&BASE_CIPHER_KEY_TEST[0], 32); deterministicRandom()->randomBytes(&BASE_CIPHER_KEY_TEST[0], 32);
wait(testEmptyValidationFileDetails(ctx)); wait(testEmptyValidationFileDetails(ctx));
wait(testMalformedFileValidationTokenDetails(ctx)); wait(testMalformedFileValidationTokenDetails(ctx));
@ -1380,7 +1380,7 @@ TEST_CASE("/KmsConnector/REST/ParseKmsResponse") {
state Arena arena; state Arena arena;
// initialize cipher key used for testing // initialize cipher key used for testing
generateRandomData(&BASE_CIPHER_KEY_TEST[0], 32); deterministicRandom()->randomBytes(&BASE_CIPHER_KEY_TEST[0], 32);
testMissingCipherDetailsTag(ctx); testMissingCipherDetailsTag(ctx);
testMalformedCipherDetails(ctx); testMalformedCipherDetails(ctx);
@ -1394,7 +1394,7 @@ TEST_CASE("/KmsConnector/REST/GetEncryptionKeyOps") {
state Arena arena; state Arena arena;
// initialize cipher key used for testing // initialize cipher key used for testing
generateRandomData(&BASE_CIPHER_KEY_TEST[0], 32); deterministicRandom()->randomBytes(&BASE_CIPHER_KEY_TEST[0], 32);
// Prepare KmsConnector context details // Prepare KmsConnector context details
wait(testParseDiscoverKmsUrlFile(ctx)); wait(testParseDiscoverKmsUrlFile(ctx));

View File

@ -10273,7 +10273,7 @@ TEST_CASE(":/redwood/performance/extentQueue") {
state int v; state int v;
state ExtentQueueEntry<16> e; state ExtentQueueEntry<16> e;
generateRandomData(e.entry, 16); deterministicRandom()->randomBytes(e.entry, 16);
state int sinceYield = 0; state int sinceYield = 0;
for (v = 1; v <= numEntries; ++v) { for (v = 1; v <= numEntries; ++v) {
// Sometimes do a commit // Sometimes do a commit

View File

@ -65,7 +65,7 @@ struct EncryptedMutationMessage {
ASSERT(textCipherItr != cipherKeys.end() && textCipherItr->second.isValid()); ASSERT(textCipherItr != cipherKeys.end() && textCipherItr->second.isValid());
ASSERT(headerCipherItr != cipherKeys.end() && headerCipherItr->second.isValid()); ASSERT(headerCipherItr != cipherKeys.end() && headerCipherItr->second.isValid());
uint8_t iv[AES_256_IV_LENGTH]; uint8_t iv[AES_256_IV_LENGTH];
generateRandomData(iv, AES_256_IV_LENGTH); deterministicRandom()->randomBytes(iv, AES_256_IV_LENGTH);
BinaryWriter bw(AssumeVersion(g_network->protocolVersion())); BinaryWriter bw(AssumeVersion(g_network->protocolVersion()));
bw << mutation; bw << mutation;
EncryptedMutationMessage encrypted_mutation; EncryptedMutationMessage encrypted_mutation;
@ -114,4 +114,4 @@ struct EncryptedMutationMessage {
return mutation; return mutation;
} }
}; };
#endif #endif

View File

@ -377,7 +377,7 @@ struct AsyncFileCorrectnessWorkload : public AsyncFileWorkload {
} }
} else if (info.operation == WRITE) { } else if (info.operation == WRITE) {
info.data = self->allocateBuffer(info.length); info.data = self->allocateBuffer(info.length);
generateRandomData(reinterpret_cast<uint8_t*>(info.data->buffer), info.length); deterministicRandom()->randomBytes(reinterpret_cast<uint8_t*>(info.data->buffer), info.length);
memcpy(&self->memoryFile->buffer[info.offset], info.data->buffer, info.length); memcpy(&self->memoryFile->buffer[info.offset], info.data->buffer, info.length);
memset(&self->fileValidityMask[info.offset], 0xFF, info.length); memset(&self->fileValidityMask[info.offset], 0xFF, info.length);

View File

@ -159,7 +159,7 @@ struct EncryptionOpsWorkload : TestWorkload {
void generateRandomBaseCipher(const int maxLen, uint8_t* buff, int* retLen) { void generateRandomBaseCipher(const int maxLen, uint8_t* buff, int* retLen) {
memset(buff, 0, maxLen); memset(buff, 0, maxLen);
*retLen = deterministicRandom()->randomInt(maxLen / 2, maxLen); *retLen = deterministicRandom()->randomInt(maxLen / 2, maxLen);
generateRandomData(buff, *retLen); deterministicRandom()->randomBytes(buff, *retLen);
} }
void setupCipherEssentials() { void setupCipherEssentials() {
@ -247,7 +247,7 @@ struct EncryptionOpsWorkload : TestWorkload {
const EncryptAuthTokenMode authMode, const EncryptAuthTokenMode authMode,
BlobCipherEncryptHeader* header) { BlobCipherEncryptHeader* header) {
uint8_t iv[AES_256_IV_LENGTH]; uint8_t iv[AES_256_IV_LENGTH];
generateRandomData(&iv[0], AES_256_IV_LENGTH); deterministicRandom()->randomBytes(&iv[0], AES_256_IV_LENGTH);
EncryptBlobCipherAes265Ctr encryptor(textCipherKey, headerCipherKey, &iv[0], AES_256_IV_LENGTH, authMode); EncryptBlobCipherAes265Ctr encryptor(textCipherKey, headerCipherKey, &iv[0], AES_256_IV_LENGTH, authMode);
auto start = std::chrono::high_resolution_clock::now(); auto start = std::chrono::high_resolution_clock::now();
@ -341,7 +341,7 @@ struct EncryptionOpsWorkload : TestWorkload {
} }
int dataLen = isFixedSizePayload() ? pageSize : deterministicRandom()->randomInt(100, maxBufSize); int dataLen = isFixedSizePayload() ? pageSize : deterministicRandom()->randomInt(100, maxBufSize);
generateRandomData(buff.get(), dataLen); deterministicRandom()->randomBytes(buff.get(), dataLen);
// Encrypt the payload - generates BlobCipherEncryptHeader to assist decryption later // Encrypt the payload - generates BlobCipherEncryptHeader to assist decryption later
BlobCipherEncryptHeader header; BlobCipherEncryptHeader header;

View File

@ -85,7 +85,7 @@ struct TenantManagementWorkload : TestWorkload {
if (self->clientId == 0) { if (self->clientId == 0) {
self->tenantSubspace = makeString(deterministicRandom()->randomInt(0, 10)); self->tenantSubspace = makeString(deterministicRandom()->randomInt(0, 10));
loop { loop {
generateRandomData(mutateString(self->tenantSubspace), self->tenantSubspace.size()); deterministicRandom()->randomBytes(mutateString(self->tenantSubspace), self->tenantSubspace.size());
if (!self->tenantSubspace.startsWith(systemKeys.begin)) { if (!self->tenantSubspace.startsWith(systemKeys.begin)) {
break; break;
} }

View File

@ -389,7 +389,7 @@ EncryptBlobCipherAes265Ctr::EncryptBlobCipherAes265Ctr(Reference<BlobCipherKey>
const EncryptAuthTokenMode mode) const EncryptAuthTokenMode mode)
: ctx(EVP_CIPHER_CTX_new()), textCipherKey(tCipherKey), headerCipherKey(hCipherKey), authTokenMode(mode) { : ctx(EVP_CIPHER_CTX_new()), textCipherKey(tCipherKey), headerCipherKey(hCipherKey), authTokenMode(mode) {
ASSERT(isEncryptHeaderAuthTokenModeValid(mode)); ASSERT(isEncryptHeaderAuthTokenModeValid(mode));
generateRandomData(iv, AES_256_IV_LENGTH); deterministicRandom()->randomBytes(iv, AES_256_IV_LENGTH);
init(); init();
} }
@ -767,7 +767,7 @@ TEST_CASE("flow/BlobCipher") {
BaseCipher(const EncryptCipherDomainId& dId, const EncryptCipherBaseKeyId& kId) BaseCipher(const EncryptCipherDomainId& dId, const EncryptCipherBaseKeyId& kId)
: domainId(dId), len(deterministicRandom()->randomInt(AES_256_KEY_LENGTH / 2, AES_256_KEY_LENGTH + 1)), : domainId(dId), len(deterministicRandom()->randomInt(AES_256_KEY_LENGTH / 2, AES_256_KEY_LENGTH + 1)),
keyId(kId), key(std::make_unique<uint8_t[]>(len)) { keyId(kId), key(std::make_unique<uint8_t[]>(len)) {
generateRandomData(key.get(), len); deterministicRandom()->randomBytes(key.get(), len);
} }
}; };
@ -870,11 +870,11 @@ TEST_CASE("flow/BlobCipher") {
Reference<BlobCipherKey> headerCipherKey = cipherKeyCache->getLatestCipherKey(ENCRYPT_HEADER_DOMAIN_ID); Reference<BlobCipherKey> headerCipherKey = cipherKeyCache->getLatestCipherKey(ENCRYPT_HEADER_DOMAIN_ID);
const int bufLen = deterministicRandom()->randomInt(786, 2127) + 512; const int bufLen = deterministicRandom()->randomInt(786, 2127) + 512;
uint8_t orgData[bufLen]; uint8_t orgData[bufLen];
generateRandomData(&orgData[0], bufLen); deterministicRandom()->randomBytes(&orgData[0], bufLen);
Arena arena; Arena arena;
uint8_t iv[AES_256_IV_LENGTH]; uint8_t iv[AES_256_IV_LENGTH];
generateRandomData(&iv[0], AES_256_IV_LENGTH); deterministicRandom()->randomBytes(&iv[0], AES_256_IV_LENGTH);
BlobCipherEncryptHeader headerCopy; BlobCipherEncryptHeader headerCopy;
// validate basic encrypt followed by decrypt operation for AUTH_MODE_NONE // validate basic encrypt followed by decrypt operation for AUTH_MODE_NONE

View File

@ -19,6 +19,7 @@
*/ */
#include "fmt/format.h" #include "fmt/format.h"
#include "flow/Arena.h"
#include "flow/DeterministicRandom.h" #include "flow/DeterministicRandom.h"
#include <cstring> #include <cstring>
@ -124,6 +125,23 @@ std::string DeterministicRandom::randomAlphaNumeric(int length) {
return s; return s;
} }
void DeterministicRandom::randomBytes(uint8_t* buf, int length) {
constexpr const int unitLen = sizeof(decltype(gen64()));
for (int i = 0; i < length; i += unitLen) {
auto val = gen64();
memcpy(buf + i, &val, std::min(unitLen, length - i));
}
if (randLog && useRandLog) {
constexpr const int cutOff = 32;
bool tooLong = length > cutOff;
fmt::print(randLog,
"Rbytes[{}] {}{}\n",
length,
StringRef(buf, std::min(cutOff, length)).printable(),
tooLong ? "..." : "");
}
}
uint64_t DeterministicRandom::peek() const { uint64_t DeterministicRandom::peek() const {
return next; return next;
} }
@ -134,10 +152,3 @@ void DeterministicRandom::addref() {
void DeterministicRandom::delref() { void DeterministicRandom::delref() {
ReferenceCounted<DeterministicRandom>::delref(); ReferenceCounted<DeterministicRandom>::delref();
} }
void generateRandomData(uint8_t* buffer, int length) {
for (int i = 0; i < length; i += sizeof(uint32_t)) {
uint32_t val = deterministicRandom()->randomUInt32();
memcpy(&buffer[i], &val, std::min(length - i, (int)sizeof(uint32_t)));
}
}

View File

@ -199,11 +199,11 @@ TEST_CASE("flow/StreamCipher") {
StreamCipherKey const* key = StreamCipherKey::getGlobalCipherKey(); StreamCipherKey const* key = StreamCipherKey::getGlobalCipherKey();
StreamCipher::IV iv; StreamCipher::IV iv;
generateRandomData(iv.data(), iv.size()); deterministicRandom()->randomBytes(iv.data(), iv.size());
Arena arena; Arena arena;
std::vector<unsigned char> plaintext(deterministicRandom()->randomInt(0, 10001)); std::vector<unsigned char> plaintext(deterministicRandom()->randomInt(0, 10001));
generateRandomData(&plaintext.front(), plaintext.size()); deterministicRandom()->randomBytes(&plaintext.front(), plaintext.size());
std::vector<unsigned char> ciphertext(plaintext.size() + AES_BLOCK_SIZE); std::vector<unsigned char> ciphertext(plaintext.size() + AES_BLOCK_SIZE);
std::vector<unsigned char> decryptedtext(plaintext.size() + AES_BLOCK_SIZE); std::vector<unsigned char> decryptedtext(plaintext.size() + AES_BLOCK_SIZE);

View File

@ -49,6 +49,7 @@ public:
UID randomUniqueID() override; UID randomUniqueID() override;
char randomAlphaNumeric() override; char randomAlphaNumeric() override;
std::string randomAlphaNumeric(int length) override; std::string randomAlphaNumeric(int length) override;
void randomBytes(uint8_t* buf, int length) override;
uint64_t peek() const override; uint64_t peek() const override;
void addref() override; void addref() override;
void delref() override; void delref() override;

View File

@ -143,6 +143,7 @@ public:
virtual UID randomUniqueID() = 0; virtual UID randomUniqueID() = 0;
virtual char randomAlphaNumeric() = 0; virtual char randomAlphaNumeric() = 0;
virtual std::string randomAlphaNumeric(int length) = 0; virtual std::string randomAlphaNumeric(int length) = 0;
virtual void randomBytes(uint8_t* buf, int length) = 0;
virtual uint32_t randomSkewedUInt32(uint32_t min, uint32_t maxPlusOne) = 0; virtual uint32_t randomSkewedUInt32(uint32_t min, uint32_t maxPlusOne) = 0;
virtual uint64_t peek() const = 0; // returns something that is probably different for different random states. virtual uint64_t peek() const = 0; // returns something that is probably different for different random states.
// Deterministic (and idempotent) for a deterministic generator. // Deterministic (and idempotent) for a deterministic generator.
@ -196,7 +197,4 @@ Reference<IRandom> nondeterministicRandom();
// WARNING: This is not thread safe and must not be called from any other thread than the network thread! // WARNING: This is not thread safe and must not be called from any other thread than the network thread!
Reference<IRandom> debugRandom(); Reference<IRandom> debugRandom();
// Populates a buffer with a random sequence of bytes
void generateRandomData(uint8_t* buffer, int length);
#endif #endif

View File

@ -53,7 +53,7 @@ public:
int size() const { return keySize; } int size() const { return keySize; }
uint8_t* data() const { return arr.get(); } uint8_t* data() const { return arr.get(); }
void initializeKey(uint8_t* data, int len); void initializeKey(uint8_t* data, int len);
void initializeRandomTestKey() { generateRandomData(arr.get(), keySize); } void initializeRandomTestKey() { deterministicRandom()->randomBytes(arr.get(), keySize); }
void reset() { memset(arr.get(), 0, keySize); } void reset() { memset(arr.get(), 0, keySize); }
static bool isGlobalKeyPresent(); static bool isGlobalKeyPresent();

View File

@ -25,7 +25,7 @@
static StreamCipher::IV getRandomIV() { static StreamCipher::IV getRandomIV() {
StreamCipher::IV iv; StreamCipher::IV iv;
generateRandomData(iv.data(), iv.size()); deterministicRandom()->randomBytes(iv.data(), iv.size());
return iv; return iv;
} }

View File

@ -28,7 +28,7 @@ static inline void initGlobalData() {
if (!globalData) { if (!globalData) {
globalData = static_cast<uint8_t*>(allocateFast(globalDataSize)); globalData = static_cast<uint8_t*>(allocateFast(globalDataSize));
} }
generateRandomData(globalData, globalDataSize); deterministicRandom()->randomBytes(globalData, globalDataSize);
} }
KeyValueRef getKV(size_t keySize, size_t valueSize) { KeyValueRef getKV(size_t keySize, size_t valueSize) {