Skip to content

Commit

Permalink
Add random IO buffer generation
Browse files Browse the repository at this point in the history
Summary: Moves random IO buffer generation into Factory base for reusability.

Reviewed By: mingtaoy

Differential Revision: D65498181

fbshipit-source-id: 05729cfcb484dcd8d34c9772603fe3cf573f820b
  • Loading branch information
abakiaydin authored and facebook-github-bot committed Nov 7, 2024
1 parent 10e835c commit 1b76a3d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 24 deletions.
13 changes: 1 addition & 12 deletions fizz/client/ClientProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -793,17 +793,6 @@ static ClientHello constructEncryptedClientHello(
return chloOuter;
}

static std::unique_ptr<folly::IOBuf> randomIOBuf(
const Factory& factory,
size_t count) {
auto buf = folly::IOBuf::create(count);
if (count > 0) {
factory.makeRandomBytes(buf->writableData(), count);
buf->append(count);
}
return buf;
}

Actions
EventHandler<ClientTypes, StateEnum::Uninitialized, Event::Connect>::handle(
const State& /*state*/,
Expand Down Expand Up @@ -846,7 +835,7 @@ EventHandler<ClientTypes, StateEnum::Uninitialized, Event::Connect>::handle(
Buf legacySessionId;
if (context->getCompatibilityMode()) {
constexpr size_t kRandomSize = Random().size();
legacySessionId = randomIOBuf(*context->getFactory(), kRandomSize);
legacySessionId = context->getFactory()->makeRandomIOBuf(kRandomSize);
} else {
legacySessionId = folly::IOBuf::create(0);
}
Expand Down
9 changes: 9 additions & 0 deletions fizz/protocol/Factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ std::unique_ptr<KeyDerivation> Factory::makeKeyDeriver(
std::shared_ptr<Cert> Factory::makeIdentityOnlyCert(std::string ident) const {
return std::make_shared<IdentityCert>(std::move(ident));
}

Buf Factory::makeRandomIOBuf(size_t size) const {
auto buf = folly::IOBuf::create(size);
if (size > 0) {
makeRandomBytes(buf->writableData(), size);
buf->append(size);
}
return buf;
}
} // namespace fizz
2 changes: 2 additions & 0 deletions fizz/protocol/Factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,7 @@ class Factory {
* Should not be overridden *unless* for testing.
*/
virtual std::shared_ptr<Cert> makeIdentityOnlyCert(std::string ident) const;

Buf makeRandomIOBuf(size_t size) const;
};
} // namespace fizz
14 changes: 2 additions & 12 deletions fizz/protocol/ech/Encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,6 @@ void setAcceptConfirmation(

namespace {

static std::unique_ptr<folly::IOBuf> randomIOBuf(
const Factory& factory,
size_t count) {
auto buf = folly::IOBuf::create(count);
if (count > 0) {
factory.makeRandomBytes(buf->writableData(), count);
buf->append(count);
}
return buf;
}
// GREASE PSKs are essentially the same size as the source PSK with the actual
// contents of all fields replaced with random data. For the HRR case, the PSK
// identity is preserved.
Expand All @@ -421,7 +411,7 @@ ClientPresharedKey generateGreasePskCommon(
greaseIdentity.psk_identity = identity.psk_identity->clone();
} else {
size_t identitySize = identity.psk_identity->computeChainDataLength();
greaseIdentity.psk_identity = randomIOBuf(*factory, identitySize);
greaseIdentity.psk_identity = factory->makeRandomIOBuf(identitySize);
}

factory->makeRandomBytes(
Expand All @@ -432,7 +422,7 @@ ClientPresharedKey generateGreasePskCommon(
const auto& binder = source.binders.at(i);
PskBinder greaseBinder;
size_t binderSize = binder.binder->computeChainDataLength();
greaseBinder.binder = randomIOBuf(*factory, binderSize);
greaseBinder.binder = factory->makeRandomIOBuf(binderSize);
grease.binders.push_back(std::move(greaseBinder));
}
return grease;
Expand Down

0 comments on commit 1b76a3d

Please sign in to comment.