From 2526e14645abd742d29416c3a2d13d9be30df567 Mon Sep 17 00:00:00 2001 From: Franziskus Kiefer Date: Wed, 30 Nov 2022 16:33:19 +0100 Subject: [PATCH] add cmake usage example --- examples/cmake/.gitignore | 1 + examples/cmake/CMakeLists.txt | 25 +++++++++++++++++++ examples/cmake/Readme.md | 12 +++++++++ examples/cmake/blake-example.cc | 44 +++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+) create mode 100644 examples/cmake/.gitignore create mode 100644 examples/cmake/CMakeLists.txt create mode 100644 examples/cmake/Readme.md create mode 100644 examples/cmake/blake-example.cc 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; +}