bn_deprecated.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #include "tommath_private.h"
  2. #ifdef BN_DEPRECATED_C
  3. /* LibTomMath, multiple-precision integer library -- Tom St Denis */
  4. /* SPDX-License-Identifier: Unlicense */
  5. #ifdef BN_MP_GET_BIT_C
  6. int mp_get_bit(const mp_int *a, int b)
  7. {
  8. if (b < 0) {
  9. return MP_VAL;
  10. }
  11. return (s_mp_get_bit(a, (unsigned int)b) == MP_YES) ? MP_YES : MP_NO;
  12. }
  13. #endif
  14. #ifdef BN_MP_JACOBI_C
  15. mp_err mp_jacobi(const mp_int *a, const mp_int *n, int *c)
  16. {
  17. if (a->sign == MP_NEG) {
  18. return MP_VAL;
  19. }
  20. if (mp_cmp_d(n, 0uL) != MP_GT) {
  21. return MP_VAL;
  22. }
  23. return mp_kronecker(a, n, c);
  24. }
  25. #endif
  26. #ifdef BN_MP_PRIME_RANDOM_EX_C
  27. mp_err mp_prime_random_ex(mp_int *a, int t, int size, int flags, private_mp_prime_callback cb, void *dat)
  28. {
  29. return s_mp_prime_random_ex(a, t, size, flags, cb, dat);
  30. }
  31. #endif
  32. #ifdef BN_MP_RAND_DIGIT_C
  33. mp_err mp_rand_digit(mp_digit *r)
  34. {
  35. mp_err err = s_mp_rand_source(r, sizeof(mp_digit));
  36. *r &= MP_MASK;
  37. return err;
  38. }
  39. #endif
  40. #ifdef BN_FAST_MP_INVMOD_C
  41. mp_err fast_mp_invmod(const mp_int *a, const mp_int *b, mp_int *c)
  42. {
  43. return s_mp_invmod_fast(a, b, c);
  44. }
  45. #endif
  46. #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
  47. mp_err fast_mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
  48. {
  49. return s_mp_montgomery_reduce_fast(x, n, rho);
  50. }
  51. #endif
  52. #ifdef BN_FAST_S_MP_MUL_DIGS_C
  53. mp_err fast_s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
  54. {
  55. return s_mp_mul_digs_fast(a, b, c, digs);
  56. }
  57. #endif
  58. #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
  59. mp_err fast_s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
  60. {
  61. return s_mp_mul_high_digs_fast(a, b, c, digs);
  62. }
  63. #endif
  64. #ifdef BN_FAST_S_MP_SQR_C
  65. mp_err fast_s_mp_sqr(const mp_int *a, mp_int *b)
  66. {
  67. return s_mp_sqr_fast(a, b);
  68. }
  69. #endif
  70. #ifdef BN_MP_BALANCE_MUL_C
  71. mp_err mp_balance_mul(const mp_int *a, const mp_int *b, mp_int *c)
  72. {
  73. return s_mp_balance_mul(a, b, c);
  74. }
  75. #endif
  76. #ifdef BN_MP_EXPTMOD_FAST_C
  77. mp_err mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode)
  78. {
  79. return s_mp_exptmod_fast(G, X, P, Y, redmode);
  80. }
  81. #endif
  82. #ifdef BN_MP_INVMOD_SLOW_C
  83. mp_err mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c)
  84. {
  85. return s_mp_invmod_slow(a, b, c);
  86. }
  87. #endif
  88. #ifdef BN_MP_KARATSUBA_MUL_C
  89. mp_err mp_karatsuba_mul(const mp_int *a, const mp_int *b, mp_int *c)
  90. {
  91. return s_mp_karatsuba_mul(a, b, c);
  92. }
  93. #endif
  94. #ifdef BN_MP_KARATSUBA_SQR_C
  95. mp_err mp_karatsuba_sqr(const mp_int *a, mp_int *b)
  96. {
  97. return s_mp_karatsuba_sqr(a, b);
  98. }
  99. #endif
  100. #ifdef BN_MP_TOOM_MUL_C
  101. mp_err mp_toom_mul(const mp_int *a, const mp_int *b, mp_int *c)
  102. {
  103. return s_mp_toom_mul(a, b, c);
  104. }
  105. #endif
  106. #ifdef BN_MP_TOOM_SQR_C
  107. mp_err mp_toom_sqr(const mp_int *a, mp_int *b)
  108. {
  109. return s_mp_toom_sqr(a, b);
  110. }
  111. #endif
  112. #ifdef S_MP_REVERSE_C
  113. void bn_reverse(unsigned char *s, int len)
  114. {
  115. if (len > 0) {
  116. s_mp_reverse(s, (size_t)len);
  117. }
  118. }
  119. #endif
  120. #ifdef BN_MP_TC_AND_C
  121. mp_err mp_tc_and(const mp_int *a, const mp_int *b, mp_int *c)
  122. {
  123. return mp_and(a, b, c);
  124. }
  125. #endif
  126. #ifdef BN_MP_TC_OR_C
  127. mp_err mp_tc_or(const mp_int *a, const mp_int *b, mp_int *c)
  128. {
  129. return mp_or(a, b, c);
  130. }
  131. #endif
  132. #ifdef BN_MP_TC_XOR_C
  133. mp_err mp_tc_xor(const mp_int *a, const mp_int *b, mp_int *c)
  134. {
  135. return mp_xor(a, b, c);
  136. }
  137. #endif
  138. #ifdef BN_MP_TC_DIV_2D_C
  139. mp_err mp_tc_div_2d(const mp_int *a, int b, mp_int *c)
  140. {
  141. return mp_signed_rsh(a, b, c);
  142. }
  143. #endif
  144. #ifdef BN_MP_INIT_SET_INT_C
  145. mp_err mp_init_set_int(mp_int *a, unsigned long b)
  146. {
  147. return mp_init_u32(a, (uint32_t)b);
  148. }
  149. #endif
  150. #ifdef BN_MP_SET_INT_C
  151. mp_err mp_set_int(mp_int *a, unsigned long b)
  152. {
  153. mp_set_u32(a, (uint32_t)b);
  154. return MP_OKAY;
  155. }
  156. #endif
  157. #ifdef BN_MP_SET_LONG_C
  158. mp_err mp_set_long(mp_int *a, unsigned long b)
  159. {
  160. mp_set_u64(a, b);
  161. return MP_OKAY;
  162. }
  163. #endif
  164. #ifdef BN_MP_SET_LONG_LONG_C
  165. mp_err mp_set_long_long(mp_int *a, unsigned long long b)
  166. {
  167. mp_set_u64(a, b);
  168. return MP_OKAY;
  169. }
  170. #endif
  171. #ifdef BN_MP_GET_INT_C
  172. unsigned long mp_get_int(const mp_int *a)
  173. {
  174. return (unsigned long)mp_get_mag_u32(a);
  175. }
  176. #endif
  177. #ifdef BN_MP_GET_LONG_C
  178. unsigned long mp_get_long(const mp_int *a)
  179. {
  180. return (unsigned long)mp_get_mag_ul(a);
  181. }
  182. #endif
  183. #ifdef BN_MP_GET_LONG_LONG_C
  184. unsigned long long mp_get_long_long(const mp_int *a)
  185. {
  186. return mp_get_mag_ull(a);
  187. }
  188. #endif
  189. #ifdef BN_MP_PRIME_IS_DIVISIBLE_C
  190. mp_err mp_prime_is_divisible(const mp_int *a, mp_bool *result)
  191. {
  192. return s_mp_prime_is_divisible(a, result);
  193. }
  194. #endif
  195. #ifdef BN_MP_EXPT_D_EX_C
  196. mp_err mp_expt_d_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
  197. {
  198. (void)fast;
  199. if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
  200. return MP_VAL;
  201. }
  202. return mp_expt_u32(a, (uint32_t)b, c);
  203. }
  204. #endif
  205. #ifdef BN_MP_EXPT_D_C
  206. mp_err mp_expt_d(const mp_int *a, mp_digit b, mp_int *c)
  207. {
  208. if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
  209. return MP_VAL;
  210. }
  211. return mp_expt_u32(a, (uint32_t)b, c);
  212. }
  213. #endif
  214. #ifdef BN_MP_N_ROOT_EX_C
  215. mp_err mp_n_root_ex(const mp_int *a, mp_digit b, mp_int *c, int fast)
  216. {
  217. (void)fast;
  218. if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
  219. return MP_VAL;
  220. }
  221. return mp_root_u32(a, (uint32_t)b, c);
  222. }
  223. #endif
  224. #ifdef BN_MP_N_ROOT_C
  225. mp_err mp_n_root(const mp_int *a, mp_digit b, mp_int *c)
  226. {
  227. if (b > MP_MIN(MP_DIGIT_MAX, UINT32_MAX)) {
  228. return MP_VAL;
  229. }
  230. return mp_root_u32(a, (uint32_t)b, c);
  231. }
  232. #endif
  233. #ifdef BN_MP_UNSIGNED_BIN_SIZE_C
  234. int mp_unsigned_bin_size(const mp_int *a)
  235. {
  236. return (int)mp_ubin_size(a);
  237. }
  238. #endif
  239. #ifdef BN_MP_READ_UNSIGNED_BIN_C
  240. mp_err mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
  241. {
  242. return mp_from_ubin(a, b, (size_t) c);
  243. }
  244. #endif
  245. #ifdef BN_MP_TO_UNSIGNED_BIN_C
  246. mp_err mp_to_unsigned_bin(const mp_int *a, unsigned char *b)
  247. {
  248. return mp_to_ubin(a, b, SIZE_MAX, NULL);
  249. }
  250. #endif
  251. #ifdef BN_MP_TO_UNSIGNED_BIN_N_C
  252. mp_err mp_to_unsigned_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
  253. {
  254. size_t n = mp_ubin_size(a);
  255. if (*outlen < (unsigned long)n) {
  256. return MP_VAL;
  257. }
  258. *outlen = (unsigned long)n;
  259. return mp_to_ubin(a, b, n, NULL);
  260. }
  261. #endif
  262. #ifdef BN_MP_SIGNED_BIN_SIZE_C
  263. int mp_signed_bin_size(const mp_int *a)
  264. {
  265. return (int)mp_sbin_size(a);
  266. }
  267. #endif
  268. #ifdef BN_MP_READ_SIGNED_BIN_C
  269. mp_err mp_read_signed_bin(mp_int *a, const unsigned char *b, int c)
  270. {
  271. return mp_from_sbin(a, b, (size_t) c);
  272. }
  273. #endif
  274. #ifdef BN_MP_TO_SIGNED_BIN_C
  275. mp_err mp_to_signed_bin(const mp_int *a, unsigned char *b)
  276. {
  277. return mp_to_sbin(a, b, SIZE_MAX, NULL);
  278. }
  279. #endif
  280. #ifdef BN_MP_TO_SIGNED_BIN_N_C
  281. mp_err mp_to_signed_bin_n(const mp_int *a, unsigned char *b, unsigned long *outlen)
  282. {
  283. size_t n = mp_sbin_size(a);
  284. if (*outlen < (unsigned long)n) {
  285. return MP_VAL;
  286. }
  287. *outlen = (unsigned long)n;
  288. return mp_to_sbin(a, b, n, NULL);
  289. }
  290. #endif
  291. #ifdef BN_MP_TORADIX_N_C
  292. mp_err mp_toradix_n(const mp_int *a, char *str, int radix, int maxlen)
  293. {
  294. if (maxlen < 0) {
  295. return MP_VAL;
  296. }
  297. return mp_to_radix(a, str, (size_t)maxlen, NULL, radix);
  298. }
  299. #endif
  300. #ifdef BN_MP_TORADIX_C
  301. mp_err mp_toradix(const mp_int *a, char *str, int radix)
  302. {
  303. return mp_to_radix(a, str, SIZE_MAX, NULL, radix);
  304. }
  305. #endif
  306. #ifdef BN_MP_IMPORT_C
  307. mp_err mp_import(mp_int *rop, size_t count, int order, size_t size, int endian, size_t nails,
  308. const void *op)
  309. {
  310. return mp_unpack(rop, count, order, size, endian, nails, op);
  311. }
  312. #endif
  313. #ifdef BN_MP_EXPORT_C
  314. mp_err mp_export(void *rop, size_t *countp, int order, size_t size,
  315. int endian, size_t nails, const mp_int *op)
  316. {
  317. return mp_pack(rop, SIZE_MAX, countp, order, size, endian, nails, op);
  318. }
  319. #endif
  320. #endif