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

mbedtls: use static key slot buffers in the PSA Crypto core #80368

Conversation

valeriosetti
Copy link
Collaborator

@valeriosetti valeriosetti commented Oct 24, 2024

What is done in this PR

  • update Mbed TLS repo reference in order to include that patch that implement static key slot buffers
  • add MBEDTLS_PSA_STATIC_KEY_SLOTS build symbol to the Mbed TLS build in order to enable the feature

Goal

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 the nrf52840dk/nrf52840 as example for the improvements. Prior to this PR (a):

west build -p always -b nrf52840dk/nrf52840 -T tests/crypto/secp256r1/crypto.secp256r1.mbedtls
[...]
Memory region         Used Size  Region Size  %age Used
           FLASH:       44443 B         1 MB      4.24%
             RAM:       15680 B       256 KB      5.98%
        IDT_LIST:          0 GB        32 KB      0.00%

while after this PR (b):

west build -p always -b nrf52840dk/nrf52840 -T tests/crypto/secp256r1/crypto.secp256r1.mbedtls
[...]
Memory region         Used Size  Region Size  %age Used
           FLASH:       41875 B         1 MB      3.99%
             RAM:       17728 B       256 KB      6.76%
        IDT_LIST:          0 GB        32 KB      0.00%

which is a -2568 bytes less than before and it's also only 1260 bytes more than the very same example that uses TinyCrypt instead of Mbed TLS:

west build -p always -b nrf52840dk/nrf52840 -T tests/crypto/secp256r1/crypto.secp256r1.tinycrypt
[...]
Memory region         Used Size  Region Size  %age Used
           FLASH:       40615 B         1 MB      3.87%
             RAM:         14 KB       256 KB      5.47%
        IDT_LIST:          0 GB        32 KB      0.00%

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:

  • wrappers, i.e. a call to any PSA Crypto API is dispatched to some built-in implementation;
  • it includes key material management which is totally absent in TinyCrypt.

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 in config-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:

west build -p always -b nrf52840dk/nrf52840 -T tests/crypto/secp256r1/crypto.secp256r1.mbedtls
[...]
Memory region         Used Size  Region Size  %age Used
           FLASH:       41891 B         1 MB      4.00%
             RAM:       14592 B       256 KB      5.57%
        IDT_LIST:          0 GB        32 KB      0.00%

which is definitely closer to TinyCrypt counterpart for both RAM and ROM point of view.

@zephyrbot
Copy link
Collaborator

zephyrbot commented Oct 24, 2024

The following west manifest projects have changed revision in this Pull Request:

Name Old Revision New Revision Diff
mbedtls zephyrproject-rtos/mbedtls@a78176c zephyrproject-rtos/mbedtls@4952e13 (zephyr) zephyrproject-rtos/[email protected]

All manifest checks OK

Note: This message is automatically posted and updated by the Manifest GitHub Action.

@zephyrbot zephyrbot added manifest manifest-mbedtls DNM This PR should not be merged (Do Not Merge) labels Oct 24, 2024
@valeriosetti valeriosetti changed the title mbedtls: use static key slot buffers in the PSA core mbedtls: use static key slot buffers in the PSA Core Oct 24, 2024
@valeriosetti valeriosetti changed the title mbedtls: use static key slot buffers in the PSA Core mbedtls: use static key slot buffers in the PSA Crypto core Oct 25, 2024
@valeriosetti valeriosetti marked this pull request as ready for review October 25, 2024 03:36
@zephyrbot zephyrbot added area: Bluetooth area: Bluetooth Mesh Release Notes To be mentioned in the release notes labels Oct 25, 2024
@valeriosetti valeriosetti force-pushed the manifest-for-psa-use-static-key-slots branch from 3c27a7e to 8bf2199 Compare October 25, 2024 07:11
tomi-font
tomi-font previously approved these changes Nov 6, 2024
@@ -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`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
* 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`

Comment on lines 80 to 82
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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
MBEDTLS_PSA_STATIC_KEY_SLOTS is not set
MBEDTLS_PSA_STATIC_KEY_SLOTS is not set.

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:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
by ~40 bytes of overhead for each slot plus:
by ~40 bytes plus:

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).
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
is set (all of this defined statically at build time).
is set. (This is all defined statically at build time).

@tomi-font tomi-font added this to the v4.0.0 milestone Nov 6, 2024
@tomi-font
Copy link
Collaborator

please review @ceolin @d3zd3z

@mmahadevan108 mmahadevan108 modified the milestones: v4.0.0, v4.1.0 Nov 12, 2024
d3zd3z
d3zd3z previously approved these changes Nov 13, 2024
@valeriosetti valeriosetti dismissed stale reviews from d3zd3z and tomi-font via 0efedf1 November 18, 2024 12:38
@valeriosetti valeriosetti force-pushed the manifest-for-psa-use-static-key-slots branch 2 times, most recently from 0efedf1 to 8b1a8c1 Compare November 18, 2024 12:44
@valeriosetti
Copy link
Collaborator Author

valeriosetti commented Nov 18, 2024

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:

  • from f89ccee to 0efedf1 --> simply rebase the PR on top of main. Here I had to solve some conflicts on the documentation, but no code was affected by this.
  • from 0efedf1 to 8b1a8c1 --> to move documentation changes from 4.0 to 4.1.
  • [EDIT] from 8b1a8c1 to 461304f --> to address latest comments from @tomi-font.

I hope this helps with the review process.

@valeriosetti valeriosetti force-pushed the manifest-for-psa-use-static-key-slots branch from 8b1a8c1 to 461304f Compare November 18, 2024 12:51
doc/releases/release-notes-4.1.rst Outdated Show resolved Hide resolved
doc/releases/release-notes-4.1.rst Outdated Show resolved Hide resolved
modules/mbedtls/Kconfig.tls-generic Show resolved Hide resolved
doc/releases/migration-guide-4.1.rst Show resolved Hide resolved
modules/mbedtls/Kconfig.tls-generic Outdated Show resolved Hide resolved
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]>
@valeriosetti valeriosetti force-pushed the manifest-for-psa-use-static-key-slots branch from 2495f3b to 74840b4 Compare November 21, 2024 05:10
@zephyrbot zephyrbot removed the DNM This PR should not be merged (Do Not Merge) label Nov 21, 2024
@valeriosetti
Copy link
Collaborator Author

valeriosetti commented Nov 21, 2024

I update the PR as follows:

  • from 7577149 to 2495f3b --> rebase on main to solve a conflict on migration-guide-4.1.
  • from 2495f3b to 74840b4 --> I just updated the first commit about the Mbed TLS repo revision since the dependence PR was merged.
  • from 74840b4 to 7dc3c18 --> to address the last comment by @tomi-font.

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]>
@valeriosetti valeriosetti force-pushed the manifest-for-psa-use-static-key-slots branch from 74840b4 to 7dc3c18 Compare November 21, 2024 05:24
@kartben kartben merged commit 95aaa97 into zephyrproject-rtos:main Nov 25, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.