1 #ifndef _ESP_BIGNUM_H_ 2 #define _ESP_BIGNUM_H_ 3 4 #include <mbedtls/bignum.h> 5 #include <stdbool.h> 6 7 /* Use montgomery exponentiation (HAC 14.94) for calculating X ^ Y mod M, 8 this may be faster for some targets. The hardware acceleration support for modular 9 exponentiation on the ESP32 is slow for public key operations, so use montgomery 10 exponentiation instead. 11 */ 12 #if CONFIG_IDF_TARGET_ESP32 13 #define ESP_MPI_USE_MONT_EXP 14 #endif 15 16 /** 17 * @brief Enable the MPI hardware and acquire the lock 18 * 19 */ 20 void esp_mpi_enable_hardware_hw_op( void ); 21 22 /** 23 * @brief Disable the MPI hardware and release the lock 24 * 25 */ 26 void esp_mpi_disable_hardware_hw_op( void ); 27 28 /** 29 * @brief Calculate the number of words needed to represent the input word in hardware 30 * 31 * @param words The number of words to be represented 32 * 33 * @return size_t Number of words required 34 */ 35 size_t esp_mpi_hardware_words(size_t words); 36 37 /** 38 * @brief Starts a (X * Y) Mod M calculation in hardware. Rinv and M_prime needs to be precalculated in software. 39 * 40 */ 41 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 hw_words); 42 43 /** 44 * @brief Starts a (X * Y) calculation in hardware. 45 * 46 */ 47 void esp_mpi_mul_mpi_hw_op(const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t num_words); 48 49 /** 50 * @brief Special-case of (X * Y), where we use hardware montgomery mod 51 multiplication to calculate result where either A or B are >2048 bits so 52 can't use the standard multiplication method. 53 * 54 */ 55 void esp_mpi_mult_mpi_failover_mod_mult_hw_op(const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t num_words); 56 57 /** 58 * @brief Read out the result from the previous calculation. 59 * 60 */ 61 void esp_mpi_read_result_hw_op(mbedtls_mpi *Z, size_t z_words); 62 63 #ifdef ESP_MPI_USE_MONT_EXP 64 /** 65 * @brief Starts a montgomery multiplication calculation in hardware 66 * 67 */ 68 int esp_mont_hw_op(mbedtls_mpi* Z, const mbedtls_mpi* X, const mbedtls_mpi* Y, const mbedtls_mpi* M, 69 mbedtls_mpi_uint Mprime, 70 size_t hw_words, 71 bool again); 72 73 #else 74 75 /** 76 * @brief Starts a (X ^ Y) Mod M calculation in hardware. Rinv and M_prime needs to be precalculated in software. 77 * 78 */ 79 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 hw_words); 80 81 #endif //ESP_MPI_USE_MONT_EXP 82 83 #endif 84