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

nrf_security: drivers: cracen: make single-part AEAD operations single-part #20827

Merged

Conversation

tomi-font
Copy link
Contributor

@tomi-font tomi-font commented Mar 10, 2025

Make the single-part AEAD operations not use HW context switching (save/resume).

As a bonus, make algorithm-specific code (especially for CCM) depend on its respective PSA_NEED_CRACEN so that it gets compiled out if the algorithm is disabled.

ref: NCSDK-31362

@tomi-font tomi-font requested a review from a team as a code owner March 10, 2025 12:02
@github-actions github-actions bot added the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Mar 10, 2025
@NordicBuilder
Copy link
Contributor

NordicBuilder commented Mar 10, 2025

CI Information

To view the history of this post, clich the 'edited' button above
Build number: 8

Inputs:

Sources:

sdk-nrf: PR head: 080ecefe3e56df73ea57de4b3b43661808afada9

more details

sdk-nrf:

PR head: 080ecefe3e56df73ea57de4b3b43661808afada9
merge base: 98235e864d717da654cd5f8100cf0de5c4a45a82
target head (main): e2b93963284bbe111511c05edfea07ed53e6c1ff
Diff

Github labels

Enabled Name Description
ci-disabled Disable the ci execution
ci-all-test Run all of ci, no test spec filtering will be done
ci-force-downstream Force execution of downstream even if twister fails
ci-run-twister Force run twister
ci-run-zephyr-twister Force run zephyr twister
List of changed files detected by CI (2)
subsys
│  ├── nrf_security
│  │  ├── src
│  │  │  ├── drivers
│  │  │  │  ├── cracen
│  │  │  │  │  ├── cracenpsa
│  │  │  │  │  │  ├── src
│  │  │  │  │  │  │  ├── aead.c
│  │  │  │  │  │  │  │ blkcipher.c

Outputs:

Toolchain

Version: 4ffa2202d5
Build docker image: docker-dtr.nordicsemi.no/sw-production/ncs-build:4ffa2202d5_e579f9fbe6

Test Spec & Results: ✅ Success; ❌ Failure; 🟠 Queued; 🟡 Progress; ◻️ Skipped; ⚠️ Quarantine

  • ◻️ Toolchain - Skipped: existing toolchain is used
  • ✅ Build twister - Skipped: Skipping Build & Test as it succeeded in a previous run: 7
  • ✅ Integration tests
    • ✅ test-fw-nrfconnect-chip - Skipped: Job was skipped as it succeeded in a previous run
    • ✅ test-fw-nrfconnect-nrf-iot_cloud - Skipped: Job was skipped as it succeeded in a previous run
    • ✅ test-fw-nrfconnect-nrf_crypto - Skipped: Job was skipped as it succeeded in a previous run
    • ✅ test-fw-nrfconnect-tfm - Skipped: Job was skipped as it succeeded in a previous run
    • ✅ test-sdk-find-my
    • ✅ test-sdk-sidewalk - Skipped: Job was skipped as it succeeded in a previous run
    • ✅ test-sdk-dfu - Skipped: Job was skipped as it succeeded in a previous run
Disabled integration tests
    • desktop52_verification
    • doc-internal
    • test_ble_nrf_config
    • test-fw-nrfconnect-apps
    • test-fw-nrfconnect-ble_mesh
    • test-fw-nrfconnect-ble_samples
    • test-fw-nrfconnect-boot
    • test-fw-nrfconnect-fem
    • test-fw-nrfconnect-nfc
    • test-fw-nrfconnect-nrf-iot_libmodem-nrf
    • test-fw-nrfconnect-nrf-iot_mosh
    • test-fw-nrfconnect-nrf-iot_positioning
    • test-fw-nrfconnect-nrf-iot_samples
    • test-fw-nrfconnect-nrf-iot_serial_lte_modem
    • test-fw-nrfconnect-nrf-iot_thingy91
    • test-fw-nrfconnect-nrf-iot_zephyr_lwm2m
    • test-fw-nrfconnect-proprietary_esb
    • test-fw-nrfconnect-ps
    • test-fw-nrfconnect-rpc
    • test-fw-nrfconnect-rs
    • test-fw-nrfconnect-thread
    • test-fw-nrfconnect-zigbee
    • test-low-level
    • test-sdk-audio
    • test-sdk-mcuboot
    • test-sdk-pmic-samples
    • test-sdk-wifi
    • test-secdom-samples-public

Note: This message is automatically posted and updated by the CI

@tomi-font tomi-font force-pushed the security_cracen_single-part_AEAD branch 2 times, most recently from e655e6b to f7180a3 Compare March 13, 2025 11:42
@tomi-font tomi-font removed the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Mar 14, 2025
@@ -57,8 +55,10 @@ static bool is_nonce_length_supported(psa_algorithm_t alg, size_t nonce_length)
case PSA_ALG_GCM:
case PSA_ALG_CHACHA20_POLY1305:
return nonce_length == 12u;
#ifdef PSA_NEED_CRACEN_CCM_AES
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you did that for CCM in this function I would say it makes sense to do it for the other algorithms as well. Just for the consistency.

@@ -195,12 +195,12 @@ static void cracen_writebe(uint8_t *out, uint64_t data, uint16_t targetsz)
}
}

static psa_status_t create_aead_ccmheader(cracen_aead_operation_t *operation)
static void create_aead_ccmheader(cracen_aead_operation_t *operation,
uint8_t header[static CCM_HEADER_MAX_LENGTH],
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the static keyword inside the array argument valid C? Whats up with this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, I was not aware of this, thanks!

/* Create the CCM header */
if (operation->alg == PSA_ALG_CCM) {
return create_aead_ccmheader(operation);
if (IS_ENABLED(PSA_NEED_CRACEN_CCM_AES) && operation->alg == PSA_ALG_CCM) {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the CCM is not enabled but the operation set the CCM algorithm it will return success with this logic. Please update.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No it won't because set_nonce() is called before, which calls is_nonce_length_supported(), which does the checking.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right on this, I missed that part. You need to go two functions down and check an ifdef to realize that this is not possible though. So if you keep the logic please explain what you are doing there.

/* Data fed to CRACEN needs to remain untouched until it's been consumed
* (sx_aead_wait()), so don't put the CCM header buffer on the stack.
*/
static uint8_t ccm_header_aad[ROUND_UP(CCM_HEADER_MAX_LENGTH,
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not thread safe I think. How would this work with multiple threads?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed it's not thread-safe but I've been told that there is some synchronization mechanism at a higher level which makes it so that only a single thread can be here at the same time?

Copy link
Contributor

Choose a reason for hiding this comment

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

Discussed offline

if (*ciphertext_length + tag_length > ciphertext_size) {
*ciphertext_length = 0;
status = PSA_ERROR_BUFFER_TOO_SMALL;
status = cracen_aead_finish(&operation, NULL, 0, NULL,
Copy link
Contributor

Choose a reason for hiding this comment

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

It is a bit strange and error prone that you are using a function which may resume the context depending on the operation state. If you insist to do that please add a comment to explain here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see your point. My goal with that was to use already existing functions where possible to avoid code duplication in order to minimize code size increases. No strong opinion on that. I'll rework how it's done.

if (status != PSA_SUCCESS) {
*ciphertext_length = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you remove that? This will for sure cause issues since the caller will have no way to distinguish what happened with the ciphertext buffer.
Please re add this in all cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed that because with my changes ciphertext_length is not touched anymore unless the function returns PSA_SUCCESS. Previously it could be set by cracen_aead_update() and so in case of error we'd erase that value by setting it to 0.

See https://arm-software.github.io/psa-api/crypto/1.2/api/ops/aead.html#c.psa_aead_encrypt:

ciphertext_length
    On success, the size of the output in the ciphertext buffer.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see your point but I see that in the decrypt function we set the plaintext length as 0. And I still believe that we don't break the spec if we set that to 0 as well. So I would still suggest to do that.

@tomi-font tomi-font force-pushed the security_cracen_single-part_AEAD branch from f7180a3 to 0d87b24 Compare March 18, 2025 11:40
@github-actions github-actions bot added the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Mar 18, 2025
@tomi-font tomi-font requested review from Vge0rge and degjorva March 18, 2025 11:40

Verified

This commit was signed with the committer’s verified signature.
kilfoyle David Kilfoyle
…e-part

Make the single-part AEAD operations not use HW context switching
(save/resume).

As a bonus, make algorithm-specific code (especially for CCM)
depend on its respective `PSA_NEED_CRACEN` so that
it gets compiled out if the algorithm is disabled.

ref: NCSDK-31362

Signed-off-by: Tomi Fontanilles <tomi.fontanilles@nordicsemi.no>
@tomi-font tomi-font force-pushed the security_cracen_single-part_AEAD branch from 0d87b24 to 080ecef Compare March 18, 2025 12:51
Copy link

@tomi-font tomi-font removed the changelog-entry-required Update changelog before merge. Remove label if entry is not needed or already added. label Mar 19, 2025
@endre-nordic endre-nordic merged commit 6d2a4a0 into nrfconnect:main Mar 19, 2025
14 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.

None yet

5 participants