Skip to content

Commit

Permalink
Allowing for dynamic top-level namespacing (#359)
Browse files Browse the repository at this point in the history
* Allowing dynamic top-level namespace
* changing namespace from mls to MLS_NAMESPACE
* changing namespace hpke with MLS_NAMESPACE::hpke
* changing bytes_ns with MLS_NAMESPACE::bytes_ns
* changing tls with MLS_NAMESPACE::tls

* Reduced to one cmake option

* adding underscore to CXX namespace
  • Loading branch information
Greg Hewett authored Aug 31, 2023
1 parent f27e8b7 commit 0e6ce41
Show file tree
Hide file tree
Showing 84 changed files with 796 additions and 608 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ jobs:
- 'cmd'
- 'lib'
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Run clang-format style check for C/C++ programs.
uses: jidicula/clang-format-action@v4.6.2
uses: jidicula/clang-format-action@v4.11.0
with:
clang-format-version: '14'
clang-format-version: '16'
check-path: ${{ matrix.path }}
fallback-style: 'Mozilla'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ out
*.swp
.vs/**
.vscode/**
include/namespace.h
62 changes: 47 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,30 @@ project(mlspp
option(TESTING "Build tests" OFF)
option(CLANG_TIDY "Perform linting with clang-tidy" OFF)
option(SANITIZERS "Enable sanitizers" OFF)
option(MLS_NAMESPACE_SUFFIX "Namespace Suffix for CXX and CMake Export")

if(MLS_NAMESPACE_SUFFIX)
set(MLS_CXX_NAMESPACE "mls_${MLS_NAMESPACE_SUFFIX}" CACHE STRING "Top-level Namespace for CXX")
set(MLS_EXPORT_NAMESPACE "MLSPP${MLS_NAMESPACE_SUFFIX}" CACHE STRING "Namespace for CMake Export")
else()
set(MLS_CXX_NAMESPACE "mls" CACHE STRING "Top-level Namespace for CXX")
set(MLS_EXPORT_NAMESPACE "MLSPP" CACHE STRING "Namespace for CMake Export")
endif()
message(STATUS "CXX Namespace: ${MLS_CXX_NAMESPACE}")
message(STATUS "CMake Export Namespace: ${MLS_EXPORT_NAMESPACE}")


###
### Global Config
###
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/namespace.h.in"
"${CMAKE_CURRENT_SOURCE_DIR}/include/namespace.h"
@ONLY
)

include(CheckCXXCompilerFlag)
include(CMakePackageConfigHelpers)
include(GNUInstallDirs)
Expand Down Expand Up @@ -127,23 +145,39 @@ endif()
### Exports
###
set(CMAKE_EXPORT_PACKAGE_REGISTRY ON)
export(EXPORT mlspp-targets NAMESPACE MLSPP:: FILE mlspp-targets.cmake)
export(PACKAGE MLSPP)
export(
EXPORT
mlspp-targets
NAMESPACE
${MLS_EXPORT_NAMESPACE}::
FILE
${MLS_EXPORT_NAMESPACE}Targets.cmake)
export(PACKAGE ${MLS_EXPORT_NAMESPACE})

configure_package_config_file(cmake/config.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/mlspp-config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/mlspp
${CMAKE_CURRENT_BINARY_DIR}/${MLS_EXPORT_NAMESPACE}Config.cmake
INSTALL_DESTINATION ${CMAKE_INSTALL_DATADIR}/${MLS_EXPORT_NAMESPACE}
NO_SET_AND_CHECK_MACRO)

write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/mlspp-config-version.cmake
${CMAKE_CURRENT_BINARY_DIR}/${MLS_EXPORT_NAMESPACE}ConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion)

###
### Install
###

install(
EXPORT
mlspp-targets
NAMESPACE
${MLS_EXPORT_NAMESPACE}::
FILE
${MLS_EXPORT_NAMESPACE}Targets.cmake
DESTINATION
${CMAKE_INSTALL_DATADIR}/${MLS_EXPORT_NAMESPACE})

install(
DIRECTORY
include/
Expand All @@ -152,18 +186,16 @@ install(

install(
FILES
${CMAKE_CURRENT_BINARY_DIR}/mlspp-config.cmake
${CMAKE_CURRENT_BINARY_DIR}/mlspp-config-version.cmake
${CMAKE_CURRENT_BINARY_DIR}/${MLS_EXPORT_NAMESPACE}Config.cmake
${CMAKE_CURRENT_BINARY_DIR}/${MLS_EXPORT_NAMESPACE}ConfigVersion.cmake
DESTINATION
${CMAKE_INSTALL_DATADIR}/mlspp)
${CMAKE_INSTALL_DATADIR}/${MLS_EXPORT_NAMESPACE})

install(
EXPORT
mlspp-targets
NAMESPACE
MLSPP::
FILE
mlspp-targets.cmake
FILES
${CMAKE_CURRENT_SOURCE_DIR}/LICENSE
DESTINATION
${CMAKE_INSTALL_DATADIR}/mlspp)
${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME}
RENAME
copyright)

1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,5 @@ format:
find include -iname "*.h" -or -iname "*.cpp" | xargs ${CLANG_FORMAT}
find src -iname "*.h" -or -iname "*.cpp" | xargs ${CLANG_FORMAT}
find test -iname "*.h" -or -iname "*.cpp" | xargs ${CLANG_FORMAT}
find cmd -iname "*.h" -or -iname "*.cpp" | xargs ${CLANG_FORMAT}
find lib -iname "*.h" -or -iname "*.cpp" | grep -v "test_vectors.cpp" | xargs ${CLANG_FORMAT}
2 changes: 1 addition & 1 deletion cmake/config.cmake.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@PACKAGE_INIT@

include(${CMAKE_CURRENT_LIST_DIR}/mlspp-targets.cmake)
include(${CMAKE_CURRENT_LIST_DIR}/@MLS_EXPORT_NAMESPACE@Targets.cmake)
check_required_components(mlspp)
4 changes: 4 additions & 0 deletions cmake/namespace.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

// Configurable top-level MLS namespace
#define MLS_NAMESPACE @MLS_CXX_NAMESPACE@
69 changes: 35 additions & 34 deletions cmd/interop/src/json_details.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ struct adl_serializer<std::optional<T>>
};

// LeafCount, NodeCount, etc.
// XXX(RLB): For some reason, just defining this for mls::Uint32 didn't work.
// XXX(RLB): For some reason, just defining this for MLS_NAMESPACE::Uint32
// didn't work.
template<typename T>
struct uint_serializer
{
Expand All @@ -54,26 +55,25 @@ struct uint_serializer
#define UINT_SERIALIZER(T) \
template<> \
struct adl_serializer<T> : uint_serializer<T> \
{ \
};
{};

UINT_SERIALIZER(mls::LeafCount)
UINT_SERIALIZER(mls::NodeCount)
UINT_SERIALIZER(mls::LeafIndex)
UINT_SERIALIZER(mls::NodeIndex)
UINT_SERIALIZER(MLS_NAMESPACE::LeafCount)
UINT_SERIALIZER(MLS_NAMESPACE::NodeCount)
UINT_SERIALIZER(MLS_NAMESPACE::LeafIndex)
UINT_SERIALIZER(MLS_NAMESPACE::NodeIndex)

// mls::Ciphersuite
// MLS_NAMESPACE::Ciphersuite
template<>
struct adl_serializer<mls::CipherSuite>
struct adl_serializer<MLS_NAMESPACE::CipherSuite>
{
static void to_json(json& j, const mls::CipherSuite& v)
static void to_json(json& j, const MLS_NAMESPACE::CipherSuite& v)
{
j = v.cipher_suite();
}

static void from_json(const json& j, mls::CipherSuite& v)
static void from_json(const json& j, MLS_NAMESPACE::CipherSuite& v)
{
v = mls::CipherSuite(j.get<mls::CipherSuite::ID>());
v = MLS_NAMESPACE::CipherSuite(j.get<MLS_NAMESPACE::CipherSuite::ID>());
}
};

Expand All @@ -94,43 +94,44 @@ struct asymmetric_key_serializer
#define ASYMM_KEY_SERIALIZER(T) \
template<> \
struct adl_serializer<T> : asymmetric_key_serializer<T> \
{ \
};
{};

ASYMM_KEY_SERIALIZER(mls::HPKEPublicKey)
ASYMM_KEY_SERIALIZER(mls::HPKEPrivateKey)
ASYMM_KEY_SERIALIZER(mls::SignaturePublicKey)
ASYMM_KEY_SERIALIZER(mls::SignaturePrivateKey)
ASYMM_KEY_SERIALIZER(MLS_NAMESPACE::HPKEPublicKey)
ASYMM_KEY_SERIALIZER(MLS_NAMESPACE::HPKEPrivateKey)
ASYMM_KEY_SERIALIZER(MLS_NAMESPACE::SignaturePublicKey)
ASYMM_KEY_SERIALIZER(MLS_NAMESPACE::SignaturePrivateKey)

// Other TLS-serializable things
template<typename T>
struct tls_serializer
{
static void to_json(json& j, const T& v) { j = bytes(tls::marshal(v)); }
static void to_json(json& j, const T& v)
{
j = bytes(MLS_NAMESPACE::tls::marshal(v));
}

static void from_json(const json& j, T& v)
{
v = tls::get<T>(j.get<bytes>());
v = MLS_NAMESPACE::tls::get<T>(j.get<bytes>());
}
};

#define TLS_SERIALIZER(T) \
template<> \
struct adl_serializer<T> : tls_serializer<T> \
{ \
};

TLS_SERIALIZER(mls::TreeKEMPublicKey)
TLS_SERIALIZER(mls::AuthenticatedContent)
TLS_SERIALIZER(mls::Credential)
TLS_SERIALIZER(mls::Proposal)
TLS_SERIALIZER(mls::Commit)
TLS_SERIALIZER(mls::ApplicationData)
TLS_SERIALIZER(mls::MLSMessage)
TLS_SERIALIZER(mls::LeafNode)
TLS_SERIALIZER(mls::UpdatePath)
TLS_SERIALIZER(mls::KeyPackage)
TLS_SERIALIZER(mls::Welcome)
{};

TLS_SERIALIZER(MLS_NAMESPACE::TreeKEMPublicKey)
TLS_SERIALIZER(MLS_NAMESPACE::AuthenticatedContent)
TLS_SERIALIZER(MLS_NAMESPACE::Credential)
TLS_SERIALIZER(MLS_NAMESPACE::Proposal)
TLS_SERIALIZER(MLS_NAMESPACE::Commit)
TLS_SERIALIZER(MLS_NAMESPACE::ApplicationData)
TLS_SERIALIZER(MLS_NAMESPACE::MLSMessage)
TLS_SERIALIZER(MLS_NAMESPACE::LeafNode)
TLS_SERIALIZER(MLS_NAMESPACE::UpdatePath)
TLS_SERIALIZER(MLS_NAMESPACE::KeyPackage)
TLS_SERIALIZER(MLS_NAMESPACE::Welcome)

} // namespace nlohmann

Expand Down
21 changes: 11 additions & 10 deletions cmd/interop/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::crypto_basics: {
auto cases = std::vector<CryptoBasicsTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite);
}

Expand All @@ -59,7 +59,7 @@ make_test_vector(uint64_t type)
auto cases = std::vector<SecretTreeTestVector>();
auto generations = std::vector<uint32_t>{ 1, 15 };

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite, 15, generations);
}

Expand All @@ -69,7 +69,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::message_protection: {
auto cases = std::vector<MessageProtectionTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite);
}

Expand All @@ -79,7 +79,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::key_schedule: {
auto cases = std::vector<KeyScheduleTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite, n);
}

Expand All @@ -89,7 +89,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::pre_shared_keys: {
auto cases = std::vector<PSKSecretTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite, 5);
}

Expand All @@ -99,7 +99,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::tree_validation: {
auto cases = std::vector<TreeHashTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
for (const auto& tree_structure : all_tree_structures) {
cases.emplace_back(suite, tree_structure);
}
Expand All @@ -111,7 +111,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::transcript_hash: {
auto cases = std::vector<TranscriptTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite);
}

Expand All @@ -121,7 +121,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::welcome: {
auto cases = std::vector<WelcomeTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
cases.emplace_back(suite);
}

Expand All @@ -131,7 +131,8 @@ make_test_vector(uint64_t type)
case TestVectorClass::tree_modifications: {
auto cases = std::vector<TreeOperationsTestVector>();

auto suite = mls::CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519;
auto suite =
MLS_NAMESPACE::CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519;
for (auto scenario : TreeOperationsTestVector::all_scenarios) {
cases.emplace_back(suite, scenario);
}
Expand All @@ -142,7 +143,7 @@ make_test_vector(uint64_t type)
case TestVectorClass::treekem: {
auto cases = std::vector<TreeKEMTestVector>();

for (const auto& suite : mls::all_supported_suites) {
for (const auto& suite : MLS_NAMESPACE::all_supported_suites) {
for (const auto& tree_structure : treekem_test_tree_structures) {
cases.emplace_back(suite, tree_structure);
}
Expand Down
Loading

0 comments on commit 0e6ce41

Please sign in to comment.