hw_bignum.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (c) 2006-2021, 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. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <hw_bignum.h>
  13. static struct rt_hwcrypto_ctx *bignum_default;
  14. rt_inline rt_err_t hwcrypto_bignum_dev_is_init(void)
  15. {
  16. struct rt_hwcrypto_device *dev;
  17. if (bignum_default)
  18. {
  19. return RT_EOK;
  20. }
  21. dev = rt_hwcrypto_dev_default();
  22. if (dev == RT_NULL)
  23. {
  24. return -RT_ERROR;
  25. }
  26. return rt_hwcrypto_bignum_default(dev);
  27. }
  28. /**
  29. * @brief Setting bignum default devices
  30. *
  31. * @return RT_EOK on success.
  32. */
  33. rt_err_t rt_hwcrypto_bignum_default(struct rt_hwcrypto_device *device)
  34. {
  35. if (bignum_default)
  36. {
  37. rt_hwcrypto_ctx_destroy(bignum_default);
  38. bignum_default = RT_NULL;
  39. }
  40. if (device == RT_NULL)
  41. {
  42. return RT_EOK;
  43. }
  44. bignum_default = rt_hwcrypto_ctx_create(device, HWCRYPTO_TYPE_BIGNUM, sizeof(struct hwcrypto_bignum));
  45. if (bignum_default == RT_NULL)
  46. {
  47. return -RT_ERROR;
  48. }
  49. return RT_EOK;
  50. }
  51. /**
  52. * @brief Init bignum obj
  53. *
  54. * @param n bignum obj
  55. */
  56. void rt_hwcrypto_bignum_init(struct hw_bignum_mpi *n)
  57. {
  58. if(n == RT_NULL)
  59. return;
  60. n->sign = 1;
  61. n->total = 0;
  62. n->p = RT_NULL;
  63. }
  64. /**
  65. * @brief free a bignum obj
  66. *
  67. * @param Pointer to bignum obj
  68. */
  69. void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n)
  70. {
  71. if (n)
  72. {
  73. rt_free(n->p);
  74. n->sign = 0;
  75. n->total = 0;
  76. n->p = RT_NULL;
  77. }
  78. }
  79. /**
  80. * @brief Get length of bignum as an unsigned binary buffer
  81. *
  82. * @param n bignum obj
  83. *
  84. * @return binary buffer length
  85. */
  86. int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n)
  87. {
  88. int tmp_len, total;
  89. if (n == RT_NULL || n->p == RT_NULL)
  90. {
  91. return 0;
  92. }
  93. tmp_len = 0;
  94. total = n->total;
  95. while ((total > 0) && (n->p[total - 1] == 0))
  96. {
  97. tmp_len++;
  98. total--;
  99. }
  100. return n->total - tmp_len;
  101. }
  102. /**
  103. * @brief Export n into unsigned binary data, big endian
  104. *
  105. * @param n bignum obj
  106. * @param buf Buffer for the binary number
  107. * @param len Length of the buffer
  108. *
  109. * @return export bin length
  110. */
  111. int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
  112. {
  113. int cp_len, i, j;
  114. if (n == RT_NULL || buf == RT_NULL)
  115. {
  116. return 0;
  117. }
  118. rt_memset(buf, 0, len);
  119. cp_len = n->total > len ? len : n->total;
  120. for(i = cp_len, j = 0; i > 0; i--, j++)
  121. {
  122. buf[i - 1] = n->p[j];
  123. }
  124. return cp_len;
  125. }
  126. /**
  127. * @brief Import n from unsigned binary data, big endian
  128. *
  129. * @param n bignum obj
  130. * @param buf Buffer for the binary number
  131. * @param len Length of the buffer
  132. *
  133. * @return RT_EOK on success.
  134. */
  135. rt_err_t rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
  136. {
  137. int cp_len, i, j;
  138. void *temp_p;
  139. if (n == RT_NULL || buf == RT_NULL)
  140. {
  141. return 0;
  142. }
  143. if (n->total < len)
  144. {
  145. temp_p = rt_malloc(len);
  146. if (temp_p == RT_NULL)
  147. {
  148. return 0;
  149. }
  150. rt_memset(temp_p, 0, len);
  151. rt_free(n->p);
  152. n->p = temp_p;
  153. n->total = len;
  154. }
  155. cp_len = n->total > len ? len : n->total;
  156. for(i = cp_len, j = 0; i > 0; i--, j++)
  157. {
  158. n->p[j] = buf[i - 1];
  159. }
  160. return cp_len;
  161. }
  162. /**
  163. * @brief x = a + b
  164. *
  165. * @param a bignum obj
  166. * @param b bignum obj
  167. * @param c bignum obj
  168. *
  169. * @return RT_EOK on success.
  170. */
  171. rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x,
  172. const struct hw_bignum_mpi *a,
  173. const struct hw_bignum_mpi *b)
  174. {
  175. struct hwcrypto_bignum *bignum_ctx;
  176. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  177. {
  178. return -RT_ERROR;
  179. }
  180. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  181. if (bignum_ctx->ops->add)
  182. {
  183. return bignum_ctx->ops->add(bignum_ctx, x, a, b);
  184. }
  185. return -RT_ERROR;
  186. }
  187. /**
  188. * @brief x = a - b
  189. *
  190. * @param a bignum obj
  191. * @param b bignum obj
  192. * @param c bignum obj
  193. *
  194. * @return RT_EOK on success.
  195. */
  196. rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x,
  197. const struct hw_bignum_mpi *a,
  198. const struct hw_bignum_mpi *b)
  199. {
  200. struct hwcrypto_bignum *bignum_ctx;
  201. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  202. {
  203. return -RT_ERROR;
  204. }
  205. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  206. if (bignum_ctx->ops->sub)
  207. {
  208. return bignum_ctx->ops->sub(bignum_ctx, x, a, b);
  209. }
  210. return -RT_ERROR;
  211. }
  212. /**
  213. * @brief x = a * b
  214. *
  215. * @param a bignum obj
  216. * @param b bignum obj
  217. * @param c bignum obj
  218. *
  219. * @return RT_EOK on success.
  220. */
  221. rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x,
  222. const struct hw_bignum_mpi *a,
  223. const struct hw_bignum_mpi *b)
  224. {
  225. struct hwcrypto_bignum *bignum_ctx;
  226. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  227. {
  228. return -RT_ERROR;
  229. }
  230. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  231. if (bignum_ctx->ops->mul)
  232. {
  233. return bignum_ctx->ops->mul(bignum_ctx, x, a, b);
  234. }
  235. return -RT_ERROR;
  236. }
  237. /**
  238. * @brief x = a * b (mod c)
  239. *
  240. * @param a bignum obj
  241. * @param b bignum obj
  242. * @param c bignum obj
  243. *
  244. * @return RT_EOK on success.
  245. */
  246. rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x,
  247. const struct hw_bignum_mpi *a,
  248. const struct hw_bignum_mpi *b,
  249. const struct hw_bignum_mpi *c)
  250. {
  251. struct hwcrypto_bignum *bignum_ctx;
  252. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  253. {
  254. return -RT_ERROR;
  255. }
  256. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  257. if (bignum_ctx->ops->mulmod)
  258. {
  259. return bignum_ctx->ops->mulmod(bignum_ctx, x, a, b, c);
  260. }
  261. return -RT_ERROR;
  262. }
  263. /**
  264. * @brief x = a ^ b (mod c)
  265. *
  266. * @param a bignum obj
  267. * @param b bignum obj
  268. * @param c bignum obj
  269. *
  270. * @return RT_EOK on success.
  271. */
  272. rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x,
  273. const struct hw_bignum_mpi *a,
  274. const struct hw_bignum_mpi *b,
  275. const struct hw_bignum_mpi *c)
  276. {
  277. struct hwcrypto_bignum *bignum_ctx;
  278. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  279. {
  280. return -RT_ERROR;
  281. }
  282. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  283. if (bignum_ctx->ops->exptmod)
  284. {
  285. return bignum_ctx->ops->exptmod(bignum_ctx, x, a, b, c);
  286. }
  287. return -RT_ERROR;
  288. }