Skip to content
Permalink
Browse files

Merge branch 'master' of github.com:jedisct1/libhydrogen

* 'master' of github.com:jedisct1/libhydrogen:
  Add a test
  hash: absorb the last full block
  Add wasm32-wasi CMake cross-compile toolchain
  cmake: Expand Arduino toolchain and tidy up flags
  Prefix Arduino toolchain options with HYDROGEN_*
  Set HYDRO_TARGET_DEVICE_XXX in main CMakeLists.txt
  Add config file to improve cmake_format
  Looks like cmake-format breaks cmake syntax
  cmake-format
  cmake: Add Arduino cross-compiling and package gen
  cmake: Reorganize a bit for maintainability
  Add a link to the latest Gimli specification
  • Loading branch information...
jedisct1 committed Jul 3, 2019
2 parents 86cc353 + 614c1dc commit e2918254f74866dafe1acea86bcdc886b2c3e6c4
Showing with 399 additions and 94 deletions.
  1. +47 −0 .cmake-format.json
  2. +0 −1 .gitignore
  3. +155 −85 CMakeLists.txt
  4. +1 −1 README.md
  5. +127 −0 cmake/arduino-avr-toolchain.cmake
  6. +1 −1 { → cmake}/hydrogen-config.cmake.in
  7. +58 −0 cmake/wasm32-wasi-toolchain.cmake
  8. +6 −6 impl/hash.h
  9. +4 −0 tests/tests.c
@@ -0,0 +1,47 @@
{
"additional_commands": {
"add_custom_command": {
"kwargs": {
"ARGS": "*",
"COMMAND": 1,
"OUTPUT": 1,
"WORKING_DIRECTORY": 1
}
},
"configure_package_config_file": {
"kwargs": {
"INSTALL_DESTINATION": 1
}
},
"include_directories": {
"kwargs": {
"SYSTEM": 0
}
},
"list": {
"kwargs": {
"APPEND": 1,
"OUTPUT_VARIABLE": 1,
"PREPEND": 1,
"TRANSFORM": 1
}
},
"set_target_properties": {
"kwargs": {
"EXCLUDE_FROM_ALL": 1,
"EXCLUDE_FROM_DEFAULT_BUILD": 1,
"PROPERTIES": 0
}
},
"string": {
"kwargs": {
"APPEND": 1,
"CONCAT": 1
}
}
},
"keyword_case": "upper",
"line_width": 99,
"max_subargs_per_line": 6
}

@@ -1,5 +1,4 @@
*.bc
*.cmake
*.dSYM
*.done
*.final
@@ -1,132 +1,202 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.12)

project(hydrogen
LANGUAGES C)
project(hydrogen LANGUAGES C)

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

if(NOT CMAKE_CROSSCOMPILING)
set(default_build_arch native)
endif()

set(BUILD_ARCH "${default_build_arch}" CACHE STRING
"Target system architecture (fed to the compiler's -march=...)")
string(TOUPPER "${PROJECT_NAME}" setting_prefix)
function(get_setting setting_name setting_type setting_description)
string(TOUPPER "${setting_prefix}_${setting_name}" setting_external_name)
set("${setting_external_name}" "" CACHE "${setting_type}" "${setting_description}")
set("${setting_name}" "${${setting_external_name}}" PARENT_SCOPE)
endfunction()

set(compile_options
$<$<OR:$<C_COMPILER_ID:AppleClang>,$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:GNU>>:
# Optimizations
-Os $<$<BOOL:${BUILD_ARCH}>:-march=${BUILD_ARCH}> -fno-exceptions
# Warnings
-Wall -Wextra -Wmissing-prototypes -Wdiv-by-zero -Wbad-function-cast -Wcast-align
-Wcast-qual -Wfloat-equal -Wmissing-declarations -Wnested-externs -Wno-unknown-pragmas
-Wpointer-arith -Wredundant-decls -Wstrict-prototypes -Wswitch-enum -Wno-type-limits>
$<$<C_COMPILER_ID:MSVC>:
# Optimizations
/Os /EHsc
# Warnings
/WX /W4
/wd4197 # suppress warning "top-level volatile in cast is ignored"
/wd4146 # suppress warning "unary minus operator applied to unsigned type, result still unsigned"
/wd4310 # suppress warning "cast truncates constant value"
>)
# Project files

set(source_files
"${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.c"
"${PROJECT_SOURCE_DIR}/impl/common.h"
"${PROJECT_SOURCE_DIR}/impl/core.h"
"${PROJECT_SOURCE_DIR}/impl/gimli-core.h"
"${PROJECT_SOURCE_DIR}/impl/gimli-core/portable.h"
"${PROJECT_SOURCE_DIR}/impl/gimli-core/sse2.h"
"${PROJECT_SOURCE_DIR}/impl/hash.h"
"${PROJECT_SOURCE_DIR}/impl/${PROJECT_NAME}_p.h"
"${PROJECT_SOURCE_DIR}/impl/kdf.h"
"${PROJECT_SOURCE_DIR}/impl/kx.h"
"${PROJECT_SOURCE_DIR}/impl/pwhash.h"
"${PROJECT_SOURCE_DIR}/impl/random.h"
"${PROJECT_SOURCE_DIR}/impl/secretbox.h"
"${PROJECT_SOURCE_DIR}/impl/sign.h"
"${PROJECT_SOURCE_DIR}/impl/x25519.h")
set(header_files
"${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.h")
set(test_files
"${PROJECT_SOURCE_DIR}/tests/tests.c")

set(config_file_name "${PROJECT_NAME}-config.cmake")
set(config_template_file "${PROJECT_SOURCE_DIR}/${config_file_name}.in")
set(config_file "${PROJECT_BINARY_DIR}/${config_file_name}")

set(targets_export_name "${PROJECT_NAME}-targets")
set(targets_export_file_name "${targets_export_name}.cmake")
set(targets_export_file "${PROJECT_BINARY_DIR}/${targets_export_file_name}")
"${PROJECT_NAME}.c"
"impl/common.h"
"impl/core.h"
"impl/gimli-core.h"
"impl/gimli-core/portable.h"
"impl/gimli-core/sse2.h"
"impl/hash.h"
"impl/${PROJECT_NAME}_p.h"
"impl/kdf.h"
"impl/kx.h"
"impl/pwhash.h"
"impl/random.h"
"impl/secretbox.h"
"impl/sign.h"
"impl/x25519.h")

set(header_files "${PROJECT_NAME}.h")

set(test_files "tests/tests.c")

set(arduino_files "library.properties")

# Compile options

get_setting(target_arch STRING "Target system architecture (fed to the compiler's -march=XXX).")
if(NOT target_arch AND NOT CMAKE_CROSSCOMPILING)
set(target_arch native)
endif()

set(install_config_dir "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}")
get_setting(target_device STRING "Target device identifier (defines HYDRO_TARGET_DEVICE_XXX).")

set(tests_executable "${PROJECT_NAME}-tests")
set(tests_done_target "${tests_executable}-done")
set(tests_done_file "${PROJECT_BINARY_DIR}/${tests_executable}.done")
set(compile_options
# --- GNU, Clang ---
$<$<OR:$<C_COMPILER_ID:AppleClang>,$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:GNU>>:
# ---- Definitions ----
$<$<BOOL:${target_device}>:-DHYDRO_TARGET_DEVICE_${target_device}>
# ---- Optimizations ----
-Os $<$<BOOL:${target_arch}>:-march=${target_arch}> -fno-exceptions
# ---- Warnings ----
-Wall
-Wextra
-Wmissing-prototypes
-Wdiv-by-zero
-Wbad-function-cast
-Wcast-align
-Wcast-qual
-Wfloat-equal
-Wmissing-declarations
-Wnested-externs
-Wno-unknown-pragmas
-Wpointer-arith
-Wredundant-decls
-Wstrict-prototypes
-Wswitch-enum
-Wno-type-limits
>
# --- MSVC ---
$<$<C_COMPILER_ID:MSVC>:
# ---- Definitions ----
$<$<BOOL:${target_device}>:/DHYDRO_TARGET_DEVICE_${target_device}>
# ---- Optimizations ----
/Os /EHsc
# ---- Warnings ----
/WX
/W4
/wd4197 # * suppress warning "top-level volatile in cast is ignored"
/wd4146 # * suppress warning "unary minus operator applied to unsigned type, result still
# unsigned"
/wd4310 # * suppress warning "cast truncates constant value"
>)

# Prefix project files with the project root

function(prefix_project_paths list_name)
list(TRANSFORM "${list_name}" PREPEND "${PROJECT_SOURCE_DIR}/" OUTPUT_VARIABLE prefixed_list)
set("project_${list_name}" ${prefixed_list} PARENT_SCOPE)
endfunction()

prefix_project_paths(source_files)
prefix_project_paths(header_files)
prefix_project_paths(test_files)
prefix_project_paths(arduino_files)

# Main library

add_library("${PROJECT_NAME}")
add_library("${PROJECT_NAME}::${PROJECT_NAME}" ALIAS "${PROJECT_NAME}")

target_sources("${PROJECT_NAME}" PRIVATE ${source_files})
target_sources("${PROJECT_NAME}" PRIVATE ${project_source_files})

target_include_directories("${PROJECT_NAME}" PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
target_include_directories("${PROJECT_NAME}"
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)

target_compile_options(${PROJECT_NAME} PRIVATE ${compile_options})
target_compile_options("${PROJECT_NAME}" PRIVATE ${compile_options})

# Installation

set(targets_export_name "${PROJECT_NAME}-targets")

install(TARGETS "${PROJECT_NAME}"
EXPORT "${targets_export_name}"
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}")

install(FILES ${header_files}
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")
install(FILES ${project_header_files} DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}")

# CMake find_package() support

set(install_config_dir "${CMAKE_INSTALL_DATADIR}/cmake/${PROJECT_NAME}")

set(targets_export_file_name "${targets_export_name}.cmake")
set(targets_export_file "${PROJECT_BINARY_DIR}/${targets_export_file_name}")

install(EXPORT "${targets_export_name}"
FILE "${targets_export_file_name}"
NAMESPACE "${PROJECT_NAME}::"
DESTINATION "${install_config_dir}")

CONFIGURE_PACKAGE_CONFIG_FILE("${config_template_file}" "${config_file}"
INSTALL_DESTINATION "${install_config_dir}")
set(config_file_name "${PROJECT_NAME}-config.cmake")
set(config_template_file "${PROJECT_SOURCE_DIR}/cmake/${config_file_name}.in")
set(config_file "${PROJECT_BINARY_DIR}/${config_file_name}")

configure_package_config_file("${config_template_file}" "${config_file}"
INSTALL_DESTINATION "${install_config_dir}")

install(FILES ${config_file}
DESTINATION ${install_config_dir})
install(FILES "${config_file}" DESTINATION "${install_config_dir}")

export(EXPORT "${targets_export_name}"
FILE "${targets_export_file}"
NAMESPACE "${PROJECT_NAME}::")
export(EXPORT "${targets_export_name}" FILE "${targets_export_file}" NAMESPACE "${PROJECT_NAME}::")

export(PACKAGE "${PROJECT_NAME}")

# Tests

set(tests_executable "${PROJECT_NAME}-tests")
set(tests_run_target "${PROJECT_NAME}-run-tests")
set(tests_run_file "${PROJECT_BINARY_DIR}/${tests_run_target}.done")

enable_testing()
add_executable("${tests_executable}" ${test_files})
add_executable("${tests_executable}" ${project_test_files})
target_compile_options("${tests_executable}" PRIVATE ${compile_options})
target_link_libraries("${tests_executable}" "${PROJECT_NAME}")
add_test(NAME "${tests_executable}" COMMAND "${tests_executable}")

# Auto-run tests on build (unless cross-compiling)

if(NOT CMAKE_CROSSCOMPILING)
add_custom_command(OUTPUT "${tests_done_file}"
DEPENDS "${tests_executable}"
COMMAND cmake
ARGS -E remove "${tests_done_file}"
COMMAND ctest
ARGS -C $<CONFIGURATION> --output-on-failure
COMMAND cmake
ARGS -E touch "${tests_done_file}"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}")
add_custom_target("${tests_done_target}" ALL DEPENDS "${tests_done_file}")
if(CMAKE_CROSSCOMPILING)
# Disable tests executable by default when cross-compiling (as it will fail to build when, e.g.,
# cross-compiling for Arduino/AVR).
set_target_properties("${tests_executable}"
PROPERTIES
EXCLUDE_FROM_ALL 1
EXCLUDE_FROM_DEFAULT_BUILD 1)
else()
# Otherwise, auto-run the tests on build.
add_custom_command(OUTPUT "${tests_run_file}"
DEPENDS "${tests_executable}"
COMMAND cmake
ARGS -E remove "${tests_run_file}"
COMMAND ctest
ARGS -C $<CONFIGURATION> --output-on-failure
COMMAND cmake
ARGS -E touch "${tests_run_file}"
WORKING_DIRECTORY "${PROJECT_BINARY_DIR}")
add_custom_target("${tests_run_target}" ALL DEPENDS "${tests_run_file}")
endif()

# Generate Arduino package

set(arduino_package_file "${PROJECT_BINARY_DIR}/hydrogen-crypto.zip")

# Use the relative versions of the file path lists or else the full paths will end up in the
# generated archive.
add_custom_command(OUTPUT "${arduino_package_file}"
COMMAND cmake
ARGS -E
tar
cf
"${arduino_package_file}"
--format=zip
--
${source_files}
${header_files}
${arduino_files}
WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}")

add_custom_target("${PROJECT_NAME}-arduino-package" DEPENDS "${arduino_package_file}")
@@ -8,7 +8,7 @@ The Hydrogen library is a small, easy-to-use, hard-to-misuse cryptographic libra

Features:
- Consistent high-level API, inspired by libsodium. Instead of low-level primitives, it exposes simple functions to solve common problems that cryptography can solve.
- 100% built using just two cryptographic building blocks: the [Curve25519](https://cr.yp.to/ecdh.html) elliptic curve, and the [Gimli](https://gimli.cr.yp.to/) permutation.
- 100% built using just two cryptographic building blocks: the [Curve25519](https://cr.yp.to/ecdh.html) elliptic curve, and the [Gimli](https://csrc.nist.gov/CSRC/media/Projects/Lightweight-Cryptography/documents/round-1/spec-doc/gimli-spec.pdf) permutation.
- Small and easy to audit. Implemented as one tiny file for every set of operation, and adding a single `.c` file to your project is all it takes to use libhydrogen in your project.
- The whole code is released under a single, very liberal license (ISC).
- Zero dynamic memory allocations and low stack requirements (median: 32 bytes, max: 128 bytes). This makes it usable in constrained environments such as microcontrollers.

0 comments on commit e291825

Please sign in to comment.
You can’t perform that action at this time.