hw_bignum.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_memset(n->p, 0xFF, n->total);
  74. rt_free(n->p);
  75. n->sign = 0;
  76. n->total = 0;
  77. n->p = RT_NULL;
  78. }
  79. }
  80. /**
  81. * @brief Get length of bignum as an unsigned binary buffer
  82. *
  83. * @param n bignum obj
  84. *
  85. * @return binary buffer length
  86. */
  87. int rt_hwcrypto_bignum_get_len(const struct hw_bignum_mpi *n)
  88. {
  89. int tmp_len, total;
  90. if (n == RT_NULL || n->p == RT_NULL)
  91. {
  92. return 0;
  93. }
  94. tmp_len = 0;
  95. total = n->total;
  96. while ((total > 0) && (n->p[total - 1] == 0))
  97. {
  98. tmp_len++;
  99. total--;
  100. }
  101. return n->total - tmp_len;
  102. }
  103. /**
  104. * @brief Export n into unsigned binary data, big endian
  105. *
  106. * @param n bignum obj
  107. * @param buf Buffer for the binary number
  108. * @param len Length of the buffer
  109. *
  110. * @return export bin length
  111. */
  112. int rt_hwcrypto_bignum_export_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
  113. {
  114. int cp_len, i, j;
  115. if (n == RT_NULL || buf == RT_NULL)
  116. {
  117. return 0;
  118. }
  119. rt_memset(buf, 0, len);
  120. cp_len = (int)n->total > len ? len : (int)n->total;
  121. for(i = cp_len, j = 0; i > 0; i--, j++)
  122. {
  123. buf[i - 1] = n->p[j];
  124. }
  125. return cp_len;
  126. }
  127. /**
  128. * @brief Import n from unsigned binary data, big endian
  129. *
  130. * @param n bignum obj
  131. * @param buf Buffer for the binary number
  132. * @param len Length of the buffer
  133. *
  134. * @return import length.
  135. */
  136. int rt_hwcrypto_bignum_import_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
  137. {
  138. int cp_len, i, j;
  139. void *temp_p;
  140. if (n == RT_NULL || buf == RT_NULL)
  141. {
  142. return 0;
  143. }
  144. if ((int)n->total < len)
  145. {
  146. temp_p = rt_malloc(len);
  147. if (temp_p == RT_NULL)
  148. {
  149. return 0;
  150. }
  151. rt_free(n->p);
  152. n->p = temp_p;
  153. n->total = len;
  154. }
  155. n->sign = 1;
  156. rt_memset(n->p, 0, n->total);
  157. cp_len = (int)n->total > len ? len : n->total;
  158. for(i = cp_len, j = 0; i > 0; i--, j++)
  159. {
  160. n->p[j] = buf[i - 1];
  161. }
  162. return cp_len;
  163. }
  164. /**
  165. * @brief x = a + b
  166. *
  167. * @param a bignum obj
  168. * @param b bignum obj
  169. * @param c bignum obj
  170. *
  171. * @return RT_EOK on success.
  172. */
  173. rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *x,
  174. const struct hw_bignum_mpi *a,
  175. const struct hw_bignum_mpi *b)
  176. {
  177. struct hwcrypto_bignum *bignum_ctx;
  178. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  179. {
  180. return -RT_ERROR;
  181. }
  182. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  183. if (bignum_ctx->ops->add)
  184. {
  185. return bignum_ctx->ops->add(bignum_ctx, x, a, b);
  186. }
  187. return -RT_ERROR;
  188. }
  189. /**
  190. * @brief x = a - b
  191. *
  192. * @param a bignum obj
  193. * @param b bignum obj
  194. * @param c bignum obj
  195. *
  196. * @return RT_EOK on success.
  197. */
  198. rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *x,
  199. const struct hw_bignum_mpi *a,
  200. const struct hw_bignum_mpi *b)
  201. {
  202. struct hwcrypto_bignum *bignum_ctx;
  203. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  204. {
  205. return -RT_ERROR;
  206. }
  207. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  208. if (bignum_ctx->ops->sub)
  209. {
  210. return bignum_ctx->ops->sub(bignum_ctx, x, a, b);
  211. }
  212. return -RT_ERROR;
  213. }
  214. /**
  215. * @brief x = a * b
  216. *
  217. * @param a bignum obj
  218. * @param b bignum obj
  219. * @param c bignum obj
  220. *
  221. * @return RT_EOK on success.
  222. */
  223. rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *x,
  224. const struct hw_bignum_mpi *a,
  225. const struct hw_bignum_mpi *b)
  226. {
  227. struct hwcrypto_bignum *bignum_ctx;
  228. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  229. {
  230. return -RT_ERROR;
  231. }
  232. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  233. if (bignum_ctx->ops->mul)
  234. {
  235. return bignum_ctx->ops->mul(bignum_ctx, x, a, b);
  236. }
  237. return -RT_ERROR;
  238. }
  239. /**
  240. * @brief x = a * b (mod c)
  241. *
  242. * @param a bignum obj
  243. * @param b bignum obj
  244. * @param c bignum obj
  245. *
  246. * @return RT_EOK on success.
  247. */
  248. rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *x,
  249. const struct hw_bignum_mpi *a,
  250. const struct hw_bignum_mpi *b,
  251. const struct hw_bignum_mpi *c)
  252. {
  253. struct hwcrypto_bignum *bignum_ctx;
  254. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  255. {
  256. return -RT_ERROR;
  257. }
  258. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  259. if (bignum_ctx->ops->mulmod)
  260. {
  261. return bignum_ctx->ops->mulmod(bignum_ctx, x, a, b, c);
  262. }
  263. return -RT_ERROR;
  264. }
  265. /**
  266. * @brief x = a ^ b (mod c)
  267. *
  268. * @param a bignum obj
  269. * @param b bignum obj
  270. * @param c bignum obj
  271. *
  272. * @return RT_EOK on success.
  273. */
  274. rt_err_t rt_hwcrypto_bignum_exptmod(struct hw_bignum_mpi *x,
  275. const struct hw_bignum_mpi *a,
  276. const struct hw_bignum_mpi *b,
  277. const struct hw_bignum_mpi *c)
  278. {
  279. struct hwcrypto_bignum *bignum_ctx;
  280. if (hwcrypto_bignum_dev_is_init() != RT_EOK)
  281. {
  282. return -RT_ERROR;
  283. }
  284. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  285. if (bignum_ctx->ops->exptmod)
  286. {
  287. return bignum_ctx->ops->exptmod(bignum_ctx, x, a, b, c);
  288. }
  289. return -RT_ERROR;
  290. }