From 3a3b188affa752df7703a63b742dc5fb760501b2 Mon Sep 17 00:00:00 2001 From: Bohdan Date: Mon, 11 Mar 2019 12:25:07 +0200 Subject: [PATCH] Add fuzzing targets (#23) * Add fuzzing targets Signed-off-by: Bogdan Vaneev * Add init to fuzz sign Signed-off-by: Bogdan Vaneev --- CMakeLists.txt | 7 ++++++ cmake/ed25519_add_fuzz.cmake | 8 +++++++ fuzzing/CMakeLists.txt | 43 ++++++++++++++++++++++++++++++++++++ fuzzing/fuzz_sign.cc | 22 ++++++++++++++++++ test/ed25519/CMakeLists.txt | 16 +++++++++----- 5 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 cmake/ed25519_add_fuzz.cmake create mode 100644 fuzzing/CMakeLists.txt create mode 100644 fuzzing/fuzz_sign.cc diff --git a/CMakeLists.txt b/CMakeLists.txt index 723ac27..ab2f207 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ set(CMAKE_CXX_EXTENSIONS OFF) option(TESTING "Enable testing" OFF) option(COVERAGE "Enable coverage" OFF) option(BENCHMARKING "Enable benchmarking" OFF) +option(FUZZING "Enable fuzzing targets" OFF) include_directories( ${CMAKE_BINARY_DIR}/ @@ -54,6 +55,7 @@ include(cmake/ed25519_target_link_libraries.cmake) include(cmake/ed25519_add_library.cmake) include(cmake/ed25519_merge_libraries.cmake) include(cmake/ed25519_add_test.cmake) +include(cmake/ed25519_add_fuzz.cmake) include(cmake/ed25519_select_supported_impl.cmake) test_build_amd64(CAN_BUILD_AMD64) @@ -159,3 +161,8 @@ endif () if (BENCHMARK) add_subdirectory(benchmark) endif () + + +if (FUZZING) + add_subdirectory(fuzzing) +endif () diff --git a/cmake/ed25519_add_fuzz.cmake b/cmake/ed25519_add_fuzz.cmake new file mode 100644 index 0000000..0835aa7 --- /dev/null +++ b/cmake/ed25519_add_fuzz.cmake @@ -0,0 +1,8 @@ +function(ed25519_add_fuzz fuzz_name) + add_executable(${fuzz_name} ${ARGN}) + set_target_properties(${fuzz_name} PROPERTIES + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/fuzz_bin + ARCHIVE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/fuzz_lib + LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/fuzz_lib + ) +endfunction() diff --git a/fuzzing/CMakeLists.txt b/fuzzing/CMakeLists.txt new file mode 100644 index 0000000..ded66f7 --- /dev/null +++ b/fuzzing/CMakeLists.txt @@ -0,0 +1,43 @@ +# Copyright Soramitsu Co., Ltd. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 + +set(compiler "clang-7") +set(flags "-g -O1") +set(options "-fsanitize=fuzzer,undefined") + +set(CMAKE_C_COMPILER ${compiler}) +set(CMAKE_CXX_COMPILER ${compiler}) +set(CMAKE_C_FLAGS ${flags}) +set(CMAKE_CXX_FLAGS ${flags}) + +add_compile_options(${options}) +add_link_options(${options}) + + +set(_random ${RANDOM}) +foreach(_edimpl ${ED25519_SUPPORTED_LIBRARIES_EDIMPL}) + foreach(_hash ${ED25519_SUPPORTED_LIBRARIES_HASH}) + foreach(_build STATIC SHARED) + + set(_target ${_edimpl}-${_hash}-${_random}-${_build}) + + if(NOT TARGET ${edtarget}) + ed25519_merge_libraries(fuzz-ed25519-${_target} ${_build} + LIBRARIES + ${_edimpl} + ${_hash} + ${_random} + ) + endif() + + + ed25519_add_fuzz(fuzz-sign-${_target} + fuzz_sign.cc + ) + ed25519_target_link_libraries(fuzz-sign-${_target} + fuzz-ed25519-${_target} + ) + + endforeach() + endforeach() +endforeach() diff --git a/fuzzing/fuzz_sign.cc b/fuzzing/fuzz_sign.cc new file mode 100644 index 0000000..5fe32b9 --- /dev/null +++ b/fuzzing/fuzz_sign.cc @@ -0,0 +1,22 @@ +/** + * Copyright Soramitsu Co., Ltd. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include + +static public_key_t pub; +static private_key_t priv; +static signature_t sig; + +extern "C" int LLVMFuzzerInitialize(int argc, char** argv){ + ed25519_create_keypair(&priv, &pub); + return 0; +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { + ed25519_sign(&sig, Data, Size, &pub, &priv); + return 0; // Non-zero return values are reserved for future use. +} diff --git a/test/ed25519/CMakeLists.txt b/test/ed25519/CMakeLists.txt index d908ba3..5840319 100644 --- a/test/ed25519/CMakeLists.txt +++ b/test/ed25519/CMakeLists.txt @@ -4,12 +4,16 @@ foreach(_edimpl ${ED25519_SUPPORTED_LIBRARIES_EDIMPL}) foreach(_build STATIC SHARED) set(_target ${_edimpl}-${_hash}-${_random}-${_build}) - ed25519_merge_libraries(ed25519-${_target} ${_build} - LIBRARIES - ${_edimpl} - ${_hash} - ${_random} - ) + + if(NOT TARGET ed25519-${_target}) + ed25519_merge_libraries(ed25519-${_target} ${_build} + LIBRARIES + ${_edimpl} + ${_hash} + ${_random} + ) + endif() + ed25519_add_test(test-${_target} ed25519_test.cpp )