bignum_core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * Core 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 "constant_time_internal.h"
  25. #include "mbedtls/platform.h"
  26. #include "bignum_core.h"
  27. #include "bn_mul.h"
  28. #include "constant_time_internal.h"
  29. size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
  30. {
  31. size_t j;
  32. mbedtls_mpi_uint mask = (mbedtls_mpi_uint) 1 << (biL - 1);
  33. for (j = 0; j < biL; j++) {
  34. if (a & mask) {
  35. break;
  36. }
  37. mask >>= 1;
  38. }
  39. return j;
  40. }
  41. size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
  42. {
  43. size_t i, j;
  44. if (A_limbs == 0) {
  45. return 0;
  46. }
  47. for (i = A_limbs - 1; i > 0; i--) {
  48. if (A[i] != 0) {
  49. break;
  50. }
  51. }
  52. j = biL - mbedtls_mpi_core_clz(A[i]);
  53. return (i * biL) + j;
  54. }
  55. /* Convert a big-endian byte array aligned to the size of mbedtls_mpi_uint
  56. * into the storage form used by mbedtls_mpi. */
  57. static mbedtls_mpi_uint mpi_bigendian_to_host_c(mbedtls_mpi_uint a)
  58. {
  59. uint8_t i;
  60. unsigned char *a_ptr;
  61. mbedtls_mpi_uint tmp = 0;
  62. for (i = 0, a_ptr = (unsigned char *) &a; i < ciL; i++, a_ptr++) {
  63. tmp <<= CHAR_BIT;
  64. tmp |= (mbedtls_mpi_uint) *a_ptr;
  65. }
  66. return tmp;
  67. }
  68. static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
  69. {
  70. if (MBEDTLS_IS_BIG_ENDIAN) {
  71. /* Nothing to do on bigendian systems. */
  72. return a;
  73. } else {
  74. switch (sizeof(mbedtls_mpi_uint)) {
  75. case 4:
  76. return (mbedtls_mpi_uint) MBEDTLS_BSWAP32((uint32_t) a);
  77. case 8:
  78. return (mbedtls_mpi_uint) MBEDTLS_BSWAP64((uint64_t) a);
  79. }
  80. /* Fall back to C-based reordering if we don't know the byte order
  81. * or we couldn't use a compiler-specific builtin. */
  82. return mpi_bigendian_to_host_c(a);
  83. }
  84. }
  85. void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
  86. size_t A_limbs)
  87. {
  88. mbedtls_mpi_uint *cur_limb_left;
  89. mbedtls_mpi_uint *cur_limb_right;
  90. if (A_limbs == 0) {
  91. return;
  92. }
  93. /*
  94. * Traverse limbs and
  95. * - adapt byte-order in each limb
  96. * - swap the limbs themselves.
  97. * For that, simultaneously traverse the limbs from left to right
  98. * and from right to left, as long as the left index is not bigger
  99. * than the right index (it's not a problem if limbs is odd and the
  100. * indices coincide in the last iteration).
  101. */
  102. for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
  103. cur_limb_left <= cur_limb_right;
  104. cur_limb_left++, cur_limb_right--) {
  105. mbedtls_mpi_uint tmp;
  106. /* Note that if cur_limb_left == cur_limb_right,
  107. * this code effectively swaps the bytes only once. */
  108. tmp = mpi_bigendian_to_host(*cur_limb_left);
  109. *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
  110. *cur_limb_right = tmp;
  111. }
  112. }
  113. /* Whether min <= A, in constant time.
  114. * A_limbs must be at least 1. */
  115. unsigned mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
  116. const mbedtls_mpi_uint *A,
  117. size_t A_limbs)
  118. {
  119. /* min <= least significant limb? */
  120. unsigned min_le_lsl = 1 ^ mbedtls_ct_mpi_uint_lt(A[0], min);
  121. /* limbs other than the least significant one are all zero? */
  122. mbedtls_mpi_uint msll_mask = 0;
  123. for (size_t i = 1; i < A_limbs; i++) {
  124. msll_mask |= A[i];
  125. }
  126. /* The most significant limbs of A are not all zero iff msll_mask != 0. */
  127. unsigned msll_nonzero = mbedtls_ct_mpi_uint_mask(msll_mask) & 1;
  128. /* min <= A iff the lowest limb of A is >= min or the other limbs
  129. * are not all zero. */
  130. return min_le_lsl | msll_nonzero;
  131. }
  132. void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
  133. const mbedtls_mpi_uint *A,
  134. size_t limbs,
  135. unsigned char assign)
  136. {
  137. if (X == A) {
  138. return;
  139. }
  140. mbedtls_ct_mpi_uint_cond_assign(limbs, X, A, assign);
  141. }
  142. void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
  143. mbedtls_mpi_uint *Y,
  144. size_t limbs,
  145. unsigned char swap)
  146. {
  147. if (X == Y) {
  148. return;
  149. }
  150. /* all-bits 1 if swap is 1, all-bits 0 if swap is 0 */
  151. mbedtls_mpi_uint limb_mask = mbedtls_ct_mpi_uint_mask(swap);
  152. for (size_t i = 0; i < limbs; i++) {
  153. mbedtls_mpi_uint tmp = X[i];
  154. X[i] = (X[i] & ~limb_mask) | (Y[i] & limb_mask);
  155. Y[i] = (Y[i] & ~limb_mask) | (tmp & limb_mask);
  156. }
  157. }
  158. int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
  159. size_t X_limbs,
  160. const unsigned char *input,
  161. size_t input_length)
  162. {
  163. const size_t limbs = CHARS_TO_LIMBS(input_length);
  164. if (X_limbs < limbs) {
  165. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  166. }
  167. if (X != NULL) {
  168. memset(X, 0, X_limbs * ciL);
  169. for (size_t i = 0; i < input_length; i++) {
  170. size_t offset = ((i % ciL) << 3);
  171. X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
  172. }
  173. }
  174. return 0;
  175. }
  176. int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
  177. size_t X_limbs,
  178. const unsigned char *input,
  179. size_t input_length)
  180. {
  181. const size_t limbs = CHARS_TO_LIMBS(input_length);
  182. if (X_limbs < limbs) {
  183. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  184. }
  185. /* If X_limbs is 0, input_length must also be 0 (from previous test).
  186. * Nothing to do. */
  187. if (X_limbs == 0) {
  188. return 0;
  189. }
  190. memset(X, 0, X_limbs * ciL);
  191. /* memcpy() with (NULL, 0) is undefined behaviour */
  192. if (input_length != 0) {
  193. size_t overhead = (X_limbs * ciL) - input_length;
  194. unsigned char *Xp = (unsigned char *) X;
  195. memcpy(Xp + overhead, input, input_length);
  196. }
  197. mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
  198. return 0;
  199. }
  200. int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
  201. size_t A_limbs,
  202. unsigned char *output,
  203. size_t output_length)
  204. {
  205. size_t stored_bytes = A_limbs * ciL;
  206. size_t bytes_to_copy;
  207. if (stored_bytes < output_length) {
  208. bytes_to_copy = stored_bytes;
  209. } else {
  210. bytes_to_copy = output_length;
  211. /* The output buffer is smaller than the allocated size of A.
  212. * However A may fit if its leading bytes are zero. */
  213. for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
  214. if (GET_BYTE(A, i) != 0) {
  215. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  216. }
  217. }
  218. }
  219. for (size_t i = 0; i < bytes_to_copy; i++) {
  220. output[i] = GET_BYTE(A, i);
  221. }
  222. if (stored_bytes < output_length) {
  223. /* Write trailing 0 bytes */
  224. memset(output + stored_bytes, 0, output_length - stored_bytes);
  225. }
  226. return 0;
  227. }
  228. int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
  229. size_t X_limbs,
  230. unsigned char *output,
  231. size_t output_length)
  232. {
  233. size_t stored_bytes;
  234. size_t bytes_to_copy;
  235. unsigned char *p;
  236. stored_bytes = X_limbs * ciL;
  237. if (stored_bytes < output_length) {
  238. /* There is enough space in the output buffer. Write initial
  239. * null bytes and record the position at which to start
  240. * writing the significant bytes. In this case, the execution
  241. * trace of this function does not depend on the value of the
  242. * number. */
  243. bytes_to_copy = stored_bytes;
  244. p = output + output_length - stored_bytes;
  245. memset(output, 0, output_length - stored_bytes);
  246. } else {
  247. /* The output buffer is smaller than the allocated size of X.
  248. * However X may fit if its leading bytes are zero. */
  249. bytes_to_copy = output_length;
  250. p = output;
  251. for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
  252. if (GET_BYTE(X, i) != 0) {
  253. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  254. }
  255. }
  256. }
  257. for (size_t i = 0; i < bytes_to_copy; i++) {
  258. p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
  259. }
  260. return 0;
  261. }
  262. void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
  263. size_t count)
  264. {
  265. size_t i, v0, v1;
  266. mbedtls_mpi_uint r0 = 0, r1;
  267. v0 = count / biL;
  268. v1 = count & (biL - 1);
  269. if (v0 > limbs || (v0 == limbs && v1 > 0)) {
  270. memset(X, 0, limbs * ciL);
  271. return;
  272. }
  273. /*
  274. * shift by count / limb_size
  275. */
  276. if (v0 > 0) {
  277. for (i = 0; i < limbs - v0; i++) {
  278. X[i] = X[i + v0];
  279. }
  280. for (; i < limbs; i++) {
  281. X[i] = 0;
  282. }
  283. }
  284. /*
  285. * shift by count % limb_size
  286. */
  287. if (v1 > 0) {
  288. for (i = limbs; i > 0; i--) {
  289. r1 = X[i - 1] << (biL - v1);
  290. X[i - 1] >>= v1;
  291. X[i - 1] |= r0;
  292. r0 = r1;
  293. }
  294. }
  295. }
  296. mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
  297. const mbedtls_mpi_uint *A,
  298. const mbedtls_mpi_uint *B,
  299. size_t limbs)
  300. {
  301. mbedtls_mpi_uint c = 0;
  302. for (size_t i = 0; i < limbs; i++) {
  303. mbedtls_mpi_uint t = c + A[i];
  304. c = (t < A[i]);
  305. t += B[i];
  306. c += (t < B[i]);
  307. X[i] = t;
  308. }
  309. return c;
  310. }
  311. mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
  312. const mbedtls_mpi_uint *A,
  313. size_t limbs,
  314. unsigned cond)
  315. {
  316. mbedtls_mpi_uint c = 0;
  317. /* all-bits 0 if cond is 0, all-bits 1 if cond is non-0 */
  318. const mbedtls_mpi_uint mask = mbedtls_ct_mpi_uint_mask(cond);
  319. for (size_t i = 0; i < limbs; i++) {
  320. mbedtls_mpi_uint add = mask & A[i];
  321. mbedtls_mpi_uint t = c + X[i];
  322. c = (t < X[i]);
  323. t += add;
  324. c += (t < add);
  325. X[i] = t;
  326. }
  327. return c;
  328. }
  329. mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
  330. const mbedtls_mpi_uint *A,
  331. const mbedtls_mpi_uint *B,
  332. size_t limbs)
  333. {
  334. mbedtls_mpi_uint c = 0;
  335. for (size_t i = 0; i < limbs; i++) {
  336. mbedtls_mpi_uint z = (A[i] < c);
  337. mbedtls_mpi_uint t = A[i] - c;
  338. c = (t < B[i]) + z;
  339. X[i] = t - B[i];
  340. }
  341. return c;
  342. }
  343. mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
  344. const mbedtls_mpi_uint *s, size_t s_len,
  345. mbedtls_mpi_uint b)
  346. {
  347. mbedtls_mpi_uint c = 0; /* carry */
  348. /*
  349. * It is a documented precondition of this function that d_len >= s_len.
  350. * If that's not the case, we swap these round: this turns what would be
  351. * a buffer overflow into an incorrect result.
  352. */
  353. if (d_len < s_len) {
  354. s_len = d_len;
  355. }
  356. size_t excess_len = d_len - s_len;
  357. size_t steps_x8 = s_len / 8;
  358. size_t steps_x1 = s_len & 7;
  359. while (steps_x8--) {
  360. MULADDC_X8_INIT
  361. MULADDC_X8_CORE
  362. MULADDC_X8_STOP
  363. }
  364. while (steps_x1--) {
  365. MULADDC_X1_INIT
  366. MULADDC_X1_CORE
  367. MULADDC_X1_STOP
  368. }
  369. while (excess_len--) {
  370. *d += c;
  371. c = (*d < c);
  372. d++;
  373. }
  374. return c;
  375. }
  376. /*
  377. * Fast Montgomery initialization (thanks to Tom St Denis).
  378. */
  379. mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
  380. {
  381. mbedtls_mpi_uint x = N[0];
  382. x += ((N[0] + 2) & 4) << 1;
  383. for (unsigned int i = biL; i >= 8; i /= 2) {
  384. x *= (2 - (N[0] * x));
  385. }
  386. return ~x + 1;
  387. }
  388. void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
  389. const mbedtls_mpi_uint *A,
  390. const mbedtls_mpi_uint *B,
  391. size_t B_limbs,
  392. const mbedtls_mpi_uint *N,
  393. size_t AN_limbs,
  394. mbedtls_mpi_uint mm,
  395. mbedtls_mpi_uint *T)
  396. {
  397. memset(T, 0, (2 * AN_limbs + 1) * ciL);
  398. for (size_t i = 0; i < AN_limbs; i++) {
  399. /* T = (T + u0*B + u1*N) / 2^biL */
  400. mbedtls_mpi_uint u0 = A[i];
  401. mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
  402. (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
  403. (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
  404. T++;
  405. }
  406. /*
  407. * The result we want is (T >= N) ? T - N : T.
  408. *
  409. * For better constant-time properties in this function, we always do the
  410. * subtraction, with the result in X.
  411. *
  412. * We also look to see if there was any carry in the final additions in the
  413. * loop above.
  414. */
  415. mbedtls_mpi_uint carry = T[AN_limbs];
  416. mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
  417. /*
  418. * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
  419. *
  420. * T can be in one of 3 ranges:
  421. *
  422. * 1) T < N : (carry, borrow) = (0, 1): we want T
  423. * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
  424. * 3) T >= R : (carry, borrow) = (1, 1): we want X
  425. *
  426. * and (carry, borrow) = (1, 0) can't happen.
  427. *
  428. * So the correct return value is already in X if (carry ^ borrow) = 0,
  429. * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
  430. */
  431. mbedtls_ct_mpi_uint_cond_assign(AN_limbs, X, T, (unsigned char) (carry ^ borrow));
  432. }
  433. int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
  434. const mbedtls_mpi *N)
  435. {
  436. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  437. MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
  438. MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
  439. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
  440. MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
  441. cleanup:
  442. return ret;
  443. }
  444. MBEDTLS_STATIC_TESTABLE
  445. void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
  446. const mbedtls_mpi_uint *table,
  447. size_t limbs,
  448. size_t count,
  449. size_t index)
  450. {
  451. for (size_t i = 0; i < count; i++, table += limbs) {
  452. unsigned char assign = mbedtls_ct_size_bool_eq(i, index);
  453. mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
  454. }
  455. }
  456. /* Fill X with n_bytes random bytes.
  457. * X must already have room for those bytes.
  458. * The ordering of the bytes returned from the RNG is suitable for
  459. * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
  460. * mbedtls_mpi_core_random()).
  461. */
  462. int mbedtls_mpi_core_fill_random(
  463. mbedtls_mpi_uint *X, size_t X_limbs,
  464. size_t n_bytes,
  465. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  466. {
  467. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  468. const size_t limbs = CHARS_TO_LIMBS(n_bytes);
  469. const size_t overhead = (limbs * ciL) - n_bytes;
  470. if (X_limbs < limbs) {
  471. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  472. }
  473. memset(X, 0, overhead);
  474. memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
  475. MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
  476. mbedtls_mpi_core_bigendian_to_host(X, limbs);
  477. cleanup:
  478. return ret;
  479. }
  480. int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
  481. mbedtls_mpi_uint min,
  482. const mbedtls_mpi_uint *N,
  483. size_t limbs,
  484. int (*f_rng)(void *, unsigned char *, size_t),
  485. void *p_rng)
  486. {
  487. unsigned ge_lower = 1, lt_upper = 0;
  488. size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
  489. size_t n_bytes = (n_bits + 7) / 8;
  490. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  491. /*
  492. * When min == 0, each try has at worst a probability 1/2 of failing
  493. * (the msb has a probability 1/2 of being 0, and then the result will
  494. * be < N), so after 30 tries failure probability is a most 2**(-30).
  495. *
  496. * When N is just below a power of 2, as is the case when generating
  497. * a random scalar on most elliptic curves, 1 try is enough with
  498. * overwhelming probability. When N is just above a power of 2,
  499. * as when generating a random scalar on secp224k1, each try has
  500. * a probability of failing that is almost 1/2.
  501. *
  502. * The probabilities are almost the same if min is nonzero but negligible
  503. * compared to N. This is always the case when N is crypto-sized, but
  504. * it's convenient to support small N for testing purposes. When N
  505. * is small, use a higher repeat count, otherwise the probability of
  506. * failure is macroscopic.
  507. */
  508. int count = (n_bytes > 4 ? 30 : 250);
  509. /*
  510. * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
  511. * when f_rng is a suitably parametrized instance of HMAC_DRBG:
  512. * - use the same byte ordering;
  513. * - keep the leftmost n_bits bits of the generated octet string;
  514. * - try until result is in the desired range.
  515. * This also avoids any bias, which is especially important for ECDSA.
  516. */
  517. do {
  518. MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
  519. n_bytes,
  520. f_rng, p_rng));
  521. mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
  522. if (--count == 0) {
  523. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  524. goto cleanup;
  525. }
  526. ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
  527. lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
  528. } while (ge_lower == 0 || lt_upper == 0);
  529. cleanup:
  530. return ret;
  531. }
  532. /* BEGIN MERGE SLOT 1 */
  533. static size_t exp_mod_get_window_size(size_t Ebits)
  534. {
  535. size_t wsize = (Ebits > 671) ? 6 : (Ebits > 239) ? 5 :
  536. (Ebits > 79) ? 4 : 1;
  537. #if (MBEDTLS_MPI_WINDOW_SIZE < 6)
  538. if (wsize > MBEDTLS_MPI_WINDOW_SIZE) {
  539. wsize = MBEDTLS_MPI_WINDOW_SIZE;
  540. }
  541. #endif
  542. return wsize;
  543. }
  544. size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
  545. {
  546. const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
  547. const size_t welem = ((size_t) 1) << wsize;
  548. /* How big does each part of the working memory pool need to be? */
  549. const size_t table_limbs = welem * AN_limbs;
  550. const size_t select_limbs = AN_limbs;
  551. const size_t temp_limbs = 2 * AN_limbs + 1;
  552. return table_limbs + select_limbs + temp_limbs;
  553. }
  554. static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
  555. const mbedtls_mpi_uint *N,
  556. size_t AN_limbs,
  557. mbedtls_mpi_uint mm,
  558. const mbedtls_mpi_uint *RR,
  559. size_t welem,
  560. mbedtls_mpi_uint *Wtable,
  561. mbedtls_mpi_uint *temp)
  562. {
  563. /* W[0] = 1 (in Montgomery presentation) */
  564. memset(Wtable, 0, AN_limbs * ciL);
  565. Wtable[0] = 1;
  566. mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
  567. /* W[1] = A (already in Montgomery presentation) */
  568. mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
  569. memcpy(W1, A, AN_limbs * ciL);
  570. /* W[i+1] = W[i] * W[1], i >= 2 */
  571. mbedtls_mpi_uint *Wprev = W1;
  572. for (size_t i = 2; i < welem; i++) {
  573. mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
  574. mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
  575. Wprev = Wcur;
  576. }
  577. }
  578. /* Exponentiation: X := A^E mod N.
  579. *
  580. * A must already be in Montgomery form.
  581. *
  582. * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
  583. *
  584. * RR must contain 2^{2*biL} mod N.
  585. *
  586. * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
  587. * (The difference is that the body in our loop processes a single bit instead
  588. * of a full window.)
  589. */
  590. void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
  591. const mbedtls_mpi_uint *A,
  592. const mbedtls_mpi_uint *N,
  593. size_t AN_limbs,
  594. const mbedtls_mpi_uint *E,
  595. size_t E_limbs,
  596. const mbedtls_mpi_uint *RR,
  597. mbedtls_mpi_uint *T)
  598. {
  599. const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
  600. const size_t welem = ((size_t) 1) << wsize;
  601. /* This is how we will use the temporary storage T, which must have space
  602. * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
  603. const size_t table_limbs = welem * AN_limbs;
  604. const size_t select_limbs = AN_limbs;
  605. /* Pointers to specific parts of the temporary working memory pool */
  606. mbedtls_mpi_uint *const Wtable = T;
  607. mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
  608. mbedtls_mpi_uint *const temp = Wselect + select_limbs;
  609. /*
  610. * Window precomputation
  611. */
  612. const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
  613. /* Set Wtable[i] = A^(2^i) (in Montgomery representation) */
  614. exp_mod_precompute_window(A, N, AN_limbs,
  615. mm, RR,
  616. welem, Wtable, temp);
  617. /*
  618. * Fixed window exponentiation
  619. */
  620. /* X = 1 (in Montgomery presentation) initially */
  621. memcpy(X, Wtable, AN_limbs * ciL);
  622. /* We'll process the bits of E from most significant
  623. * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
  624. * (limb_index=0, E_bit_index=0). */
  625. size_t E_limb_index = E_limbs;
  626. size_t E_bit_index = 0;
  627. /* At any given time, window contains window_bits bits from E.
  628. * window_bits can go up to wsize. */
  629. size_t window_bits = 0;
  630. mbedtls_mpi_uint window = 0;
  631. do {
  632. /* Square */
  633. mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
  634. /* Move to the next bit of the exponent */
  635. if (E_bit_index == 0) {
  636. --E_limb_index;
  637. E_bit_index = biL - 1;
  638. } else {
  639. --E_bit_index;
  640. }
  641. /* Insert next exponent bit into window */
  642. ++window_bits;
  643. window <<= 1;
  644. window |= (E[E_limb_index] >> E_bit_index) & 1;
  645. /* Clear window if it's full. Also clear the window at the end,
  646. * when we've finished processing the exponent. */
  647. if (window_bits == wsize ||
  648. (E_bit_index == 0 && E_limb_index == 0)) {
  649. /* Select Wtable[window] without leaking window through
  650. * memory access patterns. */
  651. mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
  652. AN_limbs, welem, window);
  653. /* Multiply X by the selected element. */
  654. mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
  655. temp);
  656. window = 0;
  657. window_bits = 0;
  658. }
  659. } while (!(E_bit_index == 0 && E_limb_index == 0));
  660. }
  661. /* END MERGE SLOT 1 */
  662. /* BEGIN MERGE SLOT 2 */
  663. /* END MERGE SLOT 2 */
  664. /* BEGIN MERGE SLOT 3 */
  665. mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
  666. const mbedtls_mpi_uint *A,
  667. mbedtls_mpi_uint c, /* doubles as carry */
  668. size_t limbs)
  669. {
  670. for (size_t i = 0; i < limbs; i++) {
  671. mbedtls_mpi_uint s = A[i];
  672. mbedtls_mpi_uint t = s - c;
  673. c = (t > s);
  674. X[i] = t;
  675. }
  676. return c;
  677. }
  678. mbedtls_mpi_uint mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
  679. size_t limbs)
  680. {
  681. mbedtls_mpi_uint bits = 0;
  682. for (size_t i = 0; i < limbs; i++) {
  683. bits |= A[i];
  684. }
  685. return bits;
  686. }
  687. void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
  688. const mbedtls_mpi_uint *A,
  689. const mbedtls_mpi_uint *N,
  690. size_t AN_limbs,
  691. mbedtls_mpi_uint mm,
  692. const mbedtls_mpi_uint *rr,
  693. mbedtls_mpi_uint *T)
  694. {
  695. mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
  696. }
  697. void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
  698. const mbedtls_mpi_uint *A,
  699. const mbedtls_mpi_uint *N,
  700. size_t AN_limbs,
  701. mbedtls_mpi_uint mm,
  702. mbedtls_mpi_uint *T)
  703. {
  704. const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
  705. mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
  706. }
  707. /* END MERGE SLOT 3 */
  708. /* BEGIN MERGE SLOT 4 */
  709. /* END MERGE SLOT 4 */
  710. /* BEGIN MERGE SLOT 5 */
  711. /* END MERGE SLOT 5 */
  712. /* BEGIN MERGE SLOT 6 */
  713. /* END MERGE SLOT 6 */
  714. /* BEGIN MERGE SLOT 7 */
  715. /* END MERGE SLOT 7 */
  716. /* BEGIN MERGE SLOT 8 */
  717. /* END MERGE SLOT 8 */
  718. /* BEGIN MERGE SLOT 9 */
  719. /* END MERGE SLOT 9 */
  720. /* BEGIN MERGE SLOT 10 */
  721. /* END MERGE SLOT 10 */
  722. #endif /* MBEDTLS_BIGNUM_C */