1
0

hw_bignum.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2019-04-25 tyx the first version
  9. */
  10. #ifndef __HW_BIGNUM_H__
  11. #define __HW_BIGNUM_H__
  12. #include <hwcrypto.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. struct hwcrypto_bignum;
  17. /* bignum obj */
  18. struct hw_bignum_mpi
  19. {
  20. int sign; /**< integer sign. -1 or 1 */
  21. rt_size_t total; /**< total of limbs */
  22. rt_uint8_t *p; /**< pointer to limbs */
  23. };
  24. struct hwcrypto_bignum_ops
  25. {
  26. rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx,
  27. struct hw_bignum_mpi *x,
  28. const struct hw_bignum_mpi *a,
  29. const struct hw_bignum_mpi *b); /**< x = a + b */
  30. rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx,
  31. struct hw_bignum_mpi *x,
  32. const struct hw_bignum_mpi *a,
  33. const struct hw_bignum_mpi *b); /**< x = a - b */
  34. rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx,
  35. struct hw_bignum_mpi *x,
  36. const struct hw_bignum_mpi *a,
  37. const struct hw_bignum_mpi *b); /**< x = a * b */
  38. rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx,
  39. struct hw_bignum_mpi *x,
  40. const struct hw_bignum_mpi *a,
  41. const struct hw_bignum_mpi *b,
  42. const struct hw_bignum_mpi *c); /**< x = a * b (mod c) */
  43. rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx,
  44. struct hw_bignum_mpi *x,
  45. const struct hw_bignum_mpi *a,
  46. const struct hw_bignum_mpi *b,
  47. const struct hw_bignum_mpi *c); /**< x = a ^ b (mod c) */
  48. };
  49. /**
  50. * @brief bignum context. Hardware driver usage
  51. */
  52. struct hwcrypto_bignum
  53. {
  54. struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */
  55. const struct hwcrypto_bignum_ops *ops; /**< !! Hardware initializes this value when creating context !! */
  56. };
  57. /**
  58. * @brief Setting bignum default devices
  59. *
  60. * @return RT_EOK on success.
  61. */
  62. rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device);
  63. /**
  64. * @brief Init bignum obj
  65. */
  66. void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n);
  67. /**
  68. * @brief free a bignum obj
  69. *
  70. * @param Pointer to bignum obj
  71. */
  72. void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n);
  73. /**
  74. * @brief Get length of bignum as an unsigned binary buffer
  75. *
  76. * @param n bignum obj
  77. *
  78. * @return binary buffer Length
  79. */
  80. int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n);
  81. /**
  82. * @brief Export n into unsigned binary data, big endian
  83. *
  84. * @param n bignum obj
  85. * @param buf Buffer for the binary number
  86. * @param len Length of the buffer
  87. *
  88. * @return export bin length
  89. */
  90. int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len);
  91. /**
  92. * @brief Import n from unsigned binary data, big endian
  93. *
  94. * @param n bignum obj
  95. * @param buf Buffer for the binary number
  96. * @param len Length of the buffer
  97. *
  98. * @return import length.
  99. */
  100. int rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len);
  101. /**
  102. * @brief x = a + b
  103. *
  104. * @param a bignum obj
  105. * @param b bignum obj
  106. * @param c bignum obj
  107. *
  108. * @return RT_EOK on success.
  109. */
  110. rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x,
  111. const struct hw_bignum_mpi *a,
  112. const struct hw_bignum_mpi *b);
  113. /**
  114. * @brief x = a - b
  115. *
  116. * @param a bignum obj
  117. * @param b bignum obj
  118. * @param c bignum obj
  119. *
  120. * @return RT_EOK on success.
  121. */
  122. rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x,
  123. const struct hw_bignum_mpi *a,
  124. const struct hw_bignum_mpi *b);
  125. /**
  126. * @brief x = a * b
  127. *
  128. * @param a bignum obj
  129. * @param b bignum obj
  130. * @param c bignum obj
  131. *
  132. * @return RT_EOK on success.
  133. */
  134. rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x,
  135. const struct hw_bignum_mpi *a,
  136. const struct hw_bignum_mpi *b);
  137. /**
  138. * @brief x = a * b (mod c)
  139. *
  140. * @param a bignum obj
  141. * @param b bignum obj
  142. * @param c bignum obj
  143. *
  144. * @return RT_EOK on success.
  145. */
  146. rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x,
  147. const struct hw_bignum_mpi *a,
  148. const struct hw_bignum_mpi *b,
  149. const struct hw_bignum_mpi *c);
  150. /**
  151. * @brief x = a ^ b (mod c)
  152. *
  153. * @param a bignum obj
  154. * @param b bignum obj
  155. * @param c bignum obj
  156. *
  157. * @return RT_EOK on success.
  158. */
  159. rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x,
  160. const struct hw_bignum_mpi *a,
  161. const struct hw_bignum_mpi *b,
  162. const struct hw_bignum_mpi *c);
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif