|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#include <stdbool.h> |
| 9 | +#include <string.h> |
| 10 | +#include <sys/param.h> |
| 11 | +#include "hal/assert.h" |
| 12 | +#include "hal/mpi_types.h" |
| 13 | +#include "soc/pcr_reg.h" |
| 14 | +#include "soc/pcr_struct.h" |
| 15 | +#include "soc/rsa_reg.h" |
| 16 | +#include "soc/mpi_periph.h" |
| 17 | + |
| 18 | +#ifdef __cplusplus |
| 19 | +extern "C" { |
| 20 | +#endif |
| 21 | + |
| 22 | + |
| 23 | +/** |
| 24 | + * @brief Enable the bus clock for MPI peripheral module |
| 25 | + * |
| 26 | + * @param enable true to enable the module, false to disable the module |
| 27 | + */ |
| 28 | +static inline void mpi_ll_enable_bus_clock(bool enable) |
| 29 | +{ |
| 30 | + PCR.rsa_conf.rsa_clk_en = enable; |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @brief Reset the MPI peripheral module |
| 35 | + */ |
| 36 | +static inline void mpi_ll_reset_register(void) |
| 37 | +{ |
| 38 | + PCR.rsa_conf.rsa_rst_en = 1; |
| 39 | + PCR.rsa_conf.rsa_rst_en = 0; |
| 40 | + |
| 41 | + // Clear reset on digital signature also, otherwise RSA is held in reset |
| 42 | + PCR.ds_conf.ds_rst_en = 0; |
| 43 | + PCR.ecdsa_conf.ecdsa_rst_en = 0; |
| 44 | +} |
| 45 | + |
| 46 | +static inline size_t mpi_ll_calculate_hardware_words(size_t words) |
| 47 | +{ |
| 48 | + return words; |
| 49 | +} |
| 50 | + |
| 51 | +static inline void mpi_ll_power_up(void) |
| 52 | +{ |
| 53 | + /* Power up the MPI peripheral */ |
| 54 | + REG_CLR_BIT(PCR_RSA_PD_CTRL_REG, PCR_RSA_MEM_PD); |
| 55 | + REG_CLR_BIT(PCR_RSA_PD_CTRL_REG, PCR_RSA_MEM_FORCE_PD); |
| 56 | +} |
| 57 | + |
| 58 | +static inline void mpi_ll_power_down(void) |
| 59 | +{ |
| 60 | + /* Power down the MPI peripheral */ |
| 61 | + REG_CLR_BIT(PCR_RSA_PD_CTRL_REG, PCR_RSA_MEM_FORCE_PU); |
| 62 | + REG_SET_BIT(PCR_RSA_PD_CTRL_REG, PCR_RSA_MEM_PD); |
| 63 | +} |
| 64 | + |
| 65 | +static inline void mpi_ll_enable_interrupt(void) |
| 66 | +{ |
| 67 | + REG_WRITE(RSA_INT_ENA_REG, 1); |
| 68 | +} |
| 69 | + |
| 70 | +static inline void mpi_ll_disable_interrupt(void) |
| 71 | +{ |
| 72 | + REG_WRITE(RSA_INT_ENA_REG, 0); |
| 73 | +} |
| 74 | + |
| 75 | +static inline void mpi_ll_clear_interrupt(void) |
| 76 | +{ |
| 77 | + REG_WRITE(RSA_INT_CLR_REG, 1); |
| 78 | +} |
| 79 | + |
| 80 | +static inline bool mpi_ll_check_memory_init_complete(void) |
| 81 | +{ |
| 82 | + return REG_READ(RSA_QUERY_CLEAN_REG) == 0; |
| 83 | +} |
| 84 | + |
| 85 | +static inline void mpi_ll_start_op(mpi_op_t op) |
| 86 | +{ |
| 87 | + REG_WRITE(MPI_OPERATIONS_REG[op], 1); |
| 88 | +} |
| 89 | + |
| 90 | +static inline bool mpi_ll_get_int_status(void) |
| 91 | +{ |
| 92 | + return REG_READ(RSA_QUERY_IDLE_REG) == 0; |
| 93 | +} |
| 94 | + |
| 95 | +/* Copy MPI bignum (p) to hardware memory block at 'mem_base'. |
| 96 | +
|
| 97 | + If num_words is higher than the number of words (n) in the bignum then |
| 98 | + these additional words will be zeroed in the memory buffer. |
| 99 | +*/ |
| 100 | +static inline void mpi_ll_write_to_mem_block(mpi_param_t param, size_t offset, const uint32_t* p, size_t n, size_t num_words) |
| 101 | +{ |
| 102 | + uint32_t mem_base = MPI_BLOCK_BASES[param] + offset; |
| 103 | + uint32_t* pbase = (uint32_t*) mem_base; |
| 104 | + uint32_t copy_words = MIN(num_words, n); |
| 105 | + |
| 106 | + /* Copy MPI data to memory block registers */ |
| 107 | + for (int i = 0; i < copy_words; i++) { |
| 108 | + pbase[i] = p[i]; |
| 109 | + } |
| 110 | + |
| 111 | + /* Zero any remaining memory block data */ |
| 112 | + for (int i = copy_words; i < num_words; i++) { |
| 113 | + pbase[i] = 0; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +static inline void mpi_ll_write_m_prime(uint32_t Mprime) |
| 118 | +{ |
| 119 | + REG_WRITE(RSA_M_PRIME_REG, Mprime); |
| 120 | +} |
| 121 | + |
| 122 | +static inline void mpi_ll_write_rinv(uint32_t rinv) |
| 123 | +{ |
| 124 | + REG_WRITE(MPI_BLOCK_BASES[MPI_PARAM_Z], rinv); |
| 125 | +} |
| 126 | + |
| 127 | +static inline void mpi_ll_write_at_offset(mpi_param_t param, int offset, uint32_t value) |
| 128 | +{ |
| 129 | + uint32_t mem_base = MPI_BLOCK_BASES[param] + offset; |
| 130 | + REG_WRITE(mem_base, value); |
| 131 | +} |
| 132 | + |
| 133 | +/* Read MPI bignum (p) back from hardware memory block. |
| 134 | +
|
| 135 | + Reads z_words words from block. |
| 136 | +*/ |
| 137 | +static inline void mpi_ll_read_from_mem_block(uint32_t* p, size_t n, size_t num_words) |
| 138 | +{ |
| 139 | + uint32_t mem_base = MPI_BLOCK_BASES[MPI_PARAM_Z]; |
| 140 | + /* Copy data from memory block registers */ |
| 141 | + const size_t REG_WIDTH = sizeof(uint32_t); |
| 142 | + for (size_t i = 0; i < num_words; i++) { |
| 143 | + p[i] = REG_READ(mem_base + (i * REG_WIDTH)); |
| 144 | + } |
| 145 | + /* Zero any remaining limbs in the bignum, if the buffer is bigger |
| 146 | + than num_words */ |
| 147 | + for (size_t i = num_words; i < n; i++) { |
| 148 | + p[i] = 0; |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +static inline void mpi_ll_set_mode(size_t length) |
| 153 | +{ |
| 154 | + REG_WRITE(RSA_MODE_REG, length); |
| 155 | +} |
| 156 | + |
| 157 | +static inline void mpi_ll_disable_constant_time(void) |
| 158 | +{ |
| 159 | + REG_WRITE(RSA_CONSTANT_TIME_REG, 0); |
| 160 | +} |
| 161 | + |
| 162 | +static inline void mpi_ll_enable_constant_time(void) |
| 163 | +{ |
| 164 | + REG_WRITE(RSA_CONSTANT_TIME_REG, 1); |
| 165 | +} |
| 166 | + |
| 167 | +static inline void mpi_ll_disable_search(void) |
| 168 | +{ |
| 169 | + REG_WRITE(RSA_SEARCH_ENABLE_REG, 0); |
| 170 | +} |
| 171 | + |
| 172 | +static inline void mpi_ll_enable_search(void) |
| 173 | +{ |
| 174 | + REG_WRITE(RSA_SEARCH_ENABLE_REG, 1); |
| 175 | +} |
| 176 | + |
| 177 | +static inline void mpi_ll_set_search_position(size_t pos) |
| 178 | +{ |
| 179 | + REG_WRITE(RSA_SEARCH_POS_REG, pos); |
| 180 | +} |
| 181 | + |
| 182 | +#ifdef __cplusplus |
| 183 | +} |
| 184 | +#endif |
0 commit comments