1 /**
2  * \brief  Multi-precision integer library, ESP32 S2 hardware accelerated parts
3  *
4  *  based on mbedTLS implementation
5  *
6  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7  *  Additions Copyright (C) 2016-2020, Espressif Systems (Shanghai) PTE Ltd
8  *  SPDX-License-Identifier: Apache-2.0
9  *
10  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
11  *  not use this file except in compliance with the License.
12  *  You may obtain a copy of the License at
13  *
14  *  http://www.apache.org/licenses/LICENSE-2.0
15  *
16  *  Unless required by applicable law or agreed to in writing, software
17  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  *  See the License for the specific language governing permissions and
20  *  limitations under the License.
21  *
22  */
23 #include "soc/hwcrypto_periph.h"
24 #include "driver/periph_ctrl.h"
25 #include <mbedtls/bignum.h>
26 #include "bignum_impl.h"
27 #include "soc/dport_reg.h"
28 #include "soc/periph_defs.h"
29 #include <sys/param.h>
30 #include "esp_crypto_lock.h"
31 
esp_mpi_hardware_words(size_t words)32 size_t esp_mpi_hardware_words(size_t words)
33 {
34     return words;
35 }
36 
esp_mpi_enable_hardware_hw_op(void)37 void esp_mpi_enable_hardware_hw_op( void )
38 {
39     esp_crypto_mpi_lock_acquire();
40 
41     /* Enable RSA hardware */
42     periph_module_enable(PERIPH_RSA_MODULE);
43 
44     DPORT_REG_CLR_BIT(DPORT_RSA_PD_CTRL_REG, DPORT_RSA_MEM_PD);
45 
46     while (DPORT_REG_READ(RSA_QUERY_CLEAN_REG) != 1) {
47     }
48     // Note: from enabling RSA clock to here takes about 1.3us
49 }
50 
esp_mpi_disable_hardware_hw_op(void)51 void esp_mpi_disable_hardware_hw_op( void )
52 {
53     DPORT_REG_SET_BIT(DPORT_RSA_PD_CTRL_REG, DPORT_RSA_PD);
54 
55     /* Disable RSA hardware */
56     periph_module_disable(PERIPH_RSA_MODULE);
57 
58     esp_crypto_mpi_lock_release();
59 }
60 
61 
62 /* Copy mbedTLS MPI bignum 'mpi' to hardware memory block at 'mem_base'.
63 
64    If num_words is higher than the number of words in the bignum then
65    these additional words will be zeroed in the memory buffer.
66 */
mpi_to_mem_block(uint32_t mem_base,const mbedtls_mpi * mpi,size_t num_words)67 static inline void mpi_to_mem_block(uint32_t mem_base, const mbedtls_mpi *mpi, size_t num_words)
68 {
69     uint32_t *pbase = (uint32_t *)mem_base;
70     uint32_t copy_words = MIN(num_words, mpi->n);
71 
72     /* Copy MPI data to memory block registers */
73     for (uint32_t i = 0; i < copy_words; i++) {
74         pbase[i] = mpi->p[i];
75     }
76 
77     /* Zero any remaining memory block data */
78     for (uint32_t i = copy_words; i < num_words; i++) {
79         pbase[i] = 0;
80     }
81 }
82 
83 /* Read mbedTLS MPI bignum back from hardware memory block.
84 
85    Reads num_words words from block.
86 */
mem_block_to_mpi(mbedtls_mpi * x,uint32_t mem_base,int num_words)87 static inline void mem_block_to_mpi(mbedtls_mpi *x, uint32_t mem_base, int num_words)
88 {
89 
90     /* Copy data from memory block registers */
91     esp_dport_access_read_buffer(x->p, mem_base, num_words);
92     /* Zero any remaining limbs in the bignum, if the buffer is bigger
93        than num_words */
94     for (size_t i = num_words; i < x->n; i++) {
95         x->p[i] = 0;
96     }
97 }
98 
99 
100 
101 /* Begin an RSA operation. op_reg specifies which 'START' register
102    to write to.
103 */
start_op(uint32_t op_reg)104 static inline void start_op(uint32_t op_reg)
105 {
106     /* Clear interrupt status */
107     DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
108 
109     /* Note: above REG_WRITE includes a memw, so we know any writes
110        to the memory blocks are also complete. */
111 
112     DPORT_REG_WRITE(op_reg, 1);
113 }
114 
115 /* Wait for an RSA operation to complete.
116 */
wait_op_complete(void)117 static inline void wait_op_complete(void)
118 {
119     while (DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) != 1)
120     { }
121 
122     /* clear the interrupt */
123     DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
124 }
125 
126 
127 /* Read result from last MPI operation */
esp_mpi_read_result_hw_op(mbedtls_mpi * Z,size_t z_words)128 void esp_mpi_read_result_hw_op(mbedtls_mpi *Z, size_t z_words)
129 {
130     wait_op_complete();
131     mem_block_to_mpi(Z, RSA_MEM_Z_BLOCK_BASE, z_words);
132 }
133 
134 
135 /* Z = (X * Y) mod M
136 
137    Not an mbedTLS function
138 */
esp_mpi_mul_mpi_mod_hw_op(const mbedtls_mpi * X,const mbedtls_mpi * Y,const mbedtls_mpi * M,const mbedtls_mpi * Rinv,mbedtls_mpi_uint Mprime,size_t num_words)139 void esp_mpi_mul_mpi_mod_hw_op(const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, const mbedtls_mpi *Rinv, mbedtls_mpi_uint Mprime, size_t num_words)
140 {
141     DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words - 1));
142 
143     /* Load M, X, Rinv, Mprime (Mprime is mod 2^32) */
144     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words);
145     mpi_to_mem_block(RSA_MEM_Y_BLOCK_BASE, Y, num_words);
146     mpi_to_mem_block(RSA_MEM_M_BLOCK_BASE, M, num_words);
147     mpi_to_mem_block(RSA_MEM_RB_BLOCK_BASE, Rinv, num_words);
148     DPORT_REG_WRITE(RSA_M_DASH_REG, Mprime);
149 
150     start_op(RSA_MOD_MULT_START_REG);
151 }
152 
153 /* Z = (X ^ Y) mod M
154 */
esp_mpi_exp_mpi_mod_hw_op(const mbedtls_mpi * X,const mbedtls_mpi * Y,const mbedtls_mpi * M,const mbedtls_mpi * Rinv,mbedtls_mpi_uint Mprime,size_t num_words)155 void esp_mpi_exp_mpi_mod_hw_op(const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, const mbedtls_mpi *Rinv, mbedtls_mpi_uint Mprime, size_t num_words)
156 {
157     size_t y_bits = mbedtls_mpi_bitlen(Y);
158 
159     DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words - 1));
160 
161     /* Load M, X, Rinv, Mprime (Mprime is mod 2^32) */
162     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words);
163     mpi_to_mem_block(RSA_MEM_Y_BLOCK_BASE, Y, num_words);
164     mpi_to_mem_block(RSA_MEM_M_BLOCK_BASE, M, num_words);
165     mpi_to_mem_block(RSA_MEM_RB_BLOCK_BASE, Rinv, num_words);
166     DPORT_REG_WRITE(RSA_M_DASH_REG, Mprime);
167 
168     /* Enable acceleration options */
169     DPORT_REG_WRITE(RSA_CONSTANT_TIME_REG, 0);
170     DPORT_REG_WRITE(RSA_SEARCH_OPEN_REG, 1);
171     DPORT_REG_WRITE(RSA_SEARCH_POS_REG, y_bits - 1);
172 
173     /* Execute first stage montgomery multiplication */
174     start_op(RSA_MODEXP_START_REG);
175 
176     DPORT_REG_WRITE(RSA_SEARCH_OPEN_REG, 0);
177 }
178 
179 
180 /* Z = X * Y */
esp_mpi_mul_mpi_hw_op(const mbedtls_mpi * X,const mbedtls_mpi * Y,size_t num_words)181 void esp_mpi_mul_mpi_hw_op(const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t num_words)
182 {
183     /* Copy X (right-extended) & Y (left-extended) to memory block */
184     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words);
185     mpi_to_mem_block(RSA_MEM_Z_BLOCK_BASE + num_words * 4, Y, num_words);
186     /* NB: as Y is left-extended, we don't zero the bottom words_mult words of Y block.
187        This is OK for now because zeroing is done by hardware when we do esp_mpi_acquire_hardware().
188     */
189     DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words * 2 - 1));
190     start_op(RSA_MULT_START_REG);
191 }
192 
193 
194 
195 /**
196  * @brief Special-case of (X * Y), where we use hardware montgomery mod
197    multiplication to calculate result where either A or B are >2048 bits so
198    can't use the standard multiplication method.
199  *
200  */
esp_mpi_mult_mpi_failover_mod_mult_hw_op(const mbedtls_mpi * X,const mbedtls_mpi * Y,size_t num_words)201 void esp_mpi_mult_mpi_failover_mod_mult_hw_op(const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t num_words)
202 {
203     /* M = 2^num_words - 1, so block is entirely FF */
204     for (size_t i = 0; i < num_words; i++) {
205         DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + i * 4, UINT32_MAX);
206     }
207 
208     /* Mprime = 1 */
209     DPORT_REG_WRITE(RSA_M_DASH_REG, 1);
210     DPORT_REG_WRITE(RSA_LENGTH_REG, num_words - 1);
211 
212     /* Load X & Y */
213     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words);
214     mpi_to_mem_block(RSA_MEM_Y_BLOCK_BASE, Y, num_words);
215 
216     /* Rinv = 1, write first word */
217     DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE, 1);
218 
219      /* Zero out rest of the Rinv words */
220     for (size_t i = 1; i < num_words; i++) {
221         DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + i * 4, 0);
222     }
223 
224     start_op(RSA_MOD_MULT_START_REG);
225 }
226