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

mgmt: mcumgr: replace Tinycrypt by PSA #71947

Merged

Conversation

tomi-font
Copy link
Collaborator

@tomi-font tomi-font commented Apr 25, 2024

As part of ongoing work to move away from TinyCrypt and towards PSA (#43712), make fs_mgmt use either PSA (when available) or MbedTLS (as a fallback) for SHA-256.

The use of PSA is guarded by CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT which requires a locally-built PSA core for devices without TF-M.

Copy link
Collaborator

@nordicjm nordicjm left a comment

Choose a reason for hiding this comment

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

I cannot accept this change. I understand that having multiple security backends is not great, but when I enable the existing tinycrypt mode for this feature, I get:

Memory region         Used Size  Region Size  %age Used
           FLASH:      208648 B       472 KB     43.17%
             RAM:       61280 B       256 KB     23.38%
        IDT_LIST:          0 GB        32 KB      0.00%

When I switch over to mbedtls I get:

Memory region         Used Size  Region Size  %age Used
           FLASH:      219436 B       472 KB     45.40%
             RAM:       61280 B       256 KB     23.38%
        IDT_LIST:          0 GB        32 KB      0.00%

This is an increase of 10.6KiB, for a simple sha256 hash. I don't know what the overhead of this is in tfm but the overhead without is not acceptable

@carlescufi
Copy link
Member

More context to this discussion:
#8081
#43712

@carlescufi
Copy link
Member

Would it be an option to compile this conditionally? i.e. by default mcumgr would use PSA APIs, since that is the direction the project has chosen, but with Kconfig one could still enable Tinycrypt.

@tomi-font
Copy link
Collaborator Author

tomi-font commented Apr 26, 2024

In my opinion it could definitely be.

@nandojve
Copy link
Member

Would it be an option to compile this conditionally? i.e. by default mcumgr would use PSA APIs, since that is the direction the project has chosen, but with Kconfig one could still enable Tinycrypt.

+1

@ceolin
Copy link
Member

ceolin commented Apr 26, 2024

This is an increase of 10.6KiB, for a simple sha256 hash. I don't know what the overhead of this is in tfm but the overhead without is not acceptable

@nordicjm what is the target / configuration you tested it ? 10k is really too much, I have seen this building

west build -p -b nrf52840dk/nrf52840 tests/subsys/mgmt/mcumgr/fs_mgmt_hash_supported/ -- -DEXTRA_CONF_FILE=configuration/sha256.conf

But lets not jump into early conclusions, I have just disabled some additional features that were enabled by default when building with mbedTLS and the difference was way smaller.

Before any change (building with mbedTLS):

-- Zephyr version: 3.6.99 (/home/faceolin/p/zephyrproject/zephyr), build: v3.6.0-2908-gf366bd3fb3f0
[271/271] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       75496 B         1 MB      7.20%
             RAM:       14392 B       256 KB      5.49%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/faceolin/p/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: nrf52840dk

After disable unnecessary options for this test:

 [265/265] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       65192 B         1 MB      6.22%
             RAM:       14392 B       256 KB      5.49%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/faceolin/p/zephyrproject/zephyr/build/zephyr/zephyr.elf for board: nrf52840dk

Without these changes (current upstream)

-- Zephyr version: 3.6.99 (/home/faceolin/p/zephyrproject/upstream-main), build: v3.6.0-3023-g7084662cc80b
[181/181] Linking C executable zephyr/zephyr.elf
Memory region         Used Size  Region Size  %age Used
           FLASH:       64772 B         1 MB      6.18%
             RAM:       14520 B       256 KB      5.54%
        IDT_LIST:          0 GB        32 KB      0.00%
Generating files from /home/faceolin/p/zephyrproject/upstream-main/build/zephyr/zephyr.elf for board: nrf52840dk

I don't think supporting TinyCrypt indefinitely is a proper solution (obviously, we don't want to be disruptive in a LTS release). We need to have a proper solution.

I understand the concern with additional resources needed, but there are also multiple benefits adopting it.

@nordicjm
Copy link
Collaborator

Tinycrypt itself I'm not so bothered about, the 10KiB jump is what I'm bothered about. In tinycrypt you enable it then have to enable what features you want, seemingly in mbedtls you enable it then have to go and disable features you don't want, that's not really useful and certainly not what other Kconfigs in zephyr subsystems do, you enable a feature with a set of minimal defaults then enable the additional ones you want, so if the size difference from that can be vastly reduced by having minimal defaults and a way for features to select what they need e.g. SHA256 which enables them then that's a path on the right track

@nordicjm
Copy link
Collaborator

Build string:

cmake -GNinja -DBOARD=nrf52840dk/nrf52840 -DOVERLAY_CONFIG="overlay-bt.conf" -DCONFIG_TINYCRYPT=y -DCONFIG_TINYCRYPT_SHA256=y -DCONFIG_MCUMGR_GRP_FS_CHECKSUM_HASH=y -DCONFIG_MCUMGR_GRP_FS_HASH_SHA256=y ..

@ceolin
Copy link
Member

ceolin commented Apr 29, 2024

Tinycrypt itself I'm not so bothered about, the 10KiB jump is what I'm bothered about. In tinycrypt you enable it then have to enable what features you want, seemingly in mbedtls you enable it then have to go and disable features you don't want, that's not really useful and certainly not what other Kconfigs in zephyr subsystems do, you enable a feature with a set of minimal defaults then enable the additional ones you want, so if the size difference from that can be vastly reduced by having minimal defaults and a way for features to select what they need e.g. SHA256 which enables them then that's a path on the right track

I agree with you, it is non-sense pulling a bunch of functionalities that were not requested. My comment was to address the size increase issue. There will much likely be an increase with PSA but that must not be in this magnitude.
Regarding TinyCrypt, everyone should care about it since that is big security problem for the project.

@tomi-font tomi-font marked this pull request as draft April 30, 2024 10:38
@tomi-font tomi-font force-pushed the psa_migration_subsys_mgmt_mcumgr branch from f366bd3 to a3c71b1 Compare April 30, 2024 12:17
@zephyrbot
Copy link
Collaborator

zephyrbot commented Apr 30, 2024

The following west manifest projects have been modified in this Pull Request:

Name Old Revision New Revision Diff

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

@tomi-font
Copy link
Collaborator Author

With the changes from #72078 that removes the default enabling of hash algorithms for MbedTLS, we get to the following differences when building smp_svr with overlay-bt.conf for nrf52840dk/nrf52840:

TinyCrypt (reference):

Memory region         Used Size  Region Size  %age Used
           FLASH:      208428 B       472 KB     43.12%
             RAM:       61248 B       256 KB     23.36%
        IDT_LIST:          0 GB        32 KB      0.00%

MbedTLS (+480 bytes):

Memory region         Used Size  Region Size  %age Used
           FLASH:      208908 B       472 KB     43.22%
             RAM:       61248 B       256 KB     23.36%
        IDT_LIST:          0 GB        32 KB      0.00%

And when using directly mbedtls_sha256 instead of mbedtls_md (which is now the case with my latest changes):

MbedTLS (+68 bytes):

Memory region         Used Size  Region Size  %age Used
           FLASH:      208496 B       472 KB     43.14%
             RAM:       61248 B       256 KB     23.36%
        IDT_LIST:          0 GB        32 KB      0.00%

Additionally, if compiling with overlay-fs.conf instead of overlay-bt.conf the difference shrinks a bit further. This is because currently the bluetooth subsystem pulls in TinyCrypt unconditionally.

Now, regarding the PSA case. As of now the PSA API will only be used when building with TF-M, in which case the crypto operations happen in TF-M. Because TF-M already has the code, the net difference in code size when going from TinyCrypt to PSA is negative.

Building with overlay-fs.conf for nrf5340dk/nrf5340/cpuapp/ns (to have TF-M):

TinyCrypt (reference):

Memory region         Used Size  Region Size  %age Used
           FLASH:       79540 B       192 KB     40.46%
             RAM:       16944 B       192 KB      8.62%
        IDT_LIST:          0 GB        32 KB      0.00%

PSA (-768 bytes):

Memory region         Used Size  Region Size  %age Used
           FLASH:       78772 B       192 KB     40.07%
             RAM:       16944 B       192 KB      8.62%
        IDT_LIST:          0 GB        32 KB      0.00%

nordicjm
nordicjm previously approved these changes May 8, 2024
de-nordic
de-nordic previously approved these changes May 8, 2024
Copy link
Collaborator

@de-nordic de-nordic left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

It is meant specifically for configuration of the PSA crypto library.

The underlying PSA configuration items are guarded by the condition
that a PSA crypto provider must be present, which is the case when
either TF-M is in use or MbedTLS's PSA core is built as part of
the application image.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
tomi-font added a commit to tomi-font/zephyr that referenced this pull request May 10, 2024
Exclude two more platforms from the tests.
They provoke devicetree-related build errors which weren't introduced
by the changes in this PR (zephyrproject-rtos#71947).

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
@tomi-font tomi-font dismissed stale reviews from de-nordic and nordicjm via 500b25c May 10, 2024 06:51
@tomi-font tomi-font force-pushed the psa_migration_subsys_mgmt_mcumgr branch from 9b8fe02 to 500b25c Compare May 10, 2024 06:51
@tomi-font
Copy link
Collaborator Author

Rebased and resolved conflicts.

nordicjm
nordicjm previously approved these changes May 10, 2024
tomi-font added a commit to tomi-font/zephyr that referenced this pull request May 10, 2024
Exclude two more platforms from the tests.
They provoke devicetree-related build errors which weren't introduced
by the changes in this PR (zephyrproject-rtos#71947).

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
@tomi-font tomi-font force-pushed the psa_migration_subsys_mgmt_mcumgr branch from 500b25c to 4341b58 Compare May 10, 2024 07:38
As part of ongoing work to move away from TinyCrypt and towards PSA
(zephyrproject-rtos#43712), make fs_mgmt use either PSA (when available) or MbedTLS
(as a fallback) for SHA-256.

The use of PSA is guarded by CONFIG_MBEDTLS_PSA_CRYPTO_CLIENT
which requires a locally-built PSA core for devices without TF-M.

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
tomi-font added a commit to tomi-font/zephyr that referenced this pull request May 10, 2024
Exclude two more platforms from the tests.
They provoke devicetree-related build errors which weren't introduced
by the changes in this PR (zephyrproject-rtos#71947).

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
@tomi-font tomi-font force-pushed the psa_migration_subsys_mgmt_mcumgr branch from 4341b58 to bf4a226 Compare May 10, 2024 08:19
de-nordic
de-nordic previously approved these changes May 10, 2024
Exclude some more platforms from the tests.
They provoke devicetree-related build errors which weren't introduced
by the changes in this PR (zephyrproject-rtos#71947).

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
@tomi-font
Copy link
Collaborator Author

All CI failures should be addressed now.

@tomi-font tomi-font requested a review from de-nordic May 10, 2024 09:21
@nashif nashif merged commit 73da457 into zephyrproject-rtos:main May 10, 2024
28 checks passed
mariopaja pushed a commit to mariopaja/zephyr that referenced this pull request May 26, 2024
Exclude some more platforms from the tests.
They provoke devicetree-related build errors which weren't introduced
by the changes in this PR (zephyrproject-rtos#71947).

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants