test_suite_random.function 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* BEGIN_HEADER */
  2. /* Test random generation as a whole. */
  3. #include "mbedtls/bignum.h"
  4. #include "mbedtls/ctr_drbg.h"
  5. #include "mbedtls/ecdsa.h"
  6. #include "mbedtls/entropy.h"
  7. #include "mbedtls/hmac_drbg.h"
  8. #include "mbedtls/psa_util.h"
  9. #include "psa/crypto.h"
  10. /* How many bytes to generate in each test case for repeated generation.
  11. * This must be high enough that the probability of generating the same
  12. * output twice is infinitesimal, but low enough that random generators
  13. * are willing to deliver that much. */
  14. #define OUTPUT_SIZE 32
  15. /* END_HEADER */
  16. /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_TEST_NULL_ENTROPY:MBEDTLS_CTR_DRBG_C */
  17. void random_twice_with_ctr_drbg( )
  18. {
  19. mbedtls_entropy_context entropy;
  20. mbedtls_ctr_drbg_context drbg;
  21. unsigned char output1[OUTPUT_SIZE];
  22. unsigned char output2[OUTPUT_SIZE];
  23. /* First round */
  24. mbedtls_entropy_init( &entropy );
  25. mbedtls_ctr_drbg_init( &drbg );
  26. TEST_EQUAL( 0, mbedtls_ctr_drbg_seed( &drbg,
  27. mbedtls_entropy_func, &entropy,
  28. NULL, 0 ) );
  29. TEST_EQUAL( 0, mbedtls_ctr_drbg_random( &drbg,
  30. output1, sizeof( output1 ) ) );
  31. mbedtls_ctr_drbg_free( &drbg );
  32. mbedtls_entropy_free( &entropy );
  33. /* Second round */
  34. mbedtls_entropy_init( &entropy );
  35. mbedtls_ctr_drbg_init( &drbg );
  36. TEST_EQUAL( 0, mbedtls_ctr_drbg_seed( &drbg,
  37. mbedtls_entropy_func, &entropy,
  38. NULL, 0 ) );
  39. TEST_EQUAL( 0, mbedtls_ctr_drbg_random( &drbg,
  40. output2, sizeof( output2 ) ) );
  41. mbedtls_ctr_drbg_free( &drbg );
  42. mbedtls_entropy_free( &entropy );
  43. /* The two rounds must generate different random data. */
  44. TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 );
  45. exit:
  46. mbedtls_ctr_drbg_free( &drbg );
  47. mbedtls_entropy_free( &entropy );
  48. }
  49. /* END_CASE */
  50. /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:!MBEDTLS_TEST_NULL_ENTROPY:MBEDTLS_HMAC_DRBG_C */
  51. void random_twice_with_hmac_drbg( int md_type )
  52. {
  53. mbedtls_entropy_context entropy;
  54. mbedtls_hmac_drbg_context drbg;
  55. unsigned char output1[OUTPUT_SIZE];
  56. unsigned char output2[OUTPUT_SIZE];
  57. const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type );
  58. /* First round */
  59. mbedtls_entropy_init( &entropy );
  60. mbedtls_hmac_drbg_init( &drbg );
  61. TEST_EQUAL( 0, mbedtls_hmac_drbg_seed( &drbg, md_info,
  62. mbedtls_entropy_func, &entropy,
  63. NULL, 0 ) );
  64. TEST_EQUAL( 0, mbedtls_hmac_drbg_random( &drbg,
  65. output1, sizeof( output1 ) ) );
  66. mbedtls_hmac_drbg_free( &drbg );
  67. mbedtls_entropy_free( &entropy );
  68. /* Second round */
  69. mbedtls_entropy_init( &entropy );
  70. mbedtls_hmac_drbg_init( &drbg );
  71. TEST_EQUAL( 0, mbedtls_hmac_drbg_seed( &drbg, md_info,
  72. mbedtls_entropy_func, &entropy,
  73. NULL, 0 ) );
  74. TEST_EQUAL( 0, mbedtls_hmac_drbg_random( &drbg,
  75. output2, sizeof( output2 ) ) );
  76. mbedtls_hmac_drbg_free( &drbg );
  77. mbedtls_entropy_free( &entropy );
  78. /* The two rounds must generate different random data. */
  79. TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 );
  80. exit:
  81. mbedtls_hmac_drbg_free( &drbg );
  82. mbedtls_entropy_free( &entropy );
  83. }
  84. /* END_CASE */
  85. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:!MBEDTLS_TEST_NULL_ENTROPY:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  86. void random_twice_with_psa_from_classic( )
  87. {
  88. unsigned char output1[OUTPUT_SIZE];
  89. unsigned char output2[OUTPUT_SIZE];
  90. /* First round */
  91. PSA_ASSERT( psa_crypto_init( ) );
  92. TEST_EQUAL( 0, mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
  93. output1, sizeof( output1 ) ) );
  94. PSA_DONE( );
  95. /* Second round */
  96. PSA_ASSERT( psa_crypto_init( ) );
  97. TEST_EQUAL( 0, mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
  98. output2, sizeof( output2 ) ) );
  99. PSA_DONE( );
  100. /* The two rounds must generate different random data. */
  101. TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 );
  102. exit:
  103. PSA_DONE( );
  104. }
  105. /* END_CASE */
  106. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:!MBEDTLS_TEST_NULL_ENTROPY:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  107. void random_twice_with_psa_from_psa( )
  108. {
  109. unsigned char output1[OUTPUT_SIZE];
  110. unsigned char output2[OUTPUT_SIZE];
  111. /* First round */
  112. PSA_ASSERT( psa_crypto_init( ) );
  113. PSA_ASSERT( psa_generate_random( output1, sizeof( output1 ) ) );
  114. PSA_DONE( );
  115. /* Second round */
  116. PSA_ASSERT( psa_crypto_init( ) );
  117. PSA_ASSERT( psa_generate_random( output2, sizeof( output2 ) ) );
  118. PSA_DONE( );
  119. /* The two rounds must generate different random data. */
  120. TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 );
  121. exit:
  122. PSA_DONE( );
  123. }
  124. /* END_CASE */
  125. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */
  126. void mbedtls_psa_get_random_no_init( )
  127. {
  128. unsigned char output[1];
  129. TEST_ASSERT( mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
  130. output, sizeof( output ) ) != 0 );
  131. }
  132. /* END_CASE */
  133. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C */
  134. void mbedtls_psa_get_random_length( int n )
  135. {
  136. unsigned char *output = NULL;
  137. PSA_ASSERT( psa_crypto_init( ) );
  138. ASSERT_ALLOC( output, n );
  139. TEST_EQUAL( 0, mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
  140. output, n ) );
  141. exit:
  142. mbedtls_free( output );
  143. PSA_DONE( );
  144. }
  145. /* END_CASE */
  146. /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_ECDSA_C */
  147. void mbedtls_psa_get_random_ecdsa_sign( int curve )
  148. {
  149. mbedtls_ecp_group grp;
  150. mbedtls_mpi d, r, s;
  151. unsigned char buf[] = "This is not a hash.";
  152. mbedtls_ecp_group_init( &grp );
  153. mbedtls_mpi_init( &d );
  154. mbedtls_mpi_init( &r );
  155. mbedtls_mpi_init( &s );
  156. TEST_EQUAL( 0, mbedtls_mpi_lset( &d, 123456789 ) );
  157. TEST_EQUAL( 0, mbedtls_ecp_group_load( &grp, curve ) );
  158. PSA_ASSERT( psa_crypto_init( ) );
  159. TEST_EQUAL( 0, mbedtls_ecdsa_sign( &grp, &r, &s, &d,
  160. buf, sizeof( buf ),
  161. mbedtls_psa_get_random,
  162. MBEDTLS_PSA_RANDOM_STATE ) );
  163. exit:
  164. mbedtls_mpi_free( &d );
  165. mbedtls_mpi_free( &r );
  166. mbedtls_mpi_free( &s );
  167. mbedtls_ecp_group_free( &grp );
  168. PSA_DONE( );
  169. }
  170. /* END_CASE */