|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 TOKITA Hiroshi |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT raspberrypi_pico_sha256 |
| 8 | + |
| 9 | +#include <zephyr/crypto/crypto.h> |
| 10 | +#include <zephyr/kernel.h> |
| 11 | +#include <zephyr/sys/util_macro.h> |
| 12 | +#include <zephyr/sys/byteorder.h> |
| 13 | + |
| 14 | +#include <pico/bootrom/lock.h> |
| 15 | +#include <pico/sha256.h> |
| 16 | + |
| 17 | +#include <zephyr/logging/log.h> |
| 18 | +LOG_MODULE_REGISTER(crypto_rpi_pico_sha256, CONFIG_CRYPTO_LOG_LEVEL); |
| 19 | + |
| 20 | +struct crypto_rpi_pico_sha256_data { |
| 21 | + pico_sha256_state_t state; |
| 22 | + struct k_mutex mutex; |
| 23 | +}; |
| 24 | + |
| 25 | +static int crypto_rpi_pico_sha256_hash_handler(struct hash_ctx *ctx, struct hash_pkt *pkt, |
| 26 | + bool finish) |
| 27 | +{ |
| 28 | + struct crypto_rpi_pico_sha256_data *data = ctx->device->data; |
| 29 | + |
| 30 | + if (!data->state.locked) { |
| 31 | + LOG_ERR("Invalid lock status: unlocked"); |
| 32 | + return -EINVAL; |
| 33 | + } |
| 34 | + |
| 35 | + data->state.cache_used = 0; |
| 36 | + data->state.cache.word = 0; |
| 37 | + data->state.total_data_size = 0; |
| 38 | + |
| 39 | + sha256_err_not_ready_clear(); |
| 40 | + sha256_set_bswap(true); |
| 41 | + sha256_start(); |
| 42 | + |
| 43 | + pico_sha256_update(&data->state, pkt->in_buf, pkt->in_len); |
| 44 | + |
| 45 | + if (!finish) { |
| 46 | + LOG_ERR("Multipart hashing not supported yet"); |
| 47 | + return -ENOTSUP; |
| 48 | + } |
| 49 | + |
| 50 | + pico_sha256_write_padding(&data->state); |
| 51 | + sha256_wait_valid_blocking(); |
| 52 | + |
| 53 | + for (uint i = 0; i < 8; i++) { |
| 54 | + ((uint32_t *)pkt->out_buf)[i] = BSWAP_32((uint32_t)sha256_hw->sum[i]); |
| 55 | + } |
| 56 | + |
| 57 | + return 0; |
| 58 | +} |
| 59 | + |
| 60 | +static int crypto_rpi_pico_sha256_query_hw_caps(const struct device *dev) |
| 61 | +{ |
| 62 | + return CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS; |
| 63 | +} |
| 64 | + |
| 65 | +static int crypto_rpi_pico_sha256_hash_begin_session(const struct device *dev, struct hash_ctx *ctx, |
| 66 | + enum hash_algo algo) |
| 67 | +{ |
| 68 | + struct crypto_rpi_pico_sha256_data *data = dev->data; |
| 69 | + int ret; |
| 70 | + |
| 71 | + ret = k_mutex_lock(&data->mutex, K_FOREVER); |
| 72 | + if (ret != 0) { |
| 73 | + LOG_ERR("Failed to lock mutex: %d", ret); |
| 74 | + return ret; |
| 75 | + } |
| 76 | + |
| 77 | + if (data->state.locked) { |
| 78 | + LOG_DBG("Hash engine busy"); |
| 79 | + ret = -EBUSY; |
| 80 | + goto end; |
| 81 | + } |
| 82 | + |
| 83 | + if (algo != CRYPTO_HASH_ALGO_SHA256) { |
| 84 | + LOG_ERR("Unsupported algo: %d", algo); |
| 85 | + ret = -EINVAL; |
| 86 | + goto end; |
| 87 | + } |
| 88 | + |
| 89 | + if (ctx->flags & ~(crypto_rpi_pico_sha256_query_hw_caps(dev))) { |
| 90 | + LOG_ERR("Unsupported flag %x", ctx->flags); |
| 91 | + ret = -EINVAL; |
| 92 | + goto end; |
| 93 | + } |
| 94 | + |
| 95 | + if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256)) { |
| 96 | + LOG_ERR("bootrom_try_acquire_lock failed"); |
| 97 | + ret = -EBUSY; |
| 98 | + goto end; |
| 99 | + } |
| 100 | + |
| 101 | + data->state.locked = true; |
| 102 | + ctx->hash_hndlr = crypto_rpi_pico_sha256_hash_handler; |
| 103 | + ret = 0; |
| 104 | + |
| 105 | +end: |
| 106 | + k_mutex_unlock(&data->mutex); |
| 107 | + |
| 108 | + return ret; |
| 109 | +} |
| 110 | + |
| 111 | +static int crypto_rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx) |
| 112 | +{ |
| 113 | + struct crypto_rpi_pico_sha256_data *data = dev->data; |
| 114 | + int ret; |
| 115 | + |
| 116 | + ret = k_mutex_lock(&data->mutex, K_FOREVER); |
| 117 | + if (ret != 0) { |
| 118 | + LOG_ERR("Failed to lock mutex: %d", ret); |
| 119 | + return ret; |
| 120 | + } |
| 121 | + |
| 122 | + if (!data->state.locked) { |
| 123 | + LOG_ERR("Invalid lock status: unlocked"); |
| 124 | + ret = -EINVAL; |
| 125 | + goto end; |
| 126 | + } |
| 127 | + |
| 128 | + bootrom_release_lock(BOOTROM_LOCK_SHA_256); |
| 129 | + data->state.locked = false; |
| 130 | + ret = 0; |
| 131 | + |
| 132 | +end: |
| 133 | + k_mutex_unlock(&data->mutex); |
| 134 | + |
| 135 | + return ret; |
| 136 | +} |
| 137 | + |
| 138 | +static int crypto_rpi_pico_sha256_init(const struct device *dev) |
| 139 | +{ |
| 140 | + struct crypto_rpi_pico_sha256_data *data = dev->data; |
| 141 | + |
| 142 | + k_mutex_init(&data->mutex); |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
| 146 | + |
| 147 | +static DEVICE_API(crypto, crypto_rpi_pico_sha256_crypto_api) = { |
| 148 | + .query_hw_caps = crypto_rpi_pico_sha256_query_hw_caps, |
| 149 | + .hash_begin_session = crypto_rpi_pico_sha256_hash_begin_session, |
| 150 | + .hash_free_session = crypto_rpi_pico_sha256_hash_session_free, |
| 151 | +}; |
| 152 | + |
| 153 | +#define CRYPTO_RPI_PICO_SHA256_INIT(idx) \ |
| 154 | + static struct crypto_rpi_pico_sha256_data crypto_rpi_pico_sha256_##idx##_data; \ |
| 155 | + DEVICE_DT_INST_DEFINE(idx, crypto_rpi_pico_sha256_init, NULL, \ |
| 156 | + &crypto_rpi_pico_sha256_##idx##_data, NULL, POST_KERNEL, \ |
| 157 | + CONFIG_CRYPTO_INIT_PRIORITY, &crypto_rpi_pico_sha256_crypto_api); |
| 158 | + |
| 159 | +DT_INST_FOREACH_STATUS_OKAY(CRYPTO_RPI_PICO_SHA256_INIT) |
0 commit comments