diff --git a/CMakeLists.txt b/CMakeLists.txt index dc217e83..0887b0fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,7 +51,14 @@ set(hacl_VERSION_TWEAK "") # This file must be generated before running cmake with ./mach.py --configure # If the build is invoked through ./mach.py, a separate configuration is not # needed. -include(build/config.cmake) +# If the file is not present, i.e. cmake was invoked directly, we copy the default +# config from config/default_config.cmake +if(NOT EXISTS ${PROJECT_SOURCE_DIR}/build/config.cmake) + configure_file(${PROJECT_SOURCE_DIR}/config/default_config.cmake ${PROJECT_SOURCE_DIR}/build/config.cmake COPYONLY) +endif() + +# Now include the config. +include(${PROJECT_SOURCE_DIR}/build/config.cmake) # Constants used throughout hacl and the build. include(config/constants.cmake) diff --git a/examples/cmake/.gitignore b/examples/cmake/.gitignore new file mode 100644 index 00000000..796b96d1 --- /dev/null +++ b/examples/cmake/.gitignore @@ -0,0 +1 @@ +/build diff --git a/examples/cmake/CMakeLists.txt b/examples/cmake/CMakeLists.txt new file mode 100644 index 00000000..b0c821a0 --- /dev/null +++ b/examples/cmake/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.10) + +include(FetchContent) +FetchContent_Declare(hacl + DOWNLOAD_EXTRACT_TIMESTAMP TRUE + GIT_REPOSITORY https://github.com/cryspen/hacl-packages/ + GIT_TAG 88560a8b5bae5db890e11f1ea71376adfa767baf +) +FetchContent_MakeAvailable(hacl) + +project(hacl-blake-example) +set(CMAKE_OSX_ARCHITECTURES arm64) + +add_executable(example blake-example.cc) + +# Add includes from HACL +target_include_directories(example PRIVATE + ${hacl_SOURCE_DIR}/include + ${hacl_SOURCE_DIR}/build + ${hacl_SOURCE_DIR}/karamel/include + ${hacl_SOURCE_DIR}/karamel/krmllib/dist/minimal + ${hacl_SOURCE_DIR}/vale/include +) +# Link the HACL library +target_link_libraries(example PRIVATE hacl) diff --git a/examples/cmake/Readme.md b/examples/cmake/Readme.md new file mode 100644 index 00000000..9ceb7a23 --- /dev/null +++ b/examples/cmake/Readme.md @@ -0,0 +1,12 @@ +# CMake Example + +This folder contains an example for how to use the HACL Packages C library in a +CMake project. + +```sh +mkdir build +cd build +cmake .. +cmake --build . +./example +``` diff --git a/examples/cmake/blake-example.cc b/examples/cmake/blake-example.cc new file mode 100644 index 00000000..3c1d17ab --- /dev/null +++ b/examples/cmake/blake-example.cc @@ -0,0 +1,44 @@ +/* + * Copyright 2022 Cryspen Sarl + * + * Licensed under the Apache License, Version 2.0 or MIT. + * - http://www.apache.org/licenses/LICENSE-2.0 + * - http://opensource.org/licenses/MIT + */ +#include "Hacl_Hash_Blake2.h" + +using namespace std; + +void print_hex_ln(size_t bytes_len, uint8_t *bytes) +{ + for (int i = 0; i < bytes_len; ++i) + { + printf("%02x", bytes[i]); + } + + printf("\n"); +} + +int main(int argc, char const *argv[]) +{ + // Reserve memory for a 64 byte digest, i.e., + // for a BLAKE2B run with full 512-bit output. + uint32_t output_len = 64; + uint8_t output[64]; + + // The message we want to hash. + const char *message = "Hello, HACL Packages!"; + uint32_t message_len = strlen(message); + + // BLAKE2B can be used as an HMAC, i.e., with a key. + // We don't want to use a key here and thus provide a zero-sized key. + uint32_t key_len = 0; + uint8_t *key = 0; + + Hacl_Blake2b_32_blake2b( + output_len, output, message_len, (uint8_t *)message, key_len, key); + + print_hex_ln(output_len, output); + + return 0; +}