Repository navigation
Support Mbed TLS 4.x - #3064
Open
yegorich wants to merge 2 commits into
Open
Support Mbed TLS 4.x#3064yegorich wants to merge 2 commits into
yegorich wants to merge 2 commits into
Conversation
Mbed TLS 4.0 moved the cryptography into TF-PSA-Crypto and made PSA Crypto its only crypto API. The cipher, AES, MD5, entropy and CTR-DRBG modules and HMAC through md are private, so the build against a system Mbed TLS 4.x fails. Add cipher wrappers in crypto.c that use the mbed TLS cipher module with 2.x and 3.x, as before, and PSA with 4.x: - AES-GCM uses psa_aead_encrypt()/psa_aead_decrypt(). - The AES-CFB, AES-CTR and Camellia-CFB stream ciphers use multi-part PSA cipher operations. - The AEAD-2022 UDP separate header uses AES-ECB through PSA. - HKDF computes the HMAC with psa_mac_*(), and MD5 uses psa_hash_compute(). - crypto_init() calls psa_crypto_init(), which 4.x needs before any hashing, including through md. Digests are looked up with mbedtls_md_info_from_type(), as mbedtls_md_info_from_string() is private in 4.x too. FindMbedTLS.cmake finds 4.x by mbedtls/version.h and links libtfpsacrypto. With 4.x it checks PSA_WANT_ALG_CFB and compares the TF-PSA-Crypto version of the headers and the library. Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com> Assisted-by: Claude:claude-opus-5
Homebrew's mbedtls formula is Mbed TLS 4.x. Build against it in system mode on macOS and run the unit tests, so that the PSA code paths are covered. Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com> Assisted-by: Claude:claude-opus-5
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Mbed TLS 4.0 (October 2025) moved the cryptography into TF-PSA-Crypto and made PSA Crypto its only
crypto API. Many modules that shadowsocks-c uses are private there:
mbedtls/cipher.h);md.So a system mode build against 4.x fails.
FindMbedTLS.cmakealso rejects 4.x.4.1 is the new LTS branch (supported until March 2029), and support for 3.6 LTS ends in March 2027.
Distributions are starting to switch: Homebrew's
mbedtlsis 4.2.0 already, and we are preparingthe switch in Buildroot, which builds shadowsocks against its own Mbed TLS package.
This PR adds 4.x support for system mode. With 2.x and 3.x the code calls the same mbed TLS functions
as before, and the bundled 3.6.7 build is unchanged.
Changes
Cipher wrappers.
crypto.cgets smallcrypto_cipher_*()wrappers.aead.c,stream.candaead2022_udp.cnow call these instead ofmbedtls_cipher_*(), so the version checks live inone place. The existing 2.x/3.x
#iffor the AEAD functions moved there as well.mbedtls_cipher_*()function.cipher_kt_t/cipher_evp_tbecome small structs with the PSA keytype and algorithm, the key ID and a cipher operation:
psa_aead_encrypt()/psa_aead_decrypt()psa_cipher_*()psa_cipher_encrypt()/psa_cipher_decrypt()psa_mac_*()crypto_md5())psa_hash_compute()Other code changes:
crypto_init()callspsa_crypto_init(). 4.x needs it before any hashing, including throughmd,where
mbedtls_md_starts()otherwise fails.mbedtls_md_info_from_type().mbedtls_md_info_from_string()is privatein 4.x.
test_crypto.ccallspsa_crypto_init()itself, because its first tests run beforecrypto_init().CMake.
FindMbedTLS.cmake:mbedtls/version.hinstead ofmbedtls/cipher.h;libtfpsacryptooverlibmbedcrypto;PSA_WANT_ALG_CFBinstead ofMBEDTLS_CIPHER_MODE_CFB;mbedtls_version_get_string()is in libmbedtls, which isn't linked.The pkg-config file lists
-ltfpsacryptowith 4.x.CI. The second commit adds a macOS job that builds against Homebrew's
mbedtls(4.2.0) in systemmode and runs ctest. Feel free to drop it if you'd rather not add a job.
Testing
On Linux x86_64 with gcc 11, in system mode, against three Mbed TLS versions: 2.28.0 (Ubuntu
22.04), 3.6.7 and 4.1.1. On all three, the project's own sources build without warnings (with
SS_WARNINGS_AS_ERRORS) and ctest passes: 41 tests, withtest_interop_rustskipped becauseshadowsocks-rust isn't installed. With 4.1.1 the binaries link
libtfpsacrypto.The default bundled build also passes ctest.
Mixed-version runs. I ran
ss-serverfrom one build andss-local/ss-tunnelfrom another.Each run downloaded a 2 MB file over TCP through
ss-local(checked with SHA-256) and sent UDPdatagrams of 1 to 1400 bytes through
ss-tunnelto an echo server. I used these ciphers:path, I ran the 4.1.1
ss-serverunderqemu-x86_64 -cpu qemu64, where libsodium reportsAES-256-GCM as unavailable. It worked with a native 3.6.7 client (libsodium AES-256-GCM).
ENABLE_SANITIZERS=ONpasses all non-memcheck tests and themixed-version runs without sanitizer errors.
master.
parse_addr()inss-server'smain(). This alsohappens on master, so it's unrelated.
tests/stress_test.pypasses with 4.1.1. aes-128-gcm throughput through Mbed TLSis the same with 3.6.7 and 4.1.1 (about 2.0 to 2.6 Gbps here, 300 MB).
Not tested: