test_suite_ctr_drbg.function 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* BEGIN_HEADER */
  2. #include "mbedtls/ctr_drbg.h"
  3. int test_offset_idx;
  4. int mbedtls_entropy_func( void *data, unsigned char *buf, size_t len )
  5. {
  6. const unsigned char *p = (unsigned char *) data;
  7. memcpy( buf, p + test_offset_idx, len );
  8. test_offset_idx += len;
  9. return( 0 );
  10. }
  11. /* END_HEADER */
  12. /* BEGIN_DEPENDENCIES
  13. * depends_on:MBEDTLS_CTR_DRBG_C
  14. * END_DEPENDENCIES
  15. */
  16. /* BEGIN_CASE */
  17. void ctr_drbg_special_behaviours( )
  18. {
  19. mbedtls_ctr_drbg_context ctx;
  20. unsigned char output[512];
  21. unsigned char additional[512];
  22. mbedtls_ctr_drbg_init( &ctx );
  23. memset( output, 0, sizeof( output ) );
  24. memset( additional, 0, sizeof( additional ) );
  25. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx,
  26. output, MBEDTLS_CTR_DRBG_MAX_REQUEST + 1,
  27. additional, 16 ) ==
  28. MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG );
  29. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx,
  30. output, 16,
  31. additional, MBEDTLS_CTR_DRBG_MAX_INPUT + 1 ) ==
  32. MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  33. TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, additional,
  34. MBEDTLS_CTR_DRBG_MAX_SEED_INPUT + 1 ) ==
  35. MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG );
  36. exit:
  37. mbedtls_ctr_drbg_free( &ctx );
  38. }
  39. /* END_CASE */
  40. /* BEGIN_CASE */
  41. void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
  42. char *add1_string, char *add2_string,
  43. char *result_str )
  44. {
  45. unsigned char entropy[512];
  46. unsigned char add_init[512];
  47. unsigned char add1[512];
  48. unsigned char add2[512];
  49. mbedtls_ctr_drbg_context ctx;
  50. unsigned char buf[512];
  51. unsigned char output_str[512];
  52. int add_init_len, add1_len, add2_len;
  53. mbedtls_ctr_drbg_init( &ctx );
  54. memset( output_str, 0, 512 );
  55. unhexify( entropy, entropy_string );
  56. add_init_len = unhexify( add_init, add_init_string );
  57. add1_len = unhexify( add1, add1_string );
  58. add2_len = unhexify( add2, add2_string );
  59. test_offset_idx = 0;
  60. TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
  61. mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
  62. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
  63. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
  64. hexify( output_str, buf, 16 );
  65. TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
  66. exit:
  67. mbedtls_ctr_drbg_free( &ctx );
  68. }
  69. /* END_CASE */
  70. /* BEGIN_CASE */
  71. void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
  72. char *add1_string, char *add_reseed_string,
  73. char *add2_string, char *result_str )
  74. {
  75. unsigned char entropy[512];
  76. unsigned char add_init[512];
  77. unsigned char add1[512];
  78. unsigned char add_reseed[512];
  79. unsigned char add2[512];
  80. mbedtls_ctr_drbg_context ctx;
  81. unsigned char buf[512];
  82. unsigned char output_str[512];
  83. int add_init_len, add1_len, add_reseed_len, add2_len;
  84. mbedtls_ctr_drbg_init( &ctx );
  85. memset( output_str, 0, 512 );
  86. unhexify( entropy, entropy_string );
  87. add_init_len = unhexify( add_init, add_init_string );
  88. add1_len = unhexify( add1, add1_string );
  89. add_reseed_len = unhexify( add_reseed, add_reseed_string );
  90. add2_len = unhexify( add2, add2_string );
  91. test_offset_idx = 0;
  92. TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
  93. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
  94. TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
  95. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2, add2_len ) == 0 );
  96. hexify( output_str, buf, 16 );
  97. TEST_ASSERT( strcmp( (char *) output_str, result_str ) == 0 );
  98. exit:
  99. mbedtls_ctr_drbg_free( &ctx );
  100. }
  101. /* END_CASE */
  102. /* BEGIN_CASE */
  103. void ctr_drbg_entropy_usage( )
  104. {
  105. unsigned char out[16];
  106. unsigned char add[16];
  107. unsigned char entropy[1024];
  108. mbedtls_ctr_drbg_context ctx;
  109. size_t i, reps = 10;
  110. int last_idx;
  111. mbedtls_ctr_drbg_init( &ctx );
  112. test_offset_idx = 0;
  113. memset( entropy, 0, sizeof( entropy ) );
  114. memset( out, 0, sizeof( out ) );
  115. memset( add, 0, sizeof( add ) );
  116. /* Init must use entropy */
  117. last_idx = test_offset_idx;
  118. TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_entropy_func, entropy, NULL, 0 ) == 0 );
  119. TEST_ASSERT( last_idx < test_offset_idx );
  120. /* By default, PR is off and reseed_interval is large,
  121. * so the next few calls should not use entropy */
  122. last_idx = test_offset_idx;
  123. for( i = 0; i < reps; i++ )
  124. {
  125. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) - 4 ) == 0 );
  126. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) - 4,
  127. add, sizeof( add ) ) == 0 );
  128. }
  129. TEST_ASSERT( last_idx == test_offset_idx );
  130. /* While at it, make sure we didn't write past the requested length */
  131. TEST_ASSERT( out[sizeof( out ) - 4] == 0 );
  132. TEST_ASSERT( out[sizeof( out ) - 3] == 0 );
  133. TEST_ASSERT( out[sizeof( out ) - 2] == 0 );
  134. TEST_ASSERT( out[sizeof( out ) - 1] == 0 );
  135. /* Set reseed_interval to the number of calls done,
  136. * so the next call should reseed */
  137. mbedtls_ctr_drbg_set_reseed_interval( &ctx, 2 * reps );
  138. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  139. TEST_ASSERT( last_idx < test_offset_idx );
  140. /* The new few calls should not reseed */
  141. last_idx = test_offset_idx;
  142. for( i = 0; i < reps / 2; i++ )
  143. {
  144. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  145. TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, out, sizeof( out ) ,
  146. add, sizeof( add ) ) == 0 );
  147. }
  148. TEST_ASSERT( last_idx == test_offset_idx );
  149. /* Call update with too much data (sizeof entropy > MAX(_SEED)_INPUT)
  150. * (just make sure it doesn't cause memory corruption) */
  151. mbedtls_ctr_drbg_update( &ctx, entropy, sizeof( entropy ) );
  152. /* Now enable PR, so the next few calls should all reseed */
  153. mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
  154. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  155. TEST_ASSERT( last_idx < test_offset_idx );
  156. /* Finally, check setting entropy_len */
  157. mbedtls_ctr_drbg_set_entropy_len( &ctx, 42 );
  158. last_idx = test_offset_idx;
  159. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  160. TEST_ASSERT( test_offset_idx - last_idx == 42 );
  161. mbedtls_ctr_drbg_set_entropy_len( &ctx, 13 );
  162. last_idx = test_offset_idx;
  163. TEST_ASSERT( mbedtls_ctr_drbg_random( &ctx, out, sizeof( out ) ) == 0 );
  164. TEST_ASSERT( test_offset_idx - last_idx == 13 );
  165. exit:
  166. mbedtls_ctr_drbg_free( &ctx );
  167. }
  168. /* END_CASE */
  169. /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */
  170. void ctr_drbg_seed_file( char *path, int ret )
  171. {
  172. mbedtls_ctr_drbg_context ctx;
  173. mbedtls_ctr_drbg_init( &ctx );
  174. TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
  175. TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret );
  176. TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret );
  177. exit:
  178. mbedtls_ctr_drbg_free( &ctx );
  179. }
  180. /* END_CASE */
  181. /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
  182. void ctr_drbg_selftest( )
  183. {
  184. TEST_ASSERT( mbedtls_ctr_drbg_self_test( 1 ) == 0 );
  185. }
  186. /* END_CASE */