bignum_core.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. void (*mbedtls_safe_codepath_hook)(void) = NULL;
  639. void (*mbedtls_unsafe_codepath_hook)(void) = NULL;
  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. if (mbedtls_unsafe_codepath_hook != NULL) {
  670. mbedtls_unsafe_codepath_hook();
  671. }
  672. #endif
  673. } else {
  674. /*
  675. * Here we need to be constant time with respect to E and can't do anything better than
  676. * start at the first allocated bit.
  677. */
  678. *E_limb_index = E_limbs;
  679. *E_bit_index = 0;
  680. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  681. if (mbedtls_safe_codepath_hook != NULL) {
  682. mbedtls_safe_codepath_hook();
  683. }
  684. #endif
  685. }
  686. }
  687. /*
  688. * Warning! If the parameter window_public has MBEDTLS_MPI_IS_PUBLIC as its value, this function is
  689. * not constant time with respect to the window parameter and consequently the exponent of the
  690. * exponentiation (parameter E of mbedtls_mpi_core_exp_mod_optionally_safe).
  691. */
  692. static inline void exp_mod_table_lookup_optionally_safe(mbedtls_mpi_uint *Wselect,
  693. mbedtls_mpi_uint *Wtable,
  694. size_t AN_limbs, size_t welem,
  695. mbedtls_mpi_uint window,
  696. int window_public)
  697. {
  698. if (window_public == MBEDTLS_MPI_IS_PUBLIC) {
  699. memcpy(Wselect, Wtable + window * AN_limbs, AN_limbs * ciL);
  700. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  701. if (mbedtls_unsafe_codepath_hook != NULL) {
  702. mbedtls_unsafe_codepath_hook();
  703. }
  704. #endif
  705. } else {
  706. /* Select Wtable[window] without leaking window through
  707. * memory access patterns. */
  708. mbedtls_mpi_core_ct_uint_table_lookup(Wselect, Wtable,
  709. AN_limbs, welem, window);
  710. #if defined(MBEDTLS_TEST_HOOKS) && !defined(MBEDTLS_THREADING_C)
  711. if (mbedtls_safe_codepath_hook != NULL) {
  712. mbedtls_safe_codepath_hook();
  713. }
  714. #endif
  715. }
  716. }
  717. /* Exponentiation: X := A^E mod N.
  718. *
  719. * Warning! If the parameter E_public has MBEDTLS_MPI_IS_PUBLIC as its value,
  720. * this function is not constant time with respect to the exponent (parameter E).
  721. *
  722. * A must already be in Montgomery form.
  723. *
  724. * As in other bignum functions, assume that AN_limbs and E_limbs are nonzero.
  725. *
  726. * RR must contain 2^{2*biL} mod N.
  727. *
  728. * The algorithm is a variant of Left-to-right k-ary exponentiation: HAC 14.82
  729. * (The difference is that the body in our loop processes a single bit instead
  730. * of a full window.)
  731. */
  732. static void mbedtls_mpi_core_exp_mod_optionally_safe(mbedtls_mpi_uint *X,
  733. const mbedtls_mpi_uint *A,
  734. const mbedtls_mpi_uint *N,
  735. size_t AN_limbs,
  736. const mbedtls_mpi_uint *E,
  737. size_t E_limbs,
  738. int E_public,
  739. const mbedtls_mpi_uint *RR,
  740. mbedtls_mpi_uint *T)
  741. {
  742. /* We'll process the bits of E from most significant
  743. * (limb_index=E_limbs-1, E_bit_index=biL-1) to least significant
  744. * (limb_index=0, E_bit_index=0). */
  745. size_t E_limb_index = E_limbs;
  746. size_t E_bit_index = 0;
  747. exp_mod_calc_first_bit_optionally_safe(E, E_limbs, E_public,
  748. &E_limb_index, &E_bit_index);
  749. const size_t wsize = exp_mod_get_window_size(E_limb_index * biL);
  750. const size_t welem = ((size_t) 1) << wsize;
  751. /* This is how we will use the temporary storage T, which must have space
  752. * for table_limbs, select_limbs and (2 * AN_limbs + 1) for montmul. */
  753. const size_t table_limbs = welem * AN_limbs;
  754. const size_t select_limbs = AN_limbs;
  755. /* Pointers to specific parts of the temporary working memory pool */
  756. mbedtls_mpi_uint *const Wtable = T;
  757. mbedtls_mpi_uint *const Wselect = Wtable + table_limbs;
  758. mbedtls_mpi_uint *const temp = Wselect + select_limbs;
  759. /*
  760. * Window precomputation
  761. */
  762. const mbedtls_mpi_uint mm = mbedtls_mpi_core_montmul_init(N);
  763. /* Set Wtable[i] = A^i (in Montgomery representation) */
  764. exp_mod_precompute_window(A, N, AN_limbs,
  765. mm, RR,
  766. welem, Wtable, temp);
  767. /*
  768. * Fixed window exponentiation
  769. */
  770. /* X = 1 (in Montgomery presentation) initially */
  771. memcpy(X, Wtable, AN_limbs * ciL);
  772. /* At any given time, window contains window_bits bits from E.
  773. * window_bits can go up to wsize. */
  774. size_t window_bits = 0;
  775. mbedtls_mpi_uint window = 0;
  776. do {
  777. /* Square */
  778. mbedtls_mpi_core_montmul(X, X, X, AN_limbs, N, AN_limbs, mm, temp);
  779. /* Move to the next bit of the exponent */
  780. if (E_bit_index == 0) {
  781. --E_limb_index;
  782. E_bit_index = biL - 1;
  783. } else {
  784. --E_bit_index;
  785. }
  786. /* Insert next exponent bit into window */
  787. ++window_bits;
  788. window <<= 1;
  789. window |= (E[E_limb_index] >> E_bit_index) & 1;
  790. /* Clear window if it's full. Also clear the window at the end,
  791. * when we've finished processing the exponent. */
  792. if (window_bits == wsize ||
  793. (E_bit_index == 0 && E_limb_index == 0)) {
  794. exp_mod_table_lookup_optionally_safe(Wselect, Wtable, AN_limbs, welem,
  795. window, E_public);
  796. /* Multiply X by the selected element. */
  797. mbedtls_mpi_core_montmul(X, X, Wselect, AN_limbs, N, AN_limbs, mm,
  798. temp);
  799. window = 0;
  800. window_bits = 0;
  801. }
  802. } while (!(E_bit_index == 0 && E_limb_index == 0));
  803. }
  804. void mbedtls_mpi_core_exp_mod(mbedtls_mpi_uint *X,
  805. const mbedtls_mpi_uint *A,
  806. const mbedtls_mpi_uint *N, size_t AN_limbs,
  807. const mbedtls_mpi_uint *E, size_t E_limbs,
  808. const mbedtls_mpi_uint *RR,
  809. mbedtls_mpi_uint *T)
  810. {
  811. mbedtls_mpi_core_exp_mod_optionally_safe(X,
  812. A,
  813. N,
  814. AN_limbs,
  815. E,
  816. E_limbs,
  817. MBEDTLS_MPI_IS_SECRET,
  818. RR,
  819. T);
  820. }
  821. void mbedtls_mpi_core_exp_mod_unsafe(mbedtls_mpi_uint *X,
  822. const mbedtls_mpi_uint *A,
  823. const mbedtls_mpi_uint *N, size_t AN_limbs,
  824. const mbedtls_mpi_uint *E, size_t E_limbs,
  825. const mbedtls_mpi_uint *RR,
  826. mbedtls_mpi_uint *T)
  827. {
  828. mbedtls_mpi_core_exp_mod_optionally_safe(X,
  829. A,
  830. N,
  831. AN_limbs,
  832. E,
  833. E_limbs,
  834. MBEDTLS_MPI_IS_PUBLIC,
  835. RR,
  836. T);
  837. }
  838. mbedtls_mpi_uint mbedtls_mpi_core_sub_int(mbedtls_mpi_uint *X,
  839. const mbedtls_mpi_uint *A,
  840. mbedtls_mpi_uint c, /* doubles as carry */
  841. size_t limbs)
  842. {
  843. for (size_t i = 0; i < limbs; i++) {
  844. mbedtls_mpi_uint s = A[i];
  845. mbedtls_mpi_uint t = s - c;
  846. c = (t > s);
  847. X[i] = t;
  848. }
  849. return c;
  850. }
  851. mbedtls_ct_condition_t mbedtls_mpi_core_check_zero_ct(const mbedtls_mpi_uint *A,
  852. size_t limbs)
  853. {
  854. volatile const mbedtls_mpi_uint *force_read_A = A;
  855. mbedtls_mpi_uint bits = 0;
  856. for (size_t i = 0; i < limbs; i++) {
  857. bits |= force_read_A[i];
  858. }
  859. return mbedtls_ct_bool(bits);
  860. }
  861. void mbedtls_mpi_core_to_mont_rep(mbedtls_mpi_uint *X,
  862. const mbedtls_mpi_uint *A,
  863. const mbedtls_mpi_uint *N,
  864. size_t AN_limbs,
  865. mbedtls_mpi_uint mm,
  866. const mbedtls_mpi_uint *rr,
  867. mbedtls_mpi_uint *T)
  868. {
  869. mbedtls_mpi_core_montmul(X, A, rr, AN_limbs, N, AN_limbs, mm, T);
  870. }
  871. void mbedtls_mpi_core_from_mont_rep(mbedtls_mpi_uint *X,
  872. const mbedtls_mpi_uint *A,
  873. const mbedtls_mpi_uint *N,
  874. size_t AN_limbs,
  875. mbedtls_mpi_uint mm,
  876. mbedtls_mpi_uint *T)
  877. {
  878. const mbedtls_mpi_uint Rinv = 1; /* 1/R in Mont. rep => 1 */
  879. mbedtls_mpi_core_montmul(X, A, &Rinv, 1, N, AN_limbs, mm, T);
  880. }
  881. #endif /* MBEDTLS_BIGNUM_C */