Skip to content

Commit

Permalink
Add fuzzing targets (#23)
Browse files Browse the repository at this point in the history
* Add fuzzing targets

Signed-off-by: Bogdan Vaneev <[email protected]>

* Add init to fuzz sign

Signed-off-by: Bogdan Vaneev <[email protected]>
  • Loading branch information
Warchant authored Mar 11, 2019
1 parent cba1c6e commit 3a3b188
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}/
Expand All @@ -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)

Expand Down Expand Up @@ -159,3 +161,8 @@ endif ()
if (BENCHMARK)
add_subdirectory(benchmark)
endif ()


if (FUZZING)
add_subdirectory(fuzzing)
endif ()
8 changes: 8 additions & 0 deletions cmake/ed25519_add_fuzz.cmake
Original file line number Diff line number Diff line change
@@ -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()
43 changes: 43 additions & 0 deletions fuzzing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions fuzzing/fuzz_sign.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

#include <stdlib.h>
#include <stdint.h>
#include <ed25519/ed25519.h>

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.
}
16 changes: 10 additions & 6 deletions test/ed25519/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 3a3b188

Please sign in to comment.