| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | /**
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |  * \brief  Multi-precision integer library, ESP32C hardware accelerated parts | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  |  * | 
					
						
							|  |  |  |  *  based on mbedTLS implementation | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
					
						
							|  |  |  |  *  Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd | 
					
						
							|  |  |  |  *  SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
					
						
							|  |  |  |  *  not use this file except in compliance with the License. | 
					
						
							|  |  |  |  *  You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
					
						
							|  |  |  |  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  *  See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  *  limitations under the License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <malloc.h>
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #include <limits.h>
 | 
					
						
							|  |  |  | #include <assert.h>
 | 
					
						
							| 
									
										
										
										
											2016-11-24 08:08:09 +11:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							| 
									
										
										
										
											2018-08-09 15:13:43 +10:00
										 |  |  | #include <sys/param.h>
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | #include "mbedtls/bignum.h"
 | 
					
						
							|  |  |  | #include "esp32s2beta/rom/bigint.h"
 | 
					
						
							|  |  |  | #include "soc/hwcrypto_reg.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #include "esp_system.h"
 | 
					
						
							|  |  |  | #include "esp_log.h"
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | #include "esp_intr.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-26 16:30:43 +08:00
										 |  |  | #include "esp_intr_alloc.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | #include "esp_attr.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | #include "soc/dport_reg.h"
 | 
					
						
							|  |  |  | #include "soc/periph_defs.h"
 | 
					
						
							| 
									
										
										
										
											2018-11-11 15:44:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #include "freertos/FreeRTOS.h"
 | 
					
						
							|  |  |  | #include "freertos/task.h"
 | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | #include "freertos/semphr.h"
 | 
					
						
							| 
									
										
										
										
											2018-08-09 15:13:43 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 16:38:22 +11:00
										 |  |  | static const __attribute__((unused)) char *TAG = "bignum"; | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  | #define ciL    (sizeof(mbedtls_mpi_uint))         /* chars in limb  */
 | 
					
						
							|  |  |  | #define biL    (ciL << 3)                         /* bits  in limb  */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | #if defined(CONFIG_MBEDTLS_MPI_USE_INTERRUPT)
 | 
					
						
							|  |  |  | static SemaphoreHandle_t op_complete_sem; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static IRAM_ATTR void rsa_complete_isr(void *arg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     BaseType_t higher_woken; | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1); | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  |     xSemaphoreGiveFromISR(op_complete_sem, &higher_woken); | 
					
						
							|  |  |  |     if (higher_woken) { | 
					
						
							|  |  |  |         portYIELD_FROM_ISR(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void rsa_isr_initialise() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (op_complete_sem == NULL) { | 
					
						
							|  |  |  |         op_complete_sem = xSemaphoreCreateBinary(); | 
					
						
							| 
									
										
										
										
											2016-11-25 17:33:51 +08:00
										 |  |  |         esp_intr_alloc(ETS_RSA_INTR_SOURCE, 0, rsa_complete_isr, NULL, NULL); | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* CONFIG_MBEDTLS_MPI_USE_INTERRUPT */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | static _lock_t mpi_lock; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 16:38:22 +11:00
										 |  |  | void esp_mpi_acquire_hardware( void ) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     /* newlib locks lazy initialize on ESP-IDF */ | 
					
						
							|  |  |  |     _lock_acquire(&mpi_lock); | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_SET_BIT(DPORT_PERI_CLK_EN_REG, DPORT_CLK_EN_RSA); | 
					
						
							|  |  |  |     /* also clear reset on digital signature & secure boot, otherwise RSA is held in reset */ | 
					
						
							|  |  |  |     DPORT_REG_CLR_BIT(DPORT_PERI_RST_EN_REG, DPORT_RST_EN_RSA | 
					
						
							|  |  |  |                       | DPORT_RST_EN_DIGITAL_SIGNATURE | 
					
						
							|  |  |  |                       | DPORT_RST_EN_SECURE_BOOT); | 
					
						
							| 
									
										
										
										
											2016-11-22 18:09:32 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_CLR_BIT(DPORT_RSA_PD_CTRL_REG, DPORT_RSA_MEM_PD); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while(DPORT_REG_READ(RSA_QUERY_CLEAN_REG) != 1) { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |     // Note: from enabling RSA clock to here takes about 1.3us
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | #ifdef CONFIG_MBEDTLS_MPI_USE_INTERRUPT
 | 
					
						
							|  |  |  |     rsa_isr_initialise(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 16:38:22 +11:00
										 |  |  | void esp_mpi_release_hardware( void ) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-03-22 17:39:59 +05:00
										 |  |  |     DPORT_REG_SET_BIT(DPORT_RSA_PD_CTRL_REG, DPORT_RSA_PD); | 
					
						
							| 
									
										
										
										
											2016-11-22 18:09:32 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     /* don't reset digital signature unit, as this resets AES also */ | 
					
						
							|  |  |  |     DPORT_REG_SET_BIT(DPORT_PERI_RST_EN_REG, DPORT_PERI_EN_RSA); | 
					
						
							|  |  |  |     DPORT_REG_CLR_BIT(DPORT_PERI_CLK_EN_REG, DPORT_PERI_EN_RSA); | 
					
						
							| 
									
										
										
										
											2016-11-22 18:09:32 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  |     _lock_release(&mpi_lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-09 15:13:43 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | /* Return the number of words actually used to represent an mpi
 | 
					
						
							|  |  |  |    number. | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | static size_t mpi_words(const mbedtls_mpi *mpi) | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     for (size_t i = mpi->n; i > 0; i--) { | 
					
						
							|  |  |  |         if (mpi->p[i - 1] != 0) { | 
					
						
							| 
									
										
										
										
											2018-08-09 15:13:43 +10:00
										 |  |  |             return i; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | /* Copy mbedTLS MPI bignum 'mpi' to hardware memory block at 'mem_base'.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |    If num_words is higher than the number of words in the bignum then | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |    these additional words will be zeroed in the memory buffer. | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |    As this function only writes to DPORT memory, no DPORT_STALL_OTHER_CPU_START() | 
					
						
							|  |  |  |    is required. | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | static inline void mpi_to_mem_block(uint32_t mem_base, const mbedtls_mpi *mpi, size_t num_words) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-21 18:08:22 +11:00
										 |  |  |     uint32_t *pbase = (uint32_t *)mem_base; | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     uint32_t copy_words = num_words < mpi->n ? num_words : mpi->n; | 
					
						
							| 
									
										
										
										
											2016-11-21 18:08:22 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Copy MPI data to memory block registers */ | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  |     for (int i = 0; i < copy_words; i++) { | 
					
						
							|  |  |  |         pbase[i] = mpi->p[i]; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-21 18:08:22 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Zero any remaining memory block data */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     for (int i = copy_words; i < num_words; i++) { | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  |         pbase[i] = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-21 18:08:22 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Note: not executing memw here, can do it before we start a bignum operation */ | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Read mbedTLS MPI bignum back from hardware memory block.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Reads num_words words from block. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |    Can return a failure result if fails to grow the MPI result. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Cannot be called inside DPORT_STALL_OTHER_CPU_START() (as may allocate memory). | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | static inline int mem_block_to_mpi(mbedtls_mpi *x, uint32_t mem_base, int num_words) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     int ret = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     MBEDTLS_MPI_CHK( mbedtls_mpi_grow(x, num_words) ); | 
					
						
							| 
									
										
										
										
											2016-10-12 17:08:05 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 18:08:22 +11:00
										 |  |  |     /* Copy data from memory block registers */ | 
					
						
							| 
									
										
										
										
											2018-03-22 17:39:59 +05:00
										 |  |  |     esp_dport_access_read_buffer(x->p, mem_base, num_words); | 
					
						
							| 
									
										
										
										
											2016-10-12 17:08:05 +11:00
										 |  |  |     /* Zero any remaining limbs in the bignum, if the buffer is bigger
 | 
					
						
							|  |  |  |        than num_words */ | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     for(size_t i = num_words; i < x->n; i++) { | 
					
						
							|  |  |  |         x->p[i] = 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     asm volatile ("memw"); | 
					
						
							|  |  |  |  cleanup: | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * There is a need for the value of integer N' such that B^-1(B-1)-N^-1N'=1, | 
					
						
							|  |  |  |  * where B^-1(B-1) mod N=1. Actually, only the least significant part of | 
					
						
							|  |  |  |  * N' is needed, hence the definition N0'=N' mod b. We reproduce below the | 
					
						
							|  |  |  |  * simple algorithm from an article by Dusse and Kaliski to efficiently | 
					
						
							|  |  |  |  * find N0' from N0 and b | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | static mbedtls_mpi_uint modular_inverse(const mbedtls_mpi *M) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     int i; | 
					
						
							|  |  |  |     uint64_t t = 1; | 
					
						
							|  |  |  |     uint64_t two_2_i_minus_1 = 2;   /* 2^(i-1) */ | 
					
						
							|  |  |  |     uint64_t two_2_i = 4;           /* 2^i */ | 
					
						
							|  |  |  |     uint64_t N = M->p[0]; | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     for (i = 2; i <= 32; i++) { | 
					
						
							|  |  |  |         if ((mbedtls_mpi_uint) N * t % two_2_i >= two_2_i_minus_1) { | 
					
						
							|  |  |  |             t += two_2_i_minus_1; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |         two_2_i_minus_1 <<= 1; | 
					
						
							|  |  |  |         two_2_i <<= 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     return (mbedtls_mpi_uint)(UINT32_MAX - t + 1); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | /* Calculate Rinv = RR^2 mod M, where:
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  R = b^n where b = 2^32, n=num_words, | 
					
						
							|  |  |  |  *  R = 2^N (where N=num_bits) | 
					
						
							|  |  |  |  *  RR = R^2 = 2^(2*N) (where N=num_bits=num_words*32) | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This calculation is computationally expensive (mbedtls_mpi_mod_mpi) | 
					
						
							|  |  |  |  * so caller should cache the result where possible. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * DO NOT call this function while holding esp_mpi_acquire_hardware(). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static int calculate_rinv(mbedtls_mpi *Rinv, const mbedtls_mpi *M, int num_words) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     size_t num_bits = num_words * 32; | 
					
						
							|  |  |  |     mbedtls_mpi RR; | 
					
						
							|  |  |  |     mbedtls_mpi_init(&RR); | 
					
						
							|  |  |  |     MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&RR, num_bits * 2, 1)); | 
					
						
							|  |  |  |     MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(Rinv, &RR, M)); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |  cleanup: | 
					
						
							|  |  |  |     mbedtls_mpi_free(&RR); | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | /* Begin an RSA operation. op_reg specifies which 'START' register
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |    to write to. | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    Because the only DPORT operations here are writes, | 
					
						
							|  |  |  |    does not need protecting via DPORT_STALL_OTHER_CPU_START(); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | static inline void start_op(uint32_t op_reg) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  |     /* Clear interrupt status */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1); | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 18:08:22 +11:00
										 |  |  |     /* Note: above REG_WRITE includes a memw, so we know any writes
 | 
					
						
							|  |  |  |        to the memory blocks are also complete. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |     DPORT_REG_WRITE(op_reg, 1); | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | /* Wait for an RSA operation to complete.
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This should NOT be called inside a DPORT_STALL_OTHER_CPU_START(), as it will stall the other CPU for an unacceptably long | 
					
						
							|  |  |  |    period (and - depending on config - may require interrupts enabled). | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | */ | 
					
						
							|  |  |  | static inline void wait_op_complete(uint32_t op_reg) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  | #ifdef CONFIG_MBEDTLS_MPI_USE_INTERRUPT
 | 
					
						
							|  |  |  |     if (!xSemaphoreTake(op_complete_sem, 2000 / portTICK_PERIOD_MS)) { | 
					
						
							|  |  |  |         ESP_LOGE(TAG, "Timed out waiting for RSA operation (op_reg 0x%x int_reg 0x%x)", | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |                  op_reg, DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG)); | 
					
						
							| 
									
										
										
										
											2016-11-18 15:53:00 +11:00
										 |  |  |         abort(); /* indicates a fundamental problem with driver */ | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     while(DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) != 1) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |        { } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* clear the interrupt */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1); | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | /* Z = (X * Y) mod M
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |    Not an mbedTLS function | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | int esp_mpi_mul_mpi_mod(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     int ret; | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     size_t m_words = mpi_words(M); | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     mbedtls_mpi Rinv; | 
					
						
							|  |  |  |     mbedtls_mpi_uint Mprime; | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     size_t num_words = MAX(MAX(m_words, mpi_words(X)), mpi_words(Y)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (num_words * 32 > 4096) { | 
					
						
							|  |  |  |         return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* Calculate and load the first stage montgomery multiplication */ | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     mbedtls_mpi_init(&Rinv); | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     MBEDTLS_MPI_CHK(calculate_rinv(&Rinv, M, m_words)); | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     Mprime = modular_inverse(M); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     esp_mpi_acquire_hardware(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_LENGTH_REG, (num_words - 1)); | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_M_DASH_REG, (uint32_t)Mprime); | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     /* Load M, X, Rinv, Mprime (Mprime is mod 2^32) */ | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_M_BLOCK_BASE, M, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_Y_BLOCK_BASE, Y, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_RB_BLOCK_BASE, &Rinv, num_words); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     /* Execute first stage montgomery multiplication */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     start_op(RSA_MOD_MULT_START_REG); | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     wait_op_complete(RSA_MOD_MULT_START_REG); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     mem_block_to_mpi(Z, RSA_MEM_Z_BLOCK_BASE, m_words); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     esp_mpi_release_hardware(); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |  cleanup: | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     mbedtls_mpi_free(&Rinv); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #if defined(MBEDTLS_MPI_EXP_MOD_ALT)
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |  * Sliding-window exponentiation: Z = X^Y mod M  (HAC 14.85) | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |  * | 
					
						
							|  |  |  |  * _Rinv is optional pre-calculated version of Rinv (via calculate_rinv()). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * (See RSA Accelerator section in Technical Reference for more about Mprime, Rinv) | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | int mbedtls_mpi_exp_mod( mbedtls_mpi* Z, const mbedtls_mpi* X, const mbedtls_mpi* Y, const mbedtls_mpi* M, mbedtls_mpi* _Rinv ) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     size_t x_words = mpi_words(X); | 
					
						
							|  |  |  |     size_t y_words = mpi_words(Y); | 
					
						
							|  |  |  |     size_t m_words = mpi_words(M); | 
					
						
							|  |  |  |     size_t num_words; | 
					
						
							| 
									
										
										
										
											2018-08-09 15:13:43 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     mbedtls_mpi Rinv_new; /* used if _Rinv == NULL */ | 
					
						
							|  |  |  |     mbedtls_mpi *Rinv;    /* points to _Rinv (if not NULL) othwerwise &RR_new */ | 
					
						
							|  |  |  |     mbedtls_mpi_uint Mprime; | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     /* "all numbers must be the same length", so choose longest number
 | 
					
						
							|  |  |  |        as cardinal length of operation... | 
					
						
							|  |  |  |     */ | 
					
						
							|  |  |  |     num_words = MAX(m_words, MAX(x_words, y_words)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (num_words * 32 > 4096) { | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |         return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE; | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     /* Determine RR pointer, either _RR for cached value
 | 
					
						
							|  |  |  |        or local RR_new */ | 
					
						
							|  |  |  |     if (_Rinv == NULL) { | 
					
						
							|  |  |  |         mbedtls_mpi_init(&Rinv_new); | 
					
						
							|  |  |  |         Rinv = &Rinv_new; | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  |     } else { | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |         Rinv = _Rinv; | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     if (Rinv->p == NULL) { | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |         MBEDTLS_MPI_CHK(calculate_rinv(Rinv, M, num_words)); | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     Mprime = modular_inverse(M); | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     esp_mpi_acquire_hardware(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_LENGTH_REG, num_words - 1); | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Load M, X, Rinv, M-prime (M-prime is mod 2^32) */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_Y_BLOCK_BASE, Y, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_M_BLOCK_BASE, M, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_RB_BLOCK_BASE, Rinv, num_words); | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_M_DASH_REG, Mprime); | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_CONSTANT_TIME_DISABLE_REG, 1); | 
					
						
							|  |  |  |     DPORT_REG_WRITE(RSA_SEARCH_OPEN_REG, 1); | 
					
						
							|  |  |  |     DPORT_REG_WRITE(RSA_SEARCH_POS_REG, (y_words * 32) - 1); | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     start_op(RSA_MODEXP_START_REG); | 
					
						
							|  |  |  |     wait_op_complete(RSA_MODEXP_START_REG); | 
					
						
							| 
									
										
										
										
											2018-08-09 15:13:43 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_SEARCH_OPEN_REG, 0); | 
					
						
							|  |  |  |     DPORT_REG_WRITE(RSA_SEARCH_POS_REG, (m_words * 32) - 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ret = mem_block_to_mpi(Z, RSA_MEM_Z_BLOCK_BASE, m_words); | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     esp_mpi_release_hardware(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |  cleanup: | 
					
						
							|  |  |  |     if (_Rinv == NULL) { | 
					
						
							|  |  |  |         mbedtls_mpi_free(&Rinv_new); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-16 20:37:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #endif /* MBEDTLS_MPI_EXP_MOD_ALT */
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #if defined(MBEDTLS_MPI_MUL_MPI_ALT) /* MBEDTLS_MPI_MUL_MPI_ALT */
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | static int mpi_mult_mpi_failover_mod_mult(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t num_words); | 
					
						
							|  |  |  | static int mpi_mult_mpi_overlong(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t Y_bits, size_t words_result); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | /* Z = X * Y */ | 
					
						
							|  |  |  | int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y ) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     size_t bits_x, bits_y, words_x, words_y, words_mult, words_z; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Count words needed for X & Y in hardware */ | 
					
						
							|  |  |  |     bits_x = mbedtls_mpi_bitlen(X); | 
					
						
							|  |  |  |     bits_y = mbedtls_mpi_bitlen(Y); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     words_x = (bits_x + 7) / 8; | 
					
						
							|  |  |  |     words_y = (bits_y + 7) / 8; | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Short-circuit eval if either argument is 0 or 1.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |        This is needed as the mpi modular division | 
					
						
							|  |  |  |        argument will sometimes call in here when one | 
					
						
							|  |  |  |        argument is too large for the hardware unit, but the other | 
					
						
							|  |  |  |        argument is zero or one. | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |        This leaks some timing information, although overall there is a | 
					
						
							|  |  |  |        lot less timing variation than a software MPI approach. | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     if (bits_x == 0 || bits_y == 0) { | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |         mbedtls_mpi_lset(Z, 0); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     if (bits_x == 1) { | 
					
						
							| 
									
										
										
										
											2017-10-28 03:47:00 +03:00
										 |  |  |         ret = mbedtls_mpi_copy(Z, Y); | 
					
						
							|  |  |  |         Z->s *= X->s; | 
					
						
							|  |  |  |         return ret; | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     if (bits_y == 1) { | 
					
						
							| 
									
										
										
										
											2017-10-28 03:47:00 +03:00
										 |  |  |         ret = mbedtls_mpi_copy(Z, X); | 
					
						
							|  |  |  |         Z->s *= Y->s; | 
					
						
							|  |  |  |         return ret; | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     words_mult = (words_x > words_y ? words_x : words_y); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Result Z has to have room for double the larger factor */ | 
					
						
							|  |  |  |     words_z = words_mult * 2; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* If either factor is over 2048 bits, we can't use the standard hardware multiplier
 | 
					
						
							|  |  |  |        (it assumes result is double longest factor, and result is max 4096 bits.) | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |        However, we can fail over to mod_mult for up to 4096 bits of result (modulo | 
					
						
							|  |  |  |        multiplication doesn't have the same restriction, so result is simply the | 
					
						
							|  |  |  |        number of bits in X plus number of bits in in Y.) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  |     */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     if (words_mult * 32 > 2048) { | 
					
						
							|  |  |  |         /* Calculate new length of Z */ | 
					
						
							|  |  |  |         words_z = (bits_x + bits_y + 31) / 32; | 
					
						
							|  |  |  |         if (words_z * 32 <= 4096) { | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |             /* Note: it's possible to use mpi_mult_mpi_overlong
 | 
					
						
							|  |  |  |                for this case as well, but it's very slightly | 
					
						
							|  |  |  |                slower and requires a memory allocation. | 
					
						
							|  |  |  |             */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |             return mpi_mult_mpi_failover_mod_mult(Z, X, Y, words_z); | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |         } else { | 
					
						
							|  |  |  |             /* Still too long for the hardware unit... */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |             if(bits_y > bits_x) { | 
					
						
							|  |  |  |                 return mpi_mult_mpi_overlong(Z, X, Y, bits_y, words_z); | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |                 return mpi_mult_mpi_overlong(Z, Y, X, bits_x, words_z); | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2016-09-20 21:02:07 +10:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* Otherwise, we can use the (faster) multiply hardware unit */ | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     esp_mpi_acquire_hardware(); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* Copy X (right-extended) & Y (left-extended) to memory block */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, words_mult); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_Z_BLOCK_BASE + words_mult * 4, Y, words_mult); | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* NB: as Y is left-extended, we don't zero the bottom words_mult words of Y block.
 | 
					
						
							|  |  |  |        This is OK for now because zeroing is done by hardware when we do esp_mpi_acquire_hardware(). | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_M_DASH_REG, 0); | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_LENGTH_REG, (words_z - 1)); | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  |     start_op(RSA_MULT_START_REG); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     wait_op_complete(RSA_MULT_START_REG); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     /* Read back the result */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     ret = mem_block_to_mpi(Z, RSA_MEM_Z_BLOCK_BASE, words_z); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     Z->s = X->s * Y->s; | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     esp_mpi_release_hardware(); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | /* Special-case of mbedtls_mpi_mult_mpi(), where we use hardware montgomery mod
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |    multiplication to calculate an mbedtls_mpi_mult_mpi result where either | 
					
						
							|  |  |  |    A or B are >2048 bits so can't use the standard multiplication method. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |    Result (A bits + B bits) must still be less than 4096 bits. | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |    This case is simpler than the general case modulo multiply of | 
					
						
							|  |  |  |    esp_mpi_mul_mpi_mod() because we can control the other arguments: | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  |    * Modulus is chosen with M=(2^num_bits - 1) (ie M=R-1), so output | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |    isn't actually modulo anything. | 
					
						
							|  |  |  |    * Mprime and Rinv are therefore predictable as follows: | 
					
						
							|  |  |  |    Mprime = 1 | 
					
						
							|  |  |  |    Rinv = 1 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |    (See RSA Accelerator section in Technical Reference for more about Mprime, Rinv) | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | static int mpi_mult_mpi_failover_mod_mult(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t num_words) | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  | { | 
					
						
							|  |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     /* Load coefficients to hardware */ | 
					
						
							|  |  |  |     esp_mpi_acquire_hardware(); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     /* M = 2^num_words - 1, so block is entirely FF */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     for(int i = 0; i < num_words; i++) { | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |         DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + i * 4, UINT32_MAX); | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     /* Mprime = 1 */ | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_M_DASH_REG, 1); | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_LENGTH_REG, num_words -1); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     /* Load X & Y */ | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_X_BLOCK_BASE, X, num_words); | 
					
						
							|  |  |  |     mpi_to_mem_block(RSA_MEM_Y_BLOCK_BASE, Y, num_words); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     /* Rinv = 1 */ | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |     DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE, 1); | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     for(int i = 1; i < num_words; i++) { | 
					
						
							| 
									
										
										
										
											2017-06-27 20:28:32 +08:00
										 |  |  |         DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + i * 4, 0); | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     start_op(RSA_MOD_MULT_START_REG); | 
					
						
							|  |  |  |     wait_op_complete(RSA_MOD_MULT_START_REG); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     mem_block_to_mpi(Z, RSA_MEM_Z_BLOCK_BASE, num_words); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     esp_mpi_release_hardware(); | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-18 13:44:37 +11:00
										 |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  | /* Deal with the case when X & Y are too long for the hardware unit, by splitting one operand
 | 
					
						
							|  |  |  |    into two halves. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Y must be the longer operand | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Slice Y into Yp, Ypp such that: | 
					
						
							|  |  |  |    Yp = lower 'b' bits of Y | 
					
						
							|  |  |  |    Ypp = upper 'b' bits of Y (right shifted) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Such that | 
					
						
							|  |  |  |    Z = X * Y | 
					
						
							|  |  |  |    Z = X * (Yp + Ypp<<b) | 
					
						
							|  |  |  |    Z = (X * Yp) + (X * Ypp<<b) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Note that this function may recurse multiple times, if both X & Y | 
					
						
							|  |  |  |    are too long for the hardware multiplication unit. | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  | static int mpi_mult_mpi_overlong(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t bits_y, size_t words_result) | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-08-16 12:09:40 +10:00
										 |  |  |     int ret = 0; | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |     mbedtls_mpi Ztemp; | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     const size_t limbs_y = (bits_y + biL - 1) / biL; | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |     /* Rather than slicing in two on bits we slice on limbs (32 bit words) */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     const size_t limbs_slice = limbs_y / 2; | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |     /* Yp holds lower bits of Y (declared to reuse Y's array contents to save on copying) */ | 
					
						
							|  |  |  |     const mbedtls_mpi Yp = { | 
					
						
							|  |  |  |         .p = Y->p, | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |         .n = limbs_slice, | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |         .s = Y->s | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     /* Ypp holds upper bits of Y, right shifted (also reuses Y's array contents) */ | 
					
						
							|  |  |  |     const mbedtls_mpi Ypp = { | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |         .p = Y->p + limbs_slice, | 
					
						
							|  |  |  |         .n = limbs_y - limbs_slice, | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  |         .s = Y->s | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     mbedtls_mpi_init(&Ztemp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Grow Z to result size early, avoid interim allocations */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     mbedtls_mpi_grow(Z, words_result); | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Get result Ztemp = Yp * X (need temporary variable Ztemp) */ | 
					
						
							|  |  |  |     MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi(&Ztemp, X, &Yp) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Z = Ypp * Y */ | 
					
						
							|  |  |  |     MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi(Z, X, &Ypp) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Z = Z << b */ | 
					
						
							| 
									
										
										
										
											2019-06-06 10:57:29 +08:00
										 |  |  |     MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l(Z, limbs_slice * biL) ); | 
					
						
							| 
									
										
										
										
											2017-01-31 12:38:43 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Z += Ztemp */ | 
					
						
							|  |  |  |     MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi(Z, Z, &Ztemp) ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  cleanup: | 
					
						
							|  |  |  |     mbedtls_mpi_free(&Ztemp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 21:24:58 +10:00
										 |  |  | #endif /* MBEDTLS_MPI_MUL_MPI_ALT */
 | 
					
						
							| 
									
										
										
										
											2016-09-08 17:41:43 +08:00
										 |  |  | 
 |