test_suite_rsa.function 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. BEGIN_HEADER
  2. #include <polarssl/rsa.h>
  3. #include <polarssl/md2.h>
  4. #include <polarssl/md4.h>
  5. #include <polarssl/md5.h>
  6. #include <polarssl/sha1.h>
  7. #include <polarssl/sha2.h>
  8. #include <polarssl/sha4.h>
  9. #include <polarssl/havege.h>
  10. END_HEADER
  11. BEGIN_DEPENDENCIES
  12. depends_on:POLARSSL_RSA_C:POLARSSL_BIGNUM_C:POLARSSL_GENPRIME
  13. END_DEPENDENCIES
  14. BEGIN_CASE
  15. rsa_pkcs1_sign:message_hex_string:padding_mode:digest:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str:result
  16. {
  17. unsigned char message_str[1000];
  18. unsigned char hash_result[1000];
  19. unsigned char output[1000];
  20. unsigned char output_str[1000];
  21. rsa_context ctx;
  22. mpi P1, Q1, H, G;
  23. int msg_len;
  24. mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
  25. rsa_init( &ctx, {padding_mode}, 0 );
  26. memset( message_str, 0x00, 1000 );
  27. memset( hash_result, 0x00, 1000 );
  28. memset( output, 0x00, 1000 );
  29. memset( output_str, 0x00, 1000 );
  30. ctx.len = {mod} / 8;
  31. TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
  32. TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
  33. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  34. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  35. TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
  36. TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
  37. TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
  38. TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
  39. TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
  40. TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
  41. TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
  42. TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
  43. TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
  44. msg_len = unhexify( message_str, {message_hex_string} );
  45. switch( {digest} )
  46. {
  47. #ifdef POLARSSL_MD2_C
  48. case SIG_RSA_MD2:
  49. md2( message_str, msg_len, hash_result );
  50. break;
  51. #endif
  52. #ifdef POLARSSL_MD4_C
  53. case SIG_RSA_MD4:
  54. md4( message_str, msg_len, hash_result );
  55. break;
  56. #endif
  57. #ifdef POLARSSL_MD5_C
  58. case SIG_RSA_MD5:
  59. md5( message_str, msg_len, hash_result );
  60. break;
  61. #endif
  62. #ifdef POLARSSL_SHA1_C
  63. case SIG_RSA_SHA1:
  64. sha1( message_str, msg_len, hash_result );
  65. break;
  66. #endif
  67. #ifdef POLARSSL_SHA2_C
  68. case SIG_RSA_SHA224:
  69. sha2( message_str, msg_len, hash_result, 1 );
  70. break;
  71. case SIG_RSA_SHA256:
  72. sha2( message_str, msg_len, hash_result, 0 );
  73. break;
  74. #endif
  75. #ifdef POLARSSL_SHA4_C
  76. case SIG_RSA_SHA384:
  77. sha4( message_str, msg_len, hash_result, 1 );
  78. break;
  79. case SIG_RSA_SHA512:
  80. sha4( message_str, msg_len, hash_result, 0 );
  81. break;
  82. #endif
  83. }
  84. TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, {digest}, 0, hash_result, output ) == {result} );
  85. if( {result} == 0 )
  86. {
  87. hexify( output_str, output, ctx.len );
  88. TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
  89. }
  90. mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
  91. }
  92. END_CASE
  93. BEGIN_CASE
  94. rsa_pkcs1_verify:message_hex_string:padding_mode:digest:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
  95. {
  96. unsigned char message_str[1000];
  97. unsigned char hash_result[1000];
  98. unsigned char result_str[1000];
  99. rsa_context ctx;
  100. int msg_len;
  101. rsa_init( &ctx, {padding_mode}, 0 );
  102. memset( message_str, 0x00, 1000 );
  103. memset( hash_result, 0x00, 1000 );
  104. memset( result_str, 0x00, 1000 );
  105. ctx.len = {mod} / 8;
  106. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  107. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  108. TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
  109. msg_len = unhexify( message_str, {message_hex_string} );
  110. unhexify( result_str, {result_hex_str} );
  111. switch( {digest} )
  112. {
  113. #ifdef POLARSSL_MD2_C
  114. case SIG_RSA_MD2:
  115. md2( message_str, msg_len, hash_result );
  116. break;
  117. #endif
  118. #ifdef POLARSSL_MD4_C
  119. case SIG_RSA_MD4:
  120. md4( message_str, msg_len, hash_result );
  121. break;
  122. #endif
  123. #ifdef POLARSSL_MD5_C
  124. case SIG_RSA_MD5:
  125. md5( message_str, msg_len, hash_result );
  126. break;
  127. #endif
  128. #ifdef POLARSSL_SHA1_C
  129. case SIG_RSA_SHA1:
  130. sha1( message_str, msg_len, hash_result );
  131. break;
  132. #endif
  133. #ifdef POLARSSL_SHA2_C
  134. case SIG_RSA_SHA224:
  135. sha2( message_str, msg_len, hash_result, 1 );
  136. break;
  137. case SIG_RSA_SHA256:
  138. sha2( message_str, msg_len, hash_result, 0 );
  139. break;
  140. #endif
  141. #ifdef POLARSSL_SHA4_C
  142. case SIG_RSA_SHA384:
  143. sha4( message_str, msg_len, hash_result, 1 );
  144. break;
  145. case SIG_RSA_SHA512:
  146. sha4( message_str, msg_len, hash_result, 0 );
  147. break;
  148. #endif
  149. }
  150. TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, {digest}, 0, hash_result, result_str ) == {result} );
  151. }
  152. END_CASE
  153. BEGIN_CASE
  154. rsa_pkcs1_sign_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str
  155. {
  156. unsigned char message_str[1000];
  157. unsigned char hash_result[1000];
  158. unsigned char output[1000];
  159. unsigned char output_str[1000];
  160. rsa_context ctx;
  161. mpi P1, Q1, H, G;
  162. int hash_len;
  163. mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
  164. rsa_init( &ctx, {padding_mode}, 0 );
  165. memset( message_str, 0x00, 1000 );
  166. memset( hash_result, 0x00, 1000 );
  167. memset( output, 0x00, 1000 );
  168. memset( output_str, 0x00, 1000 );
  169. ctx.len = {mod} / 8;
  170. TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
  171. TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
  172. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  173. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  174. TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
  175. TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
  176. TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
  177. TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
  178. TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
  179. TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
  180. TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
  181. TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
  182. TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
  183. unhexify( message_str, {message_hex_string} );
  184. hash_len = unhexify( hash_result, {hash_result_string} );
  185. TEST_ASSERT( rsa_pkcs1_sign( &ctx, NULL, NULL, RSA_PRIVATE, SIG_RSA_RAW, hash_len, hash_result, output ) == 0 );
  186. hexify( output_str, output, ctx.len );
  187. TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
  188. mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
  189. }
  190. END_CASE
  191. BEGIN_CASE
  192. rsa_pkcs1_verify_raw:message_hex_string:hash_result_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:correct
  193. {
  194. unsigned char message_str[1000];
  195. unsigned char hash_result[1000];
  196. unsigned char result_str[1000];
  197. rsa_context ctx;
  198. size_t hash_len;
  199. rsa_init( &ctx, {padding_mode}, 0 );
  200. memset( message_str, 0x00, 1000 );
  201. memset( hash_result, 0x00, 1000 );
  202. memset( result_str, 0x00, 1000 );
  203. ctx.len = {mod} / 8;
  204. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  205. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  206. TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
  207. unhexify( message_str, {message_hex_string} );
  208. hash_len = unhexify( hash_result, {hash_result_string} );
  209. unhexify( result_str, {result_hex_str} );
  210. TEST_ASSERT( rsa_pkcs1_verify( &ctx, RSA_PUBLIC, SIG_RSA_RAW, hash_len, hash_result, result_str ) == {correct} );
  211. }
  212. END_CASE
  213. BEGIN_CASE
  214. rsa_pkcs1_encrypt:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
  215. {
  216. unsigned char message_str[1000];
  217. unsigned char output[1000];
  218. unsigned char output_str[1000];
  219. rsa_context ctx;
  220. size_t msg_len;
  221. rnd_pseudo_info rnd_info;
  222. memset( &rnd_info, 0, sizeof( rnd_pseudo_info ) );
  223. rsa_init( &ctx, {padding_mode}, 0 );
  224. memset( message_str, 0x00, 1000 );
  225. memset( output, 0x00, 1000 );
  226. memset( output_str, 0x00, 1000 );
  227. ctx.len = {mod} / 8;
  228. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  229. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  230. TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
  231. msg_len = unhexify( message_str, {message_hex_string} );
  232. TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_pseudo_rand, &rnd_info, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
  233. if( {result} == 0 )
  234. {
  235. hexify( output_str, output, ctx.len );
  236. TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
  237. }
  238. }
  239. END_CASE
  240. BEGIN_CASE
  241. rsa_pkcs1_encrypt_bad_rng:message_hex_string:padding_mode:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
  242. {
  243. unsigned char message_str[1000];
  244. unsigned char output[1000];
  245. unsigned char output_str[1000];
  246. rsa_context ctx;
  247. size_t msg_len;
  248. rsa_init( &ctx, {padding_mode}, 0 );
  249. memset( message_str, 0x00, 1000 );
  250. memset( output, 0x00, 1000 );
  251. memset( output_str, 0x00, 1000 );
  252. ctx.len = {mod} / 8;
  253. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  254. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  255. TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
  256. msg_len = unhexify( message_str, {message_hex_string} );
  257. TEST_ASSERT( rsa_pkcs1_encrypt( &ctx, &rnd_zero_rand, NULL, RSA_PUBLIC, msg_len, message_str, output ) == {result} );
  258. if( {result} == 0 )
  259. {
  260. hexify( output_str, output, ctx.len );
  261. TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
  262. }
  263. }
  264. END_CASE
  265. BEGIN_CASE
  266. rsa_pkcs1_decrypt:message_hex_string:padding_mode:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:max_output:result_hex_str:result
  267. {
  268. unsigned char message_str[1000];
  269. unsigned char output[1000];
  270. unsigned char output_str[1000];
  271. rsa_context ctx;
  272. mpi P1, Q1, H, G;
  273. size_t output_len;
  274. mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
  275. rsa_init( &ctx, {padding_mode}, 0 );
  276. memset( message_str, 0x00, 1000 );
  277. memset( output, 0x00, 1000 );
  278. memset( output_str, 0x00, 1000 );
  279. ctx.len = {mod} / 8;
  280. TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
  281. TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
  282. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  283. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  284. TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
  285. TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
  286. TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
  287. TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
  288. TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
  289. TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
  290. TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
  291. TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
  292. TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
  293. unhexify( message_str, {message_hex_string} );
  294. output_len = 0;
  295. TEST_ASSERT( rsa_pkcs1_decrypt( &ctx, RSA_PRIVATE, &output_len, message_str, output, {max_output} ) == {result} );
  296. if( {result} == 0 )
  297. {
  298. hexify( output_str, output, ctx.len );
  299. TEST_ASSERT( strncasecmp( (char *) output_str, {result_hex_str}, strlen( {result_hex_str} ) ) == 0 );
  300. }
  301. mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
  302. }
  303. END_CASE
  304. BEGIN_CASE
  305. rsa_public:message_hex_string:mod:radix_N:input_N:radix_E:input_E:result_hex_str:result
  306. {
  307. unsigned char message_str[1000];
  308. unsigned char output[1000];
  309. unsigned char output_str[1000];
  310. rsa_context ctx;
  311. rsa_init( &ctx, RSA_PKCS_V15, 0 );
  312. memset( message_str, 0x00, 1000 );
  313. memset( output, 0x00, 1000 );
  314. memset( output_str, 0x00, 1000 );
  315. ctx.len = {mod} / 8;
  316. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  317. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  318. TEST_ASSERT( rsa_check_pubkey( &ctx ) == 0 );
  319. unhexify( message_str, {message_hex_string} );
  320. TEST_ASSERT( rsa_public( &ctx, message_str, output ) == {result} );
  321. if( {result} == 0 )
  322. {
  323. hexify( output_str, output, ctx.len );
  324. TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
  325. }
  326. }
  327. END_CASE
  328. BEGIN_CASE
  329. rsa_private:message_hex_string:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:result_hex_str:result
  330. {
  331. unsigned char message_str[1000];
  332. unsigned char output[1000];
  333. unsigned char output_str[1000];
  334. rsa_context ctx;
  335. mpi P1, Q1, H, G;
  336. mpi_init( &P1 ); mpi_init( &Q1 ); mpi_init( &H ); mpi_init( &G );
  337. rsa_init( &ctx, RSA_PKCS_V15, 0 );
  338. memset( message_str, 0x00, 1000 );
  339. memset( output, 0x00, 1000 );
  340. memset( output_str, 0x00, 1000 );
  341. ctx.len = {mod} / 8;
  342. TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
  343. TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
  344. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  345. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  346. TEST_ASSERT( mpi_sub_int( &P1, &ctx.P, 1 ) == 0 );
  347. TEST_ASSERT( mpi_sub_int( &Q1, &ctx.Q, 1 ) == 0 );
  348. TEST_ASSERT( mpi_mul_mpi( &H, &P1, &Q1 ) == 0 );
  349. TEST_ASSERT( mpi_gcd( &G, &ctx.E, &H ) == 0 );
  350. TEST_ASSERT( mpi_inv_mod( &ctx.D , &ctx.E, &H ) == 0 );
  351. TEST_ASSERT( mpi_mod_mpi( &ctx.DP, &ctx.D, &P1 ) == 0 );
  352. TEST_ASSERT( mpi_mod_mpi( &ctx.DQ, &ctx.D, &Q1 ) == 0 );
  353. TEST_ASSERT( mpi_inv_mod( &ctx.QP, &ctx.Q, &ctx.P ) == 0 );
  354. TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
  355. unhexify( message_str, {message_hex_string} );
  356. TEST_ASSERT( rsa_private( &ctx, message_str, output ) == {result} );
  357. if( {result} == 0 )
  358. {
  359. hexify( output_str, output, ctx.len );
  360. TEST_ASSERT( strcasecmp( (char *) output_str, {result_hex_str} ) == 0 );
  361. }
  362. mpi_free( &P1 ); mpi_free( &Q1 ); mpi_free( &H ); mpi_free( &G );
  363. }
  364. END_CASE
  365. BEGIN_CASE
  366. rsa_check_privkey_null:
  367. {
  368. rsa_context ctx;
  369. memset( &ctx, 0x00, sizeof( rsa_context ) );
  370. TEST_ASSERT( rsa_check_privkey( &ctx ) == POLARSSL_ERR_RSA_KEY_CHECK_FAILED );
  371. }
  372. END_CASE
  373. BEGIN_CASE
  374. rsa_check_pubkey:radix_N:input_N:radix_E:input_E:result
  375. {
  376. rsa_context ctx;
  377. rsa_init( &ctx, RSA_PKCS_V15, 0 );
  378. if( strlen( {input_N} ) )
  379. {
  380. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  381. }
  382. if( strlen( {input_E} ) )
  383. {
  384. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  385. }
  386. TEST_ASSERT( rsa_check_pubkey( &ctx ) == {result} );
  387. }
  388. END_CASE
  389. BEGIN_CASE
  390. rsa_check_privkey:mod:radix_P:input_P:radix_Q:input_Q:radix_N:input_N:radix_E:input_E:radix_D:input_D:result
  391. {
  392. rsa_context ctx;
  393. rsa_init( &ctx, RSA_PKCS_V15, 0 );
  394. ctx.len = {mod} / 8;
  395. if( strlen( {input_P} ) )
  396. {
  397. TEST_ASSERT( mpi_read_string( &ctx.P, {radix_P}, {input_P} ) == 0 );
  398. }
  399. if( strlen( {input_Q} ) )
  400. {
  401. TEST_ASSERT( mpi_read_string( &ctx.Q, {radix_Q}, {input_Q} ) == 0 );
  402. }
  403. if( strlen( {input_N} ) )
  404. {
  405. TEST_ASSERT( mpi_read_string( &ctx.N, {radix_N}, {input_N} ) == 0 );
  406. }
  407. if( strlen( {input_E} ) )
  408. {
  409. TEST_ASSERT( mpi_read_string( &ctx.E, {radix_E}, {input_E} ) == 0 );
  410. }
  411. if( strlen( {input_D} ) )
  412. {
  413. TEST_ASSERT( mpi_read_string( &ctx.D, {radix_D}, {input_D} ) == 0 );
  414. }
  415. TEST_ASSERT( rsa_check_privkey( &ctx ) == {result} );
  416. }
  417. END_CASE
  418. BEGIN_CASE
  419. rsa_gen_key:nrbits:exponent:result
  420. {
  421. rsa_context ctx;
  422. havege_state hs;
  423. havege_init( &hs );
  424. rsa_init( &ctx, 0, 0 );
  425. TEST_ASSERT( rsa_gen_key( &ctx, havege_rand, &hs, {nrbits}, {exponent} ) == {result} );
  426. if( {result} == 0 )
  427. {
  428. TEST_ASSERT( rsa_check_privkey( &ctx ) == 0 );
  429. }
  430. }
  431. END_CASE
  432. BEGIN_CASE
  433. rsa_selftest:
  434. {
  435. TEST_ASSERT( rsa_self_test( 0 ) == 0 );
  436. }
  437. END_CASE