|
| 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/sys/byteorder.h> |
| 11 | + |
| 12 | +#include <hardware/sha256.h> |
| 13 | +#include <pico/bootrom/lock.h> |
| 14 | + |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +LOG_MODULE_REGISTER(sha_rpi_pico_sha256, CONFIG_CRYPTO_LOG_LEVEL); |
| 17 | + |
| 18 | +/* |
| 19 | + * This is based on the pico_sha256 implementation in the Pico-SDK. |
| 20 | + */ |
| 21 | + |
| 22 | +/* We add one 0x80 byte, then 8 bytes for the size */ |
| 23 | +#define SHA256_PADDING_DATA_BYTES 9 |
| 24 | +#define SHA256_BLOCK_SIZE_BYTES 64 |
| 25 | + |
| 26 | +struct rpi_pico_sha256_data { |
| 27 | + bool locked; |
| 28 | + uint8_t cache_used; |
| 29 | + union { |
| 30 | + uint32_t word; |
| 31 | + uint8_t bytes[4]; |
| 32 | + } cache; |
| 33 | + size_t total_data_size; |
| 34 | +}; |
| 35 | + |
| 36 | +static void write_to_hardware(struct rpi_pico_sha256_data *state, const uint8_t *data, |
| 37 | + size_t data_size_bytes) |
| 38 | +{ |
| 39 | + if (!state->cache_used && !(((uintptr_t)data) & 3u)) { |
| 40 | + /* aligned writes */ |
| 41 | + while (data_size_bytes >= 4) { |
| 42 | + /* write a whole word */ |
| 43 | + sha256_wait_ready_blocking(); |
| 44 | + sha256_put_word(*(const uint32_t *)data); |
| 45 | + data += 4; |
| 46 | + data_size_bytes -= 4; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + while (data_size_bytes--) { |
| 51 | + state->cache.bytes[state->cache_used++] = *data++; |
| 52 | + if (state->cache_used == 4) { |
| 53 | + state->cache_used = 0; |
| 54 | + sha256_wait_ready_blocking(); |
| 55 | + sha256_put_word(state->cache.word); |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +static inline size_t bytes_to_write(struct rpi_pico_sha256_data *state, size_t data_size_bytes) |
| 61 | +{ |
| 62 | + return ROUND_UP(state->total_data_size, SHA256_BLOCK_SIZE_BYTES) - state->total_data_size; |
| 63 | +} |
| 64 | + |
| 65 | +static void update_internal(struct rpi_pico_sha256_data *state, const uint8_t *data, |
| 66 | + size_t data_size_bytes) |
| 67 | +{ |
| 68 | + /* must finish off the last 64 byte block first or else sha256_err_not_ready will be true */ |
| 69 | + const size_t bytes_left = MIN(bytes_to_write(state, data_size_bytes), data_size_bytes); |
| 70 | + |
| 71 | + if (bytes_left > 0) { |
| 72 | + write_to_hardware(state, data, bytes_left); |
| 73 | + state->total_data_size += bytes_left; |
| 74 | + data_size_bytes -= bytes_left; |
| 75 | + data += bytes_left; |
| 76 | + } |
| 77 | + |
| 78 | + /* Write the rest of the data */ |
| 79 | + if (data_size_bytes > 0) { |
| 80 | + write_to_hardware(state, data, data_size_bytes); |
| 81 | + state->total_data_size += data_size_bytes; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +static void add_zero_bytes(struct rpi_pico_sha256_data *state, size_t data_size_bytes) |
| 86 | +{ |
| 87 | + const uint8_t zero[] = {0, 0, 0, 0}; |
| 88 | + |
| 89 | + while ((int32_t)data_size_bytes > 0) { |
| 90 | + update_internal(state, zero, MIN(4, data_size_bytes)); |
| 91 | + data_size_bytes -= 4; |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +static void write_padding(struct rpi_pico_sha256_data *state) |
| 96 | +{ |
| 97 | + /* Has to be a multiple of 64 bytes */ |
| 98 | + |
| 99 | + uint64_t size = ROUND_UP(state->total_data_size + SHA256_PADDING_DATA_BYTES, |
| 100 | + SHA256_BLOCK_SIZE_BYTES); |
| 101 | + const size_t user_data_size = state->total_data_size; |
| 102 | + const size_t padding_size_bytes = size - state->total_data_size; |
| 103 | + const uint8_t one_bit = 0x80; |
| 104 | + |
| 105 | + /* append a single '1' bit */ |
| 106 | + update_internal(state, &one_bit, 1); |
| 107 | + |
| 108 | + /* Zero unused padding */ |
| 109 | + add_zero_bytes(state, padding_size_bytes - SHA256_PADDING_DATA_BYTES); |
| 110 | + |
| 111 | + /* Add size in bits, big endian */ |
| 112 | + size = BSWAP_64((uint64_t)(user_data_size * 8)); |
| 113 | + update_internal(state, (uint8_t *)&size, sizeof(uint64_t)); /* last write */ |
| 114 | +} |
| 115 | + |
| 116 | +static int rpi_pico_sha256_hash_handler(struct hash_ctx *ctx, struct hash_pkt *pkt, bool finish) |
| 117 | +{ |
| 118 | + struct rpi_pico_sha256_data *data = ctx->device->data; |
| 119 | + |
| 120 | + assert(data->locked); |
| 121 | + |
| 122 | + data->cache_used = 0; |
| 123 | + data->cache.word = 0; |
| 124 | + data->total_data_size = 0; |
| 125 | + |
| 126 | + sha256_err_not_ready_clear(); |
| 127 | + sha256_set_bswap(true); |
| 128 | + sha256_start(); |
| 129 | + |
| 130 | + update_internal(data, pkt->in_buf, pkt->in_len); |
| 131 | + |
| 132 | + if (!finish) { |
| 133 | + return 0; |
| 134 | + } |
| 135 | + |
| 136 | + write_padding(data); |
| 137 | + sha256_wait_valid_blocking(); |
| 138 | + |
| 139 | + for (uint i = 0; i < 8; i++) { |
| 140 | + ((uint32_t *)pkt->out_buf)[i] = BSWAP_32((uint32_t)sha256_hw->sum[i]); |
| 141 | + } |
| 142 | + |
| 143 | + return 0; |
| 144 | +} |
| 145 | + |
| 146 | +static int rpi_pico_sha256_query_hw_caps(const struct device *dev) |
| 147 | +{ |
| 148 | + return (CAP_SEPARATE_IO_BUFS | CAP_SYNC_OPS); |
| 149 | +} |
| 150 | + |
| 151 | +static int rpi_pico_sha256_hash_begin_session(const struct device *dev, struct hash_ctx *ctx, |
| 152 | + enum hash_algo algo) |
| 153 | +{ |
| 154 | + struct rpi_pico_sha256_data *data = dev->data; |
| 155 | + |
| 156 | + assert(!data->locked); |
| 157 | + |
| 158 | + if (algo != CRYPTO_HASH_ALGO_SHA256) { |
| 159 | + LOG_ERR("Unsupported algo"); |
| 160 | + return -EINVAL; |
| 161 | + } |
| 162 | + |
| 163 | + if (ctx->flags & ~(rpi_pico_sha256_query_hw_caps(dev))) { |
| 164 | + LOG_ERR("Unsupported flag"); |
| 165 | + return -EINVAL; |
| 166 | + } |
| 167 | + |
| 168 | + if (!bootrom_try_acquire_lock(BOOTROM_LOCK_SHA_256)) { |
| 169 | + return -EBUSY; |
| 170 | + } |
| 171 | + |
| 172 | + data->locked = true; |
| 173 | + ctx->hash_hndlr = rpi_pico_sha256_hash_handler; |
| 174 | + |
| 175 | + return 0; |
| 176 | +} |
| 177 | + |
| 178 | +static int rpi_pico_sha256_hash_session_free(const struct device *dev, struct hash_ctx *ctx) |
| 179 | +{ |
| 180 | + struct rpi_pico_sha256_data *data = dev->data; |
| 181 | + |
| 182 | + assert(data->locked); |
| 183 | + |
| 184 | + bootrom_release_lock(BOOTROM_LOCK_SHA_256); |
| 185 | + data->locked = false; |
| 186 | + |
| 187 | + return 0; |
| 188 | +} |
| 189 | + |
| 190 | +static DEVICE_API(crypto, rpi_pico_sha256_crypto_api) = { |
| 191 | + .query_hw_caps = rpi_pico_sha256_query_hw_caps, |
| 192 | + .hash_begin_session = rpi_pico_sha256_hash_begin_session, |
| 193 | + .hash_free_session = rpi_pico_sha256_hash_session_free, |
| 194 | +}; |
| 195 | + |
| 196 | +#define RPI_PICO_SHA256_INIT(idx) \ |
| 197 | + static struct rpi_pico_sha256_data rpi_pico_sha256_##idx##_data; \ |
| 198 | + DEVICE_DT_INST_DEFINE(idx, NULL, NULL, &rpi_pico_sha256_##idx##_data, NULL, POST_KERNEL, \ |
| 199 | + CONFIG_CRYPTO_INIT_PRIORITY, &rpi_pico_sha256_crypto_api); |
| 200 | + |
| 201 | +DT_INST_FOREACH_STATUS_OKAY(RPI_PICO_SHA256_INIT) |
0 commit comments