-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
mbedtls: use static key slot buffers in the PSA Crypto core #80368
mbedtls: use static key slot buffers in the PSA Crypto core #80368
Conversation
The following west manifest projects have changed revision in this Pull Request:
✅ All manifest checks OK Note: This message is automatically posted and updated by the Manifest GitHub Action. |
3c27a7e
to
8bf2199
Compare
doc/releases/migration-guide-4.0.rst
Outdated
@@ -75,6 +75,12 @@ Mbed TLS | |||
corresponding build symbol was removed in Mbed TLS 3.1.0 and is now assumed to | |||
be enabled. (:github:`77657`) | |||
|
|||
* The newly added Kconfig option :kconfig:option:`CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* The newly added Kconfig option :kconfig:option:`CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT` | |
* The newly-added Kconfig option :kconfig:option:`CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT` |
doc/releases/migration-guide-4.0.rst
Outdated
Previously this value was not explicitly set, so Mbed TLS default value of | ||
32 was assumed. The new Kconfig option defaults to 16 intead in order to find |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously this value was not explicitly set, so Mbed TLS default value of | |
32 was assumed. The new Kconfig option defaults to 16 intead in order to find | |
Previously this value was not explicitly set, so Mbed TLS's default value of | |
32 was used. The new Kconfig option defaults to 16 instead in order to find |
modules/mbedtls/Kconfig.tls-generic
Outdated
the build through PSA_WANT symbols, if MBEDTLS_PSA_STATIC_KEY_SLOTS | ||
is set (all of this defined statically at build time). | ||
* the key material allocated in heap memory at runtime, if | ||
MBEDTLS_PSA_STATIC_KEY_SLOTS is not set |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MBEDTLS_PSA_STATIC_KEY_SLOTS is not set | |
MBEDTLS_PSA_STATIC_KEY_SLOTS is not set. |
modules/mbedtls/Kconfig.tls-generic
Outdated
help | ||
Set the number of key slots that are available in the PSA Crypto core. | ||
Be aware that each slot, even if unused, increases RAM consumption | ||
by ~40 bytes of overhead for each slot plus: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
by ~40 bytes of overhead for each slot plus: | |
by ~40 bytes plus: |
modules/mbedtls/Kconfig.tls-generic
Outdated
by ~40 bytes of overhead for each slot plus: | ||
* the length of the largest asymmetric/symmetric key type enabled in | ||
the build through PSA_WANT symbols, if MBEDTLS_PSA_STATIC_KEY_SLOTS | ||
is set (all of this defined statically at build time). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is set (all of this defined statically at build time). | |
is set. (This is all defined statically at build time). |
0efedf1
to
8b1a8c1
Compare
I apologize for the double rebase, but since release 4.0 was done, I needed to update the PR and move documentation changes from 4.0 to 4.1. The rebase pattern is as follows:
I hope this helps with the review process. |
8b1a8c1
to
461304f
Compare
461304f
to
7577149
Compare
7577149
to
2495f3b
Compare
Update the Mbed TLS revision so as to take in a patch which allows to use static key slot buffers for the PSA Crypto core instead of dynamic (i.e. heap based) ones. Signed-off-by: Valerio Setti <[email protected]>
2495f3b
to
74840b4
Compare
I update the PR as follows:
Nothing else was change code wise, so there should be no CI failure and the PR should be ready for final review |
Adding the Kconfig symbol CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS to allow Mbed TLS's PSA Crypto core to use static key buffers to store key's material. This helps reducing heap memory usage and, potentially, it also discard code implementing heap memory management if there's no other module in the build that makes use of it. Signed-off-by: Valerio Setti <[email protected]>
Adding new CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT to select the number of key slots in PSA Crypto core. The default value is 16. Be aware that key slots consume RAM memory even if unused, so the proper value should be a compromise between the number of slots required by the application and the available RAM in the system. This commit also: - updates tests/crypto/secp256r1/mbedtls.conf to showcase how to use this new symbol to reduce RAM footprint. - tests/bsim/bluetooth/mesh/overlay_psa.conf to support all the keys used in the test. Signed-off-by: Valerio Setti <[email protected]>
74840b4
to
7dc3c18
Compare
What is done in this PR
MBEDTLS_PSA_STATIC_KEY_SLOTS
build symbol to the Mbed TLS build in order to enable the featureGoal
As anticipated in a previous PR, removing heap usage from the Mbed TLS PSA Core for key management can be helpful for both RAM and ROM footprints reduction. RAM is pretty obvious, while for ROM footprint reduction happens if no other component makes use of heap memory so that the heap management code can be completely removed.
Example
Let's take
tests/crypto/secp256r1/crypto.secp256r1.mbedtls
on thenrf52840dk/nrf52840
as example for the improvements. Prior to this PR (a):while after this PR (b):
which is a
-2568
bytes less than before and it's also only1260
bytes more than the very same example that uses TinyCrypt instead of Mbed TLS:Why does the PSA Crypto core still has larger ROM footprint than TinyCrypt even after this PR?
Because the PSA Core implemented in Mbed TLS includes:
Further RAM optimizations
Albeit there's a clear ROM reduction from case (a) to (b), it's also clear that RAM usage is heavily increased (
+2048
bytes). This is due to the fact with statically defined key slots in the PSA Core, all the slots are pre-allocated at build time and all of them are large enough to contain and symmetric/asymmetric PSA key type enabled in the build.It should then be noticed that at current state Zephyr builds Mbed TLS assuming always 32 key slots for the PSA core (i.e.
MBEDTLS_PSA_KEY_SLOT_COUNT
not set inconfig-tls-generic.h
which means that Mbed TLS uses its default value which is 32).If the user knows a priori how many keys their application is going to use, they can reduce this value. This is why I cherry-picked commit from #80136 to allow the user to select the number of key slots using the new Kconfig symbol
CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT
.Going back to the previous example (a), setting
CONFIG_MBEDTLS_PSA_KEY_SLOT_COUNT=2
(required for that test case), we have:which is definitely closer to TinyCrypt counterpart for both RAM and ROM point of view.