From 1616dd5c1b517df6ab3ec41b361c77c0491b0ead Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Mon, 16 Jun 2025 07:40:45 +0900 Subject: [PATCH 1/6] drivers: crypto: sort file entries Add `zephyr-keep-sorted` and reorder file entries alphabetically. Signed-off-by: TOKITA Hiroshi --- drivers/crypto/CMakeLists.txt | 34 +++++++++++++++++++--------------- drivers/crypto/Kconfig | 14 ++++++++------ 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/drivers/crypto/CMakeLists.txt b/drivers/crypto/CMakeLists.txt index 448febf70672..ddf7d372ed8f 100644 --- a/drivers/crypto/CMakeLists.txt +++ b/drivers/crypto/CMakeLists.txt @@ -1,19 +1,23 @@ # 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_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) diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig index 5f3c74ee77f1..405be36b0d1a 100644 --- a/drivers/crypto/Kconfig +++ b/drivers/crypto/Kconfig @@ -47,18 +47,20 @@ config CRYPTO_MBEDTLS_SHIM_MAX_SESSION This can be used to tweak the amount of sessions the driver can handle in parallel. +# zephyr-keep-sorted-start source "drivers/crypto/Kconfig.ataes132a" -source "drivers/crypto/Kconfig.stm32" -source "drivers/crypto/Kconfig.nrf_ecb" +source "drivers/crypto/Kconfig.cc23x0" source "drivers/crypto/Kconfig.intel" -source "drivers/crypto/Kconfig.npcx" -source "drivers/crypto/Kconfig.xec" source "drivers/crypto/Kconfig.it51xxx" source "drivers/crypto/Kconfig.it8xxx2" source "drivers/crypto/Kconfig.mcux_dcp" +source "drivers/crypto/Kconfig.npcx" +source "drivers/crypto/Kconfig.nrf_ecb" +source "drivers/crypto/Kconfig.rts5912" source "drivers/crypto/Kconfig.si32" source "drivers/crypto/Kconfig.smartbond" -source "drivers/crypto/Kconfig.cc23x0" -source "drivers/crypto/Kconfig.rts5912" +source "drivers/crypto/Kconfig.stm32" +source "drivers/crypto/Kconfig.xec" +# zephyr-keep-sorted-stop endif # CRYPTO From da0bf1add589f648854af22c6ad747ea237aa215 Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Mon, 16 Jun 2025 07:22:55 +0900 Subject: [PATCH 2/6] manifest: Update hal_rpi_pico module Update hal_rpi_pico to introduce the Zephyr adaptation for the sha256 driver. Signed-off-by: TOKITA Hiroshi --- west.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/west.yml b/west.yml index 3c57f452369a..b4c060609be5 100644 --- a/west.yml +++ b/west.yml @@ -231,7 +231,7 @@ manifest: - hal - name: hal_rpi_pico path: modules/hal/rpi_pico - revision: 7b57b24588797e6e7bf18b6bda168e6b96374264 + revision: pull/13/head groups: - hal - name: hal_silabs From 8bb2bed6d4d85e1e298a21f05bff3130e7df75d6 Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Mon, 3 Feb 2025 08:20:07 +0900 Subject: [PATCH 3/6] modules: hal_rpi_pico: Add sha256 driver Add include paths and source files for the sha256 driver when enabled `CONFIG_PICOSDK_USE_SHA256` config. Signed-off-by: TOKITA Hiroshi --- modules/hal_rpi_pico/CMakeLists.txt | 6 ++++++ modules/hal_rpi_pico/Kconfig | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/modules/hal_rpi_pico/CMakeLists.txt b/modules/hal_rpi_pico/CMakeLists.txt index 046660a929a3..e857e06a6061 100644 --- a/modules/hal_rpi_pico/CMakeLists.txt +++ b/modules/hal_rpi_pico/CMakeLists.txt @@ -155,6 +155,12 @@ if(CONFIG_HAS_RPI_PICO) zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_CLAIM ${common_dir}/hardware_claim/include) + zephyr_library_sources_ifdef(CONFIG_PICOSDK_USE_SHA256 + ${rp2_common_dir}/pico_sha256/sha256.c) + zephyr_include_directories_ifdef(CONFIG_PICOSDK_USE_SHA256 + ${rp2_common_dir}/hardware_sha256/include + ${rp2_common_dir}/pico_sha256/include) + zephyr_library_sources_ifdef(CONFIG_SOC_SERIES_RP2350 ${rp2_common_dir}/pico_runtime_init/runtime_init.c) zephyr_include_directories_ifdef(CONFIG_SOC_SERIES_RP2350 diff --git a/modules/hal_rpi_pico/Kconfig b/modules/hal_rpi_pico/Kconfig index 3c37c84f4c01..f79f786987a2 100644 --- a/modules/hal_rpi_pico/Kconfig +++ b/modules/hal_rpi_pico/Kconfig @@ -54,3 +54,8 @@ config PICOSDK_USE_RTC bool help Use the RTC driver from pico-sdk + +config PICOSDK_USE_SHA256 + bool + help + Use the SHA256 driver and utilities from pico-sdk From 91f721bc82cadbd07ce01b5e647828ba6d3927ac Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 2 Feb 2025 18:25:55 +0900 Subject: [PATCH 4/6] drivers: crypto: Add initial support for rpi_pico sha256 accelerator Add basic support for RaspberryPi Pico's SHA256 hardware accelerator. Signed-off-by: TOKITA Hiroshi --- drivers/crypto/CMakeLists.txt | 1 + drivers/crypto/Kconfig | 1 + drivers/crypto/Kconfig.rpi_pico | 10 ++ drivers/crypto/crypto_rpi_pico_sha256.c | 121 ++++++++++++++++++ dts/arm/raspberrypi/rpi_pico/rp2350.dtsi | 6 + .../crypto/raspberrypi,pico-sha256.yaml | 8 ++ 6 files changed, 147 insertions(+) create mode 100644 drivers/crypto/Kconfig.rpi_pico create mode 100644 drivers/crypto/crypto_rpi_pico_sha256.c create mode 100644 dts/bindings/crypto/raspberrypi,pico-sha256.yaml diff --git a/drivers/crypto/CMakeLists.txt b/drivers/crypto/CMakeLists.txt index ddf7d372ed8f..046f937a3247 100644 --- a/drivers/crypto/CMakeLists.txt +++ b/drivers/crypto/CMakeLists.txt @@ -14,6 +14,7 @@ zephyr_library_sources_ifdef(CONFIG_CRYPTO_MCHP_XEC_SYMCR crypto_mchp_xec_ 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) diff --git a/drivers/crypto/Kconfig b/drivers/crypto/Kconfig index 405be36b0d1a..61acefa73581 100644 --- a/drivers/crypto/Kconfig +++ b/drivers/crypto/Kconfig @@ -56,6 +56,7 @@ source "drivers/crypto/Kconfig.it8xxx2" source "drivers/crypto/Kconfig.mcux_dcp" source "drivers/crypto/Kconfig.npcx" source "drivers/crypto/Kconfig.nrf_ecb" +source "drivers/crypto/Kconfig.rpi_pico" source "drivers/crypto/Kconfig.rts5912" source "drivers/crypto/Kconfig.si32" source "drivers/crypto/Kconfig.smartbond" diff --git a/drivers/crypto/Kconfig.rpi_pico b/drivers/crypto/Kconfig.rpi_pico new file mode 100644 index 000000000000..09177b264958 --- /dev/null +++ b/drivers/crypto/Kconfig.rpi_pico @@ -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. diff --git a/drivers/crypto/crypto_rpi_pico_sha256.c b/drivers/crypto/crypto_rpi_pico_sha256.c new file mode 100644 index 000000000000..c67b1ec13d90 --- /dev/null +++ b/drivers/crypto/crypto_rpi_pico_sha256.c @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2025 TOKITA Hiroshi + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define DT_DRV_COMPAT raspberrypi_pico_sha256 + +#include +#include +#include +#include + +#include +#include + +#include +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) diff --git a/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi b/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi index 62da179b6679..ca3eb3a25b3d 100644 --- a/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi +++ b/dts/arm/raspberrypi/rpi_pico/rp2350.dtsi @@ -398,6 +398,12 @@ status = "disabled"; }; + sha256: sha256@400f8000 { + compatible = "raspberrypi,pico-sha256"; + reg = <0x400f8000 0x200>; + status = "disabled"; + }; + dma: dma@50000000 { compatible = "raspberrypi,pico-dma"; reg = <0x50000000 DT_SIZE_K(64)>; diff --git a/dts/bindings/crypto/raspberrypi,pico-sha256.yaml b/dts/bindings/crypto/raspberrypi,pico-sha256.yaml new file mode 100644 index 000000000000..852795a80bc7 --- /dev/null +++ b/dts/bindings/crypto/raspberrypi,pico-sha256.yaml @@ -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 From 44108a5a173ef1c1c542469df2933ca89417f61f Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 2 Feb 2025 19:14:54 +0900 Subject: [PATCH 5/6] boards: raspberrypi: rpi_pico2: Add sha256 accelerator Added sha256 accelerator to rpi_pico. Signed-off-by: TOKITA Hiroshi --- boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi | 4 ++++ boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml | 1 + 2 files changed, 5 insertions(+) diff --git a/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi b/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi index 522413c94f1a..2e5a63fd7c00 100644 --- a/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi +++ b/boards/raspberrypi/rpi_pico2/rpi_pico2.dtsi @@ -141,6 +141,10 @@ gpio0_lo: &gpio0 { status = "okay"; }; +&sha256 { + status = "okay"; +}; + zephyr_udc0: &usbd { status = "okay"; }; diff --git a/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml b/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml index f83082393e07..7cb25485b308 100644 --- a/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml +++ b/boards/raspberrypi/rpi_pico2/rpi_pico2_rp2350a_m33.yaml @@ -11,6 +11,7 @@ supported: - adc - clock - counter + - crypto - dma - gpio - hwinfo From 236fd9d0d74f07f39ba3a44db26cdd2adcca139c Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 2 Feb 2025 19:15:29 +0900 Subject: [PATCH 6/6] tests: crypto: crypto_hash: Add rpi_pico2 to test target Add rpi_pico2 to the crypto_hash test target. Signed-off-by: TOKITA Hiroshi --- tests/crypto/crypto_hash/src/main.c | 2 ++ tests/crypto/crypto_hash/testcase.yaml | 1 + 2 files changed, 3 insertions(+) diff --git a/tests/crypto/crypto_hash/src/main.c b/tests/crypto/crypto_hash/src/main.c index ea1229dda6df..ed8803f5211a 100644 --- a/tests/crypto/crypto_hash/src/main.c +++ b/tests/crypto/crypto_hash/src/main.c @@ -13,6 +13,8 @@ #define CRYPTO_DRV_NAME CONFIG_CRYPTO_MBEDTLS_SHIM_DRV_NAME #elif DT_HAS_COMPAT_STATUS_OKAY(renesas_smartbond_crypto) #define CRYPTO_DEV_COMPAT renesas_smartbond_crypto +#elif DT_HAS_COMPAT_STATUS_OKAY(raspberrypi_pico_sha256) +#define CRYPTO_DEV_COMPAT raspberrypi_pico_sha256 #else #error "You need to enable one crypto device" #endif diff --git a/tests/crypto/crypto_hash/testcase.yaml b/tests/crypto/crypto_hash/testcase.yaml index 46436f7cd9b5..080629fcf27a 100644 --- a/tests/crypto/crypto_hash/testcase.yaml +++ b/tests/crypto/crypto_hash/testcase.yaml @@ -2,6 +2,7 @@ tests: crypto.hash: platform_allow: - native_sim + - rpi_pico2/rp2350a/m33 integration_platforms: - native_sim extra_args: EXTRA_CONF_FILE=prj_mtls_shim.conf