test_suite_rsa.function 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/rsa.h"
  3. #include "rsa_alt_helpers.h"
  4. #include "mbedtls/legacy_or_psa.h"
  5. /* END_HEADER */
  6. /* BEGIN_DEPENDENCIES
  7. * depends_on:MBEDTLS_RSA_C:MBEDTLS_BIGNUM_C:MBEDTLS_GENPRIME
  8. * END_DEPENDENCIES
  9. */
  10. /* BEGIN_CASE */
  11. void rsa_invalid_param()
  12. {
  13. mbedtls_rsa_context ctx;
  14. const int invalid_padding = 42;
  15. const int invalid_hash_id = 0xff;
  16. unsigned char buf[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
  17. size_t buf_len = sizeof(buf);
  18. mbedtls_rsa_init(&ctx);
  19. TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
  20. invalid_padding,
  21. MBEDTLS_MD_NONE),
  22. MBEDTLS_ERR_RSA_INVALID_PADDING);
  23. TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
  24. MBEDTLS_RSA_PKCS_V21,
  25. invalid_hash_id),
  26. MBEDTLS_ERR_RSA_INVALID_PADDING);
  27. TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
  28. NULL, MBEDTLS_MD_NONE,
  29. buf_len,
  30. NULL, buf),
  31. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  32. TEST_EQUAL(mbedtls_rsa_pkcs1_sign(&ctx, NULL,
  33. NULL, MBEDTLS_MD_SHA256,
  34. 0,
  35. NULL, buf),
  36. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  37. TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE,
  38. buf_len,
  39. NULL, buf),
  40. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  41. TEST_EQUAL(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_SHA256,
  42. 0,
  43. NULL, buf),
  44. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  45. #if !defined(MBEDTLS_PKCS1_V15)
  46. TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
  47. MBEDTLS_RSA_PKCS_V15,
  48. MBEDTLS_MD_NONE),
  49. MBEDTLS_ERR_RSA_INVALID_PADDING);
  50. #endif
  51. #if defined(MBEDTLS_PKCS1_V15)
  52. TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
  53. NULL, MBEDTLS_MD_NONE,
  54. buf_len,
  55. NULL, buf),
  56. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  57. TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_sign(&ctx, NULL,
  58. NULL, MBEDTLS_MD_SHA256,
  59. 0,
  60. NULL, buf),
  61. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  62. TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_NONE,
  63. buf_len,
  64. NULL, buf),
  65. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  66. TEST_EQUAL(mbedtls_rsa_rsassa_pkcs1_v15_verify(&ctx, MBEDTLS_MD_SHA256,
  67. 0,
  68. NULL, buf),
  69. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  70. #endif
  71. #if !defined(MBEDTLS_PKCS1_V21)
  72. TEST_EQUAL(mbedtls_rsa_set_padding(&ctx,
  73. MBEDTLS_RSA_PKCS_V21,
  74. MBEDTLS_MD_NONE),
  75. MBEDTLS_ERR_RSA_INVALID_PADDING);
  76. #endif
  77. #if defined(MBEDTLS_PKCS1_V21)
  78. TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
  79. MBEDTLS_MD_NONE, buf_len,
  80. NULL, buf_len,
  81. buf),
  82. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  83. TEST_EQUAL(mbedtls_rsa_rsassa_pss_sign_ext(&ctx, NULL, NULL,
  84. MBEDTLS_MD_SHA256, 0,
  85. NULL, buf_len,
  86. buf),
  87. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  88. TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_NONE,
  89. buf_len, NULL,
  90. MBEDTLS_MD_NONE,
  91. buf_len, buf),
  92. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  93. TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify_ext(&ctx, MBEDTLS_MD_SHA256,
  94. 0, NULL,
  95. MBEDTLS_MD_NONE,
  96. buf_len, buf),
  97. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  98. TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_NONE,
  99. buf_len,
  100. NULL, buf),
  101. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  102. TEST_EQUAL(mbedtls_rsa_rsassa_pss_verify(&ctx, MBEDTLS_MD_SHA256,
  103. 0,
  104. NULL, buf),
  105. MBEDTLS_ERR_RSA_BAD_INPUT_DATA);
  106. #endif
  107. exit:
  108. mbedtls_rsa_free(&ctx);
  109. }
  110. /* END_CASE */
  111. /* BEGIN_CASE */
  112. void rsa_init_free(int reinit)
  113. {
  114. mbedtls_rsa_context ctx;
  115. /* Double free is not explicitly documented to work, but we rely on it
  116. * even inside the library so that you can call mbedtls_rsa_free()
  117. * unconditionally on an error path without checking whether it has
  118. * already been called in the success path. */
  119. mbedtls_rsa_init(&ctx);
  120. mbedtls_rsa_free(&ctx);
  121. if (reinit) {
  122. mbedtls_rsa_init(&ctx);
  123. }
  124. mbedtls_rsa_free(&ctx);
  125. /* This test case always succeeds, functionally speaking. A plausible
  126. * bug might trigger an invalid pointer dereference or a memory leak. */
  127. goto exit;
  128. }
  129. /* END_CASE */
  130. /* BEGIN_CASE */
  131. void mbedtls_rsa_pkcs1_sign(data_t *message_str, int padding_mode,
  132. int digest, int mod, char *input_P,
  133. char *input_Q, char *input_N, char *input_E,
  134. data_t *result_str, int result)
  135. {
  136. unsigned char output[256];
  137. mbedtls_rsa_context ctx;
  138. mbedtls_mpi N, P, Q, E;
  139. mbedtls_test_rnd_pseudo_info rnd_info;
  140. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
  141. mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
  142. mbedtls_rsa_init(&ctx);
  143. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  144. MBEDTLS_MD_NONE) == 0);
  145. memset(output, 0x00, sizeof(output));
  146. memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
  147. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  148. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  149. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  150. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  151. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
  152. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  153. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
  154. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
  155. TEST_ASSERT(mbedtls_rsa_pkcs1_sign(
  156. &ctx, &mbedtls_test_rnd_pseudo_rand, &rnd_info,
  157. digest, message_str->len, message_str->x,
  158. output) == result);
  159. if (result == 0) {
  160. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  161. ctx.len, result_str->len) == 0);
  162. }
  163. exit:
  164. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
  165. mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
  166. mbedtls_rsa_free(&ctx);
  167. }
  168. /* END_CASE */
  169. /* BEGIN_CASE */
  170. void mbedtls_rsa_pkcs1_verify(data_t *message_str, int padding_mode,
  171. int digest, int mod,
  172. char *input_N, char *input_E,
  173. data_t *result_str, int result)
  174. {
  175. mbedtls_rsa_context ctx;
  176. mbedtls_mpi N, E;
  177. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  178. mbedtls_rsa_init(&ctx);
  179. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  180. MBEDTLS_MD_NONE) == 0);
  181. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  182. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  183. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
  184. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  185. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
  186. TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, digest, message_str->len, message_str->x,
  187. result_str->x) == result);
  188. exit:
  189. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  190. mbedtls_rsa_free(&ctx);
  191. }
  192. /* END_CASE */
  193. /* BEGIN_CASE */
  194. void rsa_pkcs1_sign_raw(data_t *hash_result,
  195. int padding_mode, int mod,
  196. char *input_P, char *input_Q,
  197. char *input_N, char *input_E,
  198. data_t *result_str)
  199. {
  200. unsigned char output[256];
  201. mbedtls_rsa_context ctx;
  202. mbedtls_mpi N, P, Q, E;
  203. mbedtls_test_rnd_pseudo_info rnd_info;
  204. mbedtls_rsa_init(&ctx);
  205. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
  206. mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
  207. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  208. MBEDTLS_MD_NONE) == 0);
  209. memset(output, 0x00, sizeof(output));
  210. memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
  211. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  212. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  213. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  214. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  215. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
  216. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  217. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
  218. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
  219. TEST_ASSERT(mbedtls_rsa_pkcs1_sign(&ctx, &mbedtls_test_rnd_pseudo_rand,
  220. &rnd_info, MBEDTLS_MD_NONE,
  221. hash_result->len,
  222. hash_result->x, output) == 0);
  223. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  224. ctx.len, result_str->len) == 0);
  225. exit:
  226. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
  227. mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
  228. mbedtls_rsa_free(&ctx);
  229. }
  230. /* END_CASE */
  231. /* BEGIN_CASE */
  232. void rsa_pkcs1_verify_raw(data_t *hash_result,
  233. int padding_mode, int mod,
  234. char *input_N, char *input_E,
  235. data_t *result_str, int correct)
  236. {
  237. unsigned char output[256];
  238. mbedtls_rsa_context ctx;
  239. mbedtls_mpi N, E;
  240. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  241. mbedtls_rsa_init(&ctx);
  242. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  243. MBEDTLS_MD_NONE) == 0);
  244. memset(output, 0x00, sizeof(output));
  245. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  246. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  247. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
  248. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  249. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
  250. TEST_ASSERT(mbedtls_rsa_pkcs1_verify(&ctx, MBEDTLS_MD_NONE, hash_result->len, hash_result->x,
  251. result_str->x) == correct);
  252. exit:
  253. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  254. mbedtls_rsa_free(&ctx);
  255. }
  256. /* END_CASE */
  257. /* BEGIN_CASE */
  258. void mbedtls_rsa_pkcs1_encrypt(data_t *message_str, int padding_mode,
  259. int mod, char *input_N, char *input_E,
  260. data_t *result_str, int result)
  261. {
  262. unsigned char output[256];
  263. mbedtls_rsa_context ctx;
  264. mbedtls_test_rnd_pseudo_info rnd_info;
  265. mbedtls_mpi N, E;
  266. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  267. memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
  268. mbedtls_rsa_init(&ctx);
  269. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  270. MBEDTLS_MD_NONE) == 0);
  271. memset(output, 0x00, sizeof(output));
  272. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  273. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  274. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
  275. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  276. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
  277. TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx,
  278. &mbedtls_test_rnd_pseudo_rand,
  279. &rnd_info, message_str->len,
  280. message_str->x,
  281. output) == result);
  282. if (result == 0) {
  283. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  284. ctx.len, result_str->len) == 0);
  285. }
  286. exit:
  287. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  288. mbedtls_rsa_free(&ctx);
  289. }
  290. /* END_CASE */
  291. /* BEGIN_CASE */
  292. void rsa_pkcs1_encrypt_bad_rng(data_t *message_str, int padding_mode,
  293. int mod, char *input_N, char *input_E,
  294. data_t *result_str, int result)
  295. {
  296. unsigned char output[256];
  297. mbedtls_rsa_context ctx;
  298. mbedtls_mpi N, E;
  299. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  300. mbedtls_rsa_init(&ctx);
  301. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  302. MBEDTLS_MD_NONE) == 0);
  303. memset(output, 0x00, sizeof(output));
  304. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  305. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  306. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
  307. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  308. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
  309. TEST_ASSERT(mbedtls_rsa_pkcs1_encrypt(&ctx, &mbedtls_test_rnd_zero_rand,
  310. NULL, message_str->len,
  311. message_str->x,
  312. output) == result);
  313. if (result == 0) {
  314. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  315. ctx.len, result_str->len) == 0);
  316. }
  317. exit:
  318. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  319. mbedtls_rsa_free(&ctx);
  320. }
  321. /* END_CASE */
  322. /* BEGIN_CASE */
  323. void mbedtls_rsa_pkcs1_decrypt(data_t *message_str, int padding_mode,
  324. int mod, char *input_P,
  325. char *input_Q, char *input_N,
  326. char *input_E, int max_output,
  327. data_t *result_str, int result)
  328. {
  329. unsigned char output[32];
  330. mbedtls_rsa_context ctx;
  331. size_t output_len;
  332. mbedtls_test_rnd_pseudo_info rnd_info;
  333. mbedtls_mpi N, P, Q, E;
  334. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
  335. mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
  336. mbedtls_rsa_init(&ctx);
  337. TEST_ASSERT(mbedtls_rsa_set_padding(&ctx, padding_mode,
  338. MBEDTLS_MD_NONE) == 0);
  339. memset(output, 0x00, sizeof(output));
  340. memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
  341. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  342. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  343. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  344. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  345. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
  346. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  347. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
  348. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
  349. output_len = 0;
  350. TEST_ASSERT(mbedtls_rsa_pkcs1_decrypt(&ctx, mbedtls_test_rnd_pseudo_rand,
  351. &rnd_info,
  352. &output_len, message_str->x, output,
  353. max_output) == result);
  354. if (result == 0) {
  355. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  356. output_len,
  357. result_str->len) == 0);
  358. }
  359. exit:
  360. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
  361. mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
  362. mbedtls_rsa_free(&ctx);
  363. }
  364. /* END_CASE */
  365. /* BEGIN_CASE */
  366. void mbedtls_rsa_public(data_t *message_str, int mod,
  367. char *input_N, char *input_E,
  368. data_t *result_str, int result)
  369. {
  370. unsigned char output[256];
  371. mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
  372. mbedtls_mpi N, E;
  373. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  374. mbedtls_rsa_init(&ctx);
  375. mbedtls_rsa_init(&ctx2);
  376. memset(output, 0x00, sizeof(output));
  377. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  378. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  379. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
  380. /* Check test data consistency */
  381. TEST_ASSERT(message_str->len == (size_t) (mod / 8));
  382. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  383. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == 0);
  384. TEST_ASSERT(mbedtls_rsa_public(&ctx, message_str->x, output) == result);
  385. if (result == 0) {
  386. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  387. ctx.len, result_str->len) == 0);
  388. }
  389. /* And now with the copy */
  390. TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
  391. /* clear the original to be sure */
  392. mbedtls_rsa_free(&ctx);
  393. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx2) == 0);
  394. memset(output, 0x00, sizeof(output));
  395. TEST_ASSERT(mbedtls_rsa_public(&ctx2, message_str->x, output) == result);
  396. if (result == 0) {
  397. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  398. ctx.len, result_str->len) == 0);
  399. }
  400. exit:
  401. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  402. mbedtls_rsa_free(&ctx);
  403. mbedtls_rsa_free(&ctx2);
  404. }
  405. /* END_CASE */
  406. /* BEGIN_CASE */
  407. void mbedtls_rsa_private(data_t *message_str, int mod,
  408. char *input_P, char *input_Q,
  409. char *input_N, char *input_E,
  410. data_t *result_str, int result)
  411. {
  412. unsigned char output[256];
  413. mbedtls_rsa_context ctx, ctx2; /* Also test mbedtls_rsa_copy() while at it */
  414. mbedtls_mpi N, P, Q, E;
  415. mbedtls_test_rnd_pseudo_info rnd_info;
  416. int i;
  417. mbedtls_mpi_init(&N); mbedtls_mpi_init(&P);
  418. mbedtls_mpi_init(&Q); mbedtls_mpi_init(&E);
  419. mbedtls_rsa_init(&ctx);
  420. mbedtls_rsa_init(&ctx2);
  421. memset(&rnd_info, 0, sizeof(mbedtls_test_rnd_pseudo_info));
  422. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  423. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  424. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  425. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  426. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, &P, &Q, NULL, &E) == 0);
  427. /* Check test data consistency */
  428. TEST_ASSERT(message_str->len == (size_t) (mod / 8));
  429. TEST_ASSERT(mbedtls_rsa_get_len(&ctx) == (size_t) (mod / 8));
  430. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
  431. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
  432. /* repeat three times to test updating of blinding values */
  433. for (i = 0; i < 3; i++) {
  434. memset(output, 0x00, sizeof(output));
  435. TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_pseudo_rand,
  436. &rnd_info, message_str->x,
  437. output) == result);
  438. if (result == 0) {
  439. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  440. ctx.len,
  441. result_str->len) == 0);
  442. }
  443. }
  444. /* And now one more time with the copy */
  445. TEST_ASSERT(mbedtls_rsa_copy(&ctx2, &ctx) == 0);
  446. /* clear the original to be sure */
  447. mbedtls_rsa_free(&ctx);
  448. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx2) == 0);
  449. memset(output, 0x00, sizeof(output));
  450. TEST_ASSERT(mbedtls_rsa_private(&ctx2, mbedtls_test_rnd_pseudo_rand,
  451. &rnd_info, message_str->x,
  452. output) == result);
  453. if (result == 0) {
  454. TEST_ASSERT(mbedtls_test_hexcmp(output, result_str->x,
  455. ctx2.len,
  456. result_str->len) == 0);
  457. }
  458. exit:
  459. mbedtls_mpi_free(&N); mbedtls_mpi_free(&P);
  460. mbedtls_mpi_free(&Q); mbedtls_mpi_free(&E);
  461. mbedtls_rsa_free(&ctx); mbedtls_rsa_free(&ctx2);
  462. }
  463. /* END_CASE */
  464. /* BEGIN_CASE */
  465. void rsa_check_privkey_null()
  466. {
  467. mbedtls_rsa_context ctx;
  468. memset(&ctx, 0x00, sizeof(mbedtls_rsa_context));
  469. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == MBEDTLS_ERR_RSA_KEY_CHECK_FAILED);
  470. }
  471. /* END_CASE */
  472. /* BEGIN_CASE */
  473. void mbedtls_rsa_check_pubkey(char *input_N, char *input_E, int result)
  474. {
  475. mbedtls_rsa_context ctx;
  476. mbedtls_mpi N, E;
  477. mbedtls_mpi_init(&N); mbedtls_mpi_init(&E);
  478. mbedtls_rsa_init(&ctx);
  479. if (strlen(input_N)) {
  480. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  481. }
  482. if (strlen(input_E)) {
  483. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  484. }
  485. TEST_ASSERT(mbedtls_rsa_import(&ctx, &N, NULL, NULL, NULL, &E) == 0);
  486. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == result);
  487. exit:
  488. mbedtls_mpi_free(&N); mbedtls_mpi_free(&E);
  489. mbedtls_rsa_free(&ctx);
  490. }
  491. /* END_CASE */
  492. /* BEGIN_CASE */
  493. void mbedtls_rsa_check_privkey(int mod, char *input_P, char *input_Q,
  494. char *input_N, char *input_E, char *input_D,
  495. char *input_DP, char *input_DQ, char *input_QP,
  496. int result)
  497. {
  498. mbedtls_rsa_context ctx;
  499. mbedtls_rsa_init(&ctx);
  500. ctx.len = mod / 8;
  501. if (strlen(input_P)) {
  502. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.P, input_P) == 0);
  503. }
  504. if (strlen(input_Q)) {
  505. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.Q, input_Q) == 0);
  506. }
  507. if (strlen(input_N)) {
  508. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.N, input_N) == 0);
  509. }
  510. if (strlen(input_E)) {
  511. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.E, input_E) == 0);
  512. }
  513. if (strlen(input_D)) {
  514. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.D, input_D) == 0);
  515. }
  516. #if !defined(MBEDTLS_RSA_NO_CRT)
  517. if (strlen(input_DP)) {
  518. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DP, input_DP) == 0);
  519. }
  520. if (strlen(input_DQ)) {
  521. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.DQ, input_DQ) == 0);
  522. }
  523. if (strlen(input_QP)) {
  524. TEST_ASSERT(mbedtls_test_read_mpi(&ctx.QP, input_QP) == 0);
  525. }
  526. #else
  527. ((void) input_DP);
  528. ((void) input_DQ);
  529. ((void) input_QP);
  530. #endif
  531. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == result);
  532. exit:
  533. mbedtls_rsa_free(&ctx);
  534. }
  535. /* END_CASE */
  536. /* BEGIN_CASE */
  537. void rsa_check_pubpriv(int mod, char *input_Npub, char *input_Epub,
  538. char *input_P, char *input_Q, char *input_N,
  539. char *input_E, char *input_D, char *input_DP,
  540. char *input_DQ, char *input_QP, int result)
  541. {
  542. mbedtls_rsa_context pub, prv;
  543. mbedtls_rsa_init(&pub);
  544. mbedtls_rsa_init(&prv);
  545. pub.len = mod / 8;
  546. prv.len = mod / 8;
  547. if (strlen(input_Npub)) {
  548. TEST_ASSERT(mbedtls_test_read_mpi(&pub.N, input_Npub) == 0);
  549. }
  550. if (strlen(input_Epub)) {
  551. TEST_ASSERT(mbedtls_test_read_mpi(&pub.E, input_Epub) == 0);
  552. }
  553. if (strlen(input_P)) {
  554. TEST_ASSERT(mbedtls_test_read_mpi(&prv.P, input_P) == 0);
  555. }
  556. if (strlen(input_Q)) {
  557. TEST_ASSERT(mbedtls_test_read_mpi(&prv.Q, input_Q) == 0);
  558. }
  559. if (strlen(input_N)) {
  560. TEST_ASSERT(mbedtls_test_read_mpi(&prv.N, input_N) == 0);
  561. }
  562. if (strlen(input_E)) {
  563. TEST_ASSERT(mbedtls_test_read_mpi(&prv.E, input_E) == 0);
  564. }
  565. if (strlen(input_D)) {
  566. TEST_ASSERT(mbedtls_test_read_mpi(&prv.D, input_D) == 0);
  567. }
  568. #if !defined(MBEDTLS_RSA_NO_CRT)
  569. if (strlen(input_DP)) {
  570. TEST_ASSERT(mbedtls_test_read_mpi(&prv.DP, input_DP) == 0);
  571. }
  572. if (strlen(input_DQ)) {
  573. TEST_ASSERT(mbedtls_test_read_mpi(&prv.DQ, input_DQ) == 0);
  574. }
  575. if (strlen(input_QP)) {
  576. TEST_ASSERT(mbedtls_test_read_mpi(&prv.QP, input_QP) == 0);
  577. }
  578. #else
  579. ((void) input_DP);
  580. ((void) input_DQ);
  581. ((void) input_QP);
  582. #endif
  583. TEST_ASSERT(mbedtls_rsa_check_pub_priv(&pub, &prv) == result);
  584. exit:
  585. mbedtls_rsa_free(&pub);
  586. mbedtls_rsa_free(&prv);
  587. }
  588. /* END_CASE */
  589. /* BEGIN_CASE */
  590. void mbedtls_rsa_gen_key(int nrbits, int exponent, int result)
  591. {
  592. mbedtls_rsa_context ctx;
  593. mbedtls_rsa_init(&ctx);
  594. /* This test uses an insecure RNG, suitable only for testing.
  595. * In production, always use a cryptographically strong RNG! */
  596. TEST_ASSERT(mbedtls_rsa_gen_key(&ctx, mbedtls_test_rnd_std_rand, NULL, nrbits,
  597. exponent) == result);
  598. if (result == 0) {
  599. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == 0);
  600. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&ctx.P, &ctx.Q) > 0);
  601. }
  602. exit:
  603. mbedtls_rsa_free(&ctx);
  604. }
  605. /* END_CASE */
  606. /* BEGIN_CASE */
  607. void mbedtls_rsa_deduce_primes(char *input_N,
  608. char *input_D,
  609. char *input_E,
  610. char *output_P,
  611. char *output_Q,
  612. int corrupt, int result)
  613. {
  614. mbedtls_mpi N, P, Pp, Q, Qp, D, E;
  615. mbedtls_mpi_init(&N);
  616. mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  617. mbedtls_mpi_init(&Pp); mbedtls_mpi_init(&Qp);
  618. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
  619. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  620. TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
  621. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  622. TEST_ASSERT(mbedtls_test_read_mpi(&Qp, output_P) == 0);
  623. TEST_ASSERT(mbedtls_test_read_mpi(&Pp, output_Q) == 0);
  624. if (corrupt) {
  625. TEST_ASSERT(mbedtls_mpi_add_int(&D, &D, 2) == 0);
  626. }
  627. /* Try to deduce P, Q from N, D, E only. */
  628. TEST_ASSERT(mbedtls_rsa_deduce_primes(&N, &D, &E, &P, &Q) == result);
  629. if (!corrupt) {
  630. /* Check if (P,Q) = (Pp, Qp) or (P,Q) = (Qp, Pp) */
  631. TEST_ASSERT((mbedtls_mpi_cmp_mpi(&P, &Pp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Qp) == 0) ||
  632. (mbedtls_mpi_cmp_mpi(&P, &Qp) == 0 && mbedtls_mpi_cmp_mpi(&Q, &Pp) == 0));
  633. }
  634. exit:
  635. mbedtls_mpi_free(&N);
  636. mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  637. mbedtls_mpi_free(&Pp); mbedtls_mpi_free(&Qp);
  638. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
  639. }
  640. /* END_CASE */
  641. /* BEGIN_CASE */
  642. void mbedtls_rsa_deduce_private_exponent(char *input_P,
  643. char *input_Q,
  644. char *input_E,
  645. char *output_D,
  646. int corrupt, int result)
  647. {
  648. mbedtls_mpi P, Q, D, Dp, E, R, Rp;
  649. mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  650. mbedtls_mpi_init(&D); mbedtls_mpi_init(&Dp);
  651. mbedtls_mpi_init(&E);
  652. mbedtls_mpi_init(&R); mbedtls_mpi_init(&Rp);
  653. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  654. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  655. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  656. TEST_ASSERT(mbedtls_test_read_mpi(&Dp, output_D) == 0);
  657. if (corrupt) {
  658. /* Make E even */
  659. TEST_ASSERT(mbedtls_mpi_set_bit(&E, 0, 0) == 0);
  660. }
  661. /* Try to deduce D from N, P, Q, E. */
  662. TEST_ASSERT(mbedtls_rsa_deduce_private_exponent(&P, &Q,
  663. &E, &D) == result);
  664. if (!corrupt) {
  665. /*
  666. * Check that D and Dp agree modulo LCM(P-1, Q-1).
  667. */
  668. /* Replace P,Q by P-1, Q-1 */
  669. TEST_ASSERT(mbedtls_mpi_sub_int(&P, &P, 1) == 0);
  670. TEST_ASSERT(mbedtls_mpi_sub_int(&Q, &Q, 1) == 0);
  671. /* Check D == Dp modulo P-1 */
  672. TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &P) == 0);
  673. TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &P) == 0);
  674. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
  675. /* Check D == Dp modulo Q-1 */
  676. TEST_ASSERT(mbedtls_mpi_mod_mpi(&R, &D, &Q) == 0);
  677. TEST_ASSERT(mbedtls_mpi_mod_mpi(&Rp, &Dp, &Q) == 0);
  678. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&R, &Rp) == 0);
  679. }
  680. exit:
  681. mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  682. mbedtls_mpi_free(&D); mbedtls_mpi_free(&Dp);
  683. mbedtls_mpi_free(&E);
  684. mbedtls_mpi_free(&R); mbedtls_mpi_free(&Rp);
  685. }
  686. /* END_CASE */
  687. /* BEGIN_CASE */
  688. void mbedtls_rsa_import(char *input_N,
  689. char *input_P,
  690. char *input_Q,
  691. char *input_D,
  692. char *input_E,
  693. int successive,
  694. int is_priv,
  695. int res_check,
  696. int res_complete)
  697. {
  698. mbedtls_mpi N, P, Q, D, E;
  699. mbedtls_rsa_context ctx;
  700. /* Buffers used for encryption-decryption test */
  701. unsigned char *buf_orig = NULL;
  702. unsigned char *buf_enc = NULL;
  703. unsigned char *buf_dec = NULL;
  704. const int have_N = (strlen(input_N) > 0);
  705. const int have_P = (strlen(input_P) > 0);
  706. const int have_Q = (strlen(input_Q) > 0);
  707. const int have_D = (strlen(input_D) > 0);
  708. const int have_E = (strlen(input_E) > 0);
  709. mbedtls_rsa_init(&ctx);
  710. mbedtls_mpi_init(&N);
  711. mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  712. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
  713. if (have_N) {
  714. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  715. }
  716. if (have_P) {
  717. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  718. }
  719. if (have_Q) {
  720. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  721. }
  722. if (have_D) {
  723. TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
  724. }
  725. if (have_E) {
  726. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  727. }
  728. if (!successive) {
  729. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  730. have_N ? &N : NULL,
  731. have_P ? &P : NULL,
  732. have_Q ? &Q : NULL,
  733. have_D ? &D : NULL,
  734. have_E ? &E : NULL) == 0);
  735. } else {
  736. /* Import N, P, Q, D, E separately.
  737. * This should make no functional difference. */
  738. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  739. have_N ? &N : NULL,
  740. NULL, NULL, NULL, NULL) == 0);
  741. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  742. NULL,
  743. have_P ? &P : NULL,
  744. NULL, NULL, NULL) == 0);
  745. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  746. NULL, NULL,
  747. have_Q ? &Q : NULL,
  748. NULL, NULL) == 0);
  749. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  750. NULL, NULL, NULL,
  751. have_D ? &D : NULL,
  752. NULL) == 0);
  753. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  754. NULL, NULL, NULL, NULL,
  755. have_E ? &E : NULL) == 0);
  756. }
  757. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
  758. /* On expected success, perform some public and private
  759. * key operations to check if the key is working properly. */
  760. if (res_complete == 0) {
  761. if (is_priv) {
  762. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
  763. } else {
  764. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
  765. }
  766. if (res_check != 0) {
  767. goto exit;
  768. }
  769. buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
  770. buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
  771. buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
  772. if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
  773. goto exit;
  774. }
  775. /* This test uses an insecure RNG, suitable only for testing.
  776. * In production, always use a cryptographically strong RNG! */
  777. TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
  778. buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
  779. /* Make sure the number we're generating is smaller than the modulus */
  780. buf_orig[0] = 0x00;
  781. TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
  782. if (is_priv) {
  783. /* This test uses an insecure RNG, suitable only for testing.
  784. * In production, always use a cryptographically strong RNG! */
  785. TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
  786. NULL, buf_enc,
  787. buf_dec) == 0);
  788. TEST_ASSERT(memcmp(buf_orig, buf_dec,
  789. mbedtls_rsa_get_len(&ctx)) == 0);
  790. }
  791. }
  792. exit:
  793. mbedtls_free(buf_orig);
  794. mbedtls_free(buf_enc);
  795. mbedtls_free(buf_dec);
  796. mbedtls_rsa_free(&ctx);
  797. mbedtls_mpi_free(&N);
  798. mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  799. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
  800. }
  801. /* END_CASE */
  802. /* BEGIN_CASE */
  803. void mbedtls_rsa_export(char *input_N,
  804. char *input_P,
  805. char *input_Q,
  806. char *input_D,
  807. char *input_E,
  808. int is_priv,
  809. int successive)
  810. {
  811. /* Original MPI's with which we set up the RSA context */
  812. mbedtls_mpi N, P, Q, D, E;
  813. /* Exported MPI's */
  814. mbedtls_mpi Ne, Pe, Qe, De, Ee;
  815. const int have_N = (strlen(input_N) > 0);
  816. const int have_P = (strlen(input_P) > 0);
  817. const int have_Q = (strlen(input_Q) > 0);
  818. const int have_D = (strlen(input_D) > 0);
  819. const int have_E = (strlen(input_E) > 0);
  820. mbedtls_rsa_context ctx;
  821. mbedtls_rsa_init(&ctx);
  822. mbedtls_mpi_init(&N);
  823. mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  824. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
  825. mbedtls_mpi_init(&Ne);
  826. mbedtls_mpi_init(&Pe); mbedtls_mpi_init(&Qe);
  827. mbedtls_mpi_init(&De); mbedtls_mpi_init(&Ee);
  828. /* Setup RSA context */
  829. if (have_N) {
  830. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  831. }
  832. if (have_P) {
  833. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  834. }
  835. if (have_Q) {
  836. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  837. }
  838. if (have_D) {
  839. TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
  840. }
  841. if (have_E) {
  842. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  843. }
  844. TEST_ASSERT(mbedtls_rsa_import(&ctx,
  845. strlen(input_N) ? &N : NULL,
  846. strlen(input_P) ? &P : NULL,
  847. strlen(input_Q) ? &Q : NULL,
  848. strlen(input_D) ? &D : NULL,
  849. strlen(input_E) ? &E : NULL) == 0);
  850. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
  851. /*
  852. * Export parameters and compare to original ones.
  853. */
  854. /* N and E must always be present. */
  855. if (!successive) {
  856. TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, &Ee) == 0);
  857. } else {
  858. TEST_ASSERT(mbedtls_rsa_export(&ctx, &Ne, NULL, NULL, NULL, NULL) == 0);
  859. TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL, NULL, &Ee) == 0);
  860. }
  861. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&N, &Ne) == 0);
  862. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&E, &Ee) == 0);
  863. /* If we were providing enough information to setup a complete private context,
  864. * we expect to be able to export all core parameters. */
  865. if (is_priv) {
  866. if (!successive) {
  867. TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, &Qe,
  868. &De, NULL) == 0);
  869. } else {
  870. TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, &Pe, NULL,
  871. NULL, NULL) == 0);
  872. TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, &Qe,
  873. NULL, NULL) == 0);
  874. TEST_ASSERT(mbedtls_rsa_export(&ctx, NULL, NULL, NULL,
  875. &De, NULL) == 0);
  876. }
  877. if (have_P) {
  878. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&P, &Pe) == 0);
  879. }
  880. if (have_Q) {
  881. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&Q, &Qe) == 0);
  882. }
  883. if (have_D) {
  884. TEST_ASSERT(mbedtls_mpi_cmp_mpi(&D, &De) == 0);
  885. }
  886. /* While at it, perform a sanity check */
  887. TEST_ASSERT(mbedtls_rsa_validate_params(&Ne, &Pe, &Qe, &De, &Ee,
  888. NULL, NULL) == 0);
  889. }
  890. exit:
  891. mbedtls_rsa_free(&ctx);
  892. mbedtls_mpi_free(&N);
  893. mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  894. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
  895. mbedtls_mpi_free(&Ne);
  896. mbedtls_mpi_free(&Pe); mbedtls_mpi_free(&Qe);
  897. mbedtls_mpi_free(&De); mbedtls_mpi_free(&Ee);
  898. }
  899. /* END_CASE */
  900. /* BEGIN_CASE */
  901. void mbedtls_rsa_validate_params(char *input_N,
  902. char *input_P,
  903. char *input_Q,
  904. char *input_D,
  905. char *input_E,
  906. int prng, int result)
  907. {
  908. /* Original MPI's with which we set up the RSA context */
  909. mbedtls_mpi N, P, Q, D, E;
  910. const int have_N = (strlen(input_N) > 0);
  911. const int have_P = (strlen(input_P) > 0);
  912. const int have_Q = (strlen(input_Q) > 0);
  913. const int have_D = (strlen(input_D) > 0);
  914. const int have_E = (strlen(input_E) > 0);
  915. mbedtls_mpi_init(&N);
  916. mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
  917. mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
  918. if (have_N) {
  919. TEST_ASSERT(mbedtls_test_read_mpi(&N, input_N) == 0);
  920. }
  921. if (have_P) {
  922. TEST_ASSERT(mbedtls_test_read_mpi(&P, input_P) == 0);
  923. }
  924. if (have_Q) {
  925. TEST_ASSERT(mbedtls_test_read_mpi(&Q, input_Q) == 0);
  926. }
  927. if (have_D) {
  928. TEST_ASSERT(mbedtls_test_read_mpi(&D, input_D) == 0);
  929. }
  930. if (have_E) {
  931. TEST_ASSERT(mbedtls_test_read_mpi(&E, input_E) == 0);
  932. }
  933. /* This test uses an insecure RNG, suitable only for testing.
  934. * In production, always use a cryptographically strong RNG! */
  935. TEST_ASSERT(mbedtls_rsa_validate_params(have_N ? &N : NULL,
  936. have_P ? &P : NULL,
  937. have_Q ? &Q : NULL,
  938. have_D ? &D : NULL,
  939. have_E ? &E : NULL,
  940. prng ? mbedtls_test_rnd_std_rand : NULL,
  941. prng ? NULL : NULL) == result);
  942. exit:
  943. mbedtls_mpi_free(&N);
  944. mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
  945. mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
  946. }
  947. /* END_CASE */
  948. /* BEGIN_CASE */
  949. void mbedtls_rsa_export_raw(data_t *input_N, data_t *input_P,
  950. data_t *input_Q, data_t *input_D,
  951. data_t *input_E, int is_priv,
  952. int successive)
  953. {
  954. /* Exported buffers */
  955. unsigned char bufNe[256];
  956. unsigned char bufPe[128];
  957. unsigned char bufQe[128];
  958. unsigned char bufDe[256];
  959. unsigned char bufEe[1];
  960. mbedtls_rsa_context ctx;
  961. mbedtls_rsa_init(&ctx);
  962. /* Setup RSA context */
  963. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  964. input_N->len ? input_N->x : NULL, input_N->len,
  965. input_P->len ? input_P->x : NULL, input_P->len,
  966. input_Q->len ? input_Q->x : NULL, input_Q->len,
  967. input_D->len ? input_D->x : NULL, input_D->len,
  968. input_E->len ? input_E->x : NULL, input_E->len) == 0);
  969. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == 0);
  970. /*
  971. * Export parameters and compare to original ones.
  972. */
  973. /* N and E must always be present. */
  974. if (!successive) {
  975. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
  976. NULL, 0, NULL, 0, NULL, 0,
  977. bufEe, input_E->len) == 0);
  978. } else {
  979. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, bufNe, input_N->len,
  980. NULL, 0, NULL, 0, NULL, 0,
  981. NULL, 0) == 0);
  982. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
  983. NULL, 0, NULL, 0, NULL, 0,
  984. bufEe, input_E->len) == 0);
  985. }
  986. TEST_ASSERT(memcmp(input_N->x, bufNe, input_N->len) == 0);
  987. TEST_ASSERT(memcmp(input_E->x, bufEe, input_E->len) == 0);
  988. /* If we were providing enough information to setup a complete private context,
  989. * we expect to be able to export all core parameters. */
  990. if (is_priv) {
  991. if (!successive) {
  992. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
  993. bufPe, input_P->len ? input_P->len : sizeof(bufPe),
  994. bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
  995. bufDe, input_D->len ? input_D->len : sizeof(bufDe),
  996. NULL, 0) == 0);
  997. } else {
  998. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0,
  999. bufPe, input_P->len ? input_P->len : sizeof(bufPe),
  1000. NULL, 0, NULL, 0,
  1001. NULL, 0) == 0);
  1002. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0,
  1003. bufQe, input_Q->len ? input_Q->len : sizeof(bufQe),
  1004. NULL, 0, NULL, 0) == 0);
  1005. TEST_ASSERT(mbedtls_rsa_export_raw(&ctx, NULL, 0, NULL, 0, NULL, 0,
  1006. bufDe, input_D->len ? input_D->len : sizeof(bufDe),
  1007. NULL, 0) == 0);
  1008. }
  1009. if (input_P->len) {
  1010. TEST_ASSERT(memcmp(input_P->x, bufPe, input_P->len) == 0);
  1011. }
  1012. if (input_Q->len) {
  1013. TEST_ASSERT(memcmp(input_Q->x, bufQe, input_Q->len) == 0);
  1014. }
  1015. if (input_D->len) {
  1016. TEST_ASSERT(memcmp(input_D->x, bufDe, input_D->len) == 0);
  1017. }
  1018. }
  1019. exit:
  1020. mbedtls_rsa_free(&ctx);
  1021. }
  1022. /* END_CASE */
  1023. /* BEGIN_CASE */
  1024. void mbedtls_rsa_import_raw(data_t *input_N,
  1025. data_t *input_P, data_t *input_Q,
  1026. data_t *input_D, data_t *input_E,
  1027. int successive,
  1028. int is_priv,
  1029. int res_check,
  1030. int res_complete)
  1031. {
  1032. /* Buffers used for encryption-decryption test */
  1033. unsigned char *buf_orig = NULL;
  1034. unsigned char *buf_enc = NULL;
  1035. unsigned char *buf_dec = NULL;
  1036. mbedtls_rsa_context ctx;
  1037. mbedtls_rsa_init(&ctx);
  1038. if (!successive) {
  1039. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  1040. (input_N->len > 0) ? input_N->x : NULL, input_N->len,
  1041. (input_P->len > 0) ? input_P->x : NULL, input_P->len,
  1042. (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
  1043. (input_D->len > 0) ? input_D->x : NULL, input_D->len,
  1044. (input_E->len > 0) ? input_E->x : NULL,
  1045. input_E->len) == 0);
  1046. } else {
  1047. /* Import N, P, Q, D, E separately.
  1048. * This should make no functional difference. */
  1049. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  1050. (input_N->len > 0) ? input_N->x : NULL, input_N->len,
  1051. NULL, 0, NULL, 0, NULL, 0, NULL, 0) == 0);
  1052. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  1053. NULL, 0,
  1054. (input_P->len > 0) ? input_P->x : NULL, input_P->len,
  1055. NULL, 0, NULL, 0, NULL, 0) == 0);
  1056. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  1057. NULL, 0, NULL, 0,
  1058. (input_Q->len > 0) ? input_Q->x : NULL, input_Q->len,
  1059. NULL, 0, NULL, 0) == 0);
  1060. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  1061. NULL, 0, NULL, 0, NULL, 0,
  1062. (input_D->len > 0) ? input_D->x : NULL, input_D->len,
  1063. NULL, 0) == 0);
  1064. TEST_ASSERT(mbedtls_rsa_import_raw(&ctx,
  1065. NULL, 0, NULL, 0, NULL, 0, NULL, 0,
  1066. (input_E->len > 0) ? input_E->x : NULL,
  1067. input_E->len) == 0);
  1068. }
  1069. TEST_ASSERT(mbedtls_rsa_complete(&ctx) == res_complete);
  1070. /* On expected success, perform some public and private
  1071. * key operations to check if the key is working properly. */
  1072. if (res_complete == 0) {
  1073. if (is_priv) {
  1074. TEST_ASSERT(mbedtls_rsa_check_privkey(&ctx) == res_check);
  1075. } else {
  1076. TEST_ASSERT(mbedtls_rsa_check_pubkey(&ctx) == res_check);
  1077. }
  1078. if (res_check != 0) {
  1079. goto exit;
  1080. }
  1081. buf_orig = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
  1082. buf_enc = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
  1083. buf_dec = mbedtls_calloc(1, mbedtls_rsa_get_len(&ctx));
  1084. if (buf_orig == NULL || buf_enc == NULL || buf_dec == NULL) {
  1085. goto exit;
  1086. }
  1087. /* This test uses an insecure RNG, suitable only for testing.
  1088. * In production, always use a cryptographically strong RNG! */
  1089. TEST_ASSERT(mbedtls_test_rnd_std_rand(NULL,
  1090. buf_orig, mbedtls_rsa_get_len(&ctx)) == 0);
  1091. /* Make sure the number we're generating is smaller than the modulus */
  1092. buf_orig[0] = 0x00;
  1093. TEST_ASSERT(mbedtls_rsa_public(&ctx, buf_orig, buf_enc) == 0);
  1094. if (is_priv) {
  1095. /* This test uses an insecure RNG, suitable only for testing.
  1096. * In production, always use a cryptographically strong RNG! */
  1097. TEST_ASSERT(mbedtls_rsa_private(&ctx, mbedtls_test_rnd_std_rand,
  1098. NULL, buf_enc,
  1099. buf_dec) == 0);
  1100. TEST_ASSERT(memcmp(buf_orig, buf_dec,
  1101. mbedtls_rsa_get_len(&ctx)) == 0);
  1102. }
  1103. }
  1104. exit:
  1105. mbedtls_free(buf_orig);
  1106. mbedtls_free(buf_enc);
  1107. mbedtls_free(buf_dec);
  1108. mbedtls_rsa_free(&ctx);
  1109. }
  1110. /* END_CASE */
  1111. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  1112. void rsa_selftest()
  1113. {
  1114. TEST_ASSERT(mbedtls_rsa_self_test(1) == 0);
  1115. }
  1116. /* END_CASE */