bignum_impl.h 2.7 KB

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