diff --git a/.gitmodules b/.gitmodules index aebed30a2b..97da9da64c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -66,3 +66,11 @@ [submodule "Tools/simulation-ignition"] path = Tools/simulation-ignition url = https://github.com/Auterion/px4-simulation-ignition.git +[submodule "src/lib/crypto/libtomcrypt"] + path = src/lib/crypto/libtomcrypt + url = https://github.com/PX4/libtomcrypt.git + branch = px4 +[submodule "src/lib/crypto/libtommath"] + path = src/lib/crypto/libtommath + url = https://github.com/PX4/libtommath.git + branch = px4 diff --git a/Tools/astyle/files_to_check_code_style.sh b/Tools/astyle/files_to_check_code_style.sh index 3ef1e01e29..9098b500e9 100755 --- a/Tools/astyle/files_to_check_code_style.sh +++ b/Tools/astyle/files_to_check_code_style.sh @@ -24,4 +24,7 @@ exec find boards msg src platforms test \ -path src/modules/micrortps_bridge/micro-CDR -prune -o \ -path src/modules/micrortps_bridge/microRTPS_client -prune -o \ -path test/mavsdk_tests/catch2 -prune -o \ + -path src/lib/crypto/monocypher -prune -o \ + -path src/lib/crypto/libtomcrypt -prune -o \ + -path src/lib/crypto/libtommath -prune -o \ -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" \) | grep $PATTERN diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index 39097256bb..6444b1e29b 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -32,7 +32,6 @@ ############################################################################ px4_add_git_submodule(TARGET git_matrix PATH "matrix") -px4_add_git_submodule(TARGET git_monocypher PATH "crypto/monocypher") add_subdirectory(airspeed) add_subdirectory(avoidance) diff --git a/src/lib/crypto/CMakeLists.txt b/src/lib/crypto/CMakeLists.txt index 05b6b413b9..63f55e8a3d 100644 --- a/src/lib/crypto/CMakeLists.txt +++ b/src/lib/crypto/CMakeLists.txt @@ -31,6 +31,12 @@ # ############################################################################ +if (DEFINED PX4_CRYPTO) + +px4_add_git_submodule(TARGET git_monocypher PATH "monocypher") +px4_add_git_submodule(TARGET git_libtomcrypt PATH "libtomcrypt") +px4_add_git_submodule(TARGET git_libtommath PATH "libtommath") + px4_add_library(monocypher monocypher/src/monocypher.c monocypher/src/optional/monocypher-ed25519.c @@ -43,3 +49,54 @@ target_include_directories(monocypher # There is a one shadow warning in monocypher 3.1.2, ignore it target_compile_options(monocypher PRIVATE -Wno-shadow) + + + +file(GLOB TOMMATH_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "libtommath/*.c") +px4_add_library(libtommath + ${TOMMATH_SRC} +) + + +file(GLOB_RECURSE PK_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "libtomcrypt/src/pk/*.c") +file(GLOB_RECURSE MATH_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "libtomcrypt/src/math/*.c") + +px4_add_library(libtomcrypt +libtomcrypt_wrappers.c + ${PK_SRC} + ${MATH_SRC} + libtomcrypt/src/hashes/sha2/sha256.c + libtomcrypt/src/hashes/helper/hash_memory.c + libtomcrypt/src/prngs/sprng.c + libtomcrypt/src/misc/crypt/crypt_ltc_mp_descriptor.c + libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c + libtomcrypt/src/misc/crypt/crypt_prng_is_valid.c + libtomcrypt/src/misc/zeromem.c +) + +target_include_directories(libtomcrypt + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/libtommath + ) + +target_include_directories(libtomcrypt + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/libtomcrypt/src/headers +) + +# libtomcrypt defines: +# ARGTYPE=3: No argument checking +add_definitions(-DARGTYPE=3 -DLTC_EASY -DLTC_NO_TEST -DLTM_DESC -DMP_LOW_MEM) + +# link to libtommath and os provided random library +target_link_libraries(libtomcrypt + PRIVATE + libtommath + px4_random +) + +# Fix for erroneous warning on some compilers: +# "der_encode_asn1_identifier.c:39:18: error: comparison is always false due to limited range of data type" +target_compile_options(libtomcrypt PRIVATE -Wno-type-limits) + +endif() diff --git a/src/lib/crypto/libtomcrypt b/src/lib/crypto/libtomcrypt new file mode 160000 index 0000000000..673f5ce290 --- /dev/null +++ b/src/lib/crypto/libtomcrypt @@ -0,0 +1 @@ +Subproject commit 673f5ce29015a9bba3c96792920a10601b5b0718 diff --git a/src/lib/crypto/libtomcrypt_wrappers.c b/src/lib/crypto/libtomcrypt_wrappers.c new file mode 100644 index 0000000000..17d7027a49 --- /dev/null +++ b/src/lib/crypto/libtomcrypt_wrappers.c @@ -0,0 +1,47 @@ +#include +#include + +struct ltc_hash_descriptor hash_descriptor[] = { + { + "sha256", + 0, + 32, + 64, + + /* OID */ + { 2, 16, 840, 1, 101, 3, 4, 2, 1, }, + 9, + + &sha256_init, + &sha256_process, + &sha256_done, + &sha256_test, + NULL + } +}; + +struct ltc_prng_descriptor prng_descriptor[] = { + { + "sprng", 0, + &sprng_start, + &sprng_add_entropy, + &sprng_ready, + &sprng_read, + &sprng_done, + &sprng_export, + &sprng_import, + &sprng_test + } +}; + +unsigned long rng_get_bytes(unsigned char *out, + unsigned long outlen, + void (*callback)(void)) +{ + return px4_get_secure_random((uint8_t *)out, (size_t)outlen); +} + +void libtomcrypt_init(void) +{ + ltc_mp = ltm_desc; +} diff --git a/src/lib/crypto/libtommath b/src/lib/crypto/libtommath new file mode 160000 index 0000000000..fd73d7630b --- /dev/null +++ b/src/lib/crypto/libtommath @@ -0,0 +1 @@ +Subproject commit fd73d7630b9d3ed5a79d613ff680a549e9780de7