Skip to content

Commit

Permalink
Back out "Remove direct dependency from HPKE to OpenSSL"
Browse files Browse the repository at this point in the history
Summary:
Original commit changeset: 11ed92adddee

Original Phabricator Diff: D61632080

Reviewed By: zxjtan

Differential Revision: D62514132

fbshipit-source-id: bc284414a3d79425a26bf6c557b1c3264d643671
  • Loading branch information
Mingtao Yang authored and facebook-github-bot committed Sep 11, 2024
1 parent 2e14064 commit 21e1271
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 102 deletions.
2 changes: 1 addition & 1 deletion fizz/client/ClientProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ static folly::Optional<ECHParams> setupECH(
auto kex =
factory.makeKeyExchange(getKexGroup(kemId), KeyExchangeRole::Client);
auto setupResult =
constructHpkeSetupResult(factory, std::move(kex), supportedECHConfig);
constructHpkeSetupResult(std::move(kex), supportedECHConfig);

return ECHParams{
std::move(setupResult),
Expand Down
8 changes: 2 additions & 6 deletions fizz/client/test/ClientProtocolTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3003,9 +3003,7 @@ TEST_F(ClientProtocolTest, TestHelloRetryRequestECHFlow) {

// Make dummy payload.
size_t payloadSize = encodedClientHelloInnerAad->computeChainDataLength() +
hpke::makeCipher(
fizz::DefaultFactory(), echExtension.cipher_suite.aead_id)
->getCipherOverhead();
hpke::makeCipher(echExtension.cipher_suite.aead_id)->getCipherOverhead();
echExtension.payload = folly::IOBuf::create(payloadSize);
memset(echExtension.payload->writableData(), 0, payloadSize);
echExtension.payload->append(payloadSize);
Expand Down Expand Up @@ -3276,9 +3274,7 @@ TEST_F(ClientProtocolTest, TestHelloRetryRequestECHRejectedFlow) {

// Make dummy payload.
size_t payloadSize = encodedClientHelloInnerAad->computeChainDataLength() +
hpke::makeCipher(
fizz::DefaultFactory(), echExtension.cipher_suite.aead_id)
->getCipherOverhead();
hpke::makeCipher(echExtension.cipher_suite.aead_id)->getCipherOverhead();
echExtension.payload = folly::IOBuf::create(payloadSize);
memset(echExtension.payload->writableData(), 0, payloadSize);
echExtension.payload->append(payloadSize);
Expand Down
6 changes: 5 additions & 1 deletion fizz/crypto/hpke/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ cpp_library(
headers = [
"Utils.h",
],
deps = [
"//fizz/backend:openssl",
"//fizz/crypto/exchange:x25519",
],
exported_deps = [
":hkdf",
":types",
"//fizz/crypto:crypto",
"//fizz/crypto/aead:aead",
"//fizz/crypto/exchange:key_exchange",
"//fizz/protocol:factory",
"//fizz/protocol:types",
"//folly:optional",
],
Expand Down Expand Up @@ -82,6 +85,7 @@ cpp_library(
exported_deps = [
":hkdf",
":types",
"//fizz/crypto:crypto",
"//fizz/crypto/aead:aead",
"//fizz/protocol:types",
],
Expand Down
45 changes: 25 additions & 20 deletions fizz/crypto/hpke/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

#include <fizz/crypto/hpke/Utils.h>

#include <fizz/backend/openssl/OpenSSL.h>
#include <fizz/crypto/exchange/X25519.h>

namespace fizz {
namespace hpke {

Expand Down Expand Up @@ -139,46 +142,39 @@ CipherSuite getCipherSuite(AeadId aeadId) {
}

std::unique_ptr<Hkdf> makeHpkeHkdf(
const fizz::Factory& factory,
std::unique_ptr<folly::IOBuf> prefix,
KDFId kdfId) {
switch (kdfId) {
case KDFId::Sha256:
return std::make_unique<Hkdf>(
std::move(prefix),
std::make_unique<HkdfImpl>(HkdfImpl(
Sha256::HashLen, factory.makeHasher(HashFunction::Sha256))));
std::make_unique<HkdfImpl>(
HkdfImpl(Sha256::HashLen, openssl::makeHasher<Sha256>)));
case KDFId::Sha384:
return std::make_unique<Hkdf>(
std::move(prefix),
std::make_unique<HkdfImpl>(HkdfImpl(
Sha384::HashLen, factory.makeHasher(HashFunction::Sha384))));
std::make_unique<HkdfImpl>(
HkdfImpl(Sha384::HashLen, openssl::makeHasher<Sha384>)));
case KDFId::Sha512:
return std::make_unique<Hkdf>(
std::move(prefix),
std::make_unique<HkdfImpl>(HkdfImpl(
Sha512::HashLen, factory.makeHasher(HashFunction::Sha512))));
std::make_unique<HkdfImpl>(
HkdfImpl(Sha512::HashLen, openssl::makeHasher<Sha512>)));
default:
throw std::runtime_error("hkdf: not implemented");
}
}

std::unique_ptr<KeyExchange> makeKeyExchange(
const fizz::Factory& factory,
KEMId kemId) {
// TODO: KeyExchangeRole only matters for ML-KEM, which we do not currently
// support.
constexpr KeyExchangeRole role = KeyExchangeRole::Client;

std::unique_ptr<KeyExchange> makeKeyExchange(KEMId kemId) {
switch (kemId) {
case KEMId::secp256r1:
return factory.makeKeyExchange(NamedGroup::secp256r1, role);
return openssl::makeKeyExchange<fizz::P256>();
case KEMId::secp384r1:
return factory.makeKeyExchange(NamedGroup::secp384r1, role);
return openssl::makeKeyExchange<fizz::P384>();
case KEMId::secp521r1:
return factory.makeKeyExchange(NamedGroup::secp521r1, role);
return openssl::makeKeyExchange<fizz::P521>();
case KEMId::x25519:
return factory.makeKeyExchange(NamedGroup::x25519, role);
return std::make_unique<X25519KeyExchange>();
default:
throw std::runtime_error("can't make key exchange: not implemented");
}
Expand All @@ -200,8 +196,17 @@ size_t nenc(KEMId kemId) {
}
}

std::unique_ptr<Aead> makeCipher(const fizz::Factory& factory, AeadId aeadId) {
return factory.makeAead(getCipherSuite(aeadId));
std::unique_ptr<Aead> makeCipher(AeadId aeadId) {
switch (aeadId) {
case AeadId::TLS_CHACHA20_POLY1305_SHA256:
return openssl::OpenSSLEVPCipher::makeCipher<ChaCha20Poly1305>();
case AeadId::TLS_AES_128_GCM_SHA256:
return openssl::OpenSSLEVPCipher::makeCipher<AESGCM128>();
case AeadId::TLS_AES_256_GCM_SHA384:
return openssl::OpenSSLEVPCipher::makeCipher<AESGCM256>();
default:
throw std::runtime_error("can't make aead: not implemented");
}
}

} // namespace hpke
Expand Down
10 changes: 2 additions & 8 deletions fizz/crypto/hpke/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <fizz/crypto/aead/Aead.h>
#include <fizz/crypto/exchange/KeyExchange.h>
#include <fizz/crypto/hpke/Hkdf.h>
#include <fizz/protocol/Factory.h>
#include <fizz/protocol/Types.h>

#include <folly/Optional.h>
Expand Down Expand Up @@ -110,16 +109,13 @@ HashFunction getHashFunctionForKEM(KEMId kemId);
* The constructed `KeyExchange` object does *not* have a keypair associated
* with it yet.
*
* @param factory A fizz::Factory instance.
* @param kemId An HPKE KEM code point.
*
* @return An instance of a `fizz::KeyExchange` object without an associated
* keypair
* @throws std::runtime_error On invalid code points.
*/
std::unique_ptr<KeyExchange> makeKeyExchange(
const fizz::Factory& factory,
KEMId kemId);
std::unique_ptr<KeyExchange> makeKeyExchange(KEMId kemId);

/**
* fizz::hpke::nenc returns the size of the serialized public component (`enc`)
Expand Down Expand Up @@ -174,7 +170,6 @@ KDFId getKDFId(HashFunction hash);
* @throws std::runtime_error On invalid code points.
*/
std::unique_ptr<Hkdf> makeHpkeHkdf(
const fizz::Factory& factory,
std::unique_ptr<folly::IOBuf> prefix,
KDFId kdfId);

Expand Down Expand Up @@ -239,12 +234,11 @@ inline size_t getCipherOverhead(AeadId aeadId) {
*
* The cipher instance does *not* have the keys set.
*
* @param factory A fizz::Factory instance.
* @param aeadId An HPKE aead code point.
*
* @return An instance of a `fizz::Aead` without keying parameters.
* @throws std::runtime_error On invalid aead code point.
*/
std::unique_ptr<Aead> makeCipher(const fizz::Factory& factory, AeadId aeadId);
std::unique_ptr<Aead> makeCipher(AeadId aeadId);
} // namespace hpke
} // namespace fizz
1 change: 0 additions & 1 deletion fizz/crypto/hpke/test/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,5 @@ cpp_unittest(
"//fizz/crypto/hpke:hpke",
"//fizz/crypto/hpke:utils",
"//fizz/crypto/test:TestUtil",
"//fizz/protocol:default_factory",
],
)
3 changes: 1 addition & 2 deletions fizz/crypto/hpke/test/HpkeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <fizz/crypto/hpke/Utils.h>
#include <fizz/crypto/hpke/test/Mocks.h>
#include <fizz/crypto/test/TestUtil.h>
#include <fizz/protocol/DefaultFactory.h>
#include <gtest/gtest.h>
#include <list>

Expand Down Expand Up @@ -1957,7 +1956,7 @@ std::unique_ptr<KeyExchange> generateAuthKex(

std::unique_ptr<fizz::hpke::Hkdf> genHKDF(HashFunction f) {
return fizz::hpke::makeHpkeHkdf(
fizz::DefaultFactory(), folly::IOBuf::copyBuffer("HPKE-v1"), getKDFId(f));
folly::IOBuf::copyBuffer("HPKE-v1"), getKDFId(f));
}

SetupParam getSetupParam(
Expand Down
1 change: 0 additions & 1 deletion fizz/protocol/ech/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@ cpp_library(
exported_deps = [
":encrypted_client_hello",
":encryption",
"//fizz/protocol:factory",
],
)
11 changes: 3 additions & 8 deletions fizz/protocol/ech/Decrypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ folly::Optional<DecrypterLookupResult> decodeAndGetParam(
}

folly::Optional<DecrypterResult> tryToDecodeECH(
const fizz::Factory& factory,
const ClientHello& clientHelloOuter,
const Extension& encodedECHExtension,
const std::vector<DecrypterParams>& decrypterParams) {
Expand All @@ -64,7 +63,6 @@ folly::Optional<DecrypterResult> tryToDecodeECH(

try {
auto context = setupDecryptionContext(
factory,
configIdResult->matchingParam.echConfig,
configIdResult->echExtension.cipher_suite,
configIdResult->echExtension.enc,
Expand All @@ -91,7 +89,6 @@ folly::Optional<DecrypterResult> tryToDecodeECH(
}

ClientHello decodeClientHelloHRR(
const fizz::Factory& factory,
const ClientHello& chlo,
const std::unique_ptr<folly::IOBuf>& encapsulatedKey,
std::unique_ptr<hpke::HpkeContext>& context,
Expand Down Expand Up @@ -124,7 +121,6 @@ ClientHello decodeClientHelloHRR(
context);
} else {
auto recreatedContext = setupDecryptionContext(
factory,
configIdResult->matchingParam.echConfig,
configIdResult->echExtension.cipher_suite,
encapsulatedKey,
Expand Down Expand Up @@ -160,7 +156,7 @@ folly::Optional<DecrypterResult> ECHConfigManager::decryptClientHello(
auto it =
findExtension(chlo.extensions, ExtensionType::encrypted_client_hello);
if (it != chlo.extensions.end()) {
return tryToDecodeECH(*factory_, chlo, *it, configs_);
return tryToDecodeECH(chlo, *it, configs_);
}

return folly::none;
Expand All @@ -169,15 +165,14 @@ folly::Optional<DecrypterResult> ECHConfigManager::decryptClientHello(
ClientHello ECHConfigManager::decryptClientHelloHRR(
const ClientHello& chlo,
std::unique_ptr<hpke::HpkeContext>& context) {
return decodeClientHelloHRR(*factory_, chlo, nullptr, context, configs_);
return decodeClientHelloHRR(chlo, nullptr, context, configs_);
}

ClientHello ECHConfigManager::decryptClientHelloHRR(
const ClientHello& chlo,
const std::unique_ptr<folly::IOBuf>& encapsulatedKey) {
std::unique_ptr<hpke::HpkeContext> dummy;
return decodeClientHelloHRR(
*factory_, chlo, encapsulatedKey, dummy, configs_);
return decodeClientHelloHRR(chlo, encapsulatedKey, dummy, configs_);
}

std::vector<ech::ECHConfig> ECHConfigManager::getRetryConfigs() const {
Expand Down
4 changes: 0 additions & 4 deletions fizz/protocol/ech/Decrypter.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#pragma once

#include <fizz/protocol/Factory.h>
#include <fizz/protocol/ech/Encryption.h>
#include <fizz/protocol/ech/Types.h>

Expand Down Expand Up @@ -47,8 +46,6 @@ class Decrypter {

class ECHConfigManager : public Decrypter {
public:
explicit ECHConfigManager(std::shared_ptr<Factory> factory)
: factory_(std::move(factory)) {}
void addDecryptionConfig(DecrypterParams decrypterParams);
folly::Optional<DecrypterResult> decryptClientHello(
const ClientHello& chlo) override;
Expand All @@ -61,7 +58,6 @@ class ECHConfigManager : public Decrypter {
std::vector<ech::ECHConfig> getRetryConfigs() const override;

private:
std::shared_ptr<Factory> factory_;
std::vector<DecrypterParams> configs_;
};

Expand Down
20 changes: 6 additions & 14 deletions fizz/protocol/ech/Encryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include "fizz/record/Types.h"

#include <fizz/crypto/hpke/Utils.h>
#include <fizz/protocol/Factory.h>
#include <fizz/protocol/Protocol.h>
#include <fizz/protocol/ech/ECHExtensions.h>
#include <fizz/protocol/ech/Types.h>
Expand Down Expand Up @@ -188,7 +187,6 @@ folly::Optional<SupportedECHConfig> selectECHConfig(
}

static hpke::SetupParam getSetupParam(
const fizz::Factory& factory,
std::unique_ptr<DHKEM> dhkem,
std::unique_ptr<folly::IOBuf> prefix,
hpke::KEMId kemId,
Expand All @@ -199,19 +197,17 @@ static hpke::SetupParam getSetupParam(
auto suite = getCipherSuite(cipherSuite.aead_id);
auto suiteId = hpke::generateHpkeSuiteId(group, hash, suite);

auto hkdf =
hpke::makeHpkeHkdf(factory, std::move(prefix), cipherSuite.kdf_id);
auto hkdf = hpke::makeHpkeHkdf(std::move(prefix), cipherSuite.kdf_id);

return hpke::SetupParam{
std::move(dhkem),
hpke::makeCipher(factory, cipherSuite.aead_id),
makeCipher(cipherSuite.aead_id),
std::move(hkdf),
std::move(suiteId),
0};
}

hpke::SetupResult constructHpkeSetupResult(
const fizz::Factory& factory,
std::unique_ptr<KeyExchange> kex,
const SupportedECHConfig& supportedConfig) {
const std::unique_ptr<folly::IOBuf> prefix =
Expand All @@ -222,7 +218,7 @@ hpke::SetupResult constructHpkeSetupResult(
auto cipherSuite = supportedConfig.cipherSuite;

// Get shared secret
auto hkdf = hpke::makeHpkeHkdf(factory, prefix->clone(), cipherSuite.kdf_id);
auto hkdf = hpke::makeHpkeHkdf(prefix->clone(), cipherSuite.kdf_id);
std::unique_ptr<DHKEM> dhkem = std::make_unique<DHKEM>(
std::move(kex), getKexGroup(config.key_config.kem_id), std::move(hkdf));

Expand All @@ -236,7 +232,6 @@ hpke::SetupResult constructHpkeSetupResult(
std::move(info),
folly::none,
getSetupParam(
factory,
std::move(dhkem),
prefix->clone(),
config.key_config.kem_id,
Expand Down Expand Up @@ -657,7 +652,6 @@ ClientHello decryptECHWithContext(
}

std::unique_ptr<hpke::HpkeContext> setupDecryptionContext(
const fizz::Factory& factory,
const ECHConfig& echConfig,
HpkeSymmetricCipherSuite cipherSuite,
const std::unique_ptr<folly::IOBuf>& encapsulatedKey,
Expand All @@ -674,17 +668,15 @@ std::unique_ptr<hpke::HpkeContext> setupDecryptionContext(
NamedGroup group = hpke::getKexGroup(kemId);

auto dhkem = std::make_unique<DHKEM>(
std::move(kex),
group,
hpke::makeHpkeHkdf(factory, prefix->clone(), kdfId));
std::move(kex), group, hpke::makeHpkeHkdf(prefix->clone(), kdfId));
auto aeadId = cipherSuite.aead_id;
auto suiteId = hpke::generateHpkeSuiteId(
group, hpke::getHashFunction(kdfId), hpke::getCipherSuite(aeadId));

hpke::SetupParam setupParam{
std::move(dhkem),
makeCipher(factory, aeadId),
hpke::makeHpkeHkdf(factory, prefix->clone(), kdfId),
makeCipher(aeadId),
hpke::makeHpkeHkdf(prefix->clone(), kdfId),
std::move(suiteId),
seqNum};

Expand Down
Loading

0 comments on commit 21e1271

Please sign in to comment.