test_suite_hmac_drbg.function 9.4 KB

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