hw_bignum.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. #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 rt_hwcrypto_bignum_init(void)
  15. {
  16. struct rt_hwcrypto_device *dev;
  17. if (bignum_default)
  18. {
  19. return RT_EOK;
  20. }
  21. dev = rt_hwcrypto_dev_dufault();
  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 Allocate memory for bignum
  53. *
  54. * @return Pointer to allocated bignum obj
  55. */
  56. struct hw_bignum_mpi *rt_hwcrypto_bignum_alloc(void)
  57. {
  58. struct hw_bignum_mpi *n;
  59. n = rt_malloc(sizeof(struct hw_bignum_mpi));
  60. if (n)
  61. {
  62. rt_memset(n, 0, sizeof(struct hw_bignum_mpi));
  63. }
  64. return n;
  65. }
  66. /**
  67. * @brief free a bignum obj
  68. *
  69. * @param Pointer to bignum obj
  70. */
  71. void rt_hwcrypto_bignum_free(struct hw_bignum_mpi *n)
  72. {
  73. if (n)
  74. {
  75. rt_free(n->p);
  76. rt_free(n);
  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 Get length of bignum as an unsigned binary buffer
  104. *
  105. * @param n bignum obj
  106. * @param buf Buffer for the binary number
  107. * @param len Length of the buffer
  108. *
  109. * @return binary buffer Length
  110. */
  111. int rt_hwcrypto_bignum_get_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
  112. {
  113. int cp_len;
  114. if (n == RT_NULL || n->p == RT_NULL || buf == RT_NULL)
  115. {
  116. return 0;
  117. }
  118. cp_len = n->total > len ? len : n->total;
  119. rt_memcpy(n->p, buf, cp_len);
  120. return cp_len;
  121. }
  122. /**
  123. * @brief Set binary buffer to unsigned bignum
  124. *
  125. * @param n bignum obj
  126. * @param buf Buffer for the binary number
  127. * @param len Length of the buffer
  128. *
  129. * @return RT_EOK on success.
  130. */
  131. rt_err_t rt_hwcrypto_bignum_set_bin(struct hw_bignum_mpi *n, rt_uint8_t *buf, int len)
  132. {
  133. void *temp_p;
  134. if (n == RT_NULL)
  135. {
  136. return -RT_EINVAL;
  137. }
  138. if (n->p && n->total >= len)
  139. {
  140. rt_memcpy(n->p, buf, len);
  141. return RT_EOK;
  142. }
  143. temp_p = rt_malloc(len);
  144. if (temp_p == RT_NULL)
  145. {
  146. return -RT_ENOMEM;
  147. }
  148. if (n->p)
  149. {
  150. rt_free(n->p);
  151. n->p = temp_p;
  152. n->total = 0;
  153. }
  154. rt_memcpy(n->p, buf, len);
  155. n->total = len;
  156. return RT_EOK;
  157. }
  158. /**
  159. * @brief Unsigned comparison
  160. *
  161. * @param a bignum obj
  162. * @param b bignum obj
  163. *
  164. * @return 0 is equal
  165. */
  166. int rt_hwcrypto_bignum_cmp(const struct hw_bignum_mpi *a,
  167. const struct hw_bignum_mpi *b)
  168. {
  169. int a_len, b_len;
  170. if (a == RT_NULL || a->p == RT_NULL
  171. || b == RT_NULL || b->p == RT_NULL)
  172. {
  173. return -1;
  174. }
  175. a_len = rt_hwcrypto_bignum_get_len(a);
  176. b_len = rt_hwcrypto_bignum_get_len(b);
  177. if (a_len != b_len)
  178. {
  179. return a_len - b_len;
  180. }
  181. return rt_memcmp(a->p, b->p, a_len);
  182. }
  183. /**
  184. * @brief Compare bignum to standard Unsigned integer
  185. *
  186. * @param a bignum obj
  187. * @param b Unsigned integer
  188. *
  189. * @return 0 is equal
  190. */
  191. int rt_hwcrypto_bignum_cmp_d(const struct hw_bignum_mpi *a, unsigned long b)
  192. {
  193. struct hw_bignum_mpi tmp_b;
  194. b = b <= 0 ? -b : b;
  195. tmp_b.total = sizeof(unsigned long);
  196. tmp_b.p = &b;
  197. return rt_hwcrypto_bignum_cmp(a, &tmp_b);
  198. }
  199. /**
  200. * @brief a = b + c
  201. *
  202. * @param a bignum obj
  203. * @param b bignum obj
  204. * @param c bignum obj
  205. *
  206. * @return RT_EOK on success.
  207. */
  208. rt_err_t rt_hwcrypto_bignum_add(struct hw_bignum_mpi *a,
  209. const struct hw_bignum_mpi *b,
  210. const struct hw_bignum_mpi *c)
  211. {
  212. struct hwcrypto_bignum *bignum_ctx;
  213. if (rt_hwcrypto_bignum_init() != RT_EOK)
  214. {
  215. return -RT_ERROR;
  216. }
  217. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  218. if (bignum_ctx->ops->add)
  219. {
  220. return bignum_ctx->ops->add(bignum_ctx, a, b, c);
  221. }
  222. return -RT_ERROR;
  223. }
  224. /**
  225. * @brief a = b - c
  226. *
  227. * @param a bignum obj
  228. * @param b bignum obj
  229. * @param c bignum obj
  230. *
  231. * @return RT_EOK on success.
  232. */
  233. rt_err_t rt_hwcrypto_bignum_sub(struct hw_bignum_mpi *a,
  234. const struct hw_bignum_mpi *b,
  235. const struct hw_bignum_mpi *c)
  236. {
  237. struct hwcrypto_bignum *bignum_ctx;
  238. if (rt_hwcrypto_bignum_init() != RT_EOK)
  239. {
  240. return -RT_ERROR;
  241. }
  242. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  243. if (bignum_ctx->ops->sub)
  244. {
  245. return bignum_ctx->ops->sub(bignum_ctx, a, b, c);
  246. }
  247. return -RT_ERROR;
  248. }
  249. /**
  250. * @brief a = b * c
  251. *
  252. * @param a bignum obj
  253. * @param b bignum obj
  254. * @param c bignum obj
  255. *
  256. * @return RT_EOK on success.
  257. */
  258. rt_err_t rt_hwcrypto_bignum_mul(struct hw_bignum_mpi *a,
  259. const struct hw_bignum_mpi *b,
  260. const struct hw_bignum_mpi *c)
  261. {
  262. struct hwcrypto_bignum *bignum_ctx;
  263. if (rt_hwcrypto_bignum_init() != RT_EOK)
  264. {
  265. return -RT_ERROR;
  266. }
  267. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  268. if (bignum_ctx->ops->mul)
  269. {
  270. return bignum_ctx->ops->mul(bignum_ctx, a, b, c);
  271. }
  272. return -RT_ERROR;
  273. }
  274. /**
  275. * @brief a = b * c (mod d)
  276. *
  277. * @param a bignum obj
  278. * @param b bignum obj
  279. * @param c bignum obj
  280. *
  281. * @return RT_EOK on success.
  282. */
  283. rt_err_t rt_hwcrypto_bignum_mulmod(struct hw_bignum_mpi *a,
  284. const struct hw_bignum_mpi *b,
  285. const struct hw_bignum_mpi *c,
  286. const struct hw_bignum_mpi *d)
  287. {
  288. struct hwcrypto_bignum *bignum_ctx;
  289. if (rt_hwcrypto_bignum_init() != RT_EOK)
  290. {
  291. return -RT_ERROR;
  292. }
  293. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  294. if (bignum_ctx->ops->mulmod)
  295. {
  296. return bignum_ctx->ops->mulmod(bignum_ctx, a, b, c, d);
  297. }
  298. return -RT_ERROR;
  299. }
  300. /**
  301. * @brief a = b ^ c (mod d)
  302. *
  303. * @param a bignum obj
  304. * @param b bignum obj
  305. * @param c bignum obj
  306. *
  307. * @return RT_EOK on success.
  308. */
  309. rt_err_t bignum_exptmod(struct hw_bignum_mpi *a,
  310. const struct hw_bignum_mpi *b,
  311. const struct hw_bignum_mpi *c,
  312. const struct hw_bignum_mpi *d)
  313. {
  314. struct hwcrypto_bignum *bignum_ctx;
  315. if (rt_hwcrypto_bignum_init() != RT_EOK)
  316. {
  317. return -RT_ERROR;
  318. }
  319. bignum_ctx = (struct hwcrypto_bignum *)bignum_default;
  320. if (bignum_ctx->ops->exptmod)
  321. {
  322. return bignum_ctx->ops->exptmod(bignum_ctx, a, b, c, d);
  323. }
  324. return -RT_ERROR;
  325. }