test_suite_ecjpake.function 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/ecjpake.h"
  3. #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) && defined(MBEDTLS_SHA256_C)
  4. static const unsigned char ecjpake_test_x1[] = {
  5. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
  6. 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
  7. 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x21
  8. };
  9. static const unsigned char ecjpake_test_x2[] = {
  10. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
  11. 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  12. 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
  13. };
  14. static const unsigned char ecjpake_test_x3[] = {
  15. 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c,
  16. 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  17. 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x81
  18. };
  19. static const unsigned char ecjpake_test_x4[] = {
  20. 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc,
  21. 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
  22. 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe1
  23. };
  24. static const unsigned char ecjpake_test_X1[] = {
  25. 0x04, 0xac, 0xcf, 0x01, 0x06, 0xef, 0x85, 0x8f, 0xa2, 0xd9, 0x19, 0x33,
  26. 0x13, 0x46, 0x80, 0x5a, 0x78, 0xb5, 0x8b, 0xba, 0xd0, 0xb8, 0x44, 0xe5,
  27. 0xc7, 0x89, 0x28, 0x79, 0x14, 0x61, 0x87, 0xdd, 0x26, 0x66, 0xad, 0xa7,
  28. 0x81, 0xbb, 0x7f, 0x11, 0x13, 0x72, 0x25, 0x1a, 0x89, 0x10, 0x62, 0x1f,
  29. 0x63, 0x4d, 0xf1, 0x28, 0xac, 0x48, 0xe3, 0x81, 0xfd, 0x6e, 0xf9, 0x06,
  30. 0x07, 0x31, 0xf6, 0x94, 0xa4
  31. };
  32. static const unsigned char ecjpake_test_X2[] = {
  33. 0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7,
  34. 0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40,
  35. 0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79,
  36. 0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1,
  37. 0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3,
  38. 0x2b, 0xb0, 0x13, 0xbb, 0x2b
  39. };
  40. static const unsigned char ecjpake_test_X3[] = {
  41. 0x04, 0x7e, 0xa6, 0xe3, 0xa4, 0x48, 0x70, 0x37, 0xa9, 0xe0, 0xdb, 0xd7,
  42. 0x92, 0x62, 0xb2, 0xcc, 0x27, 0x3e, 0x77, 0x99, 0x30, 0xfc, 0x18, 0x40,
  43. 0x9a, 0xc5, 0x36, 0x1c, 0x5f, 0xe6, 0x69, 0xd7, 0x02, 0xe1, 0x47, 0x79,
  44. 0x0a, 0xeb, 0x4c, 0xe7, 0xfd, 0x65, 0x75, 0xab, 0x0f, 0x6c, 0x7f, 0xd1,
  45. 0xc3, 0x35, 0x93, 0x9a, 0xa8, 0x63, 0xba, 0x37, 0xec, 0x91, 0xb7, 0xe3,
  46. 0x2b, 0xb0, 0x13, 0xbb, 0x2b
  47. };
  48. static const unsigned char ecjpake_test_X4[] = {
  49. 0x04, 0x19, 0x0a, 0x07, 0x70, 0x0f, 0xfa, 0x4b, 0xe6, 0xae, 0x1d, 0x79,
  50. 0xee, 0x0f, 0x06, 0xae, 0xb5, 0x44, 0xcd, 0x5a, 0xdd, 0xaa, 0xbe, 0xdf,
  51. 0x70, 0xf8, 0x62, 0x33, 0x21, 0x33, 0x2c, 0x54, 0xf3, 0x55, 0xf0, 0xfb,
  52. 0xfe, 0xc7, 0x83, 0xed, 0x35, 0x9e, 0x5d, 0x0b, 0xf7, 0x37, 0x7a, 0x0f,
  53. 0xc4, 0xea, 0x7a, 0xce, 0x47, 0x3c, 0x9c, 0x11, 0x2b, 0x41, 0xcc, 0xd4,
  54. 0x1a, 0xc5, 0x6a, 0x56, 0x12
  55. };
  56. /* Load my private and public keys, and peer's public keys */
  57. static int ecjpake_test_load( mbedtls_ecjpake_context *ctx,
  58. const unsigned char *xm1, size_t len_xm1,
  59. const unsigned char *xm2, size_t len_xm2,
  60. const unsigned char *Xm1, size_t len_Xm1,
  61. const unsigned char *Xm2, size_t len_Xm2,
  62. const unsigned char *Xp1, size_t len_Xp1,
  63. const unsigned char *Xp2, size_t len_Xp2 )
  64. {
  65. int ret;
  66. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->xm1, xm1, len_xm1 ) );
  67. MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &ctx->xm2, xm2, len_xm2 ) );
  68. MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_binary( &ctx->grp,
  69. &ctx->Xm1, Xm1, len_Xm1 ) );
  70. MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_binary( &ctx->grp,
  71. &ctx->Xm2, Xm2, len_Xm2 ) );
  72. MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_binary( &ctx->grp,
  73. &ctx->Xp1, Xp1, len_Xp1 ) );
  74. MBEDTLS_MPI_CHK( mbedtls_ecp_point_read_binary( &ctx->grp,
  75. &ctx->Xp2, Xp2, len_Xp2 ) );
  76. cleanup:
  77. return( ret );
  78. }
  79. #define ADD_SIZE( x ) x, sizeof( x )
  80. #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED && MBEDTLS_SHA256_C */
  81. /* END_HEADER */
  82. /* BEGIN_DEPENDENCIES
  83. * depends_on:MBEDTLS_ECJPAKE_C
  84. * END_DEPENDENCIES
  85. */
  86. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  87. void ecjpake_invalid_param( )
  88. {
  89. mbedtls_ecjpake_context ctx;
  90. unsigned char buf[42] = { 0 };
  91. size_t olen;
  92. size_t const len = sizeof( buf );
  93. mbedtls_ecjpake_role valid_role = MBEDTLS_ECJPAKE_SERVER;
  94. mbedtls_ecjpake_role invalid_role = (mbedtls_ecjpake_role) 42;
  95. mbedtls_md_type_t valid_md = MBEDTLS_MD_SHA256;
  96. mbedtls_ecp_group_id valid_group = MBEDTLS_ECP_DP_SECP256R1;
  97. TEST_INVALID_PARAM( mbedtls_ecjpake_init( NULL ) );
  98. TEST_VALID_PARAM( mbedtls_ecjpake_free( NULL ) );
  99. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  100. mbedtls_ecjpake_setup( NULL,
  101. valid_role,
  102. valid_md,
  103. valid_group,
  104. buf, len ) );
  105. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  106. mbedtls_ecjpake_setup( &ctx,
  107. invalid_role,
  108. valid_md,
  109. valid_group,
  110. buf, len ) );
  111. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  112. mbedtls_ecjpake_setup( &ctx,
  113. valid_role,
  114. valid_md,
  115. valid_group,
  116. NULL, len ) );
  117. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  118. mbedtls_ecjpake_check( NULL ) );
  119. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  120. mbedtls_ecjpake_write_round_one( NULL, buf, len, &olen,
  121. mbedtls_test_rnd_std_rand, NULL ) );
  122. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  123. mbedtls_ecjpake_write_round_one( &ctx, NULL, len, &olen,
  124. mbedtls_test_rnd_std_rand, NULL ) );
  125. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  126. mbedtls_ecjpake_write_round_one( &ctx, buf, len, NULL,
  127. mbedtls_test_rnd_std_rand, NULL ) );
  128. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  129. mbedtls_ecjpake_write_round_one( &ctx, buf, len, &olen, NULL, NULL ) );
  130. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  131. mbedtls_ecjpake_write_round_two( NULL, buf, len, &olen,
  132. mbedtls_test_rnd_std_rand, NULL ) );
  133. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  134. mbedtls_ecjpake_write_round_two( &ctx, NULL, len, &olen,
  135. mbedtls_test_rnd_std_rand, NULL ) );
  136. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  137. mbedtls_ecjpake_write_round_two( &ctx, buf, len, NULL,
  138. mbedtls_test_rnd_std_rand, NULL ) );
  139. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  140. mbedtls_ecjpake_write_round_two( &ctx, buf, len, &olen, NULL, NULL ) );
  141. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  142. mbedtls_ecjpake_read_round_one( NULL,
  143. buf, len ) );
  144. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  145. mbedtls_ecjpake_read_round_one( &ctx,
  146. NULL, len ) );
  147. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  148. mbedtls_ecjpake_read_round_two( NULL,
  149. buf, len ) );
  150. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  151. mbedtls_ecjpake_read_round_two( &ctx,
  152. NULL, len ) );
  153. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  154. mbedtls_ecjpake_derive_secret( NULL, buf, len, &olen,
  155. mbedtls_test_rnd_std_rand, NULL ) );
  156. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  157. mbedtls_ecjpake_derive_secret( &ctx, NULL, len, &olen,
  158. mbedtls_test_rnd_std_rand, NULL ) );
  159. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  160. mbedtls_ecjpake_derive_secret( &ctx, buf, len, NULL,
  161. mbedtls_test_rnd_std_rand, NULL ) );
  162. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  163. mbedtls_ecjpake_derive_secret( &ctx, buf, len, &olen, NULL, NULL ) );
  164. exit:
  165. return;
  166. }
  167. /* END_CASE */
  168. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  169. void ecjpake_selftest( )
  170. {
  171. TEST_ASSERT( mbedtls_ecjpake_self_test( 1 ) == 0 );
  172. }
  173. /* END_CASE */
  174. /* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C */
  175. void read_bad_md( data_t *msg )
  176. {
  177. mbedtls_ecjpake_context corrupt_ctx;
  178. const unsigned char * pw = NULL;
  179. const size_t pw_len = 0;
  180. int any_role = MBEDTLS_ECJPAKE_CLIENT;
  181. mbedtls_ecjpake_init( &corrupt_ctx );
  182. TEST_ASSERT( mbedtls_ecjpake_setup( &corrupt_ctx, any_role,
  183. MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw, pw_len ) == 0 );
  184. corrupt_ctx.md_info = NULL;
  185. TEST_ASSERT( mbedtls_ecjpake_read_round_one( &corrupt_ctx, msg->x,
  186. msg->len ) == MBEDTLS_ERR_MD_BAD_INPUT_DATA );
  187. exit:
  188. mbedtls_ecjpake_free( &corrupt_ctx );
  189. }
  190. /* END_CASE */
  191. /* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C */
  192. void read_round_one( int role, data_t * msg, int ref_ret )
  193. {
  194. mbedtls_ecjpake_context ctx;
  195. const unsigned char * pw = NULL;
  196. const size_t pw_len = 0;
  197. mbedtls_ecjpake_init( &ctx );
  198. TEST_ASSERT( mbedtls_ecjpake_setup( &ctx, role,
  199. MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw, pw_len ) == 0 );
  200. TEST_ASSERT( mbedtls_ecjpake_read_round_one( &ctx, msg->x, msg->len ) == ref_ret );
  201. exit:
  202. mbedtls_ecjpake_free( &ctx );
  203. }
  204. /* END_CASE */
  205. /* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C */
  206. void read_round_two_cli( data_t * msg, int ref_ret )
  207. {
  208. mbedtls_ecjpake_context ctx;
  209. const unsigned char * pw = NULL;
  210. const size_t pw_len = 0;
  211. mbedtls_ecjpake_init( &ctx );
  212. TEST_ASSERT( mbedtls_ecjpake_setup( &ctx, MBEDTLS_ECJPAKE_CLIENT,
  213. MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw, pw_len ) == 0 );
  214. TEST_ASSERT( ecjpake_test_load( &ctx,
  215. ADD_SIZE( ecjpake_test_x1 ), ADD_SIZE( ecjpake_test_x2 ),
  216. ADD_SIZE( ecjpake_test_X1 ), ADD_SIZE( ecjpake_test_X2 ),
  217. ADD_SIZE( ecjpake_test_X3 ), ADD_SIZE( ecjpake_test_X4 ) )
  218. == 0 );
  219. TEST_ASSERT( mbedtls_ecjpake_read_round_two( &ctx, msg->x, msg->len ) == ref_ret );
  220. exit:
  221. mbedtls_ecjpake_free( &ctx );
  222. }
  223. /* END_CASE */
  224. /* BEGIN_CASE depends_on:MBEDTLS_ECP_DP_SECP256R1_ENABLED:MBEDTLS_SHA256_C */
  225. void read_round_two_srv( data_t * msg, int ref_ret )
  226. {
  227. mbedtls_ecjpake_context ctx;
  228. const unsigned char * pw = NULL;
  229. const size_t pw_len = 0;
  230. mbedtls_ecjpake_init( &ctx );
  231. TEST_ASSERT( mbedtls_ecjpake_setup( &ctx, MBEDTLS_ECJPAKE_SERVER,
  232. MBEDTLS_MD_SHA256, MBEDTLS_ECP_DP_SECP256R1, pw, pw_len ) == 0 );
  233. TEST_ASSERT( ecjpake_test_load( &ctx,
  234. ADD_SIZE( ecjpake_test_x3 ), ADD_SIZE( ecjpake_test_x4 ),
  235. ADD_SIZE( ecjpake_test_X3 ), ADD_SIZE( ecjpake_test_X4 ),
  236. ADD_SIZE( ecjpake_test_X1 ), ADD_SIZE( ecjpake_test_X2 ) )
  237. == 0 );
  238. TEST_ASSERT( mbedtls_ecjpake_read_round_two( &ctx, msg->x, msg->len ) == ref_ret );
  239. exit:
  240. mbedtls_ecjpake_free( &ctx );
  241. }
  242. /* END_CASE */