Skip to content

Use Mbedtls files directly #226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,6 @@ add_subdirectory(picoboot_connection)
add_subdirectory(elf)
add_subdirectory(elf2uf2)

# To configure mbedtls
# todo make the configuration better
set(MBEDTLS_CONFIG_FILE "mbedtls_config.h")
add_compile_options(-I${CMAKE_SOURCE_DIR}/lib/include)

add_subdirectory(lib)

add_subdirectory(bintool)
Expand Down
2 changes: 1 addition & 1 deletion lib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cc_library(
name = "mbedtls_config",
hdrs = ["include/mbedtls_config.h"],
hdrs = ["include/mbedtls_config.h", "include/picotool_mbedtls_config.h"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to list both of these files, when mbedtls_config.h is merely a wrapper around picotool_mbedtls_config.h ? Or is this a required backwards-compatibility-thing in order not to break any existing Bazel projects? (I'm afraid I don't know much about Bazel!)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If both are going to be referenced, both need to be added. This is because Bazel sandboxes things, so if a file is not listed it won't be available during compilation.

With that said, this has made me realize that the mbedtls configuration handling is wrong in Bazel, so don't worry too much about it. (you're welcome to file an issue and assign it to me)

includes = ["include"],
visibility = ["@mbedtls//:__subpackages__"],
)
132 changes: 128 additions & 4 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,132 @@ add_subdirectory(nlohmann_json EXCLUDE_FROM_ALL)

add_subdirectory(whereami EXCLUDE_FROM_ALL)

if(EXISTS "${PICO_SDK_PATH}/lib/mbedtls/CMakeLists.txt")
option(ENABLE_PROGRAMS "Build Mbed TLS programs." OFF)
option(ENABLE_TESTING "Build Mbed TLS tests." OFF)
add_subdirectory(${PICO_SDK_PATH}/lib/mbedtls mbedtls EXCLUDE_FROM_ALL)
# Taken from pico-sdk/src/rp2_common/pico_mbedtls/CMakeLists.txt
if (DEFINED ENV{PICO_MBEDTLS_PATH} AND (NOT PICO_MBEDTLS_PATH))
set(PICO_MBEDTLS_PATH $ENV{PICO_MBEDTLS_PATH})
message("Using PICO_MBEDTLS_PATH from environment ('${PICO_MBEDTLS_PATH}')")
endif()

set(MBEDTLS_TEST_PATH "library/aes.c")
if (NOT PICO_MBEDTLS_PATH)
set(PICO_MBEDTLS_PATH ${PICO_SDK_PATH}/lib/mbedtls)
elseif (NOT EXISTS "${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH}")
message(WARNING "PICO_MBEDTLS_PATH specified but content not present.")
endif()

if(EXISTS "${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH}")
# Support version 2.28.8 or 3.6.2
if (EXISTS ${PICO_MBEDTLS_PATH}/library/ssl_cli.c)
set(MBEDTLS_VERSION_MAJOR 2)
elseif (EXISTS ${PICO_MBEDTLS_PATH}/library/ssl_client.c)
set(MBEDTLS_VERSION_MAJOR 3)
else()
message(WARNING "Cannot determine the version of mbedtls")
endif()

function(src_crypto_list)
set(src_crypto
aes.c
aesni.c
aria.c
asn1parse.c
asn1write.c
base64.c
bignum.c
camellia.c
ccm.c
chacha20.c
chachapoly.c
cipher.c
cipher_wrap.c
constant_time.c
cmac.c
ctr_drbg.c
des.c
dhm.c
ecdh.c
ecdsa.c
ecjpake.c
ecp.c
ecp_curves.c
entropy.c
entropy_poll.c
error.c
gcm.c
hkdf.c
hmac_drbg.c
md.c
md5.c
memory_buffer_alloc.c
mps_reader.c
mps_trace.c
nist_kw.c
oid.c
padlock.c
pem.c
pk.c
pk_wrap.c
pkcs12.c
pkcs5.c
pkparse.c
pkwrite.c
platform.c
platform_util.c
poly1305.c
psa_crypto.c
psa_crypto_aead.c
psa_crypto_cipher.c
psa_crypto_client.c
psa_crypto_ecp.c
psa_crypto_hash.c
psa_crypto_mac.c
psa_crypto_rsa.c
psa_crypto_se.c
psa_crypto_slot_management.c
psa_crypto_storage.c
psa_its_file.c
ripemd160.c
rsa.c
sha1.c
sha256.c
sha512.c
threading.c
timing.c
version.c
version_features.c
)
if (MBEDTLS_VERSION_MAJOR EQUAL 2)
list(APPEND src_crypto
arc4.c
blowfish.c
havege.c
md2.c
md4.c
psa_crypto_driver_wrappers.c
rsa_internal.c xtea.c
)
elseif (MBEDTLS_VERSION_MAJOR EQUAL 3)
list(APPEND src_crypto
bignum_core.c
rsa_alt_helpers.c
pk_ecc.c
)
endif()
list(TRANSFORM src_crypto PREPEND ${PICO_MBEDTLS_PATH}/library/)
set(src_crypto ${src_crypto} PARENT_SCOPE)
endfunction()

src_crypto_list()


# Create library
add_library(mbedtls STATIC ${src_crypto})

if(WIN32)
target_link_libraries(mbedtls ws2_32 bcrypt)
endif(WIN32)

target_compile_definitions(mbedtls PUBLIC MBEDTLS_CONFIG_FILE="picotool_mbedtls_config.h")
target_include_directories(mbedtls SYSTEM PUBLIC ${PICO_MBEDTLS_PATH}/include)
target_include_directories(mbedtls PUBLIC ${CMAKE_CURRENT_LIST_DIR}/include)
endif()
Loading
Loading