bignum_mod_raw.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Low-level modular bignum functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #include "common.h"
  20. #if defined(MBEDTLS_BIGNUM_C)
  21. #include <string.h>
  22. #include "mbedtls/error.h"
  23. #include "mbedtls/platform_util.h"
  24. #include "mbedtls/platform.h"
  25. #include "bignum_core.h"
  26. #include "bignum_mod_raw.h"
  27. #include "bignum_mod.h"
  28. #include "constant_time_internal.h"
  29. #include "bignum_mod_raw_invasive.h"
  30. void mbedtls_mpi_mod_raw_cond_assign(mbedtls_mpi_uint *X,
  31. const mbedtls_mpi_uint *A,
  32. const mbedtls_mpi_mod_modulus *N,
  33. unsigned char assign)
  34. {
  35. mbedtls_mpi_core_cond_assign(X, A, N->limbs, assign);
  36. }
  37. void mbedtls_mpi_mod_raw_cond_swap(mbedtls_mpi_uint *X,
  38. mbedtls_mpi_uint *Y,
  39. const mbedtls_mpi_mod_modulus *N,
  40. unsigned char swap)
  41. {
  42. mbedtls_mpi_core_cond_swap(X, Y, N->limbs, swap);
  43. }
  44. int mbedtls_mpi_mod_raw_read(mbedtls_mpi_uint *X,
  45. const mbedtls_mpi_mod_modulus *N,
  46. const unsigned char *input,
  47. size_t input_length,
  48. mbedtls_mpi_mod_ext_rep ext_rep)
  49. {
  50. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  51. switch (ext_rep) {
  52. case MBEDTLS_MPI_MOD_EXT_REP_LE:
  53. ret = mbedtls_mpi_core_read_le(X, N->limbs,
  54. input, input_length);
  55. break;
  56. case MBEDTLS_MPI_MOD_EXT_REP_BE:
  57. ret = mbedtls_mpi_core_read_be(X, N->limbs,
  58. input, input_length);
  59. break;
  60. default:
  61. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  62. }
  63. if (ret != 0) {
  64. goto cleanup;
  65. }
  66. if (!mbedtls_mpi_core_lt_ct(X, N->p, N->limbs)) {
  67. ret = MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  68. goto cleanup;
  69. }
  70. cleanup:
  71. return ret;
  72. }
  73. int mbedtls_mpi_mod_raw_write(const mbedtls_mpi_uint *A,
  74. const mbedtls_mpi_mod_modulus *N,
  75. unsigned char *output,
  76. size_t output_length,
  77. mbedtls_mpi_mod_ext_rep ext_rep)
  78. {
  79. switch (ext_rep) {
  80. case MBEDTLS_MPI_MOD_EXT_REP_LE:
  81. return mbedtls_mpi_core_write_le(A, N->limbs,
  82. output, output_length);
  83. case MBEDTLS_MPI_MOD_EXT_REP_BE:
  84. return mbedtls_mpi_core_write_be(A, N->limbs,
  85. output, output_length);
  86. default:
  87. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  88. }
  89. }
  90. /* BEGIN MERGE SLOT 1 */
  91. /* END MERGE SLOT 1 */
  92. /* BEGIN MERGE SLOT 2 */
  93. void mbedtls_mpi_mod_raw_sub(mbedtls_mpi_uint *X,
  94. const mbedtls_mpi_uint *A,
  95. const mbedtls_mpi_uint *B,
  96. const mbedtls_mpi_mod_modulus *N)
  97. {
  98. mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, A, B, N->limbs);
  99. (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
  100. }
  101. #if defined(MBEDTLS_TEST_HOOKS)
  102. MBEDTLS_STATIC_TESTABLE
  103. void mbedtls_mpi_mod_raw_fix_quasi_reduction(mbedtls_mpi_uint *X,
  104. const mbedtls_mpi_mod_modulus *N)
  105. {
  106. mbedtls_mpi_uint c = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
  107. (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) c);
  108. }
  109. #endif /* MBEDTLS_TEST_HOOKS */
  110. void mbedtls_mpi_mod_raw_mul(mbedtls_mpi_uint *X,
  111. const mbedtls_mpi_uint *A,
  112. const mbedtls_mpi_uint *B,
  113. const mbedtls_mpi_mod_modulus *N,
  114. mbedtls_mpi_uint *T)
  115. {
  116. mbedtls_mpi_core_montmul(X, A, B, N->limbs, N->p, N->limbs,
  117. N->rep.mont.mm, T);
  118. }
  119. /* END MERGE SLOT 2 */
  120. /* BEGIN MERGE SLOT 3 */
  121. size_t mbedtls_mpi_mod_raw_inv_prime_working_limbs(size_t AN_limbs)
  122. {
  123. /* mbedtls_mpi_mod_raw_inv_prime() needs a temporary for the exponent,
  124. * which will be the same size as the modulus and input (AN_limbs),
  125. * and additional space to pass to mbedtls_mpi_core_exp_mod(). */
  126. return AN_limbs +
  127. mbedtls_mpi_core_exp_mod_working_limbs(AN_limbs, AN_limbs);
  128. }
  129. void mbedtls_mpi_mod_raw_inv_prime(mbedtls_mpi_uint *X,
  130. const mbedtls_mpi_uint *A,
  131. const mbedtls_mpi_uint *N,
  132. size_t AN_limbs,
  133. const mbedtls_mpi_uint *RR,
  134. mbedtls_mpi_uint *T)
  135. {
  136. /* Inversion by power: g^|G| = 1 => g^(-1) = g^(|G|-1), and
  137. * |G| = N - 1, so we want
  138. * g^(|G|-1) = g^(N - 2)
  139. */
  140. /* Use the first AN_limbs of T to hold N - 2 */
  141. mbedtls_mpi_uint *Nminus2 = T;
  142. (void) mbedtls_mpi_core_sub_int(Nminus2, N, 2, AN_limbs);
  143. /* Rest of T is given to exp_mod for its working space */
  144. mbedtls_mpi_core_exp_mod(X,
  145. A, N, AN_limbs, Nminus2, AN_limbs,
  146. RR, T + AN_limbs);
  147. }
  148. /* END MERGE SLOT 3 */
  149. /* BEGIN MERGE SLOT 4 */
  150. /* END MERGE SLOT 4 */
  151. /* BEGIN MERGE SLOT 5 */
  152. void mbedtls_mpi_mod_raw_add(mbedtls_mpi_uint *X,
  153. const mbedtls_mpi_uint *A,
  154. const mbedtls_mpi_uint *B,
  155. const mbedtls_mpi_mod_modulus *N)
  156. {
  157. mbedtls_mpi_uint carry, borrow;
  158. carry = mbedtls_mpi_core_add(X, A, B, N->limbs);
  159. borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
  160. (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) (carry ^ borrow));
  161. }
  162. /* END MERGE SLOT 5 */
  163. /* BEGIN MERGE SLOT 6 */
  164. int mbedtls_mpi_mod_raw_canonical_to_modulus_rep(
  165. mbedtls_mpi_uint *X,
  166. const mbedtls_mpi_mod_modulus *N)
  167. {
  168. switch (N->int_rep) {
  169. case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
  170. return mbedtls_mpi_mod_raw_to_mont_rep(X, N);
  171. case MBEDTLS_MPI_MOD_REP_OPT_RED:
  172. return 0;
  173. default:
  174. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  175. }
  176. }
  177. int mbedtls_mpi_mod_raw_modulus_to_canonical_rep(
  178. mbedtls_mpi_uint *X,
  179. const mbedtls_mpi_mod_modulus *N)
  180. {
  181. switch (N->int_rep) {
  182. case MBEDTLS_MPI_MOD_REP_MONTGOMERY:
  183. return mbedtls_mpi_mod_raw_from_mont_rep(X, N);
  184. case MBEDTLS_MPI_MOD_REP_OPT_RED:
  185. return 0;
  186. default:
  187. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  188. }
  189. }
  190. int mbedtls_mpi_mod_raw_random(mbedtls_mpi_uint *X,
  191. mbedtls_mpi_uint min,
  192. const mbedtls_mpi_mod_modulus *N,
  193. int (*f_rng)(void *, unsigned char *, size_t),
  194. void *p_rng)
  195. {
  196. int ret = mbedtls_mpi_core_random(X, min, N->p, N->limbs, f_rng, p_rng);
  197. if (ret != 0) {
  198. return ret;
  199. }
  200. return mbedtls_mpi_mod_raw_canonical_to_modulus_rep(X, N);
  201. }
  202. /* END MERGE SLOT 6 */
  203. /* BEGIN MERGE SLOT 7 */
  204. int mbedtls_mpi_mod_raw_to_mont_rep(mbedtls_mpi_uint *X,
  205. const mbedtls_mpi_mod_modulus *N)
  206. {
  207. mbedtls_mpi_uint *T;
  208. const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
  209. if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
  210. return MBEDTLS_ERR_MPI_ALLOC_FAILED;
  211. }
  212. mbedtls_mpi_core_to_mont_rep(X, X, N->p, N->limbs,
  213. N->rep.mont.mm, N->rep.mont.rr, T);
  214. mbedtls_platform_zeroize(T, t_limbs * ciL);
  215. mbedtls_free(T);
  216. return 0;
  217. }
  218. int mbedtls_mpi_mod_raw_from_mont_rep(mbedtls_mpi_uint *X,
  219. const mbedtls_mpi_mod_modulus *N)
  220. {
  221. const size_t t_limbs = mbedtls_mpi_core_montmul_working_limbs(N->limbs);
  222. mbedtls_mpi_uint *T;
  223. if ((T = (mbedtls_mpi_uint *) mbedtls_calloc(t_limbs, ciL)) == NULL) {
  224. return MBEDTLS_ERR_MPI_ALLOC_FAILED;
  225. }
  226. mbedtls_mpi_core_from_mont_rep(X, X, N->p, N->limbs, N->rep.mont.mm, T);
  227. mbedtls_platform_zeroize(T, t_limbs * ciL);
  228. mbedtls_free(T);
  229. return 0;
  230. }
  231. void mbedtls_mpi_mod_raw_neg(mbedtls_mpi_uint *X,
  232. const mbedtls_mpi_uint *A,
  233. const mbedtls_mpi_mod_modulus *N)
  234. {
  235. mbedtls_mpi_core_sub(X, N->p, A, N->limbs);
  236. /* If A=0 initially, then X=N now. Detect this by
  237. * subtracting N and catching the carry. */
  238. mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, X, N->p, N->limbs);
  239. (void) mbedtls_mpi_core_add_if(X, N->p, N->limbs, (unsigned) borrow);
  240. }
  241. /* END MERGE SLOT 7 */
  242. /* BEGIN MERGE SLOT 8 */
  243. /* END MERGE SLOT 8 */
  244. /* BEGIN MERGE SLOT 9 */
  245. /* END MERGE SLOT 9 */
  246. /* BEGIN MERGE SLOT 10 */
  247. /* END MERGE SLOT 10 */
  248. #endif /* MBEDTLS_BIGNUM_C */