-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Open
soburi
wants to merge
6
commits into
zephyrproject-rtos:main
Choose a base branch
from
soburi:rpi_pico_sha256
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+194
−22
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1616dd5
drivers: crypto: sort file entries
soburi da0bf1a
manifest: Update hal_rpi_pico module
soburi 8bb2bed
modules: hal_rpi_pico: Add sha256 driver
soburi 91f721b
drivers: crypto: Add initial support for rpi_pico sha256 accelerator
soburi 44108a5
boards: raspberrypi: rpi_pico2: Add sha256 accelerator
soburi 236fd9d
tests: crypto: crypto_hash: Add rpi_pico2 to test target
soburi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ supported: | |
- adc | ||
- clock | ||
- counter | ||
- crypto | ||
- dma | ||
- gpio | ||
- hwinfo | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,24 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
zephyr_library() | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_ATAES132A crypto_ataes132a.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MBEDTLS_SHIM crypto_mtls_shim.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_STM32 crypto_stm32.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SMARTBOND crypto_smartbond.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NRF_ECB crypto_nrf_ecb.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_INTEL_SHA crypto_intel_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NPCX_SHA crypto_npcx_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCHP_XEC_SYMCR crypto_mchp_xec_symcr.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_IT51XXX_SHA crypto_it51xxx_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_IT8XXX2_SHA crypto_it8xxx2_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_IT8XXX2_SHA_V2 crypto_it8xxx2_sha_v2.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCUX_DCP crypto_mcux_dcp.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SI32 crypto_si32.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_CC23X0 crypto_cc23x0.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RTS5912_SHA crypto_rts5912_sha.c) | ||
|
||
# zephyr-keep-sorted-start | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_ATAES132A crypto_ataes132a.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_CC23X0 crypto_cc23x0.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_INTEL_SHA crypto_intel_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_IT51XXX_SHA crypto_it51xxx_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_IT8XXX2_SHA crypto_it8xxx2_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_IT8XXX2_SHA_V2 crypto_it8xxx2_sha_v2.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MBEDTLS_SHIM crypto_mtls_shim.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCHP_XEC_SYMCR crypto_mchp_xec_symcr.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCUX_DCP crypto_mcux_dcp.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NPCX_SHA crypto_npcx_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_NRF_ECB crypto_nrf_ecb.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RPI_PICO_SHA256 crypto_rpi_pico_sha256.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_RTS5912_SHA crypto_rts5912_sha.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SI32 crypto_si32.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_SMARTBOND crypto_smartbond.c) | ||
zephyr_library_sources_ifdef(CONFIG_CRYPTO_STM32 crypto_stm32.c) | ||
# zephyr-keep-sorted-stop | ||
|
||
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "RasberryPi Pico SHA256 Accelerator driver" | ||
default y | ||
depends on DT_HAS_RASPBERRYPI_PICO_SHA256_ENABLED | ||
select PICOSDK_USE_SHA256 | ||
help | ||
RaspberryPi Pico SHA256 accelarator driver. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* 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(sha_rpi_pico_sha256, CONFIG_CRYPTO_LOG_LEVEL); | ||
|
||
struct crypto_rpi_pico_sha256_data { | ||
pico_sha256_state_t state; | ||
struct k_spinlock lock; | ||
}; | ||
|
||
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; | ||
|
||
if (!data->state.locked) { | ||
LOG_ERR("Invalid status"); | ||
return -EINVAL; | ||
} | ||
|
||
data->state.cache_used = 0; | ||
data->state.cache.word = 0; | ||
data->state.total_data_size = 0; | ||
|
||
sha256_err_not_ready_clear(); | ||
sha256_set_bswap(true); | ||
sha256_start(); | ||
|
||
pico_sha256_update(&data->state, pkt->in_buf, pkt->in_len); | ||
|
||
if (!finish) { | ||
return 0; | ||
} | ||
|
||
pico_sha256_write_padding(&data->state); | ||
sha256_wait_valid_blocking(); | ||
|
||
for (uint i = 0; i < 8; i++) { | ||
((uint32_t *)pkt->out_buf)[i] = BSWAP_32((uint32_t)sha256_hw->sum[i]); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static int crypto_rpi_pico_sha256_query_hw_caps(const struct device *dev) | ||
{ | ||
return (CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS); | ||
} | ||
|
||
static int crypto_rpi_pico_sha256_hash_begin_session(const struct device *dev, struct hash_ctx *ctx, | ||
enum hash_algo algo) | ||
{ | ||
struct crypto_rpi_pico_sha256_data *data = dev->data; | ||
k_spinlock_key_t key; | ||
int ret; | ||
|
||
if (algo != CRYPTO_HASH_ALGO_SHA256) { | ||
LOG_ERR("Unsupported algo: %d", algo); | ||
return -EINVAL; | ||
} | ||
|
||
if (ctx->flags & ~(crypto_rpi_pico_sha256_query_hw_caps(dev))) { | ||
LOG_ERR("Unsupported flag %x", ctx->flags); | ||
return -EINVAL; | ||
} | ||
|
||
key = k_spin_lock(&data->lock); | ||
|
||
ret = bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256); | ||
if (!ret) { | ||
LOG_ERR("bootrom_try_acquire_lock failed"); | ||
k_spin_unlock(&data->lock, key); | ||
return -EBUSY; | ||
} | ||
|
||
data->state.locked = true; | ||
|
||
k_spin_unlock(&data->lock, key); | ||
|
||
ctx->hash_hndlr = crypto_rpi_pico_sha256_hash_handler; | ||
|
||
return 0; | ||
} | ||
|
||
static int crypto_rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx) | ||
{ | ||
struct crypto_rpi_pico_sha256_data *data = dev->data; | ||
|
||
data->state.locked = false; | ||
bootrom_release_lock(BOOTROM_LOCK_SHA_256); | ||
|
||
return 0; | ||
} | ||
|
||
static DEVICE_API(crypto, crypto_rpi_pico_sha256_crypto_api) = { | ||
.query_hw_caps = crypto_rpi_pico_sha256_query_hw_caps, | ||
.hash_begin_session = crypto_rpi_pico_sha256_hash_begin_session, | ||
.hash_free_session = crypto_rpi_pico_sha256_hash_session_free, | ||
}; | ||
|
||
#define CRYPTO_RPI_PICO_SHA256_INIT(idx) \ | ||
static struct crypto_rpi_pico_sha256_data crypto_rpi_pico_sha256_##idx##_data; \ | ||
DEVICE_DT_INST_DEFINE(idx, NULL, NULL, &crypto_rpi_pico_sha256_##idx##_data, NULL, \ | ||
POST_KERNEL, CONFIG_CRYPTO_INIT_PRIORITY, \ | ||
&crypto_rpi_pico_sha256_crypto_api); | ||
|
||
DT_INST_FOREACH_STATUS_OKAY(CRYPTO_RPI_PICO_SHA256_INIT) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 SHA256 accelerator | ||
|
||
compatible: "raspberrypi,pico-sha256" | ||
|
||
include: base.yaml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sorting - good idea!
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.
nit: I'd prefer it if the sorting is one commit, and the addition is a separate commit. The end result is the same, but it's easier to to see the two orthogonal things going on.