test_suite_pkcs1_v21.function 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/rsa.h"
  3. #include "mbedtls/md.h"
  4. /* END_HEADER */
  5. /* BEGIN_DEPENDENCIES
  6. * depends_on:MBEDTLS_PKCS1_V21:MBEDTLS_RSA_C:MBEDTLS_SHA1_C
  7. * END_DEPENDENCIES
  8. */
  9. /* BEGIN_CASE */
  10. void pkcs1_rsaes_oaep_encrypt( int mod, data_t * input_N, data_t * input_E,
  11. int hash, data_t * message_str, data_t * rnd_buf,
  12. data_t * result_str, int result )
  13. {
  14. unsigned char output[256];
  15. mbedtls_rsa_context ctx;
  16. mbedtls_test_rnd_buf_info info;
  17. mbedtls_mpi N, E;
  18. info.fallback_f_rng = mbedtls_test_rnd_std_rand;
  19. info.fallback_p_rng = NULL;
  20. info.buf = rnd_buf->x;
  21. info.length = rnd_buf->len;
  22. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  23. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
  24. memset( output, 0x00, sizeof( output ) );
  25. TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
  26. TEST_ASSERT( mbedtls_mpi_read_binary( &E, input_E->x, input_E->len ) == 0 );
  27. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  28. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
  29. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  30. if( message_str->len == 0 )
  31. message_str->x = NULL;
  32. TEST_ASSERT( mbedtls_rsa_pkcs1_encrypt( &ctx,
  33. &mbedtls_test_rnd_buffer_rand,
  34. &info, MBEDTLS_RSA_PUBLIC,
  35. message_str->len, message_str->x,
  36. output ) == result );
  37. if( result == 0 )
  38. {
  39. ASSERT_COMPARE( output, ctx.len, result_str->x, result_str->len );
  40. }
  41. exit:
  42. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  43. mbedtls_rsa_free( &ctx );
  44. }
  45. /* END_CASE */
  46. /* BEGIN_CASE */
  47. void pkcs1_rsaes_oaep_decrypt( int mod, data_t * input_P, data_t * input_Q,
  48. data_t * input_N, data_t * input_E, int hash,
  49. data_t * result_str, char * seed, data_t * message_str,
  50. int result )
  51. {
  52. unsigned char output[64];
  53. mbedtls_rsa_context ctx;
  54. size_t output_len;
  55. mbedtls_test_rnd_pseudo_info rnd_info;
  56. mbedtls_mpi N, P, Q, E;
  57. ((void) seed);
  58. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  59. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  60. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
  61. memset( output, 0x00, sizeof( output ) );
  62. memset( &rnd_info, 0, sizeof( mbedtls_test_rnd_pseudo_info ) );
  63. TEST_ASSERT( mbedtls_mpi_read_binary( &P, input_P->x, input_P->len ) == 0 );
  64. TEST_ASSERT( mbedtls_mpi_read_binary( &Q, input_Q->x, input_Q->len ) == 0 );
  65. TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
  66. TEST_ASSERT( mbedtls_mpi_read_binary( &E, input_E->x, input_E->len ) == 0 );
  67. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
  68. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
  69. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  70. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  71. if( result_str->len == 0 )
  72. {
  73. TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx,
  74. &mbedtls_test_rnd_pseudo_rand,
  75. &rnd_info,
  76. MBEDTLS_RSA_PRIVATE,
  77. &output_len, message_str->x,
  78. NULL, 0 ) == result );
  79. }
  80. else
  81. {
  82. TEST_ASSERT( mbedtls_rsa_pkcs1_decrypt( &ctx,
  83. &mbedtls_test_rnd_pseudo_rand,
  84. &rnd_info,
  85. MBEDTLS_RSA_PRIVATE,
  86. &output_len, message_str->x,
  87. output,
  88. sizeof( output ) ) == result );
  89. if( result == 0 )
  90. {
  91. ASSERT_COMPARE( output, output_len, result_str->x, result_str->len );
  92. }
  93. }
  94. exit:
  95. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  96. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  97. mbedtls_rsa_free( &ctx );
  98. }
  99. /* END_CASE */
  100. /* BEGIN_CASE */
  101. void pkcs1_rsassa_pss_sign( int mod, data_t * input_P, data_t * input_Q,
  102. data_t * input_N, data_t * input_E, int digest,
  103. int hash, data_t * message_str, data_t * rnd_buf,
  104. data_t * result_str, int fixed_salt_length,
  105. int result )
  106. {
  107. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  108. unsigned char output[512];
  109. mbedtls_rsa_context ctx;
  110. mbedtls_test_rnd_buf_info info;
  111. mbedtls_mpi N, P, Q, E;
  112. info.fallback_f_rng = mbedtls_test_rnd_std_rand;
  113. info.fallback_p_rng = NULL;
  114. info.buf = rnd_buf->x;
  115. info.length = rnd_buf->len;
  116. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P );
  117. mbedtls_mpi_init( &Q ); mbedtls_mpi_init( &E );
  118. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
  119. memset( hash_result, 0x00, sizeof( hash_result ) );
  120. memset( output, 0x00, sizeof( output ) );
  121. TEST_ASSERT( mbedtls_mpi_read_binary( &P, input_P->x, input_P->len ) == 0 );
  122. TEST_ASSERT( mbedtls_mpi_read_binary( &Q, input_Q->x, input_Q->len ) == 0 );
  123. TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
  124. TEST_ASSERT( mbedtls_mpi_read_binary( &E, input_E->x, input_E->len ) == 0 );
  125. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, &P, &Q, NULL, &E ) == 0 );
  126. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
  127. TEST_ASSERT( mbedtls_rsa_complete( &ctx ) == 0 );
  128. TEST_ASSERT( mbedtls_rsa_check_privkey( &ctx ) == 0 );
  129. if( mbedtls_md_info_from_type( digest ) != NULL )
  130. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
  131. if (fixed_salt_length == MBEDTLS_RSA_SALT_LEN_ANY)
  132. {
  133. TEST_ASSERT( mbedtls_rsa_pkcs1_sign( &ctx, &mbedtls_test_rnd_buffer_rand,
  134. &info, MBEDTLS_RSA_PRIVATE, digest, 0,
  135. hash_result, output ) == result );
  136. if( result == 0 )
  137. {
  138. ASSERT_COMPARE( output, ctx.len, result_str->x, result_str->len );
  139. }
  140. info.buf = rnd_buf->x;
  141. info.length = rnd_buf->len;
  142. }
  143. TEST_ASSERT( mbedtls_rsa_rsassa_pss_sign_ext( &ctx, &mbedtls_test_rnd_buffer_rand,
  144. &info, digest, 0, hash_result,
  145. fixed_salt_length, output ) == result );
  146. if( result == 0 )
  147. {
  148. ASSERT_COMPARE( output, ctx.len, result_str->x, result_str->len );
  149. }
  150. exit:
  151. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P );
  152. mbedtls_mpi_free( &Q ); mbedtls_mpi_free( &E );
  153. mbedtls_rsa_free( &ctx );
  154. }
  155. /* END_CASE */
  156. /* BEGIN_CASE */
  157. void pkcs1_rsassa_pss_verify( int mod, data_t * input_N, data_t * input_E,
  158. int digest, int hash, data_t * message_str,
  159. char * salt, data_t * result_str, int result )
  160. {
  161. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  162. mbedtls_rsa_context ctx;
  163. mbedtls_mpi N, E;
  164. ((void) salt);
  165. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  166. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, hash );
  167. memset( hash_result, 0x00, sizeof( hash_result ) );
  168. TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
  169. TEST_ASSERT( mbedtls_mpi_read_binary( &E, input_E->x, input_E->len ) == 0 );
  170. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  171. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
  172. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  173. if( mbedtls_md_info_from_type( digest ) != NULL )
  174. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( digest ), message_str->x, message_str->len, hash_result ) == 0 );
  175. TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC, digest, 0, hash_result, result_str->x ) == result );
  176. exit:
  177. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  178. mbedtls_rsa_free( &ctx );
  179. }
  180. /* END_CASE */
  181. /* BEGIN_CASE */
  182. void pkcs1_rsassa_pss_verify_ext( int mod, data_t * input_N, data_t * input_E,
  183. int msg_digest_id, int ctx_hash,
  184. int mgf_hash, int salt_len,
  185. data_t * message_str,
  186. data_t * result_str, int result_simple,
  187. int result_full )
  188. {
  189. unsigned char hash_result[MBEDTLS_MD_MAX_SIZE];
  190. mbedtls_rsa_context ctx;
  191. size_t hash_len;
  192. mbedtls_mpi N, E;
  193. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
  194. mbedtls_rsa_init( &ctx, MBEDTLS_RSA_PKCS_V21, ctx_hash );
  195. memset( hash_result, 0x00, sizeof( hash_result ) );
  196. TEST_ASSERT( mbedtls_mpi_read_binary( &N, input_N->x, input_N->len ) == 0 );
  197. TEST_ASSERT( mbedtls_mpi_read_binary( &E, input_E->x, input_E->len ) == 0 );
  198. TEST_ASSERT( mbedtls_rsa_import( &ctx, &N, NULL, NULL, NULL, &E ) == 0 );
  199. TEST_ASSERT( mbedtls_rsa_get_len( &ctx ) == (size_t) ( ( mod + 7 ) / 8 ) );
  200. TEST_ASSERT( mbedtls_rsa_check_pubkey( &ctx ) == 0 );
  201. if( msg_digest_id != MBEDTLS_MD_NONE )
  202. {
  203. TEST_ASSERT( mbedtls_md( mbedtls_md_info_from_type( msg_digest_id ),
  204. message_str->x, message_str->len, hash_result ) == 0 );
  205. hash_len = 0;
  206. }
  207. else
  208. {
  209. memcpy( hash_result, message_str->x, message_str->len );
  210. hash_len = message_str->len;
  211. }
  212. TEST_ASSERT( mbedtls_rsa_pkcs1_verify( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC,
  213. msg_digest_id, hash_len, hash_result,
  214. result_str->x ) == result_simple );
  215. TEST_ASSERT( mbedtls_rsa_rsassa_pss_verify_ext( &ctx, NULL, NULL, MBEDTLS_RSA_PUBLIC,
  216. msg_digest_id, hash_len, hash_result,
  217. mgf_hash, salt_len,
  218. result_str->x ) == result_full );
  219. exit:
  220. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &E );
  221. mbedtls_rsa_free( &ctx );
  222. }
  223. /* END_CASE */