test_suite_rsa.function 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/rsa.h"
  3. #include "mbedtls/rsa_internal.h"
  4. #include "mbedtls/md2.h"
  5. #include "mbedtls/md4.h"
  6. #include "mbedtls/md5.h"
  7. #include "mbedtls/sha1.h"
  8. #include "mbedtls/sha256.h"
  9. #include "mbedtls/sha512.h"
  10. #include "mbedtls/entropy.h"
  11. #include "mbedtls/ctr_drbg.h"
  12. /* END_HEADER */
  13. /* BEGIN_DEPENDENCIES
  14. * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
  15. * END_DEPENDENCIES
  16. */
  17. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  18. void rsa_invalid_param( )
  19. {
  20. mbedtls_rsa_context ctx;
  21. const int valid_padding = MBEDTLS_RSA_PKCS_V21;
  22. const int invalid_padding = 42;
  23. const int valid_mode = MBEDTLS_RSA_PRIVATE;
  24. const int invalid_mode = 42;
  25. unsigned char buf[42] = { 0 };
  26. size_t olen;
  27. TEST_INVALID_PARAM( mbedtls_rsa_init( NULL, valid_padding, 0 ) );
  28. TEST_INVALID_PARAM( mbedtls_rsa_init( &ctx, invalid_padding, 0 ) );
  29. TEST_VALID_PARAM( mbedtls_rsa_free( NULL ) );
  30. /* No more variants because only the first argument must be non-NULL. */
  31. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  32. mbedtls_rsa_import( NULL, NULL, NULL,
  33. NULL, NULL, NULL ) );
  34. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  35. mbedtls_rsa_import_raw( NULL,
  36. NULL, 0,
  37. NULL, 0,
  38. NULL, 0,
  39. NULL, 0,
  40. NULL, 0 ) );
  41. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  42. mbedtls_rsa_complete( NULL ) );
  43. /* No more variants because only the first argument must be non-NULL. */
  44. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  45. mbedtls_rsa_export( NULL, NULL, NULL,
  46. NULL, NULL, NULL ) );
  47. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  48. mbedtls_rsa_export_raw( NULL,
  49. NULL, 0,
  50. NULL, 0,
  51. NULL, 0,
  52. NULL, 0,
  53. NULL, 0 ) );
  54. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  55. mbedtls_rsa_export_crt( NULL, NULL, NULL, NULL ) );
  56. TEST_INVALID_PARAM( mbedtls_rsa_set_padding( NULL,
  57. valid_padding, 0 ) );
  58. TEST_INVALID_PARAM( mbedtls_rsa_set_padding( &ctx,
  59. invalid_padding, 0 ) );
  60. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  61. mbedtls_rsa_gen_key( NULL, rnd_std_rand,
  62. NULL, 0, 0 ) );
  63. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  64. mbedtls_rsa_gen_key( &ctx, NULL,
  65. NULL, 0, 0 ) );
  66. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  67. mbedtls_rsa_check_pubkey( NULL ) );
  68. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  69. mbedtls_rsa_check_privkey( NULL ) );
  70. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  71. mbedtls_rsa_check_pub_priv( NULL, &ctx ) );
  72. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  73. mbedtls_rsa_check_pub_priv( &ctx, NULL ) );
  74. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  75. mbedtls_rsa_public( NULL, buf, buf ) );
  76. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  77. mbedtls_rsa_public( &ctx, NULL, buf ) );
  78. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  79. mbedtls_rsa_public( &ctx, buf, NULL ) );
  80. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  81. mbedtls_rsa_private( NULL, NULL, NULL,
  82. buf, buf ) );
  83. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  84. mbedtls_rsa_private( &ctx, NULL, NULL,
  85. NULL, buf ) );
  86. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  87. mbedtls_rsa_private( &ctx, NULL, NULL,
  88. buf, NULL ) );
  89. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  90. mbedtls_rsa_pkcs1_encrypt( NULL, NULL, NULL,
  91. valid_mode,
  92. sizeof( buf ), buf,
  93. buf ) );
  94. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  95. mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
  96. invalid_mode,
  97. sizeof( buf ), buf,
  98. buf ) );
  99. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  100. mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
  101. valid_mode,
  102. sizeof( buf ), NULL,
  103. buf ) );
  104. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  105. mbedtls_rsa_pkcs1_encrypt( &ctx, NULL, NULL,
  106. valid_mode,
  107. sizeof( buf ), buf,
  108. NULL ) );
  109. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  110. mbedtls_rsa_rsaes_pkcs1_v15_encrypt( NULL, NULL,
  111. NULL,
  112. valid_mode,
  113. sizeof( buf ), buf,
  114. buf ) );
  115. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  116. mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
  117. NULL,
  118. invalid_mode,
  119. sizeof( buf ), buf,
  120. buf ) );
  121. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  122. mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
  123. NULL,
  124. valid_mode,
  125. sizeof( buf ), NULL,
  126. buf ) );
  127. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  128. mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx, NULL,
  129. NULL,
  130. valid_mode,
  131. sizeof( buf ), buf,
  132. NULL ) );
  133. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  134. mbedtls_rsa_rsaes_oaep_encrypt( NULL, NULL, NULL,
  135. valid_mode,
  136. buf, sizeof( buf ),
  137. sizeof( buf ), buf,
  138. buf ) );
  139. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  140. mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
  141. invalid_mode,
  142. buf, sizeof( buf ),
  143. sizeof( buf ), buf,
  144. buf ) );
  145. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  146. mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
  147. valid_mode,
  148. NULL, sizeof( buf ),
  149. sizeof( buf ), buf,
  150. buf ) );
  151. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  152. mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
  153. valid_mode,
  154. buf, sizeof( buf ),
  155. sizeof( buf ), NULL,
  156. buf ) );
  157. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  158. mbedtls_rsa_rsaes_oaep_encrypt( &ctx, NULL, NULL,
  159. valid_mode,
  160. buf, sizeof( buf ),
  161. sizeof( buf ), buf,
  162. NULL ) );
  163. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  164. mbedtls_rsa_pkcs1_decrypt( NULL, NULL, NULL,
  165. valid_mode, &olen,
  166. buf, buf, 42 ) );
  167. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  168. mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
  169. invalid_mode, &olen,
  170. buf, buf, 42 ) );
  171. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  172. mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
  173. valid_mode, NULL,
  174. buf, buf, 42 ) );
  175. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  176. mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
  177. valid_mode, &olen,
  178. NULL, buf, 42 ) );
  179. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  180. mbedtls_rsa_pkcs1_decrypt( &ctx, NULL, NULL,
  181. valid_mode, &olen,
  182. buf, NULL, 42 ) );
  183. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  184. mbedtls_rsa_rsaes_pkcs1_v15_decrypt( NULL, NULL,
  185. NULL,
  186. valid_mode, &olen,
  187. buf, buf, 42 ) );
  188. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  189. mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
  190. NULL,
  191. invalid_mode, &olen,
  192. buf, buf, 42 ) );
  193. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  194. mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
  195. NULL,
  196. valid_mode, NULL,
  197. buf, buf, 42 ) );
  198. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  199. mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
  200. NULL,
  201. valid_mode, &olen,
  202. NULL, buf, 42 ) );
  203. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  204. mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx, NULL,
  205. NULL,
  206. valid_mode, &olen,
  207. buf, NULL, 42 ) );
  208. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  209. mbedtls_rsa_rsaes_oaep_decrypt( NULL, NULL, NULL,
  210. valid_mode,
  211. buf, sizeof( buf ),
  212. &olen,
  213. buf, buf, 42 ) );
  214. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  215. mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
  216. invalid_mode,
  217. buf, sizeof( buf ),
  218. &olen,
  219. buf, buf, 42 ) );
  220. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  221. mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
  222. valid_mode,
  223. NULL, sizeof( buf ),
  224. NULL,
  225. buf, buf, 42 ) );
  226. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  227. mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
  228. valid_mode,
  229. buf, sizeof( buf ),
  230. &olen,
  231. NULL, buf, 42 ) );
  232. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  233. mbedtls_rsa_rsaes_oaep_decrypt( &ctx, NULL, NULL,
  234. valid_mode,
  235. buf, sizeof( buf ),
  236. &olen,
  237. buf, NULL, 42 ) );
  238. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  239. mbedtls_rsa_pkcs1_sign( NULL, NULL, NULL,
  240. valid_mode,
  241. 0, sizeof( buf ), buf,
  242. buf ) );
  243. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  244. mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
  245. invalid_mode,
  246. 0, sizeof( buf ), buf,
  247. buf ) );
  248. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  249. mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
  250. valid_mode,
  251. 0, sizeof( buf ), NULL,
  252. buf ) );
  253. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  254. mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
  255. valid_mode,
  256. 0, sizeof( buf ), buf,
  257. NULL ) );
  258. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  259. mbedtls_rsa_pkcs1_sign( &ctx, NULL, NULL,
  260. valid_mode,
  261. MBEDTLS_MD_SHA1,
  262. 0, NULL,
  263. buf ) );
  264. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  265. mbedtls_rsa_rsassa_pkcs1_v15_sign( NULL, NULL, NULL,
  266. valid_mode,
  267. 0, sizeof( buf ), buf,
  268. buf ) );
  269. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  270. mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
  271. invalid_mode,
  272. 0, sizeof( buf ), buf,
  273. buf ) );
  274. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  275. mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
  276. valid_mode,
  277. 0, sizeof( buf ), NULL,
  278. buf ) );
  279. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  280. mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
  281. valid_mode,
  282. 0, sizeof( buf ), buf,
  283. NULL ) );
  284. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  285. mbedtls_rsa_rsassa_pkcs1_v15_sign( &ctx, NULL, NULL,
  286. valid_mode,
  287. MBEDTLS_MD_SHA1,
  288. 0, NULL,
  289. buf ) );
  290. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  291. mbedtls_rsa_rsassa_pss_sign( NULL, NULL, NULL,
  292. valid_mode,
  293. 0, sizeof( buf ), buf,
  294. buf ) );
  295. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  296. mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
  297. invalid_mode,
  298. 0, sizeof( buf ), buf,
  299. buf ) );
  300. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  301. mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
  302. valid_mode,
  303. 0, sizeof( buf ), NULL,
  304. buf ) );
  305. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  306. mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
  307. valid_mode,
  308. 0, sizeof( buf ), buf,
  309. NULL ) );
  310. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  311. mbedtls_rsa_rsassa_pss_sign( &ctx, NULL, NULL,
  312. valid_mode,
  313. MBEDTLS_MD_SHA1,
  314. 0, NULL,
  315. buf ) );
  316. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  317. mbedtls_rsa_pkcs1_verify( NULL, NULL, NULL,
  318. valid_mode,
  319. 0, sizeof( buf ), buf,
  320. buf ) );
  321. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  322. mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
  323. invalid_mode,
  324. 0, sizeof( buf ), buf,
  325. buf ) );
  326. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  327. mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
  328. valid_mode,
  329. 0, sizeof( buf ), NULL,
  330. buf ) );
  331. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  332. mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
  333. valid_mode,
  334. 0, sizeof( buf ), buf,
  335. NULL ) );
  336. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  337. mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL,
  338. valid_mode,
  339. MBEDTLS_MD_SHA1, 0, NULL,
  340. buf ) );
  341. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  342. mbedtls_rsa_rsassa_pkcs1_v15_verify( NULL, NULL,
  343. NULL,
  344. valid_mode,
  345. 0, sizeof( buf ), buf,
  346. buf ) );
  347. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  348. mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
  349. NULL,
  350. invalid_mode,
  351. 0, sizeof( buf ), buf,
  352. buf ) );
  353. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  354. mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
  355. NULL,
  356. valid_mode,
  357. 0, sizeof( buf ),
  358. NULL, buf ) );
  359. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  360. mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
  361. NULL,
  362. valid_mode,
  363. 0, sizeof( buf ), buf,
  364. NULL ) );
  365. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  366. mbedtls_rsa_rsassa_pkcs1_v15_verify( &ctx, NULL,
  367. NULL,
  368. valid_mode,
  369. MBEDTLS_MD_SHA1,
  370. 0, NULL,
  371. buf ) );
  372. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  373. mbedtls_rsa_rsassa_pss_verify( NULL, NULL, NULL,
  374. valid_mode,
  375. 0, sizeof( buf ),
  376. buf, buf ) );
  377. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  378. mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
  379. invalid_mode,
  380. 0, sizeof( buf ),
  381. buf, buf ) );
  382. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  383. mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
  384. valid_mode,
  385. 0, sizeof( buf ),
  386. NULL, buf ) );
  387. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  388. mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
  389. valid_mode,
  390. 0, sizeof( buf ),
  391. buf, NULL ) );
  392. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  393. mbedtls_rsa_rsassa_pss_verify( &ctx, NULL, NULL,
  394. valid_mode,
  395. MBEDTLS_MD_SHA1,
  396. 0, NULL,
  397. buf ) );
  398. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  399. mbedtls_rsa_rsassa_pss_verify_ext( NULL, NULL, NULL,
  400. valid_mode,
  401. 0, sizeof( buf ),
  402. buf,
  403. 0, 0,
  404. buf ) );
  405. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  406. mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
  407. invalid_mode,
  408. 0, sizeof( buf ),
  409. buf,
  410. 0, 0,
  411. buf ) );
  412. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  413. mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
  414. valid_mode,
  415. 0, sizeof( buf ),
  416. NULL, 0, 0,
  417. buf ) );
  418. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  419. mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
  420. valid_mode,
  421. 0, sizeof( buf ),
  422. buf, 0, 0,
  423. NULL ) );
  424. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  425. mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL,
  426. valid_mode,
  427. MBEDTLS_MD_SHA1,
  428. 0, NULL,
  429. 0, 0,
  430. buf ) );
  431. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  432. mbedtls_rsa_copy( NULL, &ctx ) );
  433. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_RSA_BAD_INPUT_DATA,
  434. mbedtls_rsa_copy( &ctx, NULL ) );
  435. exit:
  436. return;
  437. }
  438. /* END_CASE */
  439. /* BEGIN_CASE */
  440. void rsa_init_free( int reinit )
  441. {
  442. mbedtls_rsa_context ctx;
  443. /* Double free is not explicitly documented to work, but we rely on it
  444. * even inside the library so that you can call mbedtls_rsa_free()
  445. * unconditionally on an error path without checking whether it has
  446. * already been called in the success path. */
  447. mbedtls_rsa_init( &ctx, 0, 0 );
  448. mbedtls_rsa_free( &ctx );
  449. if( reinit )
  450. mbedtls_rsa_init( &ctx, 0, 0 );
  451. mbedtls_rsa_free( &ctx );
  452. /* This test case always succeeds, functionally speaking. A plausible
  453. * bug might trigger an invalid pointer dereference or a memory leak. */
  454. goto exit;
  455. }
  456. /* END_CASE */
  457. /* BEGIN_CASE */
  458. void mbedtls_rsa_pkcs1_sign( data_t * message_str, int padding_mode,
  459. int digest, int mod, int radix_P, char * input_P,
  460. int radix_Q, char * input_Q, int radix_N,
  461. char * input_N, int radix_E, char * input_E,
  462. data_t * result_str, int result )
  463. {
  464. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  465. unsigned char output[256];
  466. mbedtls_rsa_context ctx;
  467. mbedtls_mpi N, P, Q, E;
  468. rnd_pseudo_info rnd_info;
  469. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  470. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  471. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  472. memset( hash_result, 0x00, sizeof( hash_result ) );
  473. memset( output, 0x00, sizeof( output ) );
  474. memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
  475. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  476. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  477. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  478. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  479. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
  480. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  481. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  482. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  483. if( mbedtls_md_info_from_type( digest ) != NULL )
  484. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
  485. TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
  486. MBEDTLS_RSA_PRIVATE, digest, 0,
  487. hash_result, output ) == result );
  488. if( result == 0 )
  489. {
  490. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  491. ctx.len, result_str->len ) == 0 );
  492. }
  493. exit:
  494. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  495. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  496. mbedtls_rsa_free( &ctx );
  497. }
  498. /* END_CASE */
  499. /* BEGIN_CASE */
  500. void mbedtls_rsa_pkcs1_verify( data_t * message_str, int padding_mode,
  501. int digest, int mod, int radix_N,
  502. char * input_N, int radix_E, char * input_E,
  503. data_t * result_str, int result )
  504. {
  505. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  506. mbedtls_rsa_context ctx;
  507. mbedtls_mpi N, E;
  508. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  509. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  510. memset( hash_result, 0x00, sizeof( hash_result ) );
  511. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  512. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  513. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  514. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  515. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  516. if( mbedtls_md_info_from_type( digest ) != NULL )
  517. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
  518. TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
  519. exit:
  520. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  521. mbedtls_rsa_free( &ctx );
  522. }
  523. /* END_CASE */
  524. /* BEGIN_CASE */
  525. void rsa_pkcs1_sign_raw( data_t * hash_result,
  526. int padding_mode, int mod, int radix_P,
  527. char * input_P, int radix_Q, char * input_Q,
  528. int radix_N, char * input_N, int radix_E,
  529. char * input_E, data_t * result_str )
  530. {
  531. unsigned char output[256];
  532. mbedtls_rsa_context ctx;
  533. mbedtls_mpi N, P, Q, E;
  534. rnd_pseudo_info rnd_info;
  535. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  536. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  537. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  538. memset( output, 0x00, sizeof( output ) );
  539. memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
  540. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  541. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  542. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  543. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  544. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
  545. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  546. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  547. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  548. TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &rnd_pseudo_rand, &rnd_info,
  549. MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_NONE,
  550. hash_result->len, hash_result->x,
  551. output ) == 0 );
  552. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  553. ctx.len, result_str->len ) == 0 );
  554. #if defined(MBEDTLS_PKCS1_V15)
  555. /* For PKCS#1 v1.5, there is an alternative way to generate signatures */
  556. if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
  557. {
  558. int res;
  559. memset( output, 0x00, sizeof( output) );
  560. res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt( &ctx,
  561. &rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE,
  562. hash_result->len, hash_result->x, output );
  563. #if !defined(MBEDTLS_RSA_ALT)
  564. TEST_ASSERT( res == 0 );
  565. #else
  566. TEST_ASSERT( ( res == 0 ) ||
  567. ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
  568. #endif
  569. if( res == 0 )
  570. {
  571. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  572. ctx.len,
  573. result_str->len ) == 0 );
  574. }
  575. }
  576. #endif /* MBEDTLS_PKCS1_V15 */
  577. exit:
  578. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  579. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  580. mbedtls_rsa_free( &ctx );
  581. }
  582. /* END_CASE */
  583. /* BEGIN_CASE */
  584. void rsa_pkcs1_verify_raw( data_t * hash_result,
  585. int padding_mode, int mod, int radix_N,
  586. char * input_N, int radix_E, char * input_E,
  587. data_t * result_str, int correct )
  588. {
  589. unsigned char output[256];
  590. mbedtls_rsa_context ctx;
  591. mbedtls_mpi N, E;
  592. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  593. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  594. memset( output, 0x00, sizeof( output ) );
  595. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  596. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  597. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  598. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  599. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  600. TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, MBEDTLS_MD_NONE, hash_result->len, hash_result->x, result_str->x ) == correct );
  601. #if defined(MBEDTLS_PKCS1_V15)
  602. /* For PKCS#1 v1.5, there is an alternative way to verify signatures */
  603. if( padding_mode == MBEDTLS_RSA_PKCS_V15 )
  604. {
  605. int res;
  606. int ok;
  607. size_t olen;
  608. res = mbedtls_rsa_rsaes_pkcs1_v15_decrypt( &ctx,
  609. NULL, NULL, MBEDTLS_RSA_PUBLIC,
  610. &olen, result_str->x, output, sizeof( output ) );
  611. #if !defined(MBEDTLS_RSA_ALT)
  612. TEST_ASSERT( res == 0 );
  613. #else
  614. TEST_ASSERT( ( res == 0 ) ||
  615. ( res == MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION ) );
  616. #endif
  617. if( res == 0 )
  618. {
  619. ok = olen == hash_result->len && memcmp( output, hash_result->x, olen ) == 0;
  620. if( correct == 0 )
  621. TEST_ASSERT( ok == 1 );
  622. else
  623. TEST_ASSERT( ok == 0 );
  624. }
  625. }
  626. #endif /* MBEDTLS_PKCS1_V15 */
  627. exit:
  628. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  629. mbedtls_rsa_free( &ctx );
  630. }
  631. /* END_CASE */
  632. /* BEGIN_CASE */
  633. void mbedtls_rsa_pkcs1_encrypt( data_t * message_str, int padding_mode,
  634. int mod, int radix_N, char * input_N,
  635. int radix_E, char * input_E,
  636. data_t * result_str, int result )
  637. {
  638. unsigned char output[256];
  639. mbedtls_rsa_context ctx;
  640. rnd_pseudo_info rnd_info;
  641. mbedtls_mpi N, E;
  642. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  643. memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
  644. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  645. memset( output, 0x00, sizeof( output ) );
  646. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  647. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  648. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  649. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  650. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  651. TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info,
  652. MBEDTLS_RSA_PUBLIC, message_str->len,
  653. message_str->x, output ) == result );
  654. if( result == 0 )
  655. {
  656. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  657. ctx.len, result_str->len ) == 0 );
  658. }
  659. exit:
  660. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  661. mbedtls_rsa_free( &ctx );
  662. }
  663. /* END_CASE */
  664. /* BEGIN_CASE */
  665. void rsa_pkcs1_encrypt_bad_rng( data_t * message_str, int padding_mode,
  666. int mod, int radix_N, char * input_N,
  667. int radix_E, char * input_E,
  668. data_t * result_str, int result )
  669. {
  670. unsigned char output[256];
  671. mbedtls_rsa_context ctx;
  672. mbedtls_mpi N, E;
  673. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  674. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  675. memset( output, 0x00, sizeof( output ) );
  676. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  677. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  678. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  679. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  680. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  681. TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL,
  682. MBEDTLS_RSA_PUBLIC, message_str->len,
  683. message_str->x, output ) == result );
  684. if( result == 0 )
  685. {
  686. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  687. ctx.len, result_str->len ) == 0 );
  688. }
  689. exit:
  690. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  691. mbedtls_rsa_free( &ctx );
  692. }
  693. /* END_CASE */
  694. /* BEGIN_CASE */
  695. void mbedtls_rsa_pkcs1_decrypt( data_t * message_str, int padding_mode,
  696. int mod, int radix_P, char * input_P,
  697. int radix_Q, char * input_Q, int radix_N,
  698. char * input_N, int radix_E, char * input_E,
  699. int max_output, data_t * result_str,
  700. int result )
  701. {
  702. unsigned char output[32];
  703. mbedtls_rsa_context ctx;
  704. size_t output_len;
  705. rnd_pseudo_info rnd_info;
  706. mbedtls_mpi N, P, Q, E;
  707. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  708. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  709. mbedtls_rsa_init( &ctx, padding_mode, 0 );
  710. memset( output, 0x00, sizeof( output ) );
  711. memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
  712. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  713. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  714. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  715. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  716. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
  717. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  718. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  719. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  720. output_len = 0;
  721. TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx, rnd_pseudo_rand, &rnd_info, MBEDTLS_RSA_PRIVATE, &output_len, message_str->x, output, max_output ) == result );
  722. if( result == 0 )
  723. {
  724. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  725. output_len,
  726. result_str->len ) == 0 );
  727. }
  728. exit:
  729. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  730. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  731. mbedtls_rsa_free( &ctx );
  732. }
  733. /* END_CASE */
  734. /* BEGIN_CASE */
  735. void mbedtls_rsa_public( data_t * message_str, int mod, int radix_N,
  736. char * input_N, int radix_E, char * input_E,
  737. data_t * result_str, int result )
  738. {
  739. unsigned char output[256];
  740. mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
  741. mbedtls_mpi N, E;
  742. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  743. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
  744. mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
  745. memset( output, 0x00, sizeof( output ) );
  746. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  747. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  748. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  749. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  750. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  751. TEST_ASSERT( mbedtls_rsa_public( &ctx, message_str->x, output ) == result );
  752. if( result == 0 )
  753. {
  754. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  755. ctx.len, result_str->len ) == 0 );
  756. }
  757. /* And now with the copy */
  758. TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
  759. /* clear the original to be sure */
  760. mbedtls_rsa_free( &ctx );
  761. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx2 ) == 0 );
  762. memset( output, 0x00, sizeof( output ) );
  763. TEST_ASSERT( mbedtls_rsa_public( &ctx2, message_str->x, output ) == result );
  764. if( result == 0 )
  765. {
  766. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  767. ctx.len, result_str->len ) == 0 );
  768. }
  769. exit:
  770. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  771. mbedtls_rsa_free( &ctx );
  772. mbedtls_rsa_free( &ctx2 );
  773. }
  774. /* END_CASE */
  775. /* BEGIN_CASE */
  776. void mbedtls_rsa_private( data_t * message_str, int mod, int radix_P,
  777. char * input_P, int radix_Q, char * input_Q,
  778. int radix_N, char * input_N, int radix_E,
  779. char * input_E, data_t * result_str,
  780. int result )
  781. {
  782. unsigned char output[256];
  783. mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
  784. mbedtls_mpi N, P, Q, E;
  785. rnd_pseudo_info rnd_info;
  786. int i;
  787. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  788. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  789. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
  790. mbedtls_rsa_init( &ctx2, MBEDTLS_RSA_PKCS_V15, 0 );
  791. memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
  792. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  793. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  794. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  795. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  796. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
  797. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( mod / 8 ) );
  798. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  799. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  800. /* repeat three times to test updating of blinding values */
  801. for( i = 0; i < 3; i++ )
  802. {
  803. memset( output, 0x00, sizeof( output ) );
  804. TEST_ASSERT( mbedtls_rsa_private( &ctx, rnd_pseudo_rand, &rnd_info,
  805. message_str->x, output ) == result );
  806. if( result == 0 )
  807. {
  808. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  809. ctx.len,
  810. result_str->len ) == 0 );
  811. }
  812. }
  813. /* And now one more time with the copy */
  814. TEST_ASSERT( mbedtls_rsa_copy( &ctx2, &ctx ) == 0 );
  815. /* clear the original to be sure */
  816. mbedtls_rsa_free( &ctx );
  817. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx2 ) == 0 );
  818. memset( output, 0x00, sizeof( output ) );
  819. TEST_ASSERT( mbedtls_rsa_private( &ctx2, rnd_pseudo_rand, &rnd_info,
  820. message_str->x, output ) == result );
  821. if( result == 0 )
  822. {
  823. TEST_ASSERT( mbedtls_test_hexcmp( output, result_str->x,
  824. ctx2.len,
  825. result_str->len ) == 0 );
  826. }
  827. exit:
  828. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  829. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  830. mbedtls_rsa_free( &ctx ); mbedtls_rsa_free( &ctx2 );
  831. }
  832. /* END_CASE */
  833. /* BEGIN_CASE */
  834. void rsa_check_privkey_null( )
  835. {
  836. mbedtls_rsa_context ctx;
  837. memset( &ctx, 0x00, sizeof( mbedtls_rsa_context ) );
  838. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED );
  839. }
  840. /* END_CASE */
  841. /* BEGIN_CASE */
  842. void mbedtls_rsa_check_pubkey( int radix_N, char * input_N, int radix_E,
  843. char * input_E, int result )
  844. {
  845. mbedtls_rsa_context ctx;
  846. mbedtls_mpi N, E;
  847. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  848. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
  849. if( strlen( input_N ) )
  850. {
  851. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  852. }
  853. if( strlen( input_E ) )
  854. {
  855. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  856. }
  857. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  858. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == result );
  859. exit:
  860. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  861. mbedtls_rsa_free( &ctx );
  862. }
  863. /* END_CASE */
  864. /* BEGIN_CASE */
  865. void mbedtls_rsa_check_privkey( int mod, int radix_P, char * input_P,
  866. int radix_Q, char * input_Q, int radix_N,
  867. char * input_N, int radix_E, char * input_E,
  868. int radix_D, char * input_D, int radix_DP,
  869. char * input_DP, int radix_DQ,
  870. char * input_DQ, int radix_QP,
  871. char * input_QP, int result )
  872. {
  873. mbedtls_rsa_context ctx;
  874. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V15, 0 );
  875. ctx.len = mod / 8;
  876. if( strlen( input_P ) )
  877. {
  878. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.P, radix_P, input_P ) == 0 );
  879. }
  880. if( strlen( input_Q ) )
  881. {
  882. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.Q, radix_Q, input_Q ) == 0 );
  883. }
  884. if( strlen( input_N ) )
  885. {
  886. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.N, radix_N, input_N ) == 0 );
  887. }
  888. if( strlen( input_E ) )
  889. {
  890. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.E, radix_E, input_E ) == 0 );
  891. }
  892. if( strlen( input_D ) )
  893. {
  894. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.D, radix_D, input_D ) == 0 );
  895. }
  896. #if !defined(MBEDTLS_RSA_NO_CRT)
  897. if( strlen( input_DP ) )
  898. {
  899. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DP, radix_DP, input_DP ) == 0 );
  900. }
  901. if( strlen( input_DQ ) )
  902. {
  903. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.DQ, radix_DQ, input_DQ ) == 0 );
  904. }
  905. if( strlen( input_QP ) )
  906. {
  907. TEST_ASSERT( mbedtls_mpi_read_string( &ctx.QP, radix_QP, input_QP ) == 0 );
  908. }
  909. #else
  910. ((void) radix_DP); ((void) input_DP);
  911. ((void) radix_DQ); ((void) input_DQ);
  912. ((void) radix_QP); ((void) input_QP);
  913. #endif
  914. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == result );
  915. exit:
  916. mbedtls_rsa_free( &ctx );
  917. }
  918. /* END_CASE */
  919. /* BEGIN_CASE */
  920. void rsa_check_pubpriv( int mod, int radix_Npub, char * input_Npub,
  921. int radix_Epub, char * input_Epub, int radix_P,
  922. char * input_P, int radix_Q, char * input_Q,
  923. int radix_N, char * input_N, int radix_E,
  924. char * input_E, int radix_D, char * input_D,
  925. int radix_DP, char * input_DP, int radix_DQ,
  926. char * input_DQ, int radix_QP, char * input_QP,
  927. int result )
  928. {
  929. mbedtls_rsa_context pub, prv;
  930. mbedtls_rsa_init( &pub, MBEDTLS_RSA_PKCS_V15, 0 );
  931. mbedtls_rsa_init( &prv, MBEDTLS_RSA_PKCS_V15, 0 );
  932. pub.len = mod / 8;
  933. prv.len = mod / 8;
  934. if( strlen( input_Npub ) )
  935. {
  936. TEST_ASSERT( mbedtls_mpi_read_string( &pub.N, radix_Npub, input_Npub ) == 0 );
  937. }
  938. if( strlen( input_Epub ) )
  939. {
  940. TEST_ASSERT( mbedtls_mpi_read_string( &pub.E, radix_Epub, input_Epub ) == 0 );
  941. }
  942. if( strlen( input_P ) )
  943. {
  944. TEST_ASSERT( mbedtls_mpi_read_string( &prv.P, radix_P, input_P ) == 0 );
  945. }
  946. if( strlen( input_Q ) )
  947. {
  948. TEST_ASSERT( mbedtls_mpi_read_string( &prv.Q, radix_Q, input_Q ) == 0 );
  949. }
  950. if( strlen( input_N ) )
  951. {
  952. TEST_ASSERT( mbedtls_mpi_read_string( &prv.N, radix_N, input_N ) == 0 );
  953. }
  954. if( strlen( input_E ) )
  955. {
  956. TEST_ASSERT( mbedtls_mpi_read_string( &prv.E, radix_E, input_E ) == 0 );
  957. }
  958. if( strlen( input_D ) )
  959. {
  960. TEST_ASSERT( mbedtls_mpi_read_string( &prv.D, radix_D, input_D ) == 0 );
  961. }
  962. #if !defined(MBEDTLS_RSA_NO_CRT)
  963. if( strlen( input_DP ) )
  964. {
  965. TEST_ASSERT( mbedtls_mpi_read_string( &prv.DP, radix_DP, input_DP ) == 0 );
  966. }
  967. if( strlen( input_DQ ) )
  968. {
  969. TEST_ASSERT( mbedtls_mpi_read_string( &prv.DQ, radix_DQ, input_DQ ) == 0 );
  970. }
  971. if( strlen( input_QP ) )
  972. {
  973. TEST_ASSERT( mbedtls_mpi_read_string( &prv.QP, radix_QP, input_QP ) == 0 );
  974. }
  975. #else
  976. ((void) radix_DP); ((void) input_DP);
  977. ((void) radix_DQ); ((void) input_DQ);
  978. ((void) radix_QP); ((void) input_QP);
  979. #endif
  980. TEST_ASSERT( mbedtls_rsa_check_pub_priv( &pub, &prv ) == result );
  981. exit:
  982. mbedtls_rsa_free( &pub );
  983. mbedtls_rsa_free( &prv );
  984. }
  985. /* END_CASE */
  986. /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
  987. void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
  988. {
  989. mbedtls_rsa_context ctx;
  990. mbedtls_entropy_context entropy;
  991. mbedtls_ctr_drbg_context ctr_drbg;
  992. const char *pers = "test_suite_rsa";
  993. mbedtls_ctr_drbg_init( &ctr_drbg );
  994. mbedtls_entropy_init( &entropy );
  995. mbedtls_rsa_init ( &ctx, 0, 0 );
  996. TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  997. &entropy, (const unsigned char *) pers,
  998. strlen( pers ) ) == 0 );
  999. TEST_ASSERT( mbedtls_rsa_gen_key( &ctx, mbedtls_ctr_drbg_random, &ctr_drbg, nrbits, exponent ) == result );
  1000. if( result == 0 )
  1001. {
  1002. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  1003. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &ctx.Q ) > 0 );
  1004. }
  1005. exit:
  1006. mbedtls_rsa_free( &ctx );
  1007. mbedtls_ctr_drbg_free( &ctr_drbg );
  1008. mbedtls_entropy_free( &entropy );
  1009. }
  1010. /* END_CASE */
  1011. /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
  1012. void mbedtls_rsa_deduce_primes( int radix_N, char *input_N,
  1013. int radix_D, char *input_D,
  1014. int radix_E, char *input_E,
  1015. int radix_P, char *output_P,
  1016. int radix_Q, char *output_Q,
  1017. int corrupt, int result )
  1018. {
  1019. mbedtls_mpi N, P, Pp, Q, Qp, D, E;
  1020. mbedtls_mpi_init( &N );
  1021. mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  1022. mbedtls_mpi_init( &Pp ); mbedtls_mpi_init( &Qp );
  1023. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
  1024. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  1025. TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
  1026. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  1027. TEST_ASSERT( mbedtls_mpi_read_string( &Qp, radix_P, output_P ) == 0 );
  1028. TEST_ASSERT( mbedtls_mpi_read_string( &Pp, radix_Q, output_Q ) == 0 );
  1029. if( corrupt )
  1030. TEST_ASSERT( mbedtls_mpi_add_int( &D, &D, 2 ) == 0 );
  1031. /* Try to deduce P, Q from N, D, E only. */
  1032. TEST_ASSERT( mbedtls_rsa_deduce_primes( &N, &D, &E, &P, &Q ) == result );
  1033. if( !corrupt )
  1034. {
  1035. /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
  1036. TEST_ASSERT( ( mbedtls_mpi_cmp_mpi( &P, &Pp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Qp ) == 0 ) ||
  1037. ( mbedtls_mpi_cmp_mpi( &P, &Qp ) == 0 && mbedtls_mpi_cmp_mpi( &Q, &Pp ) == 0 ) );
  1038. }
  1039. exit:
  1040. mbedtls_mpi_free( &N );
  1041. mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  1042. mbedtls_mpi_free( &Pp ); mbedtls_mpi_free( &Qp );
  1043. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
  1044. }
  1045. /* END_CASE */
  1046. /* BEGIN_CASE */
  1047. void mbedtls_rsa_deduce_private_exponent( int radix_P, char *input_P,
  1048. int radix_Q, char *input_Q,
  1049. int radix_E, char *input_E,
  1050. int radix_D, char *output_D,
  1051. int corrupt, int result )
  1052. {
  1053. mbedtls_mpi P, Q, D, Dp, E, R, Rp;
  1054. mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  1055. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &Dp );
  1056. mbedtls_mpi_init( &E );
  1057. mbedtls_mpi_init( &R ); mbedtls_mpi_init( &Rp );
  1058. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  1059. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  1060. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  1061. TEST_ASSERT( mbedtls_mpi_read_string( &Dp, radix_D, output_D ) == 0 );
  1062. if( corrupt )
  1063. {
  1064. /* Make E even */
  1065. TEST_ASSERT( mbedtls_mpi_set_bit( &E, 0, 0 ) == 0 );
  1066. }
  1067. /* Try to deduce D from N, P, Q, E. */
  1068. TEST_ASSERT( mbedtls_rsa_deduce_private_exponent( &P, &Q,
  1069. &E, &D ) == result );
  1070. if( !corrupt )
  1071. {
  1072. /*
  1073. * Check that D and Dp agree modulo LCM(P-1, Q-1).
  1074. */
  1075. /* Replace P,Q by P-1, Q-1 */
  1076. TEST_ASSERT( mbedtls_mpi_sub_int( &P, &P, 1 ) == 0 );
  1077. TEST_ASSERT( mbedtls_mpi_sub_int( &Q, &Q, 1 ) == 0 );
  1078. /* Check D == Dp modulo P-1 */
  1079. TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &P ) == 0 );
  1080. TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &P ) == 0 );
  1081. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
  1082. /* Check D == Dp modulo Q-1 */
  1083. TEST_ASSERT( mbedtls_mpi_mod_mpi( &R, &D, &Q ) == 0 );
  1084. TEST_ASSERT( mbedtls_mpi_mod_mpi( &Rp, &Dp, &Q ) == 0 );
  1085. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &R, &Rp ) == 0 );
  1086. }
  1087. exit:
  1088. mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  1089. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &Dp );
  1090. mbedtls_mpi_free( &E );
  1091. mbedtls_mpi_free( &R ); mbedtls_mpi_free( &Rp );
  1092. }
  1093. /* END_CASE */
  1094. /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
  1095. void mbedtls_rsa_import( int radix_N, char *input_N,
  1096. int radix_P, char *input_P,
  1097. int radix_Q, char *input_Q,
  1098. int radix_D, char *input_D,
  1099. int radix_E, char *input_E,
  1100. int successive,
  1101. int is_priv,
  1102. int res_check,
  1103. int res_complete )
  1104. {
  1105. mbedtls_mpi N, P, Q, D, E;
  1106. mbedtls_rsa_context ctx;
  1107. /* Buffers used for encryption-decryption test */
  1108. unsigned char *buf_orig = NULL;
  1109. unsigned char *buf_enc = NULL;
  1110. unsigned char *buf_dec = NULL;
  1111. mbedtls_entropy_context entropy;
  1112. mbedtls_ctr_drbg_context ctr_drbg;
  1113. const char *pers = "test_suite_rsa";
  1114. const int have_N = ( strlen( input_N ) > 0 );
  1115. const int have_P = ( strlen( input_P ) > 0 );
  1116. const int have_Q = ( strlen( input_Q ) > 0 );
  1117. const int have_D = ( strlen( input_D ) > 0 );
  1118. const int have_E = ( strlen( input_E ) > 0 );
  1119. mbedtls_ctr_drbg_init( &ctr_drbg );
  1120. mbedtls_entropy_init( &entropy );
  1121. mbedtls_rsa_init( &ctx, 0, 0 );
  1122. mbedtls_mpi_init( &N );
  1123. mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  1124. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
  1125. TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  1126. (const unsigned char *) pers, strlen( pers ) ) == 0 );
  1127. if( have_N )
  1128. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  1129. if( have_P )
  1130. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  1131. if( have_Q )
  1132. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  1133. if( have_D )
  1134. TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
  1135. if( have_E )
  1136. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  1137. if( !successive )
  1138. {
  1139. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1140. have_N ? &N : NULL,
  1141. have_P ? &P : NULL,
  1142. have_Q ? &Q : NULL,
  1143. have_D ? &D : NULL,
  1144. have_E ? &E : NULL ) == 0 );
  1145. }
  1146. else
  1147. {
  1148. /* Import N, P, Q, D, E separately.
  1149. * This should make no functional difference. */
  1150. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1151. have_N ? &N : NULL,
  1152. NULL, NULL, NULL, NULL ) == 0 );
  1153. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1154. NULL,
  1155. have_P ? &P : NULL,
  1156. NULL, NULL, NULL ) == 0 );
  1157. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1158. NULL, NULL,
  1159. have_Q ? &Q : NULL,
  1160. NULL, NULL ) == 0 );
  1161. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1162. NULL, NULL, NULL,
  1163. have_D ? &D : NULL,
  1164. NULL ) == 0 );
  1165. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1166. NULL, NULL, NULL, NULL,
  1167. have_E ? &E : NULL ) == 0 );
  1168. }
  1169. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
  1170. /* On expected success, perform some public and private
  1171. * key operations to check if the key is working properly. */
  1172. if( res_complete == 0 )
  1173. {
  1174. if( is_priv )
  1175. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
  1176. else
  1177. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
  1178. if( res_check != 0 )
  1179. goto exit;
  1180. buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
  1181. buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
  1182. buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
  1183. if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
  1184. goto exit;
  1185. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
  1186. buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
  1187. /* Make sure the number we're generating is smaller than the modulus */
  1188. buf_orig[0] = 0x00;
  1189. TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
  1190. if( is_priv )
  1191. {
  1192. TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
  1193. &ctr_drbg, buf_enc,
  1194. buf_dec ) == 0 );
  1195. TEST_ASSERT( memcmp( buf_orig, buf_dec,
  1196. mbedtls_rsa_get_len( &ctx ) ) == 0 );
  1197. }
  1198. }
  1199. exit:
  1200. mbedtls_free( buf_orig );
  1201. mbedtls_free( buf_enc );
  1202. mbedtls_free( buf_dec );
  1203. mbedtls_rsa_free( &ctx );
  1204. mbedtls_ctr_drbg_free( &ctr_drbg );
  1205. mbedtls_entropy_free( &entropy );
  1206. mbedtls_mpi_free( &N );
  1207. mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  1208. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
  1209. }
  1210. /* END_CASE */
  1211. /* BEGIN_CASE */
  1212. void mbedtls_rsa_export( int radix_N, char *input_N,
  1213. int radix_P, char *input_P,
  1214. int radix_Q, char *input_Q,
  1215. int radix_D, char *input_D,
  1216. int radix_E, char *input_E,
  1217. int is_priv,
  1218. int successive )
  1219. {
  1220. /* Original MPI's with which we set up the RSA context */
  1221. mbedtls_mpi N, P, Q, D, E;
  1222. /* Exported MPI's */
  1223. mbedtls_mpi Ne, Pe, Qe, De, Ee;
  1224. const int have_N = ( strlen( input_N ) > 0 );
  1225. const int have_P = ( strlen( input_P ) > 0 );
  1226. const int have_Q = ( strlen( input_Q ) > 0 );
  1227. const int have_D = ( strlen( input_D ) > 0 );
  1228. const int have_E = ( strlen( input_E ) > 0 );
  1229. mbedtls_rsa_context ctx;
  1230. mbedtls_rsa_init( &ctx, 0, 0 );
  1231. mbedtls_mpi_init( &N );
  1232. mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  1233. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
  1234. mbedtls_mpi_init( &Ne );
  1235. mbedtls_mpi_init( &Pe ); mbedtls_mpi_init( &Qe );
  1236. mbedtls_mpi_init( &De ); mbedtls_mpi_init( &Ee );
  1237. /* Setup RSA context */
  1238. if( have_N )
  1239. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  1240. if( have_P )
  1241. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  1242. if( have_Q )
  1243. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  1244. if( have_D )
  1245. TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
  1246. if( have_E )
  1247. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  1248. TEST_ASSERT( mbedtls_rsa_import( &ctx,
  1249. strlen( input_N ) ? &N : NULL,
  1250. strlen( input_P ) ? &P : NULL,
  1251. strlen( input_Q ) ? &Q : NULL,
  1252. strlen( input_D ) ? &D : NULL,
  1253. strlen( input_E ) ? &E : NULL ) == 0 );
  1254. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  1255. /*
  1256. * Export parameters and compare to original ones.
  1257. */
  1258. /* N and E must always be present. */
  1259. if( !successive )
  1260. {
  1261. TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, &Ee ) == 0 );
  1262. }
  1263. else
  1264. {
  1265. TEST_ASSERT( mbedtls_rsa_export( &ctx, &Ne, NULL, NULL, NULL, NULL ) == 0 );
  1266. TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL, NULL, &Ee ) == 0 );
  1267. }
  1268. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &N, &Ne ) == 0 );
  1269. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &E, &Ee ) == 0 );
  1270. /* If we were providing enough information to setup a complete private context,
  1271. * we expect to be able to export all core parameters. */
  1272. if( is_priv )
  1273. {
  1274. if( !successive )
  1275. {
  1276. TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, &Qe,
  1277. &De, NULL ) == 0 );
  1278. }
  1279. else
  1280. {
  1281. TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, &Pe, NULL,
  1282. NULL, NULL ) == 0 );
  1283. TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, &Qe,
  1284. NULL, NULL ) == 0 );
  1285. TEST_ASSERT( mbedtls_rsa_export( &ctx, NULL, NULL, NULL,
  1286. &De, NULL ) == 0 );
  1287. }
  1288. if( have_P )
  1289. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &P, &Pe ) == 0 );
  1290. if( have_Q )
  1291. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &Q, &Qe ) == 0 );
  1292. if( have_D )
  1293. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &D, &De ) == 0 );
  1294. /* While at it, perform a sanity check */
  1295. TEST_ASSERT( mbedtls_rsa_validate_params( &Ne, &Pe, &Qe, &De, &Ee,
  1296. NULL, NULL ) == 0 );
  1297. }
  1298. exit:
  1299. mbedtls_rsa_free( &ctx );
  1300. mbedtls_mpi_free( &N );
  1301. mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  1302. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
  1303. mbedtls_mpi_free( &Ne );
  1304. mbedtls_mpi_free( &Pe ); mbedtls_mpi_free( &Qe );
  1305. mbedtls_mpi_free( &De ); mbedtls_mpi_free( &Ee );
  1306. }
  1307. /* END_CASE */
  1308. /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */
  1309. void mbedtls_rsa_validate_params( int radix_N, char *input_N,
  1310. int radix_P, char *input_P,
  1311. int radix_Q, char *input_Q,
  1312. int radix_D, char *input_D,
  1313. int radix_E, char *input_E,
  1314. int prng, int result )
  1315. {
  1316. /* Original MPI's with which we set up the RSA context */
  1317. mbedtls_mpi N, P, Q, D, E;
  1318. const int have_N = ( strlen( input_N ) > 0 );
  1319. const int have_P = ( strlen( input_P ) > 0 );
  1320. const int have_Q = ( strlen( input_Q ) > 0 );
  1321. const int have_D = ( strlen( input_D ) > 0 );
  1322. const int have_E = ( strlen( input_E ) > 0 );
  1323. mbedtls_entropy_context entropy;
  1324. mbedtls_ctr_drbg_context ctr_drbg;
  1325. const char *pers = "test_suite_rsa";
  1326. mbedtls_mpi_init( &N );
  1327. mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  1328. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E );
  1329. mbedtls_ctr_drbg_init( &ctr_drbg );
  1330. mbedtls_entropy_init( &entropy );
  1331. TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  1332. &entropy, (const unsigned char *) pers,
  1333. strlen( pers ) ) == 0 );
  1334. if( have_N )
  1335. TEST_ASSERT( mbedtls_mpi_read_string( &N, radix_N, input_N ) == 0 );
  1336. if( have_P )
  1337. TEST_ASSERT( mbedtls_mpi_read_string( &P, radix_P, input_P ) == 0 );
  1338. if( have_Q )
  1339. TEST_ASSERT( mbedtls_mpi_read_string( &Q, radix_Q, input_Q ) == 0 );
  1340. if( have_D )
  1341. TEST_ASSERT( mbedtls_mpi_read_string( &D, radix_D, input_D ) == 0 );
  1342. if( have_E )
  1343. TEST_ASSERT( mbedtls_mpi_read_string( &E, radix_E, input_E ) == 0 );
  1344. TEST_ASSERT( mbedtls_rsa_validate_params( have_N ? &N : NULL,
  1345. have_P ? &P : NULL,
  1346. have_Q ? &Q : NULL,
  1347. have_D ? &D : NULL,
  1348. have_E ? &E : NULL,
  1349. prng ? mbedtls_ctr_drbg_random : NULL,
  1350. prng ? &ctr_drbg : NULL ) == result );
  1351. exit:
  1352. mbedtls_ctr_drbg_free( &ctr_drbg );
  1353. mbedtls_entropy_free( &entropy );
  1354. mbedtls_mpi_free( &N );
  1355. mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  1356. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E );
  1357. }
  1358. /* END_CASE */
  1359. /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C */
  1360. void mbedtls_rsa_export_raw( data_t *input_N, data_t *input_P,
  1361. data_t *input_Q, data_t *input_D,
  1362. data_t *input_E, int is_priv,
  1363. int successive )
  1364. {
  1365. /* Exported buffers */
  1366. unsigned char bufNe[256];
  1367. unsigned char bufPe[128];
  1368. unsigned char bufQe[128];
  1369. unsigned char bufDe[256];
  1370. unsigned char bufEe[1];
  1371. mbedtls_rsa_context ctx;
  1372. mbedtls_rsa_init( &ctx, 0, 0 );
  1373. /* Setup RSA context */
  1374. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1375. input_N->len ? input_N->x : NULL, input_N->len,
  1376. input_P->len ? input_P->x : NULL, input_P->len,
  1377. input_Q->len ? input_Q->x : NULL, input_Q->len,
  1378. input_D->len ? input_D->x : NULL, input_D->len,
  1379. input_E->len ? input_E->x : NULL, input_E->len ) == 0 );
  1380. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  1381. /*
  1382. * Export parameters and compare to original ones.
  1383. */
  1384. /* N and E must always be present. */
  1385. if( !successive )
  1386. {
  1387. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
  1388. NULL, 0, NULL, 0, NULL, 0,
  1389. bufEe, input_E->len ) == 0 );
  1390. }
  1391. else
  1392. {
  1393. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, bufNe, input_N->len,
  1394. NULL, 0, NULL, 0, NULL, 0,
  1395. NULL, 0 ) == 0 );
  1396. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
  1397. NULL, 0, NULL, 0, NULL, 0,
  1398. bufEe, input_E->len ) == 0 );
  1399. }
  1400. TEST_ASSERT( memcmp( input_N->x, bufNe, input_N->len ) == 0 );
  1401. TEST_ASSERT( memcmp( input_E->x, bufEe, input_E->len ) == 0 );
  1402. /* If we were providing enough information to setup a complete private context,
  1403. * we expect to be able to export all core parameters. */
  1404. if( is_priv )
  1405. {
  1406. if( !successive )
  1407. {
  1408. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
  1409. bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
  1410. bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
  1411. bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
  1412. NULL, 0 ) == 0 );
  1413. }
  1414. else
  1415. {
  1416. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0,
  1417. bufPe, input_P->len ? input_P->len : sizeof( bufPe ),
  1418. NULL, 0, NULL, 0,
  1419. NULL, 0 ) == 0 );
  1420. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0,
  1421. bufQe, input_Q->len ? input_Q->len : sizeof( bufQe ),
  1422. NULL, 0, NULL, 0 ) == 0 );
  1423. TEST_ASSERT( mbedtls_rsa_export_raw( &ctx, NULL, 0, NULL, 0, NULL, 0,
  1424. bufDe, input_D->len ? input_D->len : sizeof( bufDe ),
  1425. NULL, 0 ) == 0 );
  1426. }
  1427. if( input_P->len )
  1428. TEST_ASSERT( memcmp( input_P->x, bufPe, input_P->len ) == 0 );
  1429. if( input_Q->len )
  1430. TEST_ASSERT( memcmp( input_Q->x, bufQe, input_Q->len ) == 0 );
  1431. if( input_D->len )
  1432. TEST_ASSERT( memcmp( input_D->x, bufDe, input_D->len ) == 0 );
  1433. }
  1434. exit:
  1435. mbedtls_rsa_free( &ctx );
  1436. }
  1437. /* END_CASE */
  1438. /* BEGIN_CASE depends_on:MBEDTLS_CTR_DRBG_C:MBEDTLS_ENTROPY_C:ENTROPY_HAVE_STRONG */
  1439. void mbedtls_rsa_import_raw( data_t *input_N,
  1440. data_t *input_P, data_t *input_Q,
  1441. data_t *input_D, data_t *input_E,
  1442. int successive,
  1443. int is_priv,
  1444. int res_check,
  1445. int res_complete )
  1446. {
  1447. /* Buffers used for encryption-decryption test */
  1448. unsigned char *buf_orig = NULL;
  1449. unsigned char *buf_enc = NULL;
  1450. unsigned char *buf_dec = NULL;
  1451. mbedtls_rsa_context ctx;
  1452. mbedtls_entropy_context entropy;
  1453. mbedtls_ctr_drbg_context ctr_drbg;
  1454. const char *pers = "test_suite_rsa";
  1455. mbedtls_ctr_drbg_init( &ctr_drbg );
  1456. mbedtls_entropy_init( &entropy );
  1457. mbedtls_rsa_init( &ctx, 0, 0 );
  1458. TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  1459. &entropy, (const unsigned char *) pers,
  1460. strlen( pers ) ) == 0 );
  1461. if( !successive )
  1462. {
  1463. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1464. ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
  1465. ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
  1466. ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
  1467. ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
  1468. ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
  1469. }
  1470. else
  1471. {
  1472. /* Import N, P, Q, D, E separately.
  1473. * This should make no functional difference. */
  1474. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1475. ( input_N->len > 0 ) ? input_N->x : NULL, input_N->len,
  1476. NULL, 0, NULL, 0, NULL, 0, NULL, 0 ) == 0 );
  1477. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1478. NULL, 0,
  1479. ( input_P->len > 0 ) ? input_P->x : NULL, input_P->len,
  1480. NULL, 0, NULL, 0, NULL, 0 ) == 0 );
  1481. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1482. NULL, 0, NULL, 0,
  1483. ( input_Q->len > 0 ) ? input_Q->x : NULL, input_Q->len,
  1484. NULL, 0, NULL, 0 ) == 0 );
  1485. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1486. NULL, 0, NULL, 0, NULL, 0,
  1487. ( input_D->len > 0 ) ? input_D->x : NULL, input_D->len,
  1488. NULL, 0 ) == 0 );
  1489. TEST_ASSERT( mbedtls_rsa_import_raw( &ctx,
  1490. NULL, 0, NULL, 0, NULL, 0, NULL, 0,
  1491. ( input_E->len > 0 ) ? input_E->x : NULL, input_E->len ) == 0 );
  1492. }
  1493. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == res_complete );
  1494. /* On expected success, perform some public and private
  1495. * key operations to check if the key is working properly. */
  1496. if( res_complete == 0 )
  1497. {
  1498. if( is_priv )
  1499. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == res_check );
  1500. else
  1501. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == res_check );
  1502. if( res_check != 0 )
  1503. goto exit;
  1504. buf_orig = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
  1505. buf_enc = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
  1506. buf_dec = mbedtls_calloc( 1, mbedtls_rsa_get_len( &ctx ) );
  1507. if( buf_orig == NULL || buf_enc == NULL || buf_dec == NULL )
  1508. goto exit;
  1509. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctr_drbg,
  1510. buf_orig, mbedtls_rsa_get_len( &ctx ) ) == 0 );
  1511. /* Make sure the number we're generating is smaller than the modulus */
  1512. buf_orig[0] = 0x00;
  1513. TEST_ASSERT( mbedtls_rsa_public( &ctx, buf_orig, buf_enc ) == 0 );
  1514. if( is_priv )
  1515. {
  1516. TEST_ASSERT( mbedtls_rsa_private( &ctx, mbedtls_ctr_drbg_random,
  1517. &ctr_drbg, buf_enc,
  1518. buf_dec ) == 0 );
  1519. TEST_ASSERT( memcmp( buf_orig, buf_dec,
  1520. mbedtls_rsa_get_len( &ctx ) ) == 0 );
  1521. }
  1522. }
  1523. exit:
  1524. mbedtls_free( buf_orig );
  1525. mbedtls_free( buf_enc );
  1526. mbedtls_free( buf_dec );
  1527. mbedtls_rsa_free( &ctx );
  1528. mbedtls_ctr_drbg_free( &ctr_drbg );
  1529. mbedtls_entropy_free( &entropy );
  1530. }
  1531. /* END_CASE */
  1532. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  1533. void rsa_selftest( )
  1534. {
  1535. TEST_ASSERT( mbedtls_rsa_self_test( 1 ) == 0 );
  1536. }
  1537. /* END_CASE */