bignum_core.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Core 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)
  9. #include <string.h>
  10. #include "mbedtls/error.h"
  11. #include "mbedtls/platform_util.h"
  12. #include "constant_time_internal.h"
  13. #include "mbedtls/platform.h"
  14. #include "bignum_core.h"
  15. #include "bn_mul.h"
  16. #include "constant_time_internal.h"
  17. size_t mbedtls_mpi_core_clz(mbedtls_mpi_uint a)
  18. {
  19. #if defined(__has_builtin)
  20. #if (MBEDTLS_MPI_UINT_MAX == UINT_MAX) && __has_builtin(__builtin_clz)
  21. #define core_clz __builtin_clz
  22. #elif (MBEDTLS_MPI_UINT_MAX == ULONG_MAX) && __has_builtin(__builtin_clzl)
  23. #define core_clz __builtin_clzl
  24. #elif (MBEDTLS_MPI_UINT_MAX == ULLONG_MAX) && __has_builtin(__builtin_clzll)
  25. #define core_clz __builtin_clzll
  26. #endif
  27. #endif
  28. #if defined(core_clz)
  29. return (size_t) core_clz(a);
  30. #else
  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. #endif
  41. }
  42. size_t mbedtls_mpi_core_bitlen(const mbedtls_mpi_uint *A, size_t A_limbs)
  43. {
  44. int i;
  45. size_t j;
  46. for (i = ((int) A_limbs) - 1; i >= 0; i--) {
  47. if (A[i] != 0) {
  48. j = biL - mbedtls_mpi_core_clz(A[i]);
  49. return (i * biL) + j;
  50. }
  51. }
  52. return 0;
  53. }
  54. static mbedtls_mpi_uint mpi_bigendian_to_host(mbedtls_mpi_uint a)
  55. {
  56. if (MBEDTLS_IS_BIG_ENDIAN) {
  57. /* Nothing to do on bigendian systems. */
  58. return a;
  59. } else {
  60. #if defined(MBEDTLS_HAVE_INT32)
  61. return (mbedtls_mpi_uint) MBEDTLS_BSWAP32(a);
  62. #elif defined(MBEDTLS_HAVE_INT64)
  63. return (mbedtls_mpi_uint) MBEDTLS_BSWAP64(a);
  64. #endif
  65. }
  66. }
  67. void mbedtls_mpi_core_bigendian_to_host(mbedtls_mpi_uint *A,
  68. size_t A_limbs)
  69. {
  70. mbedtls_mpi_uint *cur_limb_left;
  71. mbedtls_mpi_uint *cur_limb_right;
  72. if (A_limbs == 0) {
  73. return;
  74. }
  75. /*
  76. * Traverse limbs and
  77. * - adapt byte-order in each limb
  78. * - swap the limbs themselves.
  79. * For that, simultaneously traverse the limbs from left to right
  80. * and from right to left, as long as the left index is not bigger
  81. * than the right index (it's not a problem if limbs is odd and the
  82. * indices coincide in the last iteration).
  83. */
  84. for (cur_limb_left = A, cur_limb_right = A + (A_limbs - 1);
  85. cur_limb_left <= cur_limb_right;
  86. cur_limb_left++, cur_limb_right--) {
  87. mbedtls_mpi_uint tmp;
  88. /* Note that if cur_limb_left == cur_limb_right,
  89. * this code effectively swaps the bytes only once. */
  90. tmp = mpi_bigendian_to_host(*cur_limb_left);
  91. *cur_limb_left = mpi_bigendian_to_host(*cur_limb_right);
  92. *cur_limb_right = tmp;
  93. }
  94. }
  95. /* Whether min <= A, in constant time.
  96. * A_limbs must be at least 1. */
  97. mbedtls_ct_condition_t mbedtls_mpi_core_uint_le_mpi(mbedtls_mpi_uint min,
  98. const mbedtls_mpi_uint *A,
  99. size_t A_limbs)
  100. {
  101. /* min <= least significant limb? */
  102. mbedtls_ct_condition_t min_le_lsl = mbedtls_ct_uint_ge(A[0], min);
  103. /* limbs other than the least significant one are all zero? */
  104. mbedtls_ct_condition_t msll_mask = MBEDTLS_CT_FALSE;
  105. for (size_t i = 1; i < A_limbs; i++) {
  106. msll_mask = mbedtls_ct_bool_or(msll_mask, mbedtls_ct_bool(A[i]));
  107. }
  108. /* min <= A iff the lowest limb of A is >= min or the other limbs
  109. * are not all zero. */
  110. return mbedtls_ct_bool_or(msll_mask, min_le_lsl);
  111. }
  112. mbedtls_ct_condition_t mbedtls_mpi_core_lt_ct(const mbedtls_mpi_uint *A,
  113. const mbedtls_mpi_uint *B,
  114. size_t limbs)
  115. {
  116. mbedtls_ct_condition_t ret = MBEDTLS_CT_FALSE, cond = MBEDTLS_CT_FALSE, done = MBEDTLS_CT_FALSE;
  117. for (size_t i = limbs; i > 0; i--) {
  118. /*
  119. * If B[i - 1] < A[i - 1] then A < B is false and the result must
  120. * remain 0.
  121. *
  122. * Again even if we can make a decision, we just mark the result and
  123. * the fact that we are done and continue looping.
  124. */
  125. cond = mbedtls_ct_uint_lt(B[i - 1], A[i - 1]);
  126. done = mbedtls_ct_bool_or(done, cond);
  127. /*
  128. * If A[i - 1] < B[i - 1] then A < B is true.
  129. *
  130. * Again even if we can make a decision, we just mark the result and
  131. * the fact that we are done and continue looping.
  132. */
  133. cond = mbedtls_ct_uint_lt(A[i - 1], B[i - 1]);
  134. ret = mbedtls_ct_bool_or(ret, mbedtls_ct_bool_and(cond, mbedtls_ct_bool_not(done)));
  135. done = mbedtls_ct_bool_or(done, cond);
  136. }
  137. /*
  138. * If all the limbs were equal, then the numbers are equal, A < B is false
  139. * and leaving the result 0 is correct.
  140. */
  141. return ret;
  142. }
  143. void mbedtls_mpi_core_cond_assign(mbedtls_mpi_uint *X,
  144. const mbedtls_mpi_uint *A,
  145. size_t limbs,
  146. mbedtls_ct_condition_t assign)
  147. {
  148. if (X == A) {
  149. return;
  150. }
  151. /* This function is very performance-sensitive for RSA. For this reason
  152. * we have the loop below, instead of calling mbedtls_ct_memcpy_if
  153. * (this is more optimal since here we don't have to handle the case where
  154. * we copy awkwardly sized data).
  155. */
  156. for (size_t i = 0; i < limbs; i++) {
  157. X[i] = mbedtls_ct_mpi_uint_if(assign, A[i], X[i]);
  158. }
  159. }
  160. void mbedtls_mpi_core_cond_swap(mbedtls_mpi_uint *X,
  161. mbedtls_mpi_uint *Y,
  162. size_t limbs,
  163. mbedtls_ct_condition_t swap)
  164. {
  165. if (X == Y) {
  166. return;
  167. }
  168. for (size_t i = 0; i < limbs; i++) {
  169. mbedtls_mpi_uint tmp = X[i];
  170. X[i] = mbedtls_ct_mpi_uint_if(swap, Y[i], X[i]);
  171. Y[i] = mbedtls_ct_mpi_uint_if(swap, tmp, Y[i]);
  172. }
  173. }
  174. int mbedtls_mpi_core_read_le(mbedtls_mpi_uint *X,
  175. size_t X_limbs,
  176. const unsigned char *input,
  177. size_t input_length)
  178. {
  179. const size_t limbs = CHARS_TO_LIMBS(input_length);
  180. if (X_limbs < limbs) {
  181. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  182. }
  183. if (X != NULL) {
  184. memset(X, 0, X_limbs * ciL);
  185. for (size_t i = 0; i < input_length; i++) {
  186. size_t offset = ((i % ciL) << 3);
  187. X[i / ciL] |= ((mbedtls_mpi_uint) input[i]) << offset;
  188. }
  189. }
  190. return 0;
  191. }
  192. int mbedtls_mpi_core_read_be(mbedtls_mpi_uint *X,
  193. size_t X_limbs,
  194. const unsigned char *input,
  195. size_t input_length)
  196. {
  197. const size_t limbs = CHARS_TO_LIMBS(input_length);
  198. if (X_limbs < limbs) {
  199. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  200. }
  201. /* If X_limbs is 0, input_length must also be 0 (from previous test).
  202. * Nothing to do. */
  203. if (X_limbs == 0) {
  204. return 0;
  205. }
  206. memset(X, 0, X_limbs * ciL);
  207. /* memcpy() with (NULL, 0) is undefined behaviour */
  208. if (input_length != 0) {
  209. size_t overhead = (X_limbs * ciL) - input_length;
  210. unsigned char *Xp = (unsigned char *) X;
  211. memcpy(Xp + overhead, input, input_length);
  212. }
  213. mbedtls_mpi_core_bigendian_to_host(X, X_limbs);
  214. return 0;
  215. }
  216. int mbedtls_mpi_core_write_le(const mbedtls_mpi_uint *A,
  217. size_t A_limbs,
  218. unsigned char *output,
  219. size_t output_length)
  220. {
  221. size_t stored_bytes = A_limbs * ciL;
  222. size_t bytes_to_copy;
  223. if (stored_bytes < output_length) {
  224. bytes_to_copy = stored_bytes;
  225. } else {
  226. bytes_to_copy = output_length;
  227. /* The output buffer is smaller than the allocated size of A.
  228. * However A may fit if its leading bytes are zero. */
  229. for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
  230. if (GET_BYTE(A, i) != 0) {
  231. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  232. }
  233. }
  234. }
  235. for (size_t i = 0; i < bytes_to_copy; i++) {
  236. output[i] = GET_BYTE(A, i);
  237. }
  238. if (stored_bytes < output_length) {
  239. /* Write trailing 0 bytes */
  240. memset(output + stored_bytes, 0, output_length - stored_bytes);
  241. }
  242. return 0;
  243. }
  244. int mbedtls_mpi_core_write_be(const mbedtls_mpi_uint *X,
  245. size_t X_limbs,
  246. unsigned char *output,
  247. size_t output_length)
  248. {
  249. size_t stored_bytes;
  250. size_t bytes_to_copy;
  251. unsigned char *p;
  252. stored_bytes = X_limbs * ciL;
  253. if (stored_bytes < output_length) {
  254. /* There is enough space in the output buffer. Write initial
  255. * null bytes and record the position at which to start
  256. * writing the significant bytes. In this case, the execution
  257. * trace of this function does not depend on the value of the
  258. * number. */
  259. bytes_to_copy = stored_bytes;
  260. p = output + output_length - stored_bytes;
  261. memset(output, 0, output_length - stored_bytes);
  262. } else {
  263. /* The output buffer is smaller than the allocated size of X.
  264. * However X may fit if its leading bytes are zero. */
  265. bytes_to_copy = output_length;
  266. p = output;
  267. for (size_t i = bytes_to_copy; i < stored_bytes; i++) {
  268. if (GET_BYTE(X, i) != 0) {
  269. return MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL;
  270. }
  271. }
  272. }
  273. for (size_t i = 0; i < bytes_to_copy; i++) {
  274. p[bytes_to_copy - i - 1] = GET_BYTE(X, i);
  275. }
  276. return 0;
  277. }
  278. void mbedtls_mpi_core_shift_r(mbedtls_mpi_uint *X, size_t limbs,
  279. size_t count)
  280. {
  281. size_t i, v0, v1;
  282. mbedtls_mpi_uint r0 = 0, r1;
  283. v0 = count / biL;
  284. v1 = count & (biL - 1);
  285. if (v0 > limbs || (v0 == limbs && v1 > 0)) {
  286. memset(X, 0, limbs * ciL);
  287. return;
  288. }
  289. /*
  290. * shift by count / limb_size
  291. */
  292. if (v0 > 0) {
  293. for (i = 0; i < limbs - v0; i++) {
  294. X[i] = X[i + v0];
  295. }
  296. for (; i < limbs; i++) {
  297. X[i] = 0;
  298. }
  299. }
  300. /*
  301. * shift by count % limb_size
  302. */
  303. if (v1 > 0) {
  304. for (i = limbs; i > 0; i--) {
  305. r1 = X[i - 1] << (biL - v1);
  306. X[i - 1] >>= v1;
  307. X[i - 1] |= r0;
  308. r0 = r1;
  309. }
  310. }
  311. }
  312. void mbedtls_mpi_core_shift_l(mbedtls_mpi_uint *X, size_t limbs,
  313. size_t count)
  314. {
  315. size_t i, v0, v1;
  316. mbedtls_mpi_uint r0 = 0, r1;
  317. v0 = count / (biL);
  318. v1 = count & (biL - 1);
  319. /*
  320. * shift by count / limb_size
  321. */
  322. if (v0 > 0) {
  323. for (i = limbs; i > v0; i--) {
  324. X[i - 1] = X[i - v0 - 1];
  325. }
  326. for (; i > 0; i--) {
  327. X[i - 1] = 0;
  328. }
  329. }
  330. /*
  331. * shift by count % limb_size
  332. */
  333. if (v1 > 0) {
  334. for (i = v0; i < limbs; i++) {
  335. r1 = X[i] >> (biL - v1);
  336. X[i] <<= v1;
  337. X[i] |= r0;
  338. r0 = r1;
  339. }
  340. }
  341. }
  342. mbedtls_mpi_uint mbedtls_mpi_core_add(mbedtls_mpi_uint *X,
  343. const mbedtls_mpi_uint *A,
  344. const mbedtls_mpi_uint *B,
  345. size_t limbs)
  346. {
  347. mbedtls_mpi_uint c = 0;
  348. for (size_t i = 0; i < limbs; i++) {
  349. mbedtls_mpi_uint t = c + A[i];
  350. c = (t < A[i]);
  351. t += B[i];
  352. c += (t < B[i]);
  353. X[i] = t;
  354. }
  355. return c;
  356. }
  357. mbedtls_mpi_uint mbedtls_mpi_core_add_if(mbedtls_mpi_uint *X,
  358. const mbedtls_mpi_uint *A,
  359. size_t limbs,
  360. unsigned cond)
  361. {
  362. mbedtls_mpi_uint c = 0;
  363. mbedtls_ct_condition_t do_add = mbedtls_ct_bool(cond);
  364. for (size_t i = 0; i < limbs; i++) {
  365. mbedtls_mpi_uint add = mbedtls_ct_mpi_uint_if_else_0(do_add, A[i]);
  366. mbedtls_mpi_uint t = c + X[i];
  367. c = (t < X[i]);
  368. t += add;
  369. c += (t < add);
  370. X[i] = t;
  371. }
  372. return c;
  373. }
  374. mbedtls_mpi_uint mbedtls_mpi_core_sub(mbedtls_mpi_uint *X,
  375. const mbedtls_mpi_uint *A,
  376. const mbedtls_mpi_uint *B,
  377. size_t limbs)
  378. {
  379. mbedtls_mpi_uint c = 0;
  380. for (size_t i = 0; i < limbs; i++) {
  381. mbedtls_mpi_uint z = (A[i] < c);
  382. mbedtls_mpi_uint t = A[i] - c;
  383. c = (t < B[i]) + z;
  384. X[i] = t - B[i];
  385. }
  386. return c;
  387. }
  388. mbedtls_mpi_uint mbedtls_mpi_core_mla(mbedtls_mpi_uint *d, size_t d_len,
  389. const mbedtls_mpi_uint *s, size_t s_len,
  390. mbedtls_mpi_uint b)
  391. {
  392. mbedtls_mpi_uint c = 0; /* carry */
  393. /*
  394. * It is a documented precondition of this function that d_len >= s_len.
  395. * If that's not the case, we swap these round: this turns what would be
  396. * a buffer overflow into an incorrect result.
  397. */
  398. if (d_len < s_len) {
  399. s_len = d_len;
  400. }
  401. size_t excess_len = d_len - s_len;
  402. size_t steps_x8 = s_len / 8;
  403. size_t steps_x1 = s_len & 7;
  404. while (steps_x8--) {
  405. MULADDC_X8_INIT
  406. MULADDC_X8_CORE
  407. MULADDC_X8_STOP
  408. }
  409. while (steps_x1--) {
  410. MULADDC_X1_INIT
  411. MULADDC_X1_CORE
  412. MULADDC_X1_STOP
  413. }
  414. while (excess_len--) {
  415. *d += c;
  416. c = (*d < c);
  417. d++;
  418. }
  419. return c;
  420. }
  421. void mbedtls_mpi_core_mul(mbedtls_mpi_uint *X,
  422. const mbedtls_mpi_uint *A, size_t A_limbs,
  423. const mbedtls_mpi_uint *B, size_t B_limbs)
  424. {
  425. memset(X, 0, (A_limbs + B_limbs) * ciL);
  426. for (size_t i = 0; i < B_limbs; i++) {
  427. (void) mbedtls_mpi_core_mla(X + i, A_limbs + 1, A, A_limbs, B[i]);
  428. }
  429. }
  430. /*
  431. * Fast Montgomery initialization (thanks to Tom St Denis).
  432. */
  433. mbedtls_mpi_uint mbedtls_mpi_core_montmul_init(const mbedtls_mpi_uint *N)
  434. {
  435. mbedtls_mpi_uint x = N[0];
  436. x += ((N[0] + 2) & 4) << 1;
  437. for (unsigned int i = biL; i >= 8; i /= 2) {
  438. x *= (2 - (N[0] * x));
  439. }
  440. return ~x + 1;
  441. }
  442. void mbedtls_mpi_core_montmul(mbedtls_mpi_uint *X,
  443. const mbedtls_mpi_uint *A,
  444. const mbedtls_mpi_uint *B,
  445. size_t B_limbs,
  446. const mbedtls_mpi_uint *N,
  447. size_t AN_limbs,
  448. mbedtls_mpi_uint mm,
  449. mbedtls_mpi_uint *T)
  450. {
  451. memset(T, 0, (2 * AN_limbs + 1) * ciL);
  452. for (size_t i = 0; i < AN_limbs; i++) {
  453. /* T = (T + u0*B + u1*N) / 2^biL */
  454. mbedtls_mpi_uint u0 = A[i];
  455. mbedtls_mpi_uint u1 = (T[0] + u0 * B[0]) * mm;
  456. (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, B, B_limbs, u0);
  457. (void) mbedtls_mpi_core_mla(T, AN_limbs + 2, N, AN_limbs, u1);
  458. T++;
  459. }
  460. /*
  461. * The result we want is (T >= N) ? T - N : T.
  462. *
  463. * For better constant-time properties in this function, we always do the
  464. * subtraction, with the result in X.
  465. *
  466. * We also look to see if there was any carry in the final additions in the
  467. * loop above.
  468. */
  469. mbedtls_mpi_uint carry = T[AN_limbs];
  470. mbedtls_mpi_uint borrow = mbedtls_mpi_core_sub(X, T, N, AN_limbs);
  471. /*
  472. * Using R as the Montgomery radix (auxiliary modulus) i.e. 2^(biL*AN_limbs):
  473. *
  474. * T can be in one of 3 ranges:
  475. *
  476. * 1) T < N : (carry, borrow) = (0, 1): we want T
  477. * 2) N <= T < R : (carry, borrow) = (0, 0): we want X
  478. * 3) T >= R : (carry, borrow) = (1, 1): we want X
  479. *
  480. * and (carry, borrow) = (1, 0) can't happen.
  481. *
  482. * So the correct return value is already in X if (carry ^ borrow) = 0,
  483. * but is in (the lower AN_limbs limbs of) T if (carry ^ borrow) = 1.
  484. */
  485. mbedtls_ct_memcpy_if(mbedtls_ct_bool(carry ^ borrow),
  486. (unsigned char *) X,
  487. (unsigned char *) T,
  488. NULL,
  489. AN_limbs * sizeof(mbedtls_mpi_uint));
  490. }
  491. int mbedtls_mpi_core_get_mont_r2_unsafe(mbedtls_mpi *X,
  492. const mbedtls_mpi *N)
  493. {
  494. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  495. MBEDTLS_MPI_CHK(mbedtls_mpi_lset(X, 1));
  496. MBEDTLS_MPI_CHK(mbedtls_mpi_shift_l(X, N->n * 2 * biL));
  497. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(X, X, N));
  498. MBEDTLS_MPI_CHK(mbedtls_mpi_shrink(X, N->n));
  499. cleanup:
  500. return ret;
  501. }
  502. MBEDTLS_STATIC_TESTABLE
  503. void mbedtls_mpi_core_ct_uint_table_lookup(mbedtls_mpi_uint *dest,
  504. const mbedtls_mpi_uint *table,
  505. size_t limbs,
  506. size_t count,
  507. size_t index)
  508. {
  509. for (size_t i = 0; i < count; i++, table += limbs) {
  510. mbedtls_ct_condition_t assign = mbedtls_ct_uint_eq(i, index);
  511. mbedtls_mpi_core_cond_assign(dest, table, limbs, assign);
  512. }
  513. }
  514. /* Fill X with n_bytes random bytes.
  515. * X must already have room for those bytes.
  516. * The ordering of the bytes returned from the RNG is suitable for
  517. * deterministic ECDSA (see RFC 6979 §3.3 and the specification of
  518. * mbedtls_mpi_core_random()).
  519. */
  520. int mbedtls_mpi_core_fill_random(
  521. mbedtls_mpi_uint *X, size_t X_limbs,
  522. size_t n_bytes,
  523. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  524. {
  525. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  526. const size_t limbs = CHARS_TO_LIMBS(n_bytes);
  527. const size_t overhead = (limbs * ciL) - n_bytes;
  528. if (X_limbs < limbs) {
  529. return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
  530. }
  531. memset(X, 0, overhead);
  532. memset((unsigned char *) X + limbs * ciL, 0, (X_limbs - limbs) * ciL);
  533. MBEDTLS_MPI_CHK(f_rng(p_rng, (unsigned char *) X + overhead, n_bytes));
  534. mbedtls_mpi_core_bigendian_to_host(X, limbs);
  535. cleanup:
  536. return ret;
  537. }
  538. int mbedtls_mpi_core_random(mbedtls_mpi_uint *X,
  539. mbedtls_mpi_uint min,
  540. const mbedtls_mpi_uint *N,
  541. size_t limbs,
  542. int (*f_rng)(void *, unsigned char *, size_t),
  543. void *p_rng)
  544. {
  545. mbedtls_ct_condition_t ge_lower = MBEDTLS_CT_TRUE, lt_upper = MBEDTLS_CT_FALSE;
  546. size_t n_bits = mbedtls_mpi_core_bitlen(N, limbs);
  547. size_t n_bytes = (n_bits + 7) / 8;
  548. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  549. /*
  550. * When min == 0, each try has at worst a probability 1/2 of failing
  551. * (the msb has a probability 1/2 of being 0, and then the result will
  552. * be < N), so after 30 tries failure probability is a most 2**(-30).
  553. *
  554. * When N is just below a power of 2, as is the case when generating
  555. * a random scalar on most elliptic curves, 1 try is enough with
  556. * overwhelming probability. When N is just above a power of 2,
  557. * as when generating a random scalar on secp224k1, each try has
  558. * a probability of failing that is almost 1/2.
  559. *
  560. * The probabilities are almost the same if min is nonzero but negligible
  561. * compared to N. This is always the case when N is crypto-sized, but
  562. * it's convenient to support small N for testing purposes. When N
  563. * is small, use a higher repeat count, otherwise the probability of
  564. * failure is macroscopic.
  565. */
  566. int count = (n_bytes > 4 ? 30 : 250);
  567. /*
  568. * Match the procedure given in RFC 6979 §3.3 (deterministic ECDSA)
  569. * when f_rng is a suitably parametrized instance of HMAC_DRBG:
  570. * - use the same byte ordering;
  571. * - keep the leftmost n_bits bits of the generated octet string;
  572. * - try until result is in the desired range.
  573. * This also avoids any bias, which is especially important for ECDSA.
  574. */
  575. do {
  576. MBEDTLS_MPI_CHK(mbedtls_mpi_core_fill_random(X, limbs,
  577. n_bytes,
  578. f_rng, p_rng));
  579. mbedtls_mpi_core_shift_r(X, limbs, 8 * n_bytes - n_bits);
  580. if (--count == 0) {
  581. ret = MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
  582. goto cleanup;
  583. }
  584. ge_lower = mbedtls_mpi_core_uint_le_mpi(min, X, limbs);
  585. lt_upper = mbedtls_mpi_core_lt_ct(X, N, limbs);
  586. } while (mbedtls_ct_bool_and(ge_lower, lt_upper) == MBEDTLS_CT_FALSE);
  587. cleanup:
  588. return ret;
  589. }
  590. static size_t exp_mod_get_window_size(size_t Ebits)
  591. {
  592. #if MBEDTLS_MPI_WINDOW_SIZE >= 6
  593. return (Ebits > 671) ? 6 : (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
  594. #elif MBEDTLS_MPI_WINDOW_SIZE == 5
  595. return (Ebits > 239) ? 5 : (Ebits > 79) ? 4 : 1;
  596. #elif MBEDTLS_MPI_WINDOW_SIZE > 1
  597. return (Ebits > 79) ? MBEDTLS_MPI_WINDOW_SIZE : 1;
  598. #else
  599. (void) Ebits;
  600. return 1;
  601. #endif
  602. }
  603. size_t mbedtls_mpi_core_exp_mod_working_limbs(size_t AN_limbs, size_t E_limbs)
  604. {
  605. const size_t wsize = exp_mod_get_window_size(E_limbs * biL);
  606. const size_t welem = ((size_t) 1) << wsize;
  607. /* How big does each part of the working memory pool need to be? */
  608. const size_t table_limbs = welem * AN_limbs;
  609. const size_t select_limbs = AN_limbs;
  610. const size_t temp_limbs = 2 * AN_limbs + 1;
  611. return table_limbs + select_limbs + temp_limbs;
  612. }
  613. static void exp_mod_precompute_window(const mbedtls_mpi_uint *A,
  614. const mbedtls_mpi_uint *N,
  615. size_t AN_limbs,
  616. mbedtls_mpi_uint mm,
  617. const mbedtls_mpi_uint *RR,
  618. size_t welem,
  619. mbedtls_mpi_uint *Wtable,
  620. mbedtls_mpi_uint *temp)
  621. {
  622. /* W[0] = 1 (in Montgomery presentation) */
  623. memset(Wtable, 0, AN_limbs * ciL);
  624. Wtable[0] = 1;
  625. mbedtls_mpi_core_montmul(Wtable, Wtable, RR, AN_limbs, N, AN_limbs, mm, temp);
  626. /* W[1] = A (already in Montgomery presentation) */
  627. mbedtls_mpi_uint *W1 = Wtable + AN_limbs;
  628. memcpy(W1, A, AN_limbs * ciL);
  629. /* W[i+1] = W[i] * W[1], i >= 2 */
  630. mbedtls_mpi_uint *Wprev = W1;
  631. for (size_t i = 2; i < welem; i++) {
  632. mbedtls_mpi_uint *Wcur = Wprev + AN_limbs;
  633. mbedtls_mpi_core_montmul(Wcur, Wprev, W1, AN_limbs, N, AN_limbs, mm, temp);
  634. Wprev = Wcur;
  635. }
  636. }
  637. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  638. // Set to a default that is neither MBEDTLS_MPI_IS_PUBLIC nor MBEDTLS_MPI_IS_SECRET
  639. int mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_PUBLIC + MBEDTLS_MPI_IS_SECRET + 1;
  640. #endif
  641. /*
  642. * This function calculates the indices of the exponent where the exponentiation algorithm should
  643. * start processing.
  644. *
  645. * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value,
  646. * this function is not constant time with respect to the exponent (parameter E).
  647. */
  648. static inline void exp_mod_calc_first_bit_optionally_safe(const mbedtls_mpi_uint *E,
  649. size_t E_limbs,
  650. int E_public,
  651. size_t *E_limb_index,
  652. size_t *E_bit_index)
  653. {
  654. if (E_public == MBEDTLS_MPI_IS_PUBLIC) {
  655. /*
  656. * Skip leading zero bits.
  657. */
  658. size_t E_bits = mbedtls_mpi_core_bitlen(E, E_limbs);
  659. if (E_bits == 0) {
  660. /*
  661. * If E is 0 mbedtls_mpi_core_bitlen() returns 0. Even if that is the case, we will want
  662. * to represent it as a single 0 bit and as such the bitlength will be 1.
  663. */
  664. E_bits = 1;
  665. }
  666. *E_limb_index = E_bits / biL;
  667. *E_bit_index = E_bits % biL;
  668. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  669. mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_PUBLIC;
  670. #endif
  671. } else {
  672. /*
  673. * Here we need to be constant time with respect to E and can't do anything better than
  674. * start at the first allocated bit.
  675. */
  676. *E_limb_index = E_limbs;
  677. *E_bit_index = 0;
  678. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  679. // Only mark the codepath safe if there wasn't an unsafe codepath before
  680. if (mbedtls_mpi_optionally_safe_codepath != MBEDTLS_MPI_IS_PUBLIC) {
  681. mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_SECRET;
  682. }
  683. #endif
  684. }
  685. }
  686. /*
  687. * Warning! If the parameter window_public has MBEDTLS_MPI_IS_PUBLIC as its value, this function is
  688. * not constant time with respect to the window parameter and consequently the exponent of the
  689. * exponentiation (parameter E of mbedtls_mpi_core_exp_mod_optionally_safe).
  690. */
  691. static inline void exp_mod_table_lookup_optionally_safe(mbedtls_mpi_uint *Wselect,
  692. mbedtls_mpi_uint *Wtable,
  693. size_t AN_limbs, size_t welem,
  694. mbedtls_mpi_uint window,
  695. int window_public)
  696. {
  697. if (window_public == MBEDTLS_MPI_IS_PUBLIC) {
  698. memcpy(Wselect, Wtable + window * AN_limbs, AN_limbs * ciL);
  699. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  700. mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_PUBLIC;
  701. #endif
  702. } else {
  703. /* Select Wtable[window] without leaking window through
  704. * memory access patterns. */
  705. mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
  706. AN_limbs, welem, window);
  707. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  708. // Only mark the codepath safe if there wasn't an unsafe codepath before
  709. if (mbedtls_mpi_optionally_safe_codepath != MBEDTLS_MPI_IS_PUBLIC) {
  710. mbedtls_mpi_optionally_safe_codepath = MBEDTLS_MPI_IS_SECRET;
  711. }
  712. #endif
  713. }
  714. }
  715. /* Exponentiation: X := A^E mod N.
  716. *
  717. * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value,
  718. * this function is not constant time with respect to the exponent (parameter E).
  719. *
  720. * A must already be in Montgomery form.
  721. *
  722. * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
  723. *
  724. * RR must contain 2^{2*biL} mod N.
  725. *
  726. * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
  727. * (The difference is that the body in our loop processes a single bit instead
  728. * of a full window.)
  729. */
  730. static void mbedtls_mpi_core_exp_mod_optionally_safe(mbedtls_mpi_uint *X,
  731. const mbedtls_mpi_uint *A,
  732. const mbedtls_mpi_uint *N,
  733. size_t AN_limbs,
  734. const mbedtls_mpi_uint *E,
  735. size_t E_limbs,
  736. int E_public,
  737. const mbedtls_mpi_uint *RR,
  738. mbedtls_mpi_uint *T)
  739. {
  740. /* We'll process the bits of E from most significant
  741. * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
  742. * (limb_index=0, E_bit_index=0). */
  743. size_t E_limb_index;
  744. size_t E_bit_index;
  745. exp_mod_calc_first_bit_optionally_safe(E, E_limbs, E_public,
  746. &E_limb_index, &E_bit_index);
  747. const size_t wsize = exp_mod_get_window_size(E_limb_index * biL);
  748. const size_t welem = ((size_t) 1) << wsize;
  749. /* This is how we will use the temporary storage T, which must have space
  750. * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
  751. const size_t table_limbs = welem * AN_limbs;
  752. const size_t select_limbs = AN_limbs;
  753. /* Pointers to specific parts of the temporary working memory pool */
  754. mbedtls_mpi_uint *const Wtable = T;
  755. mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
  756. mbedtls_mpi_uint *const temp = Wselect + select_limbs;
  757. /*
  758. * Window precomputation
  759. */
  760. const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
  761. /* Set Wtable[i] = A^i (in Montgomery representation) */
  762. exp_mod_precompute_window(A, N, AN_limbs,
  763. mm, RR,
  764. welem, Wtable, temp);
  765. /*
  766. * Fixed window exponentiation
  767. */
  768. /* X = 1 (in Montgomery presentation) initially */
  769. memcpy(X, Wtable, AN_limbs * ciL);
  770. /* At any given time, window contains window_bits bits from E.
  771. * window_bits can go up to wsize. */
  772. size_t window_bits = 0;
  773. mbedtls_mpi_uint window = 0;
  774. do {
  775. /* Square */
  776. mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
  777. /* Move to the next bit of the exponent */
  778. if (E_bit_index == 0) {
  779. --E_limb_index;
  780. E_bit_index = biL - 1;
  781. } else {
  782. --E_bit_index;
  783. }
  784. /* Insert next exponent bit into window */
  785. ++window_bits;
  786. window <<= 1;
  787. window |= (E[E_limb_index] >> E_bit_index) & 1;
  788. /* Clear window if it's full. Also clear the window at the end,
  789. * when we've finished processing the exponent. */
  790. if (window_bits == wsize ||
  791. (E_bit_index == 0 && E_limb_index == 0)) {
  792. exp_mod_table_lookup_optionally_safe(Wselect, Wtable, AN_limbs, welem,
  793. window, E_public);
  794. /* Multiply X by the selected element. */
  795. mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
  796. temp);
  797. window = 0;
  798. window_bits = 0;
  799. }
  800. } while (!(E_bit_index == 0 && E_limb_index == 0));
  801. }
  802. void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
  803. const mbedtls_mpi_uint *A,
  804. const mbedtls_mpi_uint *N, size_t AN_limbs,
  805. const mbedtls_mpi_uint *E, size_t E_limbs,
  806. const mbedtls_mpi_uint *RR,
  807. mbedtls_mpi_uint *T)
  808. {
  809. mbedtls_mpi_core_exp_mod_optionally_safe(X,
  810. A,
  811. N,
  812. AN_limbs,
  813. E,
  814. E_limbs,
  815. MBEDTLS_MPI_IS_SECRET,
  816. RR,
  817. T);
  818. }
  819. void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
  820. const mbedtls_mpi_uint *A,
  821. const mbedtls_mpi_uint *N, size_t AN_limbs,
  822. const mbedtls_mpi_uint *E, size_t E_limbs,
  823. const mbedtls_mpi_uint *RR,
  824. mbedtls_mpi_uint *T)
  825. {
  826. mbedtls_mpi_core_exp_mod_optionally_safe(X,
  827. A,
  828. N,
  829. AN_limbs,
  830. E,
  831. E_limbs,
  832. MBEDTLS_MPI_IS_PUBLIC,
  833. RR,
  834. T);
  835. }
  836. mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
  837. const mbedtls_mpi_uint *A,
  838. mbedtls_mpi_uint c, /* doubles as carry */
  839. size_t limbs)
  840. {
  841. for (size_t i = 0; i < limbs; i++) {
  842. mbedtls_mpi_uint s = A[i];
  843. mbedtls_mpi_uint t = s - c;
  844. c = (t > s);
  845. X[i] = t;
  846. }
  847. return c;
  848. }
  849. mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
  850. size_t limbs)
  851. {
  852. volatile const mbedtls_mpi_uint *force_read_A = A;
  853. mbedtls_mpi_uint bits = 0;
  854. for (size_t i = 0; i < limbs; i++) {
  855. bits |= force_read_A[i];
  856. }
  857. return mbedtls_ct_bool(bits);
  858. }
  859. void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
  860. const mbedtls_mpi_uint *A,
  861. const mbedtls_mpi_uint *N,
  862. size_t AN_limbs,
  863. mbedtls_mpi_uint mm,
  864. const mbedtls_mpi_uint *rr,
  865. mbedtls_mpi_uint *T)
  866. {
  867. mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
  868. }
  869. void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
  870. const mbedtls_mpi_uint *A,
  871. const mbedtls_mpi_uint *N,
  872. size_t AN_limbs,
  873. mbedtls_mpi_uint mm,
  874. mbedtls_mpi_uint *T)
  875. {
  876. const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
  877. mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
  878. }
  879. #endif /* MBEDTLS_BIGNUM_C */