test_suite_dhm.function 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/dhm.h"
  3. /* Sanity checks on a Diffie-Hellman parameter: check the length-value
  4. * syntax and check that the value is the expected one (taken from the
  5. * DHM context by the caller). */
  6. static int check_dhm_param_output( const mbedtls_mpi *expected,
  7. const unsigned char *buffer,
  8. size_t size,
  9. size_t *offset )
  10. {
  11. size_t n;
  12. mbedtls_mpi actual;
  13. int ok = 0;
  14. mbedtls_mpi_init( &actual );
  15. ++mbedtls_test_info.step;
  16. TEST_ASSERT( size >= *offset + 2 );
  17. n = ( buffer[*offset] << 8 ) | buffer[*offset + 1];
  18. *offset += 2;
  19. /* The DHM param output from Mbed TLS has leading zeros stripped, as
  20. * permitted but not required by RFC 5246 \S4.4. */
  21. TEST_EQUAL( n, mbedtls_mpi_size( expected ) );
  22. TEST_ASSERT( size >= *offset + n );
  23. TEST_EQUAL( 0, mbedtls_mpi_read_binary( &actual, buffer + *offset, n ) );
  24. TEST_EQUAL( 0, mbedtls_mpi_cmp_mpi( expected, &actual ) );
  25. *offset += n;
  26. ok = 1;
  27. exit:
  28. mbedtls_mpi_free( &actual );
  29. return( ok );
  30. }
  31. /* Sanity checks on Diffie-Hellman parameters: syntax, range, and comparison
  32. * against the context. */
  33. static int check_dhm_params( const mbedtls_dhm_context *ctx,
  34. size_t x_size,
  35. const unsigned char *ske, size_t ske_len )
  36. {
  37. size_t offset = 0;
  38. /* Check that ctx->X and ctx->GX are within range. */
  39. TEST_ASSERT( mbedtls_mpi_cmp_int( &ctx->X, 1 ) > 0 );
  40. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) < 0 );
  41. TEST_ASSERT( mbedtls_mpi_size( &ctx->X ) <= x_size );
  42. TEST_ASSERT( mbedtls_mpi_cmp_int( &ctx->GX, 1 ) > 0 );
  43. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx->GX, &ctx->P ) < 0 );
  44. /* Check ske: it must contain P, G and G^X, each prefixed with a
  45. * 2-byte size. */
  46. if( !check_dhm_param_output( &ctx->P, ske, ske_len, &offset ) )
  47. goto exit;
  48. if( !check_dhm_param_output( &ctx->G, ske, ske_len, &offset ) )
  49. goto exit;
  50. if( !check_dhm_param_output( &ctx->GX, ske, ske_len, &offset ) )
  51. goto exit;
  52. TEST_EQUAL( offset, ske_len );
  53. return( 1 );
  54. exit:
  55. return( 0 );
  56. }
  57. /* END_HEADER */
  58. /* BEGIN_DEPENDENCIES
  59. * depends_on:MBEDTLS_DHM_C:MBEDTLS_BIGNUM_C
  60. * END_DEPENDENCIES
  61. */
  62. /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
  63. void dhm_invalid_params( )
  64. {
  65. mbedtls_dhm_context ctx;
  66. unsigned char buf[42] = { 0 };
  67. unsigned char *buf_null = NULL;
  68. mbedtls_mpi X;
  69. size_t const buflen = sizeof( buf );
  70. size_t len;
  71. TEST_INVALID_PARAM( mbedtls_dhm_init( NULL ) );
  72. TEST_VALID_PARAM( mbedtls_dhm_free( NULL ) );
  73. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  74. mbedtls_dhm_read_params( NULL,
  75. (unsigned char**) &buf,
  76. buf ) );
  77. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  78. mbedtls_dhm_read_params( &ctx, &buf_null, buf ) );
  79. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  80. mbedtls_dhm_read_params( &ctx, NULL, buf ) );
  81. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  82. mbedtls_dhm_read_params( &ctx,
  83. (unsigned char**) &buf,
  84. NULL ) );
  85. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  86. mbedtls_dhm_make_params( NULL, buflen,
  87. buf, &len,
  88. mbedtls_test_rnd_std_rand,
  89. NULL ) );
  90. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  91. mbedtls_dhm_make_params( &ctx, buflen,
  92. NULL, &len,
  93. mbedtls_test_rnd_std_rand,
  94. NULL ) );
  95. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  96. mbedtls_dhm_make_params( &ctx, buflen,
  97. buf, NULL,
  98. mbedtls_test_rnd_std_rand,
  99. NULL ) );
  100. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  101. mbedtls_dhm_make_params( &ctx, buflen,
  102. buf, &len,
  103. NULL,
  104. NULL ) );
  105. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  106. mbedtls_dhm_set_group( NULL, &X, &X ) );
  107. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  108. mbedtls_dhm_set_group( &ctx, NULL, &X ) );
  109. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  110. mbedtls_dhm_set_group( &ctx, &X, NULL ) );
  111. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  112. mbedtls_dhm_read_public( NULL, buf, buflen ) );
  113. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  114. mbedtls_dhm_read_public( &ctx, NULL, buflen ) );
  115. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  116. mbedtls_dhm_make_public( NULL, buflen,
  117. buf, buflen,
  118. mbedtls_test_rnd_std_rand,
  119. NULL ) );
  120. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  121. mbedtls_dhm_make_public( &ctx, buflen,
  122. NULL, buflen,
  123. mbedtls_test_rnd_std_rand,
  124. NULL ) );
  125. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  126. mbedtls_dhm_make_public( &ctx, buflen,
  127. buf, buflen,
  128. NULL,
  129. NULL ) );
  130. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  131. mbedtls_dhm_calc_secret( NULL, buf, buflen, &len,
  132. mbedtls_test_rnd_std_rand,
  133. NULL ) );
  134. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  135. mbedtls_dhm_calc_secret( &ctx, NULL, buflen, &len,
  136. mbedtls_test_rnd_std_rand,
  137. NULL ) );
  138. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  139. mbedtls_dhm_calc_secret( &ctx, buf, buflen, NULL,
  140. mbedtls_test_rnd_std_rand,
  141. NULL ) );
  142. #if defined(MBEDTLS_ASN1_PARSE_C)
  143. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  144. mbedtls_dhm_parse_dhm( NULL, buf, buflen ) );
  145. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  146. mbedtls_dhm_parse_dhm( &ctx, NULL, buflen ) );
  147. #if defined(MBEDTLS_FS_IO)
  148. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  149. mbedtls_dhm_parse_dhmfile( NULL, "" ) );
  150. TEST_INVALID_PARAM_RET( MBEDTLS_ERR_DHM_BAD_INPUT_DATA,
  151. mbedtls_dhm_parse_dhmfile( &ctx, NULL ) );
  152. #endif /* MBEDTLS_FS_IO */
  153. #endif /* MBEDTLS_ASN1_PARSE_C */
  154. exit:
  155. return;
  156. }
  157. /* END_CASE */
  158. /* BEGIN_CASE */
  159. void dhm_do_dhm( int radix_P, char *input_P, int x_size,
  160. int radix_G, char *input_G, int result )
  161. {
  162. mbedtls_dhm_context ctx_srv;
  163. mbedtls_dhm_context ctx_cli;
  164. unsigned char ske[1000];
  165. unsigned char *p = ske;
  166. unsigned char pub_cli[1000];
  167. unsigned char sec_srv[1000];
  168. unsigned char sec_cli[1000];
  169. size_t ske_len = 0;
  170. size_t pub_cli_len = 0;
  171. size_t sec_srv_len;
  172. size_t sec_cli_len;
  173. int i;
  174. mbedtls_test_rnd_pseudo_info rnd_info;
  175. mbedtls_dhm_init( &ctx_srv );
  176. mbedtls_dhm_init( &ctx_cli );
  177. memset( ske, 0x00, 1000 );
  178. memset( pub_cli, 0x00, 1000 );
  179. memset( sec_srv, 0x00, 1000 );
  180. memset( sec_cli, 0x00, 1000 );
  181. memset( &rnd_info, 0x00, sizeof( mbedtls_test_rnd_pseudo_info ) );
  182. /*
  183. * Set params
  184. */
  185. TEST_ASSERT( mbedtls_test_read_mpi( &ctx_srv.P, radix_P, input_P ) == 0 );
  186. TEST_ASSERT( mbedtls_test_read_mpi( &ctx_srv.G, radix_G, input_G ) == 0 );
  187. pub_cli_len = mbedtls_mpi_size( &ctx_srv.P );
  188. /*
  189. * First key exchange
  190. */
  191. mbedtls_test_set_step( 10 );
  192. TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len,
  193. &mbedtls_test_rnd_pseudo_rand,
  194. &rnd_info ) == result );
  195. if ( result != 0 )
  196. goto exit;
  197. if( !check_dhm_params( &ctx_srv, x_size, ske, ske_len ) )
  198. goto exit;
  199. ske[ske_len++] = 0;
  200. ske[ske_len++] = 0;
  201. TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
  202. TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len,
  203. &mbedtls_test_rnd_pseudo_rand,
  204. &rnd_info ) == 0 );
  205. TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
  206. TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ),
  207. &sec_srv_len,
  208. &mbedtls_test_rnd_pseudo_rand,
  209. &rnd_info ) == 0 );
  210. TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, sizeof( sec_cli ), &sec_cli_len, NULL, NULL ) == 0 );
  211. TEST_ASSERT( sec_srv_len == sec_cli_len );
  212. TEST_ASSERT( sec_srv_len != 0 );
  213. TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
  214. /* Re-do calc_secret on server a few times to test update of blinding values */
  215. for( i = 0; i < 3; i++ )
  216. {
  217. mbedtls_test_set_step( 20 + i );
  218. sec_srv_len = 1000;
  219. TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv,
  220. sizeof( sec_srv ), &sec_srv_len,
  221. &mbedtls_test_rnd_pseudo_rand,
  222. &rnd_info ) == 0 );
  223. TEST_ASSERT( sec_srv_len == sec_cli_len );
  224. TEST_ASSERT( sec_srv_len != 0 );
  225. TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
  226. }
  227. /*
  228. * Second key exchange to test change of blinding values on server
  229. */
  230. p = ske;
  231. mbedtls_test_set_step( 30 );
  232. TEST_ASSERT( mbedtls_dhm_make_params( &ctx_srv, x_size, ske, &ske_len,
  233. &mbedtls_test_rnd_pseudo_rand,
  234. &rnd_info ) == 0 );
  235. if( !check_dhm_params( &ctx_srv, x_size, ske, ske_len ) )
  236. goto exit;
  237. ske[ske_len++] = 0;
  238. ske[ske_len++] = 0;
  239. TEST_ASSERT( mbedtls_dhm_read_params( &ctx_cli, &p, ske + ske_len ) == 0 );
  240. TEST_ASSERT( mbedtls_dhm_make_public( &ctx_cli, x_size, pub_cli, pub_cli_len,
  241. &mbedtls_test_rnd_pseudo_rand,
  242. &rnd_info ) == 0 );
  243. TEST_ASSERT( mbedtls_dhm_read_public( &ctx_srv, pub_cli, pub_cli_len ) == 0 );
  244. TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_srv, sec_srv, sizeof( sec_srv ),
  245. &sec_srv_len,
  246. &mbedtls_test_rnd_pseudo_rand,
  247. &rnd_info ) == 0 );
  248. TEST_ASSERT( mbedtls_dhm_calc_secret( &ctx_cli, sec_cli, sizeof( sec_cli ), &sec_cli_len, NULL, NULL ) == 0 );
  249. TEST_ASSERT( sec_srv_len == sec_cli_len );
  250. TEST_ASSERT( sec_srv_len != 0 );
  251. TEST_ASSERT( memcmp( sec_srv, sec_cli, sec_srv_len ) == 0 );
  252. exit:
  253. mbedtls_dhm_free( &ctx_srv );
  254. mbedtls_dhm_free( &ctx_cli );
  255. }
  256. /* END_CASE */
  257. /* BEGIN_CASE */
  258. void dhm_make_public( int P_bytes, int radix_G, char *input_G, int result )
  259. {
  260. mbedtls_mpi P, G;
  261. mbedtls_dhm_context ctx;
  262. unsigned char output[MBEDTLS_MPI_MAX_SIZE];
  263. mbedtls_mpi_init( &P );
  264. mbedtls_mpi_init( &G );
  265. mbedtls_dhm_init( &ctx );
  266. TEST_ASSERT( mbedtls_mpi_lset( &P, 1 ) == 0 );
  267. TEST_ASSERT( mbedtls_mpi_shift_l( &P, ( P_bytes * 8 ) - 1 ) == 0 );
  268. TEST_ASSERT( mbedtls_mpi_set_bit( &P, 0, 1 ) == 0 );
  269. TEST_ASSERT( mbedtls_test_read_mpi( &G, radix_G, input_G ) == 0 );
  270. TEST_ASSERT( mbedtls_dhm_set_group( &ctx, &P, &G ) == 0 );
  271. TEST_ASSERT( mbedtls_dhm_make_public( &ctx, (int) mbedtls_mpi_size( &P ),
  272. output, sizeof(output),
  273. &mbedtls_test_rnd_pseudo_rand,
  274. NULL ) == result );
  275. exit:
  276. mbedtls_mpi_free( &P );
  277. mbedtls_mpi_free( &G );
  278. mbedtls_dhm_free( &ctx );
  279. }
  280. /* END_CASE */
  281. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  282. void dhm_file( char * filename, char * p, char * g, int len )
  283. {
  284. mbedtls_dhm_context ctx;
  285. mbedtls_mpi P, G;
  286. mbedtls_dhm_init( &ctx );
  287. mbedtls_mpi_init( &P ); mbedtls_mpi_init( &G );
  288. TEST_ASSERT( mbedtls_test_read_mpi( &P, 16, p ) == 0 );
  289. TEST_ASSERT( mbedtls_test_read_mpi( &G, 16, g ) == 0 );
  290. TEST_ASSERT( mbedtls_dhm_parse_dhmfile( &ctx, filename ) == 0 );
  291. TEST_ASSERT( ctx.len == (size_t) len );
  292. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.P, &P ) == 0 );
  293. TEST_ASSERT( mbedtls_mpi_cmp_mpi( &ctx.G, &G ) == 0 );
  294. exit:
  295. mbedtls_mpi_free( &P ); mbedtls_mpi_free( &G );
  296. mbedtls_dhm_free( &ctx );
  297. }
  298. /* END_CASE */
  299. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  300. void dhm_selftest( )
  301. {
  302. TEST_ASSERT( mbedtls_dhm_self_test( 1 ) == 0 );
  303. }
  304. /* END_CASE */