-
Notifications
You must be signed in to change notification settings - Fork 8.3k
drivers: crypto: Add initial support for rpi_pico sha256 accelerator #85036
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
base: main
Are you sure you want to change the base?
Changes from all commits
7283565
a6af0fe
6fe2e89
00e0e5d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,6 +11,7 @@ supported: | |
| - adc | ||
| - clock | ||
| - counter | ||
| - crypto | ||
| - dma | ||
| - gpio | ||
| - hwinfo | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # Copyright (c) 2025 TOKITA Hiroshi | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| config CRYPTO_RPI_PICO_SHA256 | ||
| bool "Raspberry Pi RP2 series SHA-256 Accelerator" | ||
| default y | ||
| depends on DT_HAS_RASPBERRYPI_PICO_SHA256_ENABLED | ||
| select PICOSDK_USE_SHA256 | ||
| help | ||
| Enable driver for RP2 series SHA-256 accelerator |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,158 @@ | ||||||
| /* | ||||||
| * Copyright (c) 2025 TOKITA Hiroshi | ||||||
| * | ||||||
| * SPDX-License-Identifier: Apache-2.0 | ||||||
| */ | ||||||
|
|
||||||
| #define DT_DRV_COMPAT raspberrypi_pico_sha256 | ||||||
|
|
||||||
| #include <zephyr/crypto/crypto.h> | ||||||
| #include <zephyr/kernel.h> | ||||||
| #include <zephyr/sys/util_macro.h> | ||||||
| #include <zephyr/sys/byteorder.h> | ||||||
|
|
||||||
| #include <pico/bootrom/lock.h> | ||||||
| #include <pico/sha256.h> | ||||||
|
|
||||||
| #include <zephyr/logging/log.h> | ||||||
| LOG_MODULE_REGISTER(crypto_rpi_pico_sha256, CONFIG_CRYPTO_LOG_LEVEL); | ||||||
|
|
||||||
| struct crypto_rpi_pico_sha256_data { | ||||||
| pico_sha256_state_t state; | ||||||
| struct k_mutex mutex; | ||||||
| }; | ||||||
|
|
||||||
| static int crypto_rpi_pico_sha256_hash_handler(struct hash_ctx *ctx, struct hash_pkt *pkt, | ||||||
| bool finish) | ||||||
| { | ||||||
| struct crypto_rpi_pico_sha256_data *data = ctx->device->data; | ||||||
| int ret; | ||||||
|
|
||||||
| ret = k_mutex_lock(&data->mutex, K_FOREVER); | ||||||
| if (ret != 0) { | ||||||
| LOG_ERR("Failed to lock mutex: %d", ret); | ||||||
| return ret; | ||||||
| } | ||||||
|
|
||||||
| if (!data->state.locked) { | ||||||
| LOG_ERR("Invalid lock status: unlocked"); | ||||||
| ret = -EINVAL; | ||||||
| goto end; | ||||||
| } | ||||||
|
|
||||||
| data->state.cache_used = 0; | ||||||
| data->state.cache.word = 0; | ||||||
| data->state.total_data_size = 0; | ||||||
|
|
||||||
|
Comment on lines
+43
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it OK to clear the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
While doing another review yesterday I realized that perhaps not all drivers support multipart hash computation. So in the end what you did might be already OK for the time being (you can extend that later if you prefer); please only add a check like the one done in that PR and I think it should be fine.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since there is an example of an implementation that is not supported, I will submit it using that implementation method for now. |
||||||
| sha256_err_not_ready_clear(); | ||||||
| sha256_set_bswap(true); | ||||||
| sha256_start(); | ||||||
|
Comment on lines
+47
to
+49
|
||||||
|
|
||||||
| pico_sha256_update(&data->state, pkt->in_buf, pkt->in_len); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there limits on the input size by the HAL? If so, please test for them to return EINVAL
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are no particular restrictions, so it's not a problem. |
||||||
|
|
||||||
| if (!finish) { | ||||||
| LOG_ERR("Multipart hashing not supported yet"); | ||||||
| ret = -ENOTSUP; | ||||||
| goto end; | ||||||
| } | ||||||
|
|
||||||
| pico_sha256_write_padding(&data->state); | ||||||
| sha256_wait_valid_blocking(); | ||||||
|
|
||||||
| for (uint i = 0; i < 8; i++) { | ||||||
|
||||||
| for (uint i = 0; i < 8; i++) { | |
| for (uint32_t i = 0; i < 8; i++) { |
Copilot
AI
Dec 3, 2025
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.
[nitpick] The explicit cast (uint32_t) before passing to BSWAP_32 is redundant since BSWAP_32 already operates on and returns uint32_t. The cast can be removed for cleaner code: BSWAP_32(sha256_hw->sum[i])
| ((uint32_t *)pkt->out_buf)[i] = BSWAP_32((uint32_t)sha256_hw->sum[i]); | |
| ((uint32_t *)pkt->out_buf)[i] = BSWAP_32(sha256_hw->sum[i]); |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,8 @@ | ||||||||||||||
| # Copyright (c) 2025 TOKITA Hiroshi | ||||||||||||||
| # SPDX-License-Identifier: Apache-2.0 | ||||||||||||||
|
|
||||||||||||||
| description: RaspberryPi Pico SHA-256 accelerator | ||||||||||||||
|
|
||||||||||||||
| compatible: "raspberrypi,pico-sha256" | ||||||||||||||
|
|
||||||||||||||
| include: base.yaml | ||||||||||||||
|
||||||||||||||
| include: base.yaml | |
| include: base.yaml | |
| properties: | |
| reg: | |
| required: true |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| /* | ||
| * Copyright (c) 2025 TOKITA Hiroshi | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| &sha256 { | ||
| status = "okay"; | ||
| }; |
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.
State initialization should not be performed in the hash handler. These fields (
cache_used,cache.word,total_data_size) should be initialized inhash_begin_sessionwhen the session starts, not at every hash operation. The current placement means multipart hashing can never work correctly even if implemented, as these fields would be reset on each call.