test_suite_hmac_drbg.function 11 KB

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