bignum_mod.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /**
  2. * Modular bignum functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ECP_WITH_MPI_UINT)
  9. #include <string.h>
  10. #include "mbedtls/platform_util.h"
  11. #include "mbedtls/error.h"
  12. #include "mbedtls/bignum.h"
  13. #include "mbedtls/platform.h"
  14. #include "bignum_core.h"
  15. #include "bignum_mod.h"
  16. #include "bignum_mod_raw.h"
  17. #include "constant_time_internal.h"
  18. int mbedtls_mpi_mod_residue_setup(mbedtls_mpi_mod_residue *r,
  19. const mbedtls_mpi_mod_modulus *N,
  20. mbedtls_mpi_uint *p,
  21. size_t p_limbs)
  22. {
  23. if (p_limbs != N->limbs || !mbedtls_mpi_core_lt_ct(p, N->p, N->limbs)) {
  24. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  25. }
  26. r->limbs = N->limbs;
  27. r->p = p;
  28. return 0;
  29. }
  30. void mbedtls_mpi_mod_residue_release(mbedtls_mpi_mod_residue *r)
  31. {
  32. if (r == NULL) {
  33. return;
  34. }
  35. r->limbs = 0;
  36. r->p = NULL;
  37. }
  38. void mbedtls_mpi_mod_modulus_init(mbedtls_mpi_mod_modulus *N)
  39. {
  40. if (N == NULL) {
  41. return;
  42. }
  43. N->p = NULL;
  44. N->limbs = 0;
  45. N->bits = 0;
  46. N->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
  47. }
  48. void mbedtls_mpi_mod_modulus_free(mbedtls_mpi_mod_modulus *N)
  49. {
  50. if (N == NULL) {
  51. return;
  52. }
  53. switch (N->int_rep) {
  54. case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
  55. if (N->rep.mont.rr != NULL) {
  56. mbedtls_zeroize_and_free((mbedtls_mpi_uint *) N->rep.mont.rr,
  57. N->limbs * sizeof(mbedtls_mpi_uint));
  58. N->rep.mont.rr = NULL;
  59. }
  60. N->rep.mont.mm = 0;
  61. break;
  62. case MBEDTLS_MPI_MOD_REP_OPT_RED:
  63. N->rep.ored.modp = NULL;
  64. break;
  65. case MBEDTLS_MPI_MOD_REP_INVALID:
  66. break;
  67. }
  68. N->p = NULL;
  69. N->limbs = 0;
  70. N->bits = 0;
  71. N->int_rep = MBEDTLS_MPI_MOD_REP_INVALID;
  72. }
  73. static int set_mont_const_square(const mbedtls_mpi_uint **X,
  74. const mbedtls_mpi_uint *A,
  75. size_t limbs)
  76. {
  77. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  78. mbedtls_mpi N;
  79. mbedtls_mpi RR;
  80. *X = NULL;
  81. mbedtls_mpi_init(&N);
  82. mbedtls_mpi_init(&RR);
  83. if (A == NULL || limbs == 0 || limbs >= (MBEDTLS_MPI_MAX_LIMBS / 2) - 2) {
  84. goto cleanup;
  85. }
  86. if (mbedtls_mpi_grow(&N, limbs)) {
  87. goto cleanup;
  88. }
  89. memcpy(N.p, A, sizeof(mbedtls_mpi_uint) * limbs);
  90. ret = mbedtls_mpi_core_get_mont_r2_unsafe(&RR, &N);
  91. if (ret == 0) {
  92. *X = RR.p;
  93. RR.p = NULL;
  94. }
  95. cleanup:
  96. mbedtls_mpi_free(&N);
  97. mbedtls_mpi_free(&RR);
  98. ret = (ret != 0) ? MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED : 0;
  99. return ret;
  100. }
  101. static inline void standard_modulus_setup(mbedtls_mpi_mod_modulus *N,
  102. const mbedtls_mpi_uint *p,
  103. size_t p_limbs,
  104. mbedtls_mpi_mod_rep_selector int_rep)
  105. {
  106. N->p = p;
  107. N->limbs = p_limbs;
  108. N->bits = mbedtls_mpi_core_bitlen(p, p_limbs);
  109. N->int_rep = int_rep;
  110. }
  111. int mbedtls_mpi_mod_modulus_setup(mbedtls_mpi_mod_modulus *N,
  112. const mbedtls_mpi_uint *p,
  113. size_t p_limbs)
  114. {
  115. int ret = 0;
  116. standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_MONTGOMERY);
  117. N->rep.mont.mm = mbedtls_mpi_core_montmul_init(N->p);
  118. ret = set_mont_const_square(&N->rep.mont.rr, N->p, N->limbs);
  119. if (ret != 0) {
  120. mbedtls_mpi_mod_modulus_free(N);
  121. }
  122. return ret;
  123. }
  124. int mbedtls_mpi_mod_optred_modulus_setup(mbedtls_mpi_mod_modulus *N,
  125. const mbedtls_mpi_uint *p,
  126. size_t p_limbs,
  127. mbedtls_mpi_modp_fn modp)
  128. {
  129. standard_modulus_setup(N, p, p_limbs, MBEDTLS_MPI_MOD_REP_OPT_RED);
  130. N->rep.ored.modp = modp;
  131. return 0;
  132. }
  133. int mbedtls_mpi_mod_mul(mbedtls_mpi_mod_residue *X,
  134. const mbedtls_mpi_mod_residue *A,
  135. const mbedtls_mpi_mod_residue *B,
  136. const mbedtls_mpi_mod_modulus *N)
  137. {
  138. if (N->limbs == 0) {
  139. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  140. }
  141. if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) {
  142. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  143. }
  144. mbedtls_mpi_uint *T = mbedtls_calloc(N->limbs * 2 + 1, ciL);
  145. if (T == NULL) {
  146. return MBEDTLS_ERR_MPI_ALLOC_FAILED;
  147. }
  148. mbedtls_mpi_mod_raw_mul(X->p, A->p, B->p, N, T);
  149. mbedtls_free(T);
  150. return 0;
  151. }
  152. int mbedtls_mpi_mod_sub(mbedtls_mpi_mod_residue *X,
  153. const mbedtls_mpi_mod_residue *A,
  154. const mbedtls_mpi_mod_residue *B,
  155. const mbedtls_mpi_mod_modulus *N)
  156. {
  157. if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) {
  158. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  159. }
  160. mbedtls_mpi_mod_raw_sub(X->p, A->p, B->p, N);
  161. return 0;
  162. }
  163. static int mbedtls_mpi_mod_inv_mont(mbedtls_mpi_mod_residue *X,
  164. const mbedtls_mpi_mod_residue *A,
  165. const mbedtls_mpi_mod_modulus *N,
  166. mbedtls_mpi_uint *working_memory)
  167. {
  168. /* Input already in Montgomery form, so there's little to do */
  169. mbedtls_mpi_mod_raw_inv_prime(X->p, A->p,
  170. N->p, N->limbs,
  171. N->rep.mont.rr,
  172. working_memory);
  173. return 0;
  174. }
  175. static int mbedtls_mpi_mod_inv_non_mont(mbedtls_mpi_mod_residue *X,
  176. const mbedtls_mpi_mod_residue *A,
  177. const mbedtls_mpi_mod_modulus *N,
  178. mbedtls_mpi_uint *working_memory)
  179. {
  180. /* Need to convert input into Montgomery form */
  181. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  182. mbedtls_mpi_mod_modulus Nmont;
  183. mbedtls_mpi_mod_modulus_init(&Nmont);
  184. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_modulus_setup(&Nmont, N->p, N->limbs));
  185. /* We'll use X->p to hold the Montgomery form of the input A->p */
  186. mbedtls_mpi_core_to_mont_rep(X->p, A->p, Nmont.p, Nmont.limbs,
  187. Nmont.rep.mont.mm, Nmont.rep.mont.rr,
  188. working_memory);
  189. mbedtls_mpi_mod_raw_inv_prime(X->p, X->p,
  190. Nmont.p, Nmont.limbs,
  191. Nmont.rep.mont.rr,
  192. working_memory);
  193. /* And convert back from Montgomery form */
  194. mbedtls_mpi_core_from_mont_rep(X->p, X->p, Nmont.p, Nmont.limbs,
  195. Nmont.rep.mont.mm, working_memory);
  196. cleanup:
  197. mbedtls_mpi_mod_modulus_free(&Nmont);
  198. return ret;
  199. }
  200. int mbedtls_mpi_mod_inv(mbedtls_mpi_mod_residue *X,
  201. const mbedtls_mpi_mod_residue *A,
  202. const mbedtls_mpi_mod_modulus *N)
  203. {
  204. if (X->limbs != N->limbs || A->limbs != N->limbs) {
  205. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  206. }
  207. /* Zero has the same value regardless of Montgomery form or not */
  208. if (mbedtls_mpi_core_check_zero_ct(A->p, A->limbs) == 0) {
  209. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  210. }
  211. size_t working_limbs =
  212. mbedtls_mpi_mod_raw_inv_prime_working_limbs(N->limbs);
  213. mbedtls_mpi_uint *working_memory = mbedtls_calloc(working_limbs,
  214. sizeof(mbedtls_mpi_uint));
  215. if (working_memory == NULL) {
  216. return MBEDTLS_ERR_MPI_ALLOC_FAILED;
  217. }
  218. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  219. switch (N->int_rep) {
  220. case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
  221. ret = mbedtls_mpi_mod_inv_mont(X, A, N, working_memory);
  222. break;
  223. case MBEDTLS_MPI_MOD_REP_OPT_RED:
  224. ret = mbedtls_mpi_mod_inv_non_mont(X, A, N, working_memory);
  225. break;
  226. default:
  227. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  228. break;
  229. }
  230. mbedtls_zeroize_and_free(working_memory,
  231. working_limbs * sizeof(mbedtls_mpi_uint));
  232. return ret;
  233. }
  234. int mbedtls_mpi_mod_add(mbedtls_mpi_mod_residue *X,
  235. const mbedtls_mpi_mod_residue *A,
  236. const mbedtls_mpi_mod_residue *B,
  237. const mbedtls_mpi_mod_modulus *N)
  238. {
  239. if (X->limbs != N->limbs || A->limbs != N->limbs || B->limbs != N->limbs) {
  240. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  241. }
  242. mbedtls_mpi_mod_raw_add(X->p, A->p, B->p, N);
  243. return 0;
  244. }
  245. int mbedtls_mpi_mod_random(mbedtls_mpi_mod_residue *X,
  246. mbedtls_mpi_uint min,
  247. const mbedtls_mpi_mod_modulus *N,
  248. int (*f_rng)(void *, unsigned char *, size_t),
  249. void *p_rng)
  250. {
  251. if (X->limbs != N->limbs) {
  252. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  253. }
  254. return mbedtls_mpi_mod_raw_random(X->p, min, N, f_rng, p_rng);
  255. }
  256. int mbedtls_mpi_mod_read(mbedtls_mpi_mod_residue *r,
  257. const mbedtls_mpi_mod_modulus *N,
  258. const unsigned char *buf,
  259. size_t buflen,
  260. mbedtls_mpi_mod_ext_rep ext_rep)
  261. {
  262. int ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  263. /* Do our best to check if r and m have been set up */
  264. if (r->limbs == 0 || N->limbs == 0) {
  265. goto cleanup;
  266. }
  267. if (r->limbs != N->limbs) {
  268. goto cleanup;
  269. }
  270. ret = mbedtls_mpi_mod_raw_read(r->p, N, buf, buflen, ext_rep);
  271. if (ret != 0) {
  272. goto cleanup;
  273. }
  274. r->limbs = N->limbs;
  275. ret = mbedtls_mpi_mod_raw_canonical_to_modulus_rep(r->p, N);
  276. cleanup:
  277. return ret;
  278. }
  279. int mbedtls_mpi_mod_write(const mbedtls_mpi_mod_residue *r,
  280. const mbedtls_mpi_mod_modulus *N,
  281. unsigned char *buf,
  282. size_t buflen,
  283. mbedtls_mpi_mod_ext_rep ext_rep)
  284. {
  285. /* Do our best to check if r and m have been set up */
  286. if (r->limbs == 0 || N->limbs == 0) {
  287. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  288. }
  289. if (r->limbs != N->limbs) {
  290. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  291. }
  292. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  293. mbedtls_mpi_uint *working_memory = r->p;
  294. size_t working_memory_len = sizeof(mbedtls_mpi_uint) * r->limbs;
  295. if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY) {
  296. working_memory = mbedtls_calloc(r->limbs, sizeof(mbedtls_mpi_uint));
  297. if (working_memory == NULL) {
  298. ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
  299. goto cleanup;
  300. }
  301. memcpy(working_memory, r->p, working_memory_len);
  302. ret = mbedtls_mpi_mod_raw_from_mont_rep(working_memory, N);
  303. if (ret != 0) {
  304. goto cleanup;
  305. }
  306. }
  307. ret = mbedtls_mpi_mod_raw_write(working_memory, N, buf, buflen, ext_rep);
  308. cleanup:
  309. if (N->int_rep == MBEDTLS_MPI_MOD_REP_MONTGOMERY &&
  310. working_memory != NULL) {
  311. mbedtls_zeroize_and_free(working_memory, working_memory_len);
  312. }
  313. return ret;
  314. }
  315. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ECP_WITH_MPI_UINT */