Skip to content
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

Import/mbedtls 3.6.2 #7135

Merged
merged 15 commits into from
Nov 25, 2024

Conversation

ysbnim
Copy link
Contributor

@ysbnim ysbnim commented Nov 19, 2024

This pull request imports MbedTLS 3.6.2 to fix CVEs (Related issue : #7133)
This also rebases several commits according to PR #6797.

ysbnim and others added 3 commits November 19, 2024 02:47
Imports Mbed TLS 3.6.2 from https://github.com/Mbed-TLS/mbedtls.git
tags mbedtls-3.6.2, v3.6.2

Files that are not needed are removed:

cd lib/libmbedtls
rm -rf mbedtls
cp -R path/to/mbedtls-3.6.2/mbedtls .
cd mbedtls
rm CMakeLists.txt DartConfiguration.tcl Makefile
rm .gitignore .travis.yml .pylintrc .globalrc .mypy.ini BRANCHES.md
rm include/.gitignore include/CMakeLists.txt library/.gitignore
rm library/CMakeLists.txt library/Makefile
rm -r cmake
rm -rf .git .github doxygen configs programs scripts tests visualc
rm -rf 3rdparty ChangeLog.d docs pkgconfig .gitmodules .readthedocs.yaml
rm library/mps_*
cd ..
git add mbedtls

This is a complete overwrite of previous code so earlier changes in the
previous branch import/mbedtls-3.6.0 will be added on top of this
commit.

Signed-off-by: Sungbae Yoo <[email protected]>
Removes default config include/mbedtls/config.h

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebased onto mbedtls-2.22.0]
[jf: rebased onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <[email protected]>
[jf: rebased onto mbedtls-2.28.1]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
Configures mbedtls to reach outside the imported source tree for
configuration .h file.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebased onto mbedtls-2.22.0]
[jf: rebased onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0 and removed inclusion of check_config.h]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
@jforissier
Copy link
Contributor

Hi @ysbnim, thanks for putting this together. This looks good overall but I do have a concern with commit "libmbedtls: bignum: restore mbedtls_mpi_exp_mod() from v3.5.2", which was a temporary measure until the performance regression in MBed TLS 3.6.0 was fixed (it was addressed upstream in Mbed-TLS/mbedtls#9281).

Ideally I would like to drop the above commit and instead use the upstream code. However if I understand correctly the performance regression was fixed for some use cases only (public exponents). The fix may be useless for our test case (xtest 4011, a Bleichenbacher attack test). There is also the more general question of whether or not it is acceptable to make RSA signing operations significantly slower (with 32-bit QEMU at least). I believe we should run a few performance tests.

@etienne-lms
Copy link
Contributor

etienne-lms commented Nov 19, 2024

The changelog of mbedtls 3.6.1 mentions the issue you reported:

  • Fix unintended performance regression when using short RSA public keys.
    Fixes #9232.

So I guess it should be fixed with 3.6.2.

(edited) Discard my comment, you already put more detailed on that. Let's try and see how OP-TEE behaves w/ and w/o this patch.

@etienne-lms
Copy link
Contributor

Tested on my 32bit platform: xtest regression_4011 is very slow without commit
"libmbedtls: bignum: restore mbedtls_mpi_exp_mod() from v3.5.2" applied.

@ysbnim
Copy link
Contributor Author

ysbnim commented Nov 20, 2024

Tested time xtest 4011 on my machine. But without commit, QEMUv7 was extremely slow.

    QEMUv8 QEMUv7
3.6.0 w/ commit 1.87s 25.22s
3.6.0 w/o commit 38.25s 14m 52.65s
3.6.2 w/ commit 1.87s 25.58s
3.6.2 w/o commit 41.54s 14m 30.97s

@jforissier
Copy link
Contributor

@ysbnim ysbnim force-pushed the import/mbedtls-3.6.2 branch 2 times, most recently from dfe22af to 0973860 Compare November 21, 2024 01:45
@ysbnim
Copy link
Contributor Author

ysbnim commented Nov 21, 2024

Checked that it takes 26.12s on my machine. Thanks @jforissier !

@etienne-lms
Copy link
Contributor

Test OK on my 32bit platforms. I see the 'unsafe' implementation seems <10% slower compared to the patch that restore the mbedTLS v3.5.2 implementation, but that is not an issue at all.

I wonder whether there should be a config switch to define which of the safe/unsafe modular exponentiation scheme to embed in mbedTLS? It should somewhat clear that a timing-attack defense of mbedTLS is withdrawn when the 'unsafe' function is used.
If so, should there be one config switch for the core and one for mbedTLS as a TA lib? (e.g. CFG_CORE_SAFE_MODEXP / CFG_TA_MBEDTLS_SAFE_MODEXP).

@jforissier
Copy link
Contributor

Test OK on my 32bit platforms. I see the 'unsafe' implementation seems <10% slower compared to the patch that restore the mbedTLS v3.5.2 implementation, but that is not an issue at all.

I wonder whether there should be a config switch to define which of the safe/unsafe modular exponentiation scheme to embed in mbedTLS? It should somewhat clear that a timing-attack defense of mbedTLS is withdrawn when the 'unsafe' function is used. If so, should there be one config switch for the core and one for mbedTLS as a TA lib? (e.g. CFG_CORE_SAFE_MODEXP / CFG_TA_MBEDTLS_SAFE_MODEXP).

That would make sense, although adding config flags is not good for test coverage...

TBH xtest 4011 should not be the only reason for choosing the unsafe algorithm. If all the other tests are reasonably quick with the safe algorithm, then we could introduce an "unsafe" CFG (CFG_CORE_UNSAFE_MODEXP?) and exclude only 4011 when that CFG is set.

@ysbnim
Copy link
Contributor Author

ysbnim commented Nov 21, 2024

How about intoducing new CFG_CORE_UNSAFE_MODEXP and then adding it only to ci check for QEMUv7?

I think QEMUv7 is the only environment that needs unsafe mod exp. 64bit QEMU also has some performance regression but it would be acceptible.

@jforissier
Copy link
Contributor

@ysbnim sounds good to me. Would you please update this PR to do just that? Thanks!

jenswi-linaro and others added 11 commits November 21, 2024 14:23
Makes mbedtls_mpi_montg_init(), mbedtls_mpi_montmul() and
mbedtls_mpi_montred() available for external use.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebased onto mbedtls-2.22.0]
[jf: rebased onto mbedtls-2.27.0, keep static functions]
Signed-off-by: Jerome Forissier <[email protected]>
[jf: rebased onto mbedtls-2.28.1]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0, replace original functions]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
Adds mbedtls_mpi_init_mempool() which initializes a mbedtls_mpi struct
to use the mempool mbedtls_mpi_mempool if configured for memory
allocation. All local memory allocation are changed to use
mbedtls_mpi_init_mempool() instead of mbedtls_mpi_init(). This will give
a stack like alloc/free pattern for which the mempool is optimized.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebased onto mbedtls-2.22.0]
[jf: rebased onto mbedtls-2.27.0, fold fixup commit:
 2df910b ("libmbedtls: mbedtls_mpi_shrink(): fix possible unwanted truncation"),
 adjust macro ECP_MPI_INIT]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0, adjust new coding style]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0, reintroduce mbedtls_mpi_zeroize]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
Increase the count limit when generating the witness in the Rabin-Miller
primality test. The previous number 30 was too low to reliably detect
000000022770A7DC599BC90B2FF981CCB5CF05703344C8F350418AAD as a prime
number.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebased onto mbedtls-2.22.0]
[jf: rebased onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
For integrating into OPTEE_OS, it needs add some interfaces:
1. add mbedtls_cipher_clone() for cipher to copy context between two
operations.
2. add mbedtls_cipher_setup_info() for cipher. cipher need to get its
"cipher_info" according the key length, while the key length is not an
input in allocate function. So, use a default key len in the beginning.
It need to reset the cipher info again in init function.
3. add mbedtls_cipher_cmac_setup() for cmac. This function is separate
from mbedtls_cipher_cmac_starts().
4. copy hmac context in md.

Acked-by: Etienne Carriere <[email protected]>
Signed-off-by: Edison Ai <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebase onto mbedtls-2.22.0]
[jf: rebase onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <[email protected]>
[jf: rebase onto mbedtls-2.28.1, fix typo in comment]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebase onto mbedtls-3.4.0, adjust new coding style]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebase onto mbedtls-3.6.0, adjust for changes between 3.4 and 3.6]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
In NO_CRT mode, Q and P may be invalid. But Q and P will be re-filled
again if PRNG function is valid. So add judgement process if it is
in NO_CRT mode.

Acked-by: Etienne Carriere <[email protected]>
Signed-off-by: Summer Qin <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[jf: rebase onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebase onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
When MBEDTLS_ECP_DP_SM2_ENABLED is set, adds support for the ECC curve
defined for the Chinese SM2 algorithm (G/MT 0003 Part 5, [1]).

Link: [1] http://www.gmbz.org.cn/upload/2018-07-24/1532401863206085511.pdf
Acked-by: Jens Wiklander <[email protected]>
Acked-by: Etienne Carriere <[email protected]>
[jf: rebased onto mbedtls-2.27.0]
Signed-off-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
Adds fault mitigation in mbedtls_rsa_rsassa_pss_verify_ext() by using
the macro FTMN_CALLEE_DONE_MEMCMP() instead of memcmp() when checking
that the hash in the RSA signature is matching the expected value.

FTMN_CALLEE_DONE_MEMCMP() saves on success the result in a thread local
storage if fault mitigations was enabled when the function was called.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
…fy()

Adds fault mitigation in mbedtls_rsa_rsassa_pkcs1_v15_verify() by using
the macro FTMN_CALLEE_DONE_MEMCMP() instead of just
mbedtls_safer_memcmp() when checking that the hash in the RSA signature
is matching the expected value.

FTMN_CALLEE_DONE_MEMCMP() saves on success the result in a thread local
storage if fault mitigations was enabled when the function was called.

Acked-by: Jerome Forissier <[email protected]>
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
For chacha20 and chachapoly, the *_ctx_clone() function is missing
and therefore the wrong function pointers are assigned to
.ctx_clone_func and .ctx_free_func when MBEDTLS_CHACHA20_C
or MBEDTLS_CHACHAPOLY_C is enabled.

Signed-off-by: Simon Ott <[email protected]>
Reviewed-by: Jerome Forissier <[email protected]>
[jw: rebased onto mbedtls-3.4.0]
Signed-off-by: Jens Wiklander <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
For AES Key Wrap mode, the *_ctx_clone() function is missing and
therefore the wrong function pointers are assigned to .ctx_clone_func
and .ctx_free_func when MBEDTLS_NIST_KW_C is enabled.

Signed-off-by: Jens Wiklander <[email protected]>
Acked-By: Jerome Forissier <[email protected]>
[tve: rebased onto mbedtls-3.6.0]
Signed-off-by: Tom Van Eyck <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
mbedtls/library/common.h includes arm_neon.h since 3.5.0, which collides
with the definition of __section and __data in compiler.h. Temporarily
remove those definitions while including arm_neon.h.

Signed-off-by: Tom Van Eyck <[email protected]>
Acked-by: Jens Wiklander <[email protected]>
[sby: rebased onto mbedtls-3.6.2]
Signed-off-by: Sungbae Yoo <[email protected]>
@etienne-lms
Copy link
Contributor

etienne-lms commented Nov 21, 2024

How about intoducing new CFG_CORE_UNSAFE_MODEXP and then adding it only to ci check for QEMUv7?

I agree, but 32bit platform maintainers should also be warned. I think i'll also default enable this switch in plat-stm32mp1/conf.mk to run xtest in a decent amout of time. On these platforms, using the 'safe' implementation also affects regression_4006 (1min=>6min) and pkcs11_1021/1022/1023/1026 (xtest -t pkcs11 lasts 20min instead of 3min). I've test the GP tests: the difference is acceptable: 9min instead of 5min, the extra 4min being spread over many gp_50xxx tests.
(edited) FYI xtest regression_4011 lasts 5min instead of 10sec in that platform.

@ysbnim ysbnim force-pushed the import/mbedtls-3.6.2 branch 2 times, most recently from 0959841 to 7707afc Compare November 21, 2024 15:09
Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

CFG_CORE_UNSAFE_MODEXP, as per its name, relates to OP-TEE core but the switch is also applied when libmbedtls is built as a user land trusted library.
It would be nice the 2 configs are distinguished. Or the swith may be renamed CFG_MBEDTLS_UNSAFE_MODEXP but IMHO 2 config switches would be better. @jforissier, what do you think?

mk/config.mk Outdated Show resolved Hide resolved
mk/config.mk Outdated Show resolved Hide resolved
Copy link
Contributor

@jforissier jforissier left a comment

Choose a reason for hiding this comment

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

No need for a blank line after the Link: tags.
With my comment below addressed, please add:

Reviewed-by: Jerome Forissier <[email protected]>`

...to commit "libmbedtls: add CFG_CORE_UNSAFE_MODEXP and CFG_TA_MEBDTLS_UNSAFE_MODEXP".

Thanks!

mk/config.mk Outdated Show resolved Hide resolved
@ysbnim
Copy link
Contributor Author

ysbnim commented Nov 22, 2024

Removed blank line and add review tag

Copy link
Contributor

@etienne-lms etienne-lms left a comment

Choose a reason for hiding this comment

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

Reviewed-by: Etienne Carriere <[email protected]> for commit
"libmbedtls: add CFG_CORE_UNSAFE_MODEXP and CFG_TA_MEBDTLS_UNSAFE_MODEXP".

Modular exponentiation in MBed TLS v3.6.2 comes in two flavors: an
'unsafe' one, mbedtls_mpi_exp_mod_unsafe(), and a 'safe' one called
mbedtls_mpi_exp_mod(). Here safe/unsafe refers to resistance against
timing attacks (the safe variant is constant-time and usually much
slower). The reason for having the two variants is that the fastest
may be used with public keys while the slowest should be used with
private keys.

This commit introduces CFG_CORE_UNSAFE_MODEXP for TEE core and
CFG_TA_MEBDTLS_UNSAFE_MODEXP for the MBedTLS library for TAs.
Those configurations switch mbedtls_mpi_exp_mod() to the unsafe variant
for better performance.

This commit adds CFG_CORE_UNSAFE_MODEXP=y to QEMUv7 tests as well
because the problem with the safe variant which is now the default is that
it introduces a large performance regression in "time xtest 4011"
which makes the QEMUv7 tests in particular impractical:

		    QEMUv8      QEMUv7
3.4.0 (OP-TEE 4.2.0)    0m 0.85s    0m 14.29s
3.6.2 w/o this commit   0m 21.83s   8m 3.04s
3.6.2 w/  this commit   0m 0.93s    0m 14.34s

Prior to v3.6.0, MBed TLS had no constant time implementation.

Link: https://optee.readthedocs.io/en/latest/building/devices/qemu.html#qemu-v7 [1]
Link: Mbed-TLS/mbedtls@1ba4058
Signed-off-by: Sungbae Yoo <[email protected]>
Reviewed-by: Jerome Forissier <[email protected]>
Reviewed-by: Etienne Carriere <[email protected]>
@jforissier
Copy link
Contributor

@ysbnim thank you for your contribution. I will now merge this PR into the import branch, and I will later take care of creating the squashed commit and pull request to merge into master.

@jforissier jforissier merged commit 85df256 into OP-TEE:import/mbedtls-3.6.2 Nov 25, 2024
8 of 9 checks passed
@jforissier
Copy link
Contributor

#7149

etienne-lms added a commit to etienne-lms/optee_os that referenced this pull request Nov 26, 2024
Default enable CFG_CORE_UNSAFE_MODEXP on plat-stm32mp1 to run xtest
regression and pkcs11 TA tests in a decent amount of time.

Link: OP-TEE#7135 (comment)
Signed-off-by: Etienne Carriere <[email protected]>
etienne-lms added a commit to etienne-lms/optee_os that referenced this pull request Nov 26, 2024
Default enable CFG_CORE_UNSAFE_MODEXP for plat-stm32mp1 to run xtest
regression and pkcs11 TA tests in a decent amount of time.

Link: OP-TEE#7135 (comment)
Signed-off-by: Etienne Carriere <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants