Skip to content

Commit

Permalink
Rename fizz::HkdfImpl -> fizz::Hkdf
Browse files Browse the repository at this point in the history
Summary:
Nothing other than `fizz::HkdfImpl` inherits from `fizz::Hkdf`. (There are
no mocks, etc.) There is no reason to have the separation since the HKDF
algorithm will not change based on Factory implementations, and `fizz::HkdfImpl`
is already written _in terms of_ a hashing primitive that is constructed by the
factory.

Reviewed By: zalecodez

Differential Revision: D62466716

fbshipit-source-id: 9428afbc5874a29df95884a9b5296fcb79f597e3
  • Loading branch information
Mingtao Yang authored and facebook-github-bot committed Sep 12, 2024
1 parent 6d5fcc6 commit d828020
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 73 deletions.
4 changes: 2 additions & 2 deletions third-party/fizz/src/fizz/backend/openssl/OpenSSL.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ makeOpenSSLECKeyExchange() {
}

template <typename Hash>
HkdfImpl createHkdf() {
return HkdfImpl(Hash::HashLen, ::fizz::openssl::makeHasher<Hash>);
Hkdf createHkdf() {
return Hkdf(Hash::HashLen, ::fizz::openssl::makeHasher<Hash>);
}

} // namespace fizz::openssl
9 changes: 4 additions & 5 deletions third-party/fizz/src/fizz/crypto/Hkdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@

namespace fizz {

std::vector<uint8_t> HkdfImpl::extract(
folly::ByteRange salt,
folly::ByteRange ikm) const {
std::vector<uint8_t> Hkdf::extract(folly::ByteRange salt, folly::ByteRange ikm)
const {
auto zeros = std::vector<uint8_t>(hashLength_, 0);
// Extraction step HMAC-HASH(salt, IKM)
std::vector<uint8_t> extractedKey(hashLength_);
Expand All @@ -26,7 +25,7 @@ std::vector<uint8_t> HkdfImpl::extract(
return extractedKey;
}

std::unique_ptr<folly::IOBuf> HkdfImpl::expand(
std::unique_ptr<folly::IOBuf> Hkdf::expand(
folly::ByteRange extractedKey,
const folly::IOBuf& info,
size_t outputBytes) const {
Expand Down Expand Up @@ -64,7 +63,7 @@ std::unique_ptr<folly::IOBuf> HkdfImpl::expand(
return expanded;
}

std::unique_ptr<folly::IOBuf> HkdfImpl::hkdf(
std::unique_ptr<folly::IOBuf> Hkdf::hkdf(
folly::ByteRange ikm,
folly::ByteRange salt,
const folly::IOBuf& info,
Expand Down
35 changes: 5 additions & 30 deletions third-party/fizz/src/fizz/crypto/Hkdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,49 +23,24 @@ namespace fizz {
*/
class Hkdf {
public:
virtual ~Hkdf() = default;

virtual std::vector<uint8_t> extract(
folly::ByteRange salt,
folly::ByteRange ikm) const = 0;

virtual std::unique_ptr<folly::IOBuf> expand(
folly::ByteRange extractedKey,
const folly::IOBuf& info,
size_t outputBytes) const = 0;

virtual std::unique_ptr<folly::IOBuf> hkdf(
folly::ByteRange ikm,
folly::ByteRange salt,
const folly::IOBuf& info,
size_t outputBytes) const = 0;

virtual size_t hashLength() const = 0;
};

/**
* HKDF implementation.
*/
class HkdfImpl : public Hkdf {
public:
HkdfImpl(size_t hashLength, HasherFactory makeHasher)
Hkdf(size_t hashLength, HasherFactory makeHasher)
: hashLength_(hashLength), makeHasher_(makeHasher) {}

std::vector<uint8_t> extract(folly::ByteRange salt, folly::ByteRange ikm)
const override;
const;

std::unique_ptr<folly::IOBuf> expand(
folly::ByteRange extractedKey,
const folly::IOBuf& info,
size_t outputBytes) const override;
size_t outputBytes) const;

std::unique_ptr<folly::IOBuf> hkdf(
folly::ByteRange ikm,
folly::ByteRange salt,
const folly::IOBuf& info,
size_t outputBytes) const override;
size_t outputBytes) const;

size_t hashLength() const override {
size_t hashLength() const {
return hashLength_;
}

Expand Down
2 changes: 1 addition & 1 deletion third-party/fizz/src/fizz/crypto/KeyDerivation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace fizz {

KeyDerivationImpl::KeyDerivationImpl(
size_t hashLength,
HkdfImpl hkdf,
Hkdf hkdf,
folly::ByteRange blankHash)
: hashLength_(hashLength), hkdf_(hkdf), blankHash_(blankHash) {}

Expand Down
7 changes: 2 additions & 5 deletions third-party/fizz/src/fizz/crypto/KeyDerivation.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ class KeyDerivation {

class KeyDerivationImpl : public KeyDerivation {
public:
KeyDerivationImpl(
size_t hashLength,
HkdfImpl hkdf,
folly::ByteRange blankHash);
KeyDerivationImpl(size_t hashLength, Hkdf hkdf, folly::ByteRange blankHash);

size_t hashLength() const override {
return hashLength_;
Expand Down Expand Up @@ -95,7 +92,7 @@ class KeyDerivationImpl : public KeyDerivation {

private:
size_t hashLength_;
HkdfImpl hkdf_;
Hkdf hkdf_;
folly::ByteRange blankHash_;
};
} // namespace fizz
20 changes: 10 additions & 10 deletions third-party/fizz/src/fizz/crypto/hpke/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,25 +141,25 @@ CipherSuite getCipherSuite(AeadId aeadId) {
}
}

std::unique_ptr<Hkdf> makeHpkeHkdf(
std::unique_ptr<::fizz::hpke::Hkdf> makeHpkeHkdf(
std::unique_ptr<folly::IOBuf> prefix,
KDFId kdfId) {
switch (kdfId) {
case KDFId::Sha256:
return std::make_unique<Hkdf>(
return std::make_unique<::fizz::hpke::Hkdf>(
std::move(prefix),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, openssl::makeHasher<Sha256>)));
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, openssl::makeHasher<Sha256>));
case KDFId::Sha384:
return std::make_unique<Hkdf>(
return std::make_unique<::fizz::hpke::Hkdf>(
std::move(prefix),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha384::HashLen, openssl::makeHasher<Sha384>)));
std::make_unique<::fizz::Hkdf>(
Sha384::HashLen, openssl::makeHasher<Sha384>));
case KDFId::Sha512:
return std::make_unique<Hkdf>(
return std::make_unique<::fizz::hpke::Hkdf>(
std::move(prefix),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha512::HashLen, openssl::makeHasher<Sha512>)));
std::make_unique<::fizz::Hkdf>(
Sha512::HashLen, openssl::makeHasher<Sha512>));
default:
throw std::runtime_error("hkdf: not implemented");
}
Expand Down
24 changes: 12 additions & 12 deletions third-party/fizz/src/fizz/crypto/hpke/test/ContextTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ TEST_P(HpkeContextTest, TestContext) {
toIOBuf(kExportSecret),
std::make_unique<fizz::hpke::Hkdf>(
kPrefix->clone(),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, &openssl::makeHasher<Sha256>))),
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, &openssl::makeHasher<Sha256>)),
suiteId->clone(),
fizz::hpke::HpkeContext::Role::Sender);
auto gotCiphertext = encryptContext.seal(
Expand All @@ -72,8 +72,8 @@ TEST_P(HpkeContextTest, TestContext) {
toIOBuf(kExportSecret),
std::make_unique<fizz::hpke::Hkdf>(
kPrefix->clone(),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, &openssl::makeHasher<Sha256>))),
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, &openssl::makeHasher<Sha256>)),
std::move(suiteId),
fizz::hpke::HpkeContext::Role::Receiver);
auto gotPlaintext = decryptContext.open(
Expand All @@ -96,8 +96,8 @@ TEST_P(HpkeContextTest, TestContextRoles) {
toIOBuf(kExportSecret),
std::make_unique<fizz::hpke::Hkdf>(
kPrefix->clone(),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, &openssl::makeHasher<Sha256>))),
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, &openssl::makeHasher<Sha256>)),
suiteId->clone(),
fizz::hpke::HpkeContext::Role::Sender);

Expand All @@ -109,8 +109,8 @@ TEST_P(HpkeContextTest, TestContextRoles) {
toIOBuf(kExportSecret),
std::make_unique<fizz::hpke::Hkdf>(
kPrefix->clone(),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, &openssl::makeHasher<Sha256>))),
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, &openssl::makeHasher<Sha256>)),
std::move(suiteId),
fizz::hpke::HpkeContext::Role::Receiver);

Expand Down Expand Up @@ -143,8 +143,8 @@ TEST_P(HpkeContextTest, TestExportSecret) {
toIOBuf(testParam.exporterSecret),
std::make_unique<fizz::hpke::Hkdf>(
kPrefix->clone(),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, &openssl::makeHasher<Sha256>))),
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, &openssl::makeHasher<Sha256>)),
std::move(suiteId),
role);
auto secret = context.exportSecret(std::move(exporterContext), 32);
Expand Down Expand Up @@ -172,8 +172,8 @@ TEST_P(HpkeContextTest, TestExportSecretThrow) {
toIOBuf(testParam.exporterSecret),
std::make_unique<fizz::hpke::Hkdf>(
kPrefix->clone(),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, &openssl::makeHasher<Sha256>))),
std::make_unique<::fizz::Hkdf>(
Sha256::HashLen, &openssl::makeHasher<Sha256>)),
std::move(suiteId),
role);

Expand Down
4 changes: 2 additions & 2 deletions third-party/fizz/src/fizz/crypto/hpke/test/DHKEMTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ DHKEM getDHKEM(std::unique_ptr<KeyExchange> actualKex, NamedGroup group) {
auto prefix = "HPKE-v1";
auto hkdf = std::make_unique<fizz::hpke::Hkdf>(
folly::IOBuf::copyBuffer(prefix),
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, openssl::makeHasher<Sha256>)));
std::make_unique<Hkdf>(
Hkdf(Sha256::HashLen, openssl::makeHasher<Sha256>)));
return DHKEM(
std::make_unique<MockKeyExchange>(std::move(actualKex)),
group,
Expand Down
2 changes: 1 addition & 1 deletion third-party/fizz/src/fizz/crypto/test/HkdfTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TEST_P(HkdfTest, TestHkdfSha256Expand) {
CHECK_EQ(outputBytes, expectedOkm->length());

auto actualOkm =
HkdfImpl(Sha256::HashLen, openssl::makeHasher<fizz::Sha256>)
Hkdf(Sha256::HashLen, openssl::makeHasher<fizz::Sha256>)
.hkdf(ikm->coalesce(), salt->coalesce(), *info, outputBytes);
EXPECT_FALSE(actualOkm->isChained());
EXPECT_EQ(outputBytes, actualOkm->length());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ template <typename Hash>
inline KeyDerivationImpl createKeyDerivationImpl() {
return KeyDerivationImpl(
Hash::HashLen,
HkdfImpl(Hash::HashLen, openssl::makeHasher<Hash>),
Hkdf(Hash::HashLen, openssl::makeHasher<Hash>),
Hash::BlankHash);
}

Expand Down
2 changes: 1 addition & 1 deletion third-party/fizz/src/fizz/protocol/HandshakeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Buf HandshakeContextImpl::getFinishedData(folly::ByteRange baseKey) const {
auto context = getHandshakeContext();
size_t hashLen = hashState_->getHashLen();
auto finishedKey =
KeyDerivationImpl(hashLen, HkdfImpl(hashLen, makeHasher_), blankHash_)
KeyDerivationImpl(hashLen, Hkdf(hashLen, makeHasher_), blankHash_)
.expandLabel(baseKey, "finished", folly::IOBuf::create(0), hashLen);
auto data = folly::IOBuf::create(hashLen);
data->append(hashLen);
Expand Down
2 changes: 1 addition & 1 deletion third-party/fizz/src/fizz/protocol/MultiBackendFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ template <typename Hash>
inline std::unique_ptr<KeyDerivation> makeKeyDerivationPtr() {
return std::unique_ptr<KeyDerivationImpl>(new KeyDerivationImpl(
Hash::HashLen,
HkdfImpl(Hash::HashLen, ::fizz::openssl::makeHasher<Hash>),
Hkdf(Hash::HashLen, ::fizz::openssl::makeHasher<Hash>),
Hash::BlankHash));
}
} // namespace detail
Expand Down
4 changes: 2 additions & 2 deletions third-party/fizz/src/fizz/server/AeadTokenCipher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool Aead128GCMTokenCipher::setSecrets(
Secret extracted(tokenSecret.begin(), tokenSecret.end());
for (const auto& contextString : contextStrings_) {
extracted =
HkdfImpl(HashType::HashLen, openssl::makeHasher<HashType>)
Hkdf(HashType::HashLen, openssl::makeHasher<HashType>)
.extract(folly::range(contextString), folly::range(extracted));
}
secrets_.push_back(std::move(extracted));
Expand Down Expand Up @@ -112,7 +112,7 @@ std::unique_ptr<Aead> Aead128GCMTokenCipher::createAead(
folly::ByteRange salt) const {
auto aead = AeadType::makeCipher<CipherType>();
std::unique_ptr<folly::IOBuf> info = folly::IOBuf::wrapBuffer(salt);
auto keys = HkdfImpl(HashType::HashLen, openssl::makeHasher<HashType>)
auto keys = Hkdf(HashType::HashLen, openssl::makeHasher<HashType>)
.expand(secret, *info, aead->keyLength() + aead->ivLength());
folly::io::Cursor cursor(keys.get());
TrafficKey key;
Expand Down

0 comments on commit d828020

Please sign in to comment.