test_suite_mpi.function 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/bignum.h"
  3. #include "mbedtls/entropy.h"
  4. #if MBEDTLS_MPI_MAX_BITS > 792
  5. #define MPI_MAX_BITS_LARGER_THAN_792
  6. #endif
  7. /* Check the validity of the sign bit in an MPI object. Reject representations
  8. * that are not supported by the rest of the library and indicate a bug when
  9. * constructing the value. */
  10. static int sign_is_valid( const mbedtls_mpi *X )
  11. {
  12. if( X->s != 1 && X->s != -1 )
  13. return( 0 ); // invalid sign bit, e.g. 0
  14. if( mbedtls_mpi_bitlen( X ) == 0 && X->s != 1 )
  15. return( 0 ); // negative zero
  16. return( 1 );
  17. }
  18. typedef struct mbedtls_test_mpi_random
  19. {
  20. data_t *data;
  21. size_t pos;
  22. size_t chunk_len;
  23. } mbedtls_test_mpi_random;
  24. /*
  25. * This function is called by the Miller-Rabin primality test each time it
  26. * chooses a random witness. The witnesses (or non-witnesses as provided by the
  27. * test) are stored in the data member of the state structure. Each number is in
  28. * the format that mbedtls_mpi_read_string understands and is chunk_len long.
  29. */
  30. int mbedtls_test_mpi_miller_rabin_determinizer( void* state,
  31. unsigned char* buf,
  32. size_t len )
  33. {
  34. mbedtls_test_mpi_random *random = (mbedtls_test_mpi_random*) state;
  35. if( random == NULL || random->data->x == NULL || buf == NULL )
  36. return( -1 );
  37. if( random->pos + random->chunk_len > random->data->len
  38. || random->chunk_len > len )
  39. {
  40. return( -1 );
  41. }
  42. memset( buf, 0, len );
  43. /* The witness is written to the end of the buffer, since the buffer is
  44. * used as big endian, unsigned binary data in mbedtls_mpi_read_binary.
  45. * Writing the witness to the start of the buffer would result in the
  46. * buffer being 'witness 000...000', which would be treated as
  47. * witness * 2^n for some n. */
  48. memcpy( buf + len - random->chunk_len, &random->data->x[random->pos],
  49. random->chunk_len );
  50. random->pos += random->chunk_len;
  51. return( 0 );
  52. }
  53. /* Random generator that is told how many bytes to return. */
  54. static int f_rng_bytes_left( void *state, unsigned char *buf, size_t len )
  55. {
  56. size_t *bytes_left = state;
  57. size_t i;
  58. for( i = 0; i < len; i++ )
  59. {
  60. if( *bytes_left == 0 )
  61. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  62. buf[i] = *bytes_left & 0xff;
  63. --( *bytes_left );
  64. }
  65. return( 0 );
  66. }
  67. /* Test whether bytes represents (in big-endian base 256) a number b that
  68. * is significantly above a power of 2. That is, b must not have a long run
  69. * of unset bits after the most significant bit.
  70. *
  71. * Let n be the bit-size of b, i.e. the integer such that 2^n <= b < 2^{n+1}.
  72. * This function returns 1 if, when drawing a number between 0 and b,
  73. * the probability that this number is at least 2^n is not negligible.
  74. * This probability is (b - 2^n) / b and this function checks that this
  75. * number is above some threshold A. The threshold value is heuristic and
  76. * based on the needs of mpi_random_many().
  77. */
  78. static int is_significantly_above_a_power_of_2( data_t *bytes )
  79. {
  80. const uint8_t *p = bytes->x;
  81. size_t len = bytes->len;
  82. unsigned x;
  83. /* Skip leading null bytes */
  84. while( len > 0 && p[0] == 0 )
  85. {
  86. ++p;
  87. --len;
  88. }
  89. /* 0 is not significantly above a power of 2 */
  90. if( len == 0 )
  91. return( 0 );
  92. /* Extract the (up to) 2 most significant bytes */
  93. if( len == 1 )
  94. x = p[0];
  95. else
  96. x = ( p[0] << 8 ) | p[1];
  97. /* Shift the most significant bit of x to position 8 and mask it out */
  98. while( ( x & 0xfe00 ) != 0 )
  99. x >>= 1;
  100. x &= 0x00ff;
  101. /* At this point, x = floor((b - 2^n) / 2^(n-8)). b is significantly above
  102. * a power of 2 iff x is significantly above 0 compared to 2^8.
  103. * Testing x >= 2^4 amounts to picking A = 1/16 in the function
  104. * description above. */
  105. return( x >= 0x10 );
  106. }
  107. /* END_HEADER */
  108. /* BEGIN_DEPENDENCIES
  109. * depends_on:MBEDTLS_BIGNUM_C
  110. * END_DEPENDENCIES
  111. */
  112. /* BEGIN_CASE */
  113. void mpi_valid_param( )
  114. {
  115. TEST_VALID_PARAM( mbedtls_mpi_free( NULL ) );
  116. }
  117. /* END_CASE */
  118. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  119. void mpi_invalid_param( )
  120. {
  121. mbedtls_mpi X;
  122. const char *s_in = "00101000101010";
  123. char s_out[16] = { 0 };
  124. unsigned char u_out[16] = { 0 };
  125. unsigned char u_in[16] = { 0 };
  126. size_t olen;
  127. mbedtls_mpi_uint mpi_uint;
  128. TEST_INVALID_PARAM( mbedtls_mpi_init( NULL ) );
  129. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  130. mbedtls_mpi_grow( NULL, 42 ) );
  131. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  132. mbedtls_mpi_copy( NULL, &X ) );
  133. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  134. mbedtls_mpi_copy( &X, NULL ) );
  135. TEST_INVALID_PARAM( mbedtls_mpi_swap( NULL, &X ) );
  136. TEST_INVALID_PARAM( mbedtls_mpi_swap( &X, NULL ) );
  137. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  138. mbedtls_mpi_safe_cond_assign( NULL, &X, 0 ) );
  139. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  140. mbedtls_mpi_safe_cond_assign( &X, NULL, 0 ) );
  141. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  142. mbedtls_mpi_safe_cond_swap( NULL, &X, 0 ) );
  143. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  144. mbedtls_mpi_safe_cond_swap( &X, NULL, 0 ) );
  145. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  146. mbedtls_mpi_lset( NULL, 42 ) );
  147. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  148. mbedtls_mpi_get_bit( NULL, 42 ) );
  149. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  150. mbedtls_mpi_set_bit( NULL, 42, 0 ) );
  151. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  152. mbedtls_mpi_read_string( NULL, 2, s_in ) );
  153. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  154. mbedtls_mpi_read_string( &X, 2, NULL ) );
  155. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  156. mbedtls_mpi_write_string( NULL, 2,
  157. s_out, sizeof( s_out ),
  158. &olen ) );
  159. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  160. mbedtls_mpi_write_string( &X, 2,
  161. NULL, sizeof( s_out ),
  162. &olen ) );
  163. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  164. mbedtls_mpi_write_string( &X, 2,
  165. s_out, sizeof( s_out ),
  166. NULL ) );
  167. #if defined(MBEDTLS_FS_IO)
  168. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  169. mbedtls_mpi_read_file( NULL, 2, stdin ) );
  170. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  171. mbedtls_mpi_read_file( &X, 2, NULL ) );
  172. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  173. mbedtls_mpi_write_file( "", NULL, 2, NULL ) );
  174. #endif /* MBEDTLS_FS_IO */
  175. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  176. mbedtls_mpi_read_binary( NULL, u_in,
  177. sizeof( u_in ) ) );
  178. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  179. mbedtls_mpi_read_binary( &X, NULL,
  180. sizeof( u_in ) ) );
  181. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  182. mbedtls_mpi_write_binary( NULL, u_out,
  183. sizeof( u_out ) ) );
  184. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  185. mbedtls_mpi_write_binary( &X, NULL,
  186. sizeof( u_out ) ) );
  187. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  188. mbedtls_mpi_shift_l( NULL, 42 ) );
  189. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  190. mbedtls_mpi_shift_r( NULL, 42 ) );
  191. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  192. mbedtls_mpi_cmp_abs( NULL, &X ) );
  193. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  194. mbedtls_mpi_cmp_abs( &X, NULL ) );
  195. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  196. mbedtls_mpi_cmp_mpi( NULL, &X ) );
  197. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  198. mbedtls_mpi_cmp_mpi( &X, NULL ) );
  199. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  200. mbedtls_mpi_cmp_int( NULL, 42 ) );
  201. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  202. mbedtls_mpi_add_abs( NULL, &X, &X ) );
  203. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  204. mbedtls_mpi_add_abs( &X, NULL, &X ) );
  205. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  206. mbedtls_mpi_add_abs( &X, &X, NULL ) );
  207. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  208. mbedtls_mpi_sub_abs( NULL, &X, &X ) );
  209. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  210. mbedtls_mpi_sub_abs( &X, NULL, &X ) );
  211. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  212. mbedtls_mpi_sub_abs( &X, &X, NULL ) );
  213. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  214. mbedtls_mpi_add_mpi( NULL, &X, &X ) );
  215. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  216. mbedtls_mpi_add_mpi( &X, NULL, &X ) );
  217. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  218. mbedtls_mpi_add_mpi( &X, &X, NULL ) );
  219. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  220. mbedtls_mpi_sub_mpi( NULL, &X, &X ) );
  221. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  222. mbedtls_mpi_sub_mpi( &X, NULL, &X ) );
  223. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  224. mbedtls_mpi_sub_mpi( &X, &X, NULL ) );
  225. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  226. mbedtls_mpi_add_int( NULL, &X, 42 ) );
  227. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  228. mbedtls_mpi_add_int( &X, NULL, 42 ) );
  229. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  230. mbedtls_mpi_sub_int( NULL, &X, 42 ) );
  231. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  232. mbedtls_mpi_sub_int( &X, NULL, 42 ) );
  233. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  234. mbedtls_mpi_mul_mpi( NULL, &X, &X ) );
  235. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  236. mbedtls_mpi_mul_mpi( &X, NULL, &X ) );
  237. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  238. mbedtls_mpi_mul_mpi( &X, &X, NULL ) );
  239. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  240. mbedtls_mpi_mul_int( NULL, &X, 42 ) );
  241. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  242. mbedtls_mpi_mul_int( &X, NULL, 42 ) );
  243. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  244. mbedtls_mpi_div_mpi( &X, &X, NULL, &X ) );
  245. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  246. mbedtls_mpi_div_mpi( &X, &X, &X, NULL ) );
  247. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  248. mbedtls_mpi_div_int( &X, &X, NULL, 42 ) );
  249. TEST_INVALID_PARAM_RET( 0, mbedtls_mpi_lsb( NULL ) );
  250. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  251. mbedtls_mpi_mod_mpi( NULL, &X, &X ) );
  252. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  253. mbedtls_mpi_mod_mpi( &X, NULL, &X ) );
  254. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  255. mbedtls_mpi_mod_mpi( &X, &X, NULL ) );
  256. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  257. mbedtls_mpi_mod_int( NULL, &X, 42 ) );
  258. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  259. mbedtls_mpi_mod_int( &mpi_uint, NULL, 42 ) );
  260. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  261. mbedtls_mpi_exp_mod( NULL, &X, &X, &X, NULL ) );
  262. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  263. mbedtls_mpi_exp_mod( &X, NULL, &X, &X, NULL ) );
  264. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  265. mbedtls_mpi_exp_mod( &X, &X, NULL, &X, NULL ) );
  266. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  267. mbedtls_mpi_exp_mod( &X, &X, &X, NULL, NULL ) );
  268. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  269. mbedtls_mpi_fill_random( NULL, 42,
  270. mbedtls_test_rnd_std_rand,
  271. NULL ) );
  272. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  273. mbedtls_mpi_fill_random( &X, 42, NULL, NULL ) );
  274. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  275. mbedtls_mpi_gcd( NULL, &X, &X ) );
  276. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  277. mbedtls_mpi_gcd( &X, NULL, &X ) );
  278. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  279. mbedtls_mpi_gcd( &X, &X, NULL ) );
  280. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  281. mbedtls_mpi_inv_mod( NULL, &X, &X ) );
  282. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  283. mbedtls_mpi_inv_mod( &X, NULL, &X ) );
  284. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_MPI_BAD_INPUT_DATA,
  285. mbedtls_mpi_inv_mod( &X, &X, NULL ) );
  286. exit:
  287. return;
  288. }
  289. /* END_CASE */
  290. /* BEGIN_CASE */
  291. void mpi_null( )
  292. {
  293. mbedtls_mpi X, Y, Z;
  294. mbedtls_mpi_init( &X );
  295. mbedtls_mpi_init( &Y );
  296. mbedtls_mpi_init( &Z );
  297. TEST_ASSERT( mbedtls_mpi_get_bit( &X, 42 ) == 0 );
  298. TEST_ASSERT( mbedtls_mpi_lsb( &X ) == 0 );
  299. TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == 0 );
  300. TEST_ASSERT( mbedtls_mpi_size( &X ) == 0 );
  301. exit:
  302. mbedtls_mpi_free( &X );
  303. }
  304. /* END_CASE */
  305. /* BEGIN_CASE */
  306. void mpi_read_write_string( int radix_X, char * input_X, int radix_A,
  307. char * input_A, int output_size, int result_read,
  308. int result_write )
  309. {
  310. mbedtls_mpi X;
  311. char str[1000];
  312. size_t len;
  313. mbedtls_mpi_init( &X );
  314. memset( str, '!', sizeof( str ) );
  315. TEST_ASSERT( mbedtls_mpi_read_string( &X, radix_X, input_X ) == result_read );
  316. if( result_read == 0 )
  317. {
  318. TEST_ASSERT( sign_is_valid( &X ) );
  319. TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, output_size, &len ) == result_write );
  320. if( result_write == 0 )
  321. {
  322. TEST_ASSERT( strcasecmp( str, input_A ) == 0 );
  323. TEST_ASSERT( str[len] == '!' );
  324. }
  325. }
  326. exit:
  327. mbedtls_mpi_free( &X );
  328. }
  329. /* END_CASE */
  330. /* BEGIN_CASE */
  331. void mbedtls_mpi_read_binary( data_t * buf, int radix_A, char * input_A )
  332. {
  333. mbedtls_mpi X;
  334. char str[1000];
  335. size_t len;
  336. mbedtls_mpi_init( &X );
  337. TEST_ASSERT( mbedtls_mpi_read_binary( &X, buf->x, buf->len ) == 0 );
  338. TEST_ASSERT( sign_is_valid( &X ) );
  339. TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
  340. TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
  341. exit:
  342. mbedtls_mpi_free( &X );
  343. }
  344. /* END_CASE */
  345. /* BEGIN_CASE */
  346. void mbedtls_mpi_read_binary_le( data_t * buf, int radix_A, char * input_A )
  347. {
  348. mbedtls_mpi X;
  349. char str[1000];
  350. size_t len;
  351. mbedtls_mpi_init( &X );
  352. TEST_ASSERT( mbedtls_mpi_read_binary_le( &X, buf->x, buf->len ) == 0 );
  353. TEST_ASSERT( sign_is_valid( &X ) );
  354. TEST_ASSERT( mbedtls_mpi_write_string( &X, radix_A, str, sizeof( str ), &len ) == 0 );
  355. TEST_ASSERT( strcmp( (char *) str, input_A ) == 0 );
  356. exit:
  357. mbedtls_mpi_free( &X );
  358. }
  359. /* END_CASE */
  360. /* BEGIN_CASE */
  361. void mbedtls_mpi_write_binary( int radix_X, char * input_X,
  362. data_t * input_A, int output_size,
  363. int result )
  364. {
  365. mbedtls_mpi X;
  366. unsigned char buf[1000];
  367. size_t buflen;
  368. memset( buf, 0x00, 1000 );
  369. mbedtls_mpi_init( &X );
  370. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  371. buflen = mbedtls_mpi_size( &X );
  372. if( buflen > (size_t) output_size )
  373. buflen = (size_t) output_size;
  374. TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == result );
  375. if( result == 0)
  376. {
  377. TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
  378. buflen, input_A->len ) == 0 );
  379. }
  380. exit:
  381. mbedtls_mpi_free( &X );
  382. }
  383. /* END_CASE */
  384. /* BEGIN_CASE */
  385. void mbedtls_mpi_write_binary_le( int radix_X, char * input_X,
  386. data_t * input_A, int output_size,
  387. int result )
  388. {
  389. mbedtls_mpi X;
  390. unsigned char buf[1000];
  391. size_t buflen;
  392. memset( buf, 0x00, 1000 );
  393. mbedtls_mpi_init( &X );
  394. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  395. buflen = mbedtls_mpi_size( &X );
  396. if( buflen > (size_t) output_size )
  397. buflen = (size_t) output_size;
  398. TEST_ASSERT( mbedtls_mpi_write_binary_le( &X, buf, buflen ) == result );
  399. if( result == 0)
  400. {
  401. TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
  402. buflen, input_A->len ) == 0 );
  403. }
  404. exit:
  405. mbedtls_mpi_free( &X );
  406. }
  407. /* END_CASE */
  408. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  409. void mbedtls_mpi_read_file( int radix_X, char * input_file,
  410. data_t * input_A, int result )
  411. {
  412. mbedtls_mpi X;
  413. unsigned char buf[1000];
  414. size_t buflen;
  415. FILE *file;
  416. int ret;
  417. memset( buf, 0x00, 1000 );
  418. mbedtls_mpi_init( &X );
  419. file = fopen( input_file, "r" );
  420. TEST_ASSERT( file != NULL );
  421. ret = mbedtls_mpi_read_file( &X, radix_X, file );
  422. fclose(file);
  423. TEST_ASSERT( ret == result );
  424. if( result == 0 )
  425. {
  426. TEST_ASSERT( sign_is_valid( &X ) );
  427. buflen = mbedtls_mpi_size( &X );
  428. TEST_ASSERT( mbedtls_mpi_write_binary( &X, buf, buflen ) == 0 );
  429. TEST_ASSERT( mbedtls_test_hexcmp( buf, input_A->x,
  430. buflen, input_A->len ) == 0 );
  431. }
  432. exit:
  433. mbedtls_mpi_free( &X );
  434. }
  435. /* END_CASE */
  436. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  437. void mbedtls_mpi_write_file( int radix_X, char * input_X, int output_radix,
  438. char * output_file )
  439. {
  440. mbedtls_mpi X, Y;
  441. FILE *file_out, *file_in;
  442. int ret;
  443. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  444. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  445. file_out = fopen( output_file, "w" );
  446. TEST_ASSERT( file_out != NULL );
  447. ret = mbedtls_mpi_write_file( NULL, &X, output_radix, file_out );
  448. fclose(file_out);
  449. TEST_ASSERT( ret == 0 );
  450. file_in = fopen( output_file, "r" );
  451. TEST_ASSERT( file_in != NULL );
  452. ret = mbedtls_mpi_read_file( &Y, output_radix, file_in );
  453. fclose(file_in);
  454. TEST_ASSERT( ret == 0 );
  455. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
  456. exit:
  457. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  458. }
  459. /* END_CASE */
  460. /* BEGIN_CASE */
  461. void mbedtls_mpi_get_bit( int radix_X, char * input_X, int pos, int val )
  462. {
  463. mbedtls_mpi X;
  464. mbedtls_mpi_init( &X );
  465. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  466. TEST_ASSERT( mbedtls_mpi_get_bit( &X, pos ) == val );
  467. exit:
  468. mbedtls_mpi_free( &X );
  469. }
  470. /* END_CASE */
  471. /* BEGIN_CASE */
  472. void mbedtls_mpi_set_bit( int radix_X, char * input_X, int pos, int val,
  473. int radix_Y, char * output_Y, int result )
  474. {
  475. mbedtls_mpi X, Y;
  476. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  477. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  478. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, output_Y ) == 0 );
  479. TEST_ASSERT( mbedtls_mpi_set_bit( &X, pos, val ) == result );
  480. if( result == 0 )
  481. {
  482. TEST_ASSERT( sign_is_valid( &X ) );
  483. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == 0 );
  484. }
  485. exit:
  486. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  487. }
  488. /* END_CASE */
  489. /* BEGIN_CASE */
  490. void mbedtls_mpi_lsb( int radix_X, char * input_X, int nr_bits )
  491. {
  492. mbedtls_mpi X;
  493. mbedtls_mpi_init( &X );
  494. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  495. TEST_ASSERT( mbedtls_mpi_lsb( &X ) == (size_t) nr_bits );
  496. exit:
  497. mbedtls_mpi_free( &X );
  498. }
  499. /* END_CASE */
  500. /* BEGIN_CASE */
  501. void mbedtls_mpi_bitlen( int radix_X, char * input_X, int nr_bits )
  502. {
  503. mbedtls_mpi X;
  504. mbedtls_mpi_init( &X );
  505. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  506. TEST_ASSERT( mbedtls_mpi_bitlen( &X ) == (size_t) nr_bits );
  507. exit:
  508. mbedtls_mpi_free( &X );
  509. }
  510. /* END_CASE */
  511. /* BEGIN_CASE */
  512. void mbedtls_mpi_gcd( int radix_X, char * input_X, int radix_Y,
  513. char * input_Y, int radix_A, char * input_A )
  514. {
  515. mbedtls_mpi A, X, Y, Z;
  516. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z );
  517. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  518. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  519. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  520. TEST_ASSERT( mbedtls_mpi_gcd( &Z, &X, &Y ) == 0 );
  521. TEST_ASSERT( sign_is_valid( &Z ) );
  522. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  523. exit:
  524. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z );
  525. }
  526. /* END_CASE */
  527. /* BEGIN_CASE */
  528. void mbedtls_mpi_cmp_int( int input_X, int input_A, int result_CMP )
  529. {
  530. mbedtls_mpi X;
  531. mbedtls_mpi_init( &X );
  532. TEST_ASSERT( mbedtls_mpi_lset( &X, input_X ) == 0);
  533. TEST_ASSERT( mbedtls_mpi_cmp_int( &X, input_A ) == result_CMP);
  534. exit:
  535. mbedtls_mpi_free( &X );
  536. }
  537. /* END_CASE */
  538. /* BEGIN_CASE */
  539. void mbedtls_mpi_cmp_mpi( int radix_X, char * input_X, int radix_Y,
  540. char * input_Y, int input_A )
  541. {
  542. mbedtls_mpi X, Y;
  543. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  544. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  545. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  546. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y ) == input_A );
  547. exit:
  548. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  549. }
  550. /* END_CASE */
  551. /* BEGIN_CASE */
  552. void mbedtls_mpi_lt_mpi_ct( int size_X, char * input_X,
  553. int size_Y, char * input_Y,
  554. int input_ret, int input_err )
  555. {
  556. unsigned ret = -1;
  557. unsigned input_uret = input_ret;
  558. mbedtls_mpi X, Y;
  559. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  560. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
  561. TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, input_Y ) == 0 );
  562. TEST_ASSERT( mbedtls_mpi_grow( &X, size_X ) == 0 );
  563. TEST_ASSERT( mbedtls_mpi_grow( &Y, size_Y ) == 0 );
  564. TEST_ASSERT( mbedtls_mpi_lt_mpi_ct( &X, &Y, &ret ) == input_err );
  565. if( input_err == 0 )
  566. TEST_ASSERT( ret == input_uret );
  567. exit:
  568. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  569. }
  570. /* END_CASE */
  571. /* BEGIN_CASE */
  572. void mbedtls_mpi_cmp_abs( int radix_X, char * input_X, int radix_Y,
  573. char * input_Y, int input_A )
  574. {
  575. mbedtls_mpi X, Y;
  576. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  577. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  578. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  579. TEST_ASSERT( mbedtls_mpi_cmp_abs( &X, &Y ) == input_A );
  580. exit:
  581. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  582. }
  583. /* END_CASE */
  584. /* BEGIN_CASE */
  585. void mbedtls_mpi_copy( char *src_hex, char *dst_hex )
  586. {
  587. mbedtls_mpi src, dst, ref;
  588. mbedtls_mpi_init( &src );
  589. mbedtls_mpi_init( &dst );
  590. mbedtls_mpi_init( &ref );
  591. TEST_ASSERT( mbedtls_test_read_mpi( &src, 16, src_hex ) == 0 );
  592. TEST_ASSERT( mbedtls_test_read_mpi( &ref, 16, dst_hex ) == 0 );
  593. /* mbedtls_mpi_copy() */
  594. TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
  595. TEST_ASSERT( mbedtls_mpi_copy( &dst, &src ) == 0 );
  596. TEST_ASSERT( sign_is_valid( &dst ) );
  597. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
  598. /* mbedtls_mpi_safe_cond_assign(), assignment done */
  599. mbedtls_mpi_free( &dst );
  600. TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
  601. TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 1 ) == 0 );
  602. TEST_ASSERT( sign_is_valid( &dst ) );
  603. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &src ) == 0 );
  604. /* mbedtls_mpi_safe_cond_assign(), assignment not done */
  605. mbedtls_mpi_free( &dst );
  606. TEST_ASSERT( mbedtls_test_read_mpi( &dst, 16, dst_hex ) == 0 );
  607. TEST_ASSERT( mbedtls_mpi_safe_cond_assign( &dst, &src, 0 ) == 0 );
  608. TEST_ASSERT( sign_is_valid( &dst ) );
  609. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &dst, &ref ) == 0 );
  610. exit:
  611. mbedtls_mpi_free( &src );
  612. mbedtls_mpi_free( &dst );
  613. mbedtls_mpi_free( &ref );
  614. }
  615. /* END_CASE */
  616. /* BEGIN_CASE */
  617. void mpi_copy_self( char *input_X )
  618. {
  619. mbedtls_mpi X, A;
  620. mbedtls_mpi_init( &A );
  621. mbedtls_mpi_init( &X );
  622. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, input_X ) == 0 );
  623. TEST_ASSERT( mbedtls_mpi_copy( &X, &X ) == 0 );
  624. TEST_ASSERT( mbedtls_test_read_mpi( &A, 16, input_X ) == 0 );
  625. TEST_ASSERT( sign_is_valid( &X ) );
  626. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  627. exit:
  628. mbedtls_mpi_free( &A );
  629. mbedtls_mpi_free( &X );
  630. }
  631. /* END_CASE */
  632. /* BEGIN_CASE */
  633. void mbedtls_mpi_swap( char *X_hex, char *Y_hex )
  634. {
  635. mbedtls_mpi X, Y, X0, Y0;
  636. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y );
  637. mbedtls_mpi_init( &X0 ); mbedtls_mpi_init( &Y0 );
  638. TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
  639. TEST_ASSERT( mbedtls_test_read_mpi( &Y0, 16, Y_hex ) == 0 );
  640. /* mbedtls_mpi_swap() */
  641. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
  642. TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
  643. mbedtls_mpi_swap( &X, &Y );
  644. TEST_ASSERT( sign_is_valid( &X ) );
  645. TEST_ASSERT( sign_is_valid( &Y ) );
  646. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
  647. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
  648. /* mbedtls_mpi_safe_cond_swap(), swap done */
  649. mbedtls_mpi_free( &X );
  650. mbedtls_mpi_free( &Y );
  651. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
  652. TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
  653. TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 1 ) == 0 );
  654. TEST_ASSERT( sign_is_valid( &X ) );
  655. TEST_ASSERT( sign_is_valid( &Y ) );
  656. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &Y0 ) == 0 );
  657. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &X0 ) == 0 );
  658. /* mbedtls_mpi_safe_cond_swap(), swap not done */
  659. mbedtls_mpi_free( &X );
  660. mbedtls_mpi_free( &Y );
  661. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
  662. TEST_ASSERT( mbedtls_test_read_mpi( &Y, 16, Y_hex ) == 0 );
  663. TEST_ASSERT( mbedtls_mpi_safe_cond_swap( &X, &Y, 0 ) == 0 );
  664. TEST_ASSERT( sign_is_valid( &X ) );
  665. TEST_ASSERT( sign_is_valid( &Y ) );
  666. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
  667. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &Y0 ) == 0 );
  668. exit:
  669. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y );
  670. mbedtls_mpi_free( &X0 ); mbedtls_mpi_free( &Y0 );
  671. }
  672. /* END_CASE */
  673. /* BEGIN_CASE */
  674. void mpi_swap_self( char *X_hex )
  675. {
  676. mbedtls_mpi X, X0;
  677. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &X0 );
  678. TEST_ASSERT( mbedtls_test_read_mpi( &X, 16, X_hex ) == 0 );
  679. TEST_ASSERT( mbedtls_test_read_mpi( &X0, 16, X_hex ) == 0 );
  680. mbedtls_mpi_swap( &X, &X );
  681. TEST_ASSERT( sign_is_valid( &X ) );
  682. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &X0 ) == 0 );
  683. exit:
  684. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &X0 );
  685. }
  686. /* END_CASE */
  687. /* BEGIN_CASE */
  688. void mbedtls_mpi_shrink( int before, int used, int min, int after )
  689. {
  690. mbedtls_mpi X;
  691. mbedtls_mpi_init( &X );
  692. TEST_ASSERT( mbedtls_mpi_grow( &X, before ) == 0 );
  693. if( used > 0 )
  694. {
  695. size_t used_bit_count = used * 8 * sizeof( mbedtls_mpi_uint );
  696. TEST_ASSERT( mbedtls_mpi_set_bit( &X, used_bit_count - 1, 1 ) == 0 );
  697. }
  698. TEST_EQUAL( X.n, (size_t) before );
  699. TEST_ASSERT( mbedtls_mpi_shrink( &X, min ) == 0 );
  700. TEST_EQUAL( X.n, (size_t) after );
  701. exit:
  702. mbedtls_mpi_free( &X );
  703. }
  704. /* END_CASE */
  705. /* BEGIN_CASE */
  706. void mbedtls_mpi_add_mpi( int radix_X, char * input_X, int radix_Y,
  707. char * input_Y, int radix_A, char * input_A )
  708. {
  709. mbedtls_mpi X, Y, Z, A;
  710. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  711. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  712. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  713. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  714. TEST_ASSERT( mbedtls_mpi_add_mpi( &Z, &X, &Y ) == 0 );
  715. TEST_ASSERT( sign_is_valid( &Z ) );
  716. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  717. /* result == first operand */
  718. TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &Y ) == 0 );
  719. TEST_ASSERT( sign_is_valid( &X ) );
  720. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  721. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  722. /* result == second operand */
  723. TEST_ASSERT( mbedtls_mpi_add_mpi( &Y, &X, &Y ) == 0 );
  724. TEST_ASSERT( sign_is_valid( &Y ) );
  725. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
  726. exit:
  727. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  728. }
  729. /* END_CASE */
  730. /* BEGIN_CASE */
  731. void mbedtls_mpi_add_mpi_inplace( int radix_X, char * input_X, int radix_A,
  732. char * input_A )
  733. {
  734. mbedtls_mpi X, A;
  735. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
  736. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  737. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  738. TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &X ) == 0 );
  739. TEST_ASSERT( mbedtls_mpi_cmp_int( &X, 0 ) == 0 );
  740. TEST_ASSERT( sign_is_valid( &X ) );
  741. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  742. TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &X ) == 0 );
  743. TEST_ASSERT( sign_is_valid( &X ) );
  744. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  745. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  746. TEST_ASSERT( mbedtls_mpi_add_mpi( &X, &X, &X ) == 0 );
  747. TEST_ASSERT( sign_is_valid( &X ) );
  748. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  749. exit:
  750. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
  751. }
  752. /* END_CASE */
  753. /* BEGIN_CASE */
  754. void mbedtls_mpi_add_abs( int radix_X, char * input_X, int radix_Y,
  755. char * input_Y, int radix_A, char * input_A )
  756. {
  757. mbedtls_mpi X, Y, Z, A;
  758. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  759. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  760. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  761. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  762. TEST_ASSERT( mbedtls_mpi_add_abs( &Z, &X, &Y ) == 0 );
  763. TEST_ASSERT( sign_is_valid( &Z ) );
  764. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  765. /* result == first operand */
  766. TEST_ASSERT( mbedtls_mpi_add_abs( &X, &X, &Y ) == 0 );
  767. TEST_ASSERT( sign_is_valid( &X ) );
  768. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  769. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  770. /* result == second operand */
  771. TEST_ASSERT( mbedtls_mpi_add_abs( &Y, &X, &Y ) == 0 );
  772. TEST_ASSERT( sign_is_valid( &Y ) );
  773. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
  774. exit:
  775. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  776. }
  777. /* END_CASE */
  778. /* BEGIN_CASE */
  779. void mbedtls_mpi_add_int( int radix_X, char * input_X, int input_Y,
  780. int radix_A, char * input_A )
  781. {
  782. mbedtls_mpi X, Z, A;
  783. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  784. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  785. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  786. TEST_ASSERT( mbedtls_mpi_add_int( &Z, &X, input_Y ) == 0 );
  787. TEST_ASSERT( sign_is_valid( &Z ) );
  788. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  789. exit:
  790. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  791. }
  792. /* END_CASE */
  793. /* BEGIN_CASE */
  794. void mbedtls_mpi_sub_mpi( int radix_X, char * input_X, int radix_Y,
  795. char * input_Y, int radix_A, char * input_A )
  796. {
  797. mbedtls_mpi X, Y, Z, A;
  798. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  799. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  800. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  801. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  802. TEST_ASSERT( mbedtls_mpi_sub_mpi( &Z, &X, &Y ) == 0 );
  803. TEST_ASSERT( sign_is_valid( &Z ) );
  804. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  805. /* result == first operand */
  806. TEST_ASSERT( mbedtls_mpi_sub_mpi( &X, &X, &Y ) == 0 );
  807. TEST_ASSERT( sign_is_valid( &X ) );
  808. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  809. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  810. /* result == second operand */
  811. TEST_ASSERT( mbedtls_mpi_sub_mpi( &Y, &X, &Y ) == 0 );
  812. TEST_ASSERT( sign_is_valid( &Y ) );
  813. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
  814. exit:
  815. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  816. }
  817. /* END_CASE */
  818. /* BEGIN_CASE */
  819. void mbedtls_mpi_sub_abs( int radix_X, char * input_X, int radix_Y,
  820. char * input_Y, int radix_A, char * input_A,
  821. int sub_result )
  822. {
  823. mbedtls_mpi X, Y, Z, A;
  824. int res;
  825. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  826. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  827. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  828. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  829. res = mbedtls_mpi_sub_abs( &Z, &X, &Y );
  830. TEST_ASSERT( res == sub_result );
  831. TEST_ASSERT( sign_is_valid( &Z ) );
  832. if( res == 0 )
  833. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  834. /* result == first operand */
  835. TEST_ASSERT( mbedtls_mpi_sub_abs( &X, &X, &Y ) == sub_result );
  836. TEST_ASSERT( sign_is_valid( &X ) );
  837. if( sub_result == 0 )
  838. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  839. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  840. /* result == second operand */
  841. TEST_ASSERT( mbedtls_mpi_sub_abs( &Y, &X, &Y ) == sub_result );
  842. TEST_ASSERT( sign_is_valid( &Y ) );
  843. if( sub_result == 0 )
  844. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Y, &A ) == 0 );
  845. exit:
  846. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  847. }
  848. /* END_CASE */
  849. /* BEGIN_CASE */
  850. void mbedtls_mpi_sub_int( int radix_X, char * input_X, int input_Y,
  851. int radix_A, char * input_A )
  852. {
  853. mbedtls_mpi X, Z, A;
  854. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  855. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  856. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  857. TEST_ASSERT( mbedtls_mpi_sub_int( &Z, &X, input_Y ) == 0 );
  858. TEST_ASSERT( sign_is_valid( &Z ) );
  859. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  860. exit:
  861. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  862. }
  863. /* END_CASE */
  864. /* BEGIN_CASE */
  865. void mbedtls_mpi_mul_mpi( int radix_X, char * input_X, int radix_Y,
  866. char * input_Y, int radix_A, char * input_A )
  867. {
  868. mbedtls_mpi X, Y, Z, A;
  869. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  870. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  871. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  872. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  873. TEST_ASSERT( mbedtls_mpi_mul_mpi( &Z, &X, &Y ) == 0 );
  874. TEST_ASSERT( sign_is_valid( &Z ) );
  875. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  876. exit:
  877. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  878. }
  879. /* END_CASE */
  880. /* BEGIN_CASE */
  881. void mbedtls_mpi_mul_int( int radix_X, char * input_X, int input_Y,
  882. int radix_A, char * input_A,
  883. char * result_comparison )
  884. {
  885. mbedtls_mpi X, Z, A;
  886. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  887. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  888. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  889. TEST_ASSERT( mbedtls_mpi_mul_int( &Z, &X, input_Y ) == 0 );
  890. TEST_ASSERT( sign_is_valid( &Z ) );
  891. if( strcmp( result_comparison, "==" ) == 0 )
  892. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  893. else if( strcmp( result_comparison, "!=" ) == 0 )
  894. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) != 0 );
  895. else
  896. TEST_ASSERT( "unknown operator" == 0 );
  897. exit:
  898. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  899. }
  900. /* END_CASE */
  901. /* BEGIN_CASE */
  902. void mbedtls_mpi_div_mpi( int radix_X, char * input_X, int radix_Y,
  903. char * input_Y, int radix_A, char * input_A,
  904. int radix_B, char * input_B, int div_result )
  905. {
  906. mbedtls_mpi X, Y, Q, R, A, B;
  907. int res;
  908. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R );
  909. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &B );
  910. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  911. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  912. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  913. TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
  914. res = mbedtls_mpi_div_mpi( &Q, &R, &X, &Y );
  915. TEST_ASSERT( res == div_result );
  916. if( res == 0 )
  917. {
  918. TEST_ASSERT( sign_is_valid( &Q ) );
  919. TEST_ASSERT( sign_is_valid( &R ) );
  920. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
  921. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
  922. }
  923. exit:
  924. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R );
  925. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &B );
  926. }
  927. /* END_CASE */
  928. /* BEGIN_CASE */
  929. void mbedtls_mpi_div_int( int radix_X, char * input_X, int input_Y,
  930. int radix_A, char * input_A, int radix_B,
  931. char * input_B, int div_result )
  932. {
  933. mbedtls_mpi X, Q, R, A, B;
  934. int res;
  935. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &R ); mbedtls_mpi_init( &A );
  936. mbedtls_mpi_init( &B );
  937. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  938. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  939. TEST_ASSERT( mbedtls_test_read_mpi( &B, radix_B, input_B ) == 0 );
  940. res = mbedtls_mpi_div_int( &Q, &R, &X, input_Y );
  941. TEST_ASSERT( res == div_result );
  942. if( res == 0 )
  943. {
  944. TEST_ASSERT( sign_is_valid( &Q ) );
  945. TEST_ASSERT( sign_is_valid( &R ) );
  946. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &A ) == 0 );
  947. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &B ) == 0 );
  948. }
  949. exit:
  950. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &R ); mbedtls_mpi_free( &A );
  951. mbedtls_mpi_free( &B );
  952. }
  953. /* END_CASE */
  954. /* BEGIN_CASE */
  955. void mbedtls_mpi_mod_mpi( int radix_X, char * input_X, int radix_Y,
  956. char * input_Y, int radix_A, char * input_A,
  957. int div_result )
  958. {
  959. mbedtls_mpi X, Y, A;
  960. int res;
  961. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &A );
  962. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  963. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  964. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  965. res = mbedtls_mpi_mod_mpi( &X, &X, &Y );
  966. TEST_ASSERT( res == div_result );
  967. if( res == 0 )
  968. {
  969. TEST_ASSERT( sign_is_valid( &X ) );
  970. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  971. }
  972. exit:
  973. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &A );
  974. }
  975. /* END_CASE */
  976. /* BEGIN_CASE */
  977. void mbedtls_mpi_mod_int( int radix_X, char * input_X, int input_Y,
  978. int input_A, int div_result )
  979. {
  980. mbedtls_mpi X;
  981. int res;
  982. mbedtls_mpi_uint r;
  983. mbedtls_mpi_init( &X );
  984. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  985. res = mbedtls_mpi_mod_int( &r, &X, input_Y );
  986. TEST_ASSERT( res == div_result );
  987. if( res == 0 )
  988. {
  989. TEST_ASSERT( r == (mbedtls_mpi_uint) input_A );
  990. }
  991. exit:
  992. mbedtls_mpi_free( &X );
  993. }
  994. /* END_CASE */
  995. /* BEGIN_CASE */
  996. void mbedtls_mpi_exp_mod( int radix_A, char * input_A, int radix_E,
  997. char * input_E, int radix_N, char * input_N,
  998. int radix_X, char * input_X, int exp_result )
  999. {
  1000. mbedtls_mpi A, E, N, RR, Z, X;
  1001. int res;
  1002. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
  1003. mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &X );
  1004. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  1005. TEST_ASSERT( mbedtls_test_read_mpi( &E, radix_E, input_E ) == 0 );
  1006. TEST_ASSERT( mbedtls_test_read_mpi( &N, radix_N, input_N ) == 0 );
  1007. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  1008. res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, NULL );
  1009. TEST_ASSERT( res == exp_result );
  1010. if( res == 0 )
  1011. {
  1012. TEST_ASSERT( sign_is_valid( &Z ) );
  1013. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
  1014. }
  1015. /* Now test again with the speed-up parameter supplied as an output. */
  1016. res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
  1017. TEST_ASSERT( res == exp_result );
  1018. if( res == 0 )
  1019. {
  1020. TEST_ASSERT( sign_is_valid( &Z ) );
  1021. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
  1022. }
  1023. /* Now test again with the speed-up parameter supplied in calculated form. */
  1024. res = mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR );
  1025. TEST_ASSERT( res == exp_result );
  1026. if( res == 0 )
  1027. {
  1028. TEST_ASSERT( sign_is_valid( &Z ) );
  1029. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &X ) == 0 );
  1030. }
  1031. exit:
  1032. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
  1033. mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &X );
  1034. }
  1035. /* END_CASE */
  1036. /* BEGIN_CASE */
  1037. void mbedtls_mpi_exp_mod_size( int A_bytes, int E_bytes, int N_bytes,
  1038. int radix_RR, char * input_RR, int exp_result )
  1039. {
  1040. mbedtls_mpi A, E, N, RR, Z;
  1041. mbedtls_mpi_init( &A ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &N );
  1042. mbedtls_mpi_init( &RR ); mbedtls_mpi_init( &Z );
  1043. /* Set A to 2^(A_bytes - 1) + 1 */
  1044. TEST_ASSERT( mbedtls_mpi_lset( &A, 1 ) == 0 );
  1045. TEST_ASSERT( mbedtls_mpi_shift_l( &A, ( A_bytes * 8 ) - 1 ) == 0 );
  1046. TEST_ASSERT( mbedtls_mpi_set_bit( &A, 0, 1 ) == 0 );
  1047. /* Set E to 2^(E_bytes - 1) + 1 */
  1048. TEST_ASSERT( mbedtls_mpi_lset( &E, 1 ) == 0 );
  1049. TEST_ASSERT( mbedtls_mpi_shift_l( &E, ( E_bytes * 8 ) - 1 ) == 0 );
  1050. TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 1 ) == 0 );
  1051. /* Set N to 2^(N_bytes - 1) + 1 */
  1052. TEST_ASSERT( mbedtls_mpi_lset( &N, 1 ) == 0 );
  1053. TEST_ASSERT( mbedtls_mpi_shift_l( &N, ( N_bytes * 8 ) - 1 ) == 0 );
  1054. TEST_ASSERT( mbedtls_mpi_set_bit( &N, 0, 1 ) == 0 );
  1055. if( strlen( input_RR ) )
  1056. TEST_ASSERT( mbedtls_test_read_mpi( &RR, radix_RR, input_RR ) == 0 );
  1057. TEST_ASSERT( mbedtls_mpi_exp_mod( &Z, &A, &E, &N, &RR ) == exp_result );
  1058. exit:
  1059. mbedtls_mpi_free( &A ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &N );
  1060. mbedtls_mpi_free( &RR ); mbedtls_mpi_free( &Z );
  1061. }
  1062. /* END_CASE */
  1063. /* BEGIN_CASE */
  1064. void mbedtls_mpi_inv_mod( int radix_X, char * input_X, int radix_Y,
  1065. char * input_Y, int radix_A, char * input_A,
  1066. int div_result )
  1067. {
  1068. mbedtls_mpi X, Y, Z, A;
  1069. int res;
  1070. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &Y ); mbedtls_mpi_init( &Z ); mbedtls_mpi_init( &A );
  1071. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  1072. TEST_ASSERT( mbedtls_test_read_mpi( &Y, radix_Y, input_Y ) == 0 );
  1073. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  1074. res = mbedtls_mpi_inv_mod( &Z, &X, &Y );
  1075. TEST_ASSERT( res == div_result );
  1076. if( res == 0 )
  1077. {
  1078. TEST_ASSERT( sign_is_valid( &Z ) );
  1079. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Z, &A ) == 0 );
  1080. }
  1081. exit:
  1082. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &Y ); mbedtls_mpi_free( &Z ); mbedtls_mpi_free( &A );
  1083. }
  1084. /* END_CASE */
  1085. /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
  1086. void mbedtls_mpi_is_prime( int radix_X, char * input_X, int div_result )
  1087. {
  1088. mbedtls_mpi X;
  1089. int res;
  1090. mbedtls_mpi_init( &X );
  1091. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  1092. res = mbedtls_mpi_is_prime_ext( &X, 40, mbedtls_test_rnd_std_rand, NULL );
  1093. TEST_ASSERT( res == div_result );
  1094. exit:
  1095. mbedtls_mpi_free( &X );
  1096. }
  1097. /* END_CASE */
  1098. /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
  1099. void mbedtls_mpi_is_prime_det( data_t * input_X, data_t * witnesses,
  1100. int chunk_len, int rounds )
  1101. {
  1102. mbedtls_mpi X;
  1103. int res;
  1104. mbedtls_test_mpi_random rand;
  1105. mbedtls_mpi_init( &X );
  1106. rand.data = witnesses;
  1107. rand.pos = 0;
  1108. rand.chunk_len = chunk_len;
  1109. TEST_ASSERT( mbedtls_mpi_read_binary( &X, input_X->x, input_X->len ) == 0 );
  1110. res = mbedtls_mpi_is_prime_ext( &X, rounds - 1,
  1111. mbedtls_test_mpi_miller_rabin_determinizer,
  1112. &rand );
  1113. TEST_ASSERT( res == 0 );
  1114. rand.data = witnesses;
  1115. rand.pos = 0;
  1116. rand.chunk_len = chunk_len;
  1117. res = mbedtls_mpi_is_prime_ext( &X, rounds,
  1118. mbedtls_test_mpi_miller_rabin_determinizer,
  1119. &rand );
  1120. TEST_ASSERT( res == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  1121. exit:
  1122. mbedtls_mpi_free( &X );
  1123. }
  1124. /* END_CASE */
  1125. /* BEGIN_CASE depends_on:MBEDTLS_GENPRIME */
  1126. void mbedtls_mpi_gen_prime( int bits, int flags, int ref_ret )
  1127. {
  1128. mbedtls_mpi X;
  1129. int my_ret;
  1130. mbedtls_mpi_init( &X );
  1131. my_ret = mbedtls_mpi_gen_prime( &X, bits, flags,
  1132. mbedtls_test_rnd_std_rand, NULL );
  1133. TEST_ASSERT( my_ret == ref_ret );
  1134. if( ref_ret == 0 )
  1135. {
  1136. size_t actual_bits = mbedtls_mpi_bitlen( &X );
  1137. TEST_ASSERT( actual_bits >= (size_t) bits );
  1138. TEST_ASSERT( actual_bits <= (size_t) bits + 1 );
  1139. TEST_ASSERT( sign_is_valid( &X ) );
  1140. TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
  1141. mbedtls_test_rnd_std_rand,
  1142. NULL ) == 0 );
  1143. if( flags & MBEDTLS_MPI_GEN_PRIME_FLAG_DH )
  1144. {
  1145. /* X = ( X - 1 ) / 2 */
  1146. TEST_ASSERT( mbedtls_mpi_shift_r( &X, 1 ) == 0 );
  1147. TEST_ASSERT( mbedtls_mpi_is_prime_ext( &X, 40,
  1148. mbedtls_test_rnd_std_rand,
  1149. NULL ) == 0 );
  1150. }
  1151. }
  1152. exit:
  1153. mbedtls_mpi_free( &X );
  1154. }
  1155. /* END_CASE */
  1156. /* BEGIN_CASE */
  1157. void mbedtls_mpi_shift_l( int radix_X, char * input_X, int shift_X,
  1158. int radix_A, char * input_A )
  1159. {
  1160. mbedtls_mpi X, A;
  1161. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
  1162. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  1163. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  1164. TEST_ASSERT( mbedtls_mpi_shift_l( &X, shift_X ) == 0 );
  1165. TEST_ASSERT( sign_is_valid( &X ) );
  1166. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  1167. exit:
  1168. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
  1169. }
  1170. /* END_CASE */
  1171. /* BEGIN_CASE */
  1172. void mbedtls_mpi_shift_r( int radix_X, char * input_X, int shift_X,
  1173. int radix_A, char * input_A )
  1174. {
  1175. mbedtls_mpi X, A;
  1176. mbedtls_mpi_init( &X ); mbedtls_mpi_init( &A );
  1177. TEST_ASSERT( mbedtls_test_read_mpi( &X, radix_X, input_X ) == 0 );
  1178. TEST_ASSERT( mbedtls_test_read_mpi( &A, radix_A, input_A ) == 0 );
  1179. TEST_ASSERT( mbedtls_mpi_shift_r( &X, shift_X ) == 0 );
  1180. TEST_ASSERT( sign_is_valid( &X ) );
  1181. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &X, &A ) == 0 );
  1182. exit:
  1183. mbedtls_mpi_free( &X ); mbedtls_mpi_free( &A );
  1184. }
  1185. /* END_CASE */
  1186. /* BEGIN_CASE */
  1187. void mpi_fill_random( int wanted_bytes, int rng_bytes,
  1188. int before, int expected_ret )
  1189. {
  1190. mbedtls_mpi X;
  1191. int ret;
  1192. size_t bytes_left = rng_bytes;
  1193. mbedtls_mpi_init( &X );
  1194. if( before != 0 )
  1195. {
  1196. /* Set X to sign(before) * 2^(|before|-1) */
  1197. TEST_ASSERT( mbedtls_mpi_lset( &X, before > 0 ? 1 : -1 ) == 0 );
  1198. if( before < 0 )
  1199. before = - before;
  1200. TEST_ASSERT( mbedtls_mpi_shift_l( &X, before - 1 ) == 0 );
  1201. }
  1202. ret = mbedtls_mpi_fill_random( &X, wanted_bytes,
  1203. f_rng_bytes_left, &bytes_left );
  1204. TEST_ASSERT( ret == expected_ret );
  1205. if( expected_ret == 0 )
  1206. {
  1207. /* mbedtls_mpi_fill_random is documented to use bytes from the RNG
  1208. * as a big-endian representation of the number. We know when
  1209. * our RNG function returns null bytes, so we know how many
  1210. * leading zero bytes the number has. */
  1211. size_t leading_zeros = 0;
  1212. if( wanted_bytes > 0 && rng_bytes % 256 == 0 )
  1213. leading_zeros = 1;
  1214. TEST_ASSERT( mbedtls_mpi_size( &X ) + leading_zeros ==
  1215. (size_t) wanted_bytes );
  1216. TEST_ASSERT( (int) bytes_left == rng_bytes - wanted_bytes );
  1217. TEST_ASSERT( sign_is_valid( &X ) );
  1218. }
  1219. exit:
  1220. mbedtls_mpi_free( &X );
  1221. }
  1222. /* END_CASE */
  1223. /* BEGIN_CASE */
  1224. void mpi_random_many( int min, data_t *bound_bytes, int iterations )
  1225. {
  1226. /* Generate numbers in the range 1..bound-1. Do it iterations times.
  1227. * This function assumes that the value of bound is at least 2 and
  1228. * that iterations is large enough that a one-in-2^iterations chance
  1229. * effectively never occurs.
  1230. */
  1231. mbedtls_mpi upper_bound;
  1232. size_t n_bits;
  1233. mbedtls_mpi result;
  1234. size_t b;
  1235. /* If upper_bound is small, stats[b] is the number of times the value b
  1236. * has been generated. Otherwise stats[b] is the number of times a
  1237. * value with bit b set has been generated. */
  1238. size_t *stats = NULL;
  1239. size_t stats_len;
  1240. int full_stats;
  1241. size_t i;
  1242. mbedtls_mpi_init( &upper_bound );
  1243. mbedtls_mpi_init( &result );
  1244. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
  1245. bound_bytes->x, bound_bytes->len ) );
  1246. n_bits = mbedtls_mpi_bitlen( &upper_bound );
  1247. /* Consider a bound "small" if it's less than 2^5. This value is chosen
  1248. * to be small enough that the probability of missing one value is
  1249. * negligible given the number of iterations. It must be less than
  1250. * 256 because some of the code below assumes that "small" values
  1251. * fit in a byte. */
  1252. if( n_bits <= 5 )
  1253. {
  1254. full_stats = 1;
  1255. stats_len = bound_bytes->x[bound_bytes->len - 1];
  1256. }
  1257. else
  1258. {
  1259. full_stats = 0;
  1260. stats_len = n_bits;
  1261. }
  1262. ASSERT_ALLOC( stats, stats_len );
  1263. for( i = 0; i < (size_t) iterations; i++ )
  1264. {
  1265. mbedtls_test_set_step( i );
  1266. TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
  1267. mbedtls_test_rnd_std_rand, NULL ) );
  1268. TEST_ASSERT( sign_is_valid( &result ) );
  1269. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
  1270. TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
  1271. if( full_stats )
  1272. {
  1273. uint8_t value;
  1274. TEST_EQUAL( 0, mbedtls_mpi_write_binary( &result, &value, 1 ) );
  1275. TEST_ASSERT( value < stats_len );
  1276. ++stats[value];
  1277. }
  1278. else
  1279. {
  1280. for( b = 0; b < n_bits; b++ )
  1281. stats[b] += mbedtls_mpi_get_bit( &result, b );
  1282. }
  1283. }
  1284. if( full_stats )
  1285. {
  1286. for( b = min; b < stats_len; b++ )
  1287. {
  1288. mbedtls_test_set_step( 1000000 + b );
  1289. /* Assert that each value has been reached at least once.
  1290. * This is almost guaranteed if the iteration count is large
  1291. * enough. This is a very crude way of checking the distribution.
  1292. */
  1293. TEST_ASSERT( stats[b] > 0 );
  1294. }
  1295. }
  1296. else
  1297. {
  1298. int statistically_safe_all_the_way =
  1299. is_significantly_above_a_power_of_2( bound_bytes );
  1300. for( b = 0; b < n_bits; b++ )
  1301. {
  1302. mbedtls_test_set_step( 1000000 + b );
  1303. /* Assert that each bit has been set in at least one result and
  1304. * clear in at least one result. Provided that iterations is not
  1305. * too small, it would be extremely unlikely for this not to be
  1306. * the case if the results are uniformly distributed.
  1307. *
  1308. * As an exception, the top bit may legitimately never be set
  1309. * if bound is a power of 2 or only slightly above.
  1310. */
  1311. if( statistically_safe_all_the_way || b != n_bits - 1 )
  1312. {
  1313. TEST_ASSERT( stats[b] > 0 );
  1314. }
  1315. TEST_ASSERT( stats[b] < (size_t) iterations );
  1316. }
  1317. }
  1318. exit:
  1319. mbedtls_mpi_free( &upper_bound );
  1320. mbedtls_mpi_free( &result );
  1321. mbedtls_free( stats );
  1322. }
  1323. /* END_CASE */
  1324. /* BEGIN_CASE */
  1325. void mpi_random_sizes( int min, data_t *bound_bytes, int nlimbs, int before )
  1326. {
  1327. mbedtls_mpi upper_bound;
  1328. mbedtls_mpi result;
  1329. mbedtls_mpi_init( &upper_bound );
  1330. mbedtls_mpi_init( &result );
  1331. if( before != 0 )
  1332. {
  1333. /* Set result to sign(before) * 2^(|before|-1) */
  1334. TEST_ASSERT( mbedtls_mpi_lset( &result, before > 0 ? 1 : -1 ) == 0 );
  1335. if( before < 0 )
  1336. before = - before;
  1337. TEST_ASSERT( mbedtls_mpi_shift_l( &result, before - 1 ) == 0 );
  1338. }
  1339. TEST_EQUAL( 0, mbedtls_mpi_grow( &result, nlimbs ) );
  1340. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
  1341. bound_bytes->x, bound_bytes->len ) );
  1342. TEST_EQUAL( 0, mbedtls_mpi_random( &result, min, &upper_bound,
  1343. mbedtls_test_rnd_std_rand, NULL ) );
  1344. TEST_ASSERT( sign_is_valid( &result ) );
  1345. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &result, &upper_bound ) < 0 );
  1346. TEST_ASSERT( mbedtls_mpi_cmp_int( &result, min ) >= 0 );
  1347. exit:
  1348. mbedtls_mpi_free( &upper_bound );
  1349. mbedtls_mpi_free( &result );
  1350. }
  1351. /* END_CASE */
  1352. /* BEGIN_CASE */
  1353. void mpi_random_fail( int min, data_t *bound_bytes, int expected_ret )
  1354. {
  1355. mbedtls_mpi upper_bound;
  1356. mbedtls_mpi result;
  1357. int actual_ret;
  1358. mbedtls_mpi_init( &upper_bound );
  1359. mbedtls_mpi_init( &result );
  1360. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &upper_bound,
  1361. bound_bytes->x, bound_bytes->len ) );
  1362. actual_ret = mbedtls_mpi_random( &result, min, &upper_bound,
  1363. mbedtls_test_rnd_std_rand, NULL );
  1364. TEST_EQUAL( expected_ret, actual_ret );
  1365. exit:
  1366. mbedtls_mpi_free( &upper_bound );
  1367. mbedtls_mpi_free( &result );
  1368. }
  1369. /* END_CASE */
  1370. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  1371. void mpi_selftest( )
  1372. {
  1373. TEST_ASSERT( mbedtls_mpi_self_test( 1 ) == 0 );
  1374. }
  1375. /* END_CASE */