hw_bignum.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (c) 2006-2019, 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. rt_size_t total; /**< Total length of data */
  21. rt_ubase_t *p; /**< pointer to data */
  22. };
  23. struct hwcrypto_bignum_ops
  24. {
  25. rt_err_t (*add)(struct hwcrypto_bignum *bignum_ctx,
  26. struct hw_bignum_mpi *a,
  27. const struct hw_bignum_mpi *b,
  28. const struct hw_bignum_mpi *c); /**< a = b + c */
  29. rt_err_t (*sub)(struct hwcrypto_bignum *bignum_ctx,
  30. struct hw_bignum_mpi *a,
  31. const struct hw_bignum_mpi *b,
  32. const struct hw_bignum_mpi *c); /**< a = b - c */
  33. rt_err_t (*mul)(struct hwcrypto_bignum *bignum_ctx,
  34. struct hw_bignum_mpi *a,
  35. const struct hw_bignum_mpi *b,
  36. const struct hw_bignum_mpi *c); /**< a = b * c */
  37. rt_err_t (*mulmod)(struct hwcrypto_bignum *bignum_ctx,
  38. struct hw_bignum_mpi *a,
  39. const struct hw_bignum_mpi *b,
  40. const struct hw_bignum_mpi *c,
  41. const struct hw_bignum_mpi *d); /**< a = b * c (mod d) */
  42. rt_err_t (*exptmod)(struct hwcrypto_bignum *bignum_ctx,
  43. struct hw_bignum_mpi *a,
  44. const struct hw_bignum_mpi *b,
  45. const struct hw_bignum_mpi *c,
  46. const struct hw_bignum_mpi *d); /**< a = b ^ c (mod d) */
  47. };
  48. /**
  49. * @brief bignum context. Hardware driver usage
  50. */
  51. struct hwcrypto_bignum
  52. {
  53. struct rt_hwcrypto_ctx parent; /**< Inheritance from hardware crypto context */
  54. const struct hwcrypto_bignum_ops *ops; /**< !! Hardware initializes this value when creating context !! */
  55. };
  56. /**
  57. * @brief Setting bignum default devices
  58. *
  59. * @return RT_EOK on success.
  60. */
  61. rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device);
  62. /**
  63. * @brief Allocate memory for bignum
  64. *
  65. * @return Pointer to allocated bignum obj
  66. */
  67. struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void);
  68. /**
  69. * @brief free a bignum obj
  70. *
  71. * @param Pointer to bignum obj
  72. */
  73. void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n);
  74. /**
  75. * @brief Get length of bignum as an unsigned binary buffer
  76. *
  77. * @param n bignum obj
  78. *
  79. * @return binary buffer Length
  80. */
  81. int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n);
  82. /**
  83. * @brief Get length of bignum as an unsigned binary buffer
  84. *
  85. * @param n bignum obj
  86. * @param buf Buffer for the binary number
  87. * @param len Length of the buffer
  88. *
  89. * @return binary buffer Length
  90. */
  91. int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len);
  92. /**
  93. * @brief Set binary buffer to unsigned bignum
  94. *
  95. * @param n bignum obj
  96. * @param buf Buffer for the binary number
  97. * @param len Length of the buffer
  98. *
  99. * @return RT_EOK on success.
  100. */
  101. rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len);
  102. /**
  103. * @brief Unsigned comparison
  104. *
  105. * @param a bignum obj
  106. * @param b bignum obj
  107. *
  108. * @return 0 is equal
  109. */
  110. int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a,
  111. const struct hw_bignum_mpi *b);
  112. /**
  113. * @brief Compare bignum to standard Unsigned integer
  114. *
  115. * @param a bignum obj
  116. * @param b Unsigned integer
  117. *
  118. * @return 0 is equal
  119. */
  120. int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b);
  121. /**
  122. * @brief a = b + c
  123. *
  124. * @param a bignum obj
  125. * @param b bignum obj
  126. * @param c bignum obj
  127. *
  128. * @return RT_EOK on success.
  129. */
  130. rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a,
  131. const struct hw_bignum_mpi *b,
  132. const struct hw_bignum_mpi *c);
  133. /**
  134. * @brief a = b - c
  135. *
  136. * @param a bignum obj
  137. * @param b bignum obj
  138. * @param c bignum obj
  139. *
  140. * @return RT_EOK on success.
  141. */
  142. rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a,
  143. const struct hw_bignum_mpi *b,
  144. const struct hw_bignum_mpi *c);
  145. /**
  146. * @brief a = b * c
  147. *
  148. * @param a bignum obj
  149. * @param b bignum obj
  150. * @param c bignum obj
  151. *
  152. * @return RT_EOK on success.
  153. */
  154. rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a,
  155. const struct hw_bignum_mpi *b,
  156. const struct hw_bignum_mpi *c);
  157. /**
  158. * @brief a = b * c (mod d)
  159. *
  160. * @param a bignum obj
  161. * @param b bignum obj
  162. * @param c bignum obj
  163. *
  164. * @return RT_EOK on success.
  165. */
  166. rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a,
  167. const struct hw_bignum_mpi *b,
  168. const struct hw_bignum_mpi *c,
  169. const struct hw_bignum_mpi *d);
  170. /**
  171. * @brief a = b ^ c (mod d)
  172. *
  173. * @param a bignum obj
  174. * @param b bignum obj
  175. * @param c bignum obj
  176. *
  177. * @return RT_EOK on success.
  178. */
  179. rt_err_t bignum_exptmod(struct hw_bignum_mpi *a,
  180. const struct hw_bignum_mpi *b,
  181. const struct hw_bignum_mpi *c,
  182. const struct hw_bignum_mpi *d);
  183. #ifdef __cplusplus
  184. }
  185. #endif
  186. #endif