test_suite_hmac_drbg.function 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/hmac_drbg.h"
  3. #include "string.h"
  4. typedef struct
  5. {
  6. unsigned char *p;
  7. size_t len;
  8. } entropy_ctx;
  9. static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
  10. {
  11. entropy_ctx *ctx = (entropy_ctx *) data;
  12. if( len > ctx->len )
  13. return( -1 );
  14. memcpy( buf, ctx->p, len );
  15. ctx->p += len;
  16. ctx->len -= len;
  17. return( 0 );
  18. }
  19. /* END_HEADER */
  20. /* BEGIN_DEPENDENCIES
  21. * depends_on:MBEDTLS_HMAC_DRBG_C
  22. * END_DEPENDENCIES
  23. */
  24. /* BEGIN_CASE */
  25. void hmac_drbg_entropy_usage( int md_alg )
  26. {
  27. unsigned char out[16];
  28. unsigned char buf[1024];
  29. const mbedtls_md_info_t *md_info;
  30. mbedtls_hmac_drbg_context ctx;
  31. entropy_ctx entropy;
  32. size_t i, reps = 10;
  33. size_t default_entropy_len;
  34. size_t expected_consumed_entropy = 0;
  35. mbedtls_hmac_drbg_init( &ctx );
  36. memset( buf, 0, sizeof( buf ) );
  37. memset( out, 0, sizeof( out ) );
  38. entropy.len = sizeof( buf );
  39. entropy.p = buf;
  40. md_info = mbedtls_md_info_from_type( md_alg );
  41. TEST_ASSERT( md_info != NULL );
  42. if( mbedtls_md_get_size( md_info ) <= 20 )
  43. default_entropy_len = 16;
  44. else if( mbedtls_md_get_size( md_info ) <= 28 )
  45. default_entropy_len = 24;
  46. else
  47. default_entropy_len = 32;
  48. /* Set reseed interval before seed */
  49. mbedtls_hmac_drbg_set_reseed_interval( &ctx, 2 * reps );
  50. /* Init must use entropy */
  51. TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &entropy,
  52. NULL, 0 ) == 0 );
  53. /* default_entropy_len of entropy, plus half as much for the nonce */
  54. expected_consumed_entropy += default_entropy_len * 3 / 2;
  55. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  56. /* By default, PR is off, and reseed interval was set to
  57. * 2 * reps so the next few calls should not use entropy */
  58. for( i = 0; i < reps; i++ )
  59. {
  60. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
  61. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
  62. buf, 16 ) == 0 );
  63. }
  64. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  65. /* While at it, make sure we didn't write past the requested length */
  66. TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
  67. TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
  68. TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
  69. TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
  70. /* There have been 2 * reps calls to random. The next call should reseed */
  71. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  72. expected_consumed_entropy += default_entropy_len;
  73. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  74. /* Set reseed interval after seed */
  75. mbedtls_hmac_drbg_set_reseed_interval( &ctx, 4 * reps + 1);
  76. /* The new few calls should not reseed */
  77. for( i = 0; i < (2 * reps); i++ )
  78. {
  79. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  80. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, out, sizeof( out ) ,
  81. buf, 16 ) == 0 );
  82. }
  83. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  84. /* Now enable PR, so the next few calls should all reseed */
  85. mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  86. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  87. expected_consumed_entropy += default_entropy_len;
  88. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  89. /* Finally, check setting entropy_len */
  90. mbedtls_hmac_drbg_set_entropy_len( &ctx, 42 );
  91. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  92. expected_consumed_entropy += 42;
  93. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  94. mbedtls_hmac_drbg_set_entropy_len( &ctx, 13 );
  95. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  96. expected_consumed_entropy += 13;
  97. TEST_EQUAL( sizeof( buf ) - entropy.len, expected_consumed_entropy );
  98. exit:
  99. mbedtls_hmac_drbg_free( &ctx );
  100. }
  101. /* END_CASE */
  102. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  103. void hmac_drbg_seed_file( int md_alg, char * path, int ret )
  104. {
  105. const mbedtls_md_info_t *md_info;
  106. mbedtls_hmac_drbg_context ctx;
  107. mbedtls_hmac_drbg_init( &ctx );
  108. md_info = mbedtls_md_info_from_type( md_alg );
  109. TEST_ASSERT( md_info != NULL );
  110. TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info,
  111. mbedtls_test_rnd_std_rand, NULL,
  112. NULL, 0 ) == 0 );
  113. TEST_ASSERT( mbedtls_hmac_drbg_write_seed_file( &ctx, path ) == ret );
  114. TEST_ASSERT( mbedtls_hmac_drbg_update_seed_file( &ctx, path ) == ret );
  115. exit:
  116. mbedtls_hmac_drbg_free( &ctx );
  117. }
  118. /* END_CASE */
  119. /* BEGIN_CASE */
  120. void hmac_drbg_buf( int md_alg )
  121. {
  122. unsigned char out[16];
  123. unsigned char buf[100];
  124. const mbedtls_md_info_t *md_info;
  125. mbedtls_hmac_drbg_context ctx;
  126. size_t i;
  127. mbedtls_hmac_drbg_init( &ctx );
  128. memset( buf, 0, sizeof( buf ) );
  129. memset( out, 0, sizeof( out ) );
  130. md_info = mbedtls_md_info_from_type( md_alg );
  131. TEST_ASSERT( md_info != NULL );
  132. TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info, buf, sizeof( buf ) ) == 0 );
  133. /* Make sure it never tries to reseed (would segfault otherwise) */
  134. mbedtls_hmac_drbg_set_reseed_interval( &ctx, 3 );
  135. mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  136. for( i = 0; i < 30; i++ )
  137. TEST_ASSERT( mbedtls_hmac_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  138. exit:
  139. mbedtls_hmac_drbg_free( &ctx );
  140. }
  141. /* END_CASE */
  142. /* BEGIN_CASE */
  143. void hmac_drbg_no_reseed( int md_alg, data_t * entropy,
  144. data_t * custom, data_t * add1,
  145. data_t * add2, data_t * output )
  146. {
  147. unsigned char data[1024];
  148. unsigned char my_output[512];
  149. entropy_ctx p_entropy;
  150. const mbedtls_md_info_t *md_info;
  151. mbedtls_hmac_drbg_context ctx;
  152. mbedtls_hmac_drbg_init( &ctx );
  153. p_entropy.p = entropy->x;
  154. p_entropy.len = entropy->len;
  155. md_info = mbedtls_md_info_from_type( md_alg );
  156. TEST_ASSERT( md_info != NULL );
  157. /* Test the simplified buffer-based variant */
  158. memcpy( data, entropy->x, p_entropy.len );
  159. memcpy( data + p_entropy.len, custom->x, custom->len );
  160. TEST_ASSERT( mbedtls_hmac_drbg_seed_buf( &ctx, md_info,
  161. data, p_entropy.len + custom->len ) == 0 );
  162. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  163. add1->x, add1->len ) == 0 );
  164. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  165. add2->x, add2->len ) == 0 );
  166. /* Reset context for second run */
  167. mbedtls_hmac_drbg_free( &ctx );
  168. TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
  169. /* And now the normal entropy-based variant */
  170. TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
  171. custom->x, custom->len ) == 0 );
  172. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  173. add1->x, add1->len ) == 0 );
  174. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  175. add2->x, add2->len ) == 0 );
  176. TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
  177. exit:
  178. mbedtls_hmac_drbg_free( &ctx );
  179. }
  180. /* END_CASE */
  181. /* BEGIN_CASE */
  182. void hmac_drbg_nopr( int md_alg, data_t * entropy, data_t * custom,
  183. data_t * add1, data_t * add2, data_t * add3,
  184. data_t * output )
  185. {
  186. unsigned char my_output[512];
  187. entropy_ctx p_entropy;
  188. const mbedtls_md_info_t *md_info;
  189. mbedtls_hmac_drbg_context ctx;
  190. mbedtls_hmac_drbg_init( &ctx );
  191. p_entropy.p = entropy->x;
  192. p_entropy.len = entropy->len;
  193. md_info = mbedtls_md_info_from_type( md_alg );
  194. TEST_ASSERT( md_info != NULL );
  195. TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
  196. custom->x, custom->len ) == 0 );
  197. TEST_ASSERT( mbedtls_hmac_drbg_reseed( &ctx, add1->x, add1->len ) == 0 );
  198. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  199. add2->x, add2->len ) == 0 );
  200. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  201. add3->x, add3->len ) == 0 );
  202. TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
  203. exit:
  204. mbedtls_hmac_drbg_free( &ctx );
  205. }
  206. /* END_CASE */
  207. /* BEGIN_CASE */
  208. void hmac_drbg_pr( int md_alg, data_t * entropy, data_t * custom,
  209. data_t * add1, data_t * add2, data_t * output )
  210. {
  211. unsigned char my_output[512];
  212. entropy_ctx p_entropy;
  213. const mbedtls_md_info_t *md_info;
  214. mbedtls_hmac_drbg_context ctx;
  215. mbedtls_hmac_drbg_init( &ctx );
  216. p_entropy.p = entropy->x;
  217. p_entropy.len = entropy->len;
  218. md_info = mbedtls_md_info_from_type( md_alg );
  219. TEST_ASSERT( md_info != NULL );
  220. TEST_ASSERT( mbedtls_hmac_drbg_seed( &ctx, md_info, mbedtls_test_entropy_func, &p_entropy,
  221. custom->x, custom->len ) == 0 );
  222. mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON );
  223. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  224. add1->x, add1->len ) == 0 );
  225. TEST_ASSERT( mbedtls_hmac_drbg_random_with_add( &ctx, my_output, output->len,
  226. add2->x, add2->len ) == 0 );
  227. TEST_ASSERT( memcmp( my_output, output->x, output->len ) == 0 );
  228. exit:
  229. mbedtls_hmac_drbg_free( &ctx );
  230. }
  231. /* END_CASE */
  232. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  233. void hmac_drbg_selftest( )
  234. {
  235. TEST_ASSERT( mbedtls_hmac_drbg_self_test( 1 ) == 0 );
  236. }
  237. /* END_CASE */