Skip to content

mbedtls: improve debug output and fix AES-ICM issues#793

Merged
pabuhler merged 1 commit into
cisco:mainfrom
seyednasermoravej:migration-to-psa-crypto
May 11, 2026
Merged

mbedtls: improve debug output and fix AES-ICM issues#793
pabuhler merged 1 commit into
cisco:mainfrom
seyednasermoravej:migration-to-psa-crypto

Conversation

@seyednasermoravej

Copy link
Copy Markdown
Contributor
  • aes_gcm: Add debug print when PSA status fails.
  • aes_icm: Destroy previous key before importing new key.
  • aes_icm: Add missing destination buffer argument to encrypt function.

After testing the new libSRTP on a microcontroller with a limited resources (RAM = 256KB), I faced two issues:

  1. We need to check if the key is available, first we need to destroy it, after that we can address its pointer to the NULL pointer.
  2. When performing in-place encryption/decryption (input buffer == output buffer), the input length must be divisible by the cipher block size. Please check this:
    https://github.com/Mbed-TLS/TF-PSA-Crypto/blob/76920edddcad00ac41b248e12d937b845df7bedb/drivers/builtin/src/cipher.c#L641

Comment thread crypto/cipher/aes_icm_mbedtls.c
@seyednasermoravej seyednasermoravej force-pushed the migration-to-psa-crypto branch 3 times, most recently from a2192e0 to e8441a8 Compare February 27, 2026 13:16
Comment thread crypto/cipher/aes_icm_mbedtls.c Outdated
@seyednasermoravej seyednasermoravej force-pushed the migration-to-psa-crypto branch 4 times, most recently from 3357061 to ff42e7c Compare February 27, 2026 13:37
Comment thread crypto/cipher/aes_icm_mbedtls.c
Comment thread crypto/cipher/aes_gcm_mbedtls.c Outdated
Comment thread crypto/cipher/aes_icm_mbedtls.c Outdated
@seyednasermoravej seyednasermoravej force-pushed the migration-to-psa-crypto branch 2 times, most recently from 584c295 to 8f88503 Compare April 4, 2026 10:25
@pabuhler

Copy link
Copy Markdown
Member

I am still unsure if you would like to try and merge this or have it as a local fix only?

Comment thread crypto/cipher/aes_icm_mbedtls.c Outdated
Comment thread crypto/cipher/aes_icm_mbedtls.c Outdated
Comment thread crypto/cipher/aes_icm_mbedtls.c Outdated
@seyednasermoravej

Copy link
Copy Markdown
Contributor Author

I am still unsure if you would like to try and merge this or have it as a local fix only?

Based on this comment Mbed-TLS/TF-PSA-Crypto#711 (comment) the bug is on the MbedTLS side. I figured it out why the code works on Ubuntu while doesn't on Zephyr and fixed the issue. The fact is somewhere in the code two micros are defined differently for Zephyr and Ubuntu. For example, LOCAL_INPUT_ALLOC by default is defined
https://github.com/Mbed-TLS/TF-PSA-Crypto/blob/8c29e401e9c1a3180a1eca6aed13958453276550/core/psa_crypto.c#L291
in Zephyr RTOS and is defined
https://github.com/Mbed-TLS/TF-PSA-Crypto/blob/8c29e401e9c1a3180a1eca6aed13958453276550/core/psa_crypto.c#L218
in the Ubuntu. I changed the configurations for Zephyr to works like the Ubuntu. The point is in the deep inside with Ubuntu configurations, the function is NOT in-place at all. It seems to be in-place but in the background it allocates another buffer for the input and a another buffer for the output. In summery.
Ubuntu way:
input -> copy to new buffer, allocates a new output buffer -> three buffers + 1 copy at least.
So, the current code (this modified aes_icm_mbedtls.c) is more optimized.

So, we have two options

  1. Merging the current code to the main (better for optimization, Also MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS should be enabled for Ubuntu).
  2. Removing the code added to aes_icm_mbedtls.c (cleaner code, maybe some day MbedTLS fixes this bug).
    What do you suggest? @pabuhler

@pabuhler

pabuhler commented May 7, 2026

Copy link
Copy Markdown
Member

I would personally go for option 2 if there is no actual need for this code now, mostly to avoid maintaining special case handling.
But that said I do not use this back end and so will not block it from being merge if it serves a purpose for some one.

- aes_gcm: Add debug print when PSA status fails.
- aes_icm: Destroy previous key before importing new key.
- aes_icm: Add missing destination buffer argument to encrypt function.

Signed-off-by: Sayed Naser Moravej <seyednasermoravej@gmail.com>
@seyednasermoravej seyednasermoravej force-pushed the migration-to-psa-crypto branch from 8f88503 to 1b7ca3c Compare May 10, 2026 04:03
@seyednasermoravej

Copy link
Copy Markdown
Contributor Author

I would personally go for option 2 if there is no actual need for this code now, mostly to avoid maintaining special case handling. But that said I do not use this back end and so will not block it from being merge if it serves a purpose for some one.

I applied the option 2. I'm handling the issue on Zephyr side with the same configurations like PC. zephyrproject-rtos/zephyr#108519
Now I think the code is more cleaner. The only important issue with the current main branch is the problem with not deleting keys which this commit fix it.

@pabuhler pabuhler merged commit 2499c94 into cisco:main May 11, 2026
46 checks passed
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.

2 participants