|
| 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_spinlock lock; |
| 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 | + k_spinlock_key_t key; |
| 70 | + int ret; |
| 71 | + |
| 72 | + if (data->state.locked) { |
| 73 | + LOG_ERR("Invalid lock status: locked"); |
| 74 | + return -EINVAL; |
| 75 | + } |
| 76 | + |
| 77 | + if (algo != CRYPTO_HASH_ALGO_SHA256) { |
| 78 | + LOG_ERR("Unsupported algo: %d", algo); |
| 79 | + return -EINVAL; |
| 80 | + } |
| 81 | + |
| 82 | + if (ctx->flags & ~(crypto_rpi_pico_sha256_query_hw_caps(dev))) { |
| 83 | + LOG_ERR("Unsupported flag %x", ctx->flags); |
| 84 | + return -EINVAL; |
| 85 | + } |
| 86 | + |
| 87 | + key = k_spin_lock(&data->lock); |
| 88 | + |
| 89 | + ret = bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256); |
| 90 | + if (!ret) { |
| 91 | + LOG_ERR("bootrom_try_acquire_lock failed"); |
| 92 | + k_spin_unlock(&data->lock, key); |
| 93 | + return -EBUSY; |
| 94 | + } |
| 95 | + |
| 96 | + data->state.locked = true; |
| 97 | + |
| 98 | + k_spin_unlock(&data->lock, key); |
| 99 | + |
| 100 | + ctx->hash_hndlr = crypto_rpi_pico_sha256_hash_handler; |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
| 104 | + |
| 105 | +static int crypto_rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx) |
| 106 | +{ |
| 107 | + struct crypto_rpi_pico_sha256_data *data = dev->data; |
| 108 | + k_spinlock_key_t key; |
| 109 | + |
| 110 | + if (!data->state.locked) { |
| 111 | + LOG_ERR("Invalid lock status: unlocked"); |
| 112 | + return -EINVAL; |
| 113 | + } |
| 114 | + |
| 115 | + key = k_spin_lock(&data->lock); |
| 116 | + bootrom_release_lock(BOOTROM_LOCK_SHA_256); |
| 117 | + data->state.locked = false; |
| 118 | + k_spin_unlock(&data->lock, key); |
| 119 | + |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +static DEVICE_API(crypto, crypto_rpi_pico_sha256_crypto_api) = { |
| 124 | + .query_hw_caps = crypto_rpi_pico_sha256_query_hw_caps, |
| 125 | + .hash_begin_session = crypto_rpi_pico_sha256_hash_begin_session, |
| 126 | + .hash_free_session = crypto_rpi_pico_sha256_hash_session_free, |
| 127 | +}; |
| 128 | + |
| 129 | +#define CRYPTO_RPI_PICO_SHA256_INIT(idx) \ |
| 130 | + static struct crypto_rpi_pico_sha256_data crypto_rpi_pico_sha256_##idx##_data; \ |
| 131 | + DEVICE_DT_INST_DEFINE(idx, NULL, NULL, &crypto_rpi_pico_sha256_##idx##_data, NULL, \ |
| 132 | + POST_KERNEL, CONFIG_CRYPTO_INIT_PRIORITY, \ |
| 133 | + &crypto_rpi_pico_sha256_crypto_api); |
| 134 | + |
| 135 | +DT_INST_FOREACH_STATUS_OKAY(CRYPTO_RPI_PICO_SHA256_INIT) |
0 commit comments