Gilles Peskine | 077599a | 2021-02-03 18:55:39 +0100 | [diff] [blame^] | 1 | /* BEGIN_HEADER */ |
| 2 | |
| 3 | /* Test random generation as a whole. */ |
| 4 | |
| 5 | #include "mbedtls/ctr_drbg.h" |
| 6 | #include "mbedtls/entropy.h" |
| 7 | #include "mbedtls/hmac_drbg.h" |
| 8 | #include "psa/crypto.h" |
| 9 | |
| 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 | |
| 16 | /* END_HEADER */ |
| 17 | |
| 18 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:MBEDTLS_CTR_DRBG_C */ |
| 19 | void random_twice_with_ctr_drbg( ) |
| 20 | { |
| 21 | mbedtls_entropy_context entropy; |
| 22 | mbedtls_ctr_drbg_context drbg; |
| 23 | unsigned char output1[OUTPUT_SIZE]; |
| 24 | unsigned char output2[OUTPUT_SIZE]; |
| 25 | |
| 26 | /* First round */ |
| 27 | mbedtls_entropy_init( &entropy ); |
| 28 | mbedtls_ctr_drbg_init( &drbg ); |
| 29 | TEST_EQUAL( 0, mbedtls_ctr_drbg_seed( &drbg, |
| 30 | mbedtls_entropy_func, &entropy, |
| 31 | NULL, 0 ) ); |
| 32 | TEST_EQUAL( 0, mbedtls_ctr_drbg_random( &drbg, |
| 33 | output1, sizeof( output1 ) ) ); |
| 34 | mbedtls_ctr_drbg_free( &drbg ); |
| 35 | mbedtls_entropy_free( &entropy ); |
| 36 | |
| 37 | /* Second round */ |
| 38 | mbedtls_entropy_init( &entropy ); |
| 39 | mbedtls_ctr_drbg_init( &drbg ); |
| 40 | TEST_EQUAL( 0, mbedtls_ctr_drbg_seed( &drbg, |
| 41 | mbedtls_entropy_func, &entropy, |
| 42 | NULL, 0 ) ); |
| 43 | TEST_EQUAL( 0, mbedtls_ctr_drbg_random( &drbg, |
| 44 | output2, sizeof( output2 ) ) ); |
| 45 | mbedtls_ctr_drbg_free( &drbg ); |
| 46 | mbedtls_entropy_free( &entropy ); |
| 47 | |
| 48 | /* The two rounds must generate different random data. */ |
| 49 | TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 ); |
| 50 | |
| 51 | exit: |
| 52 | mbedtls_ctr_drbg_free( &drbg ); |
| 53 | mbedtls_entropy_free( &entropy ); |
| 54 | } |
| 55 | /* END_CASE */ |
| 56 | |
| 57 | /* BEGIN_CASE depends_on:MBEDTLS_ENTROPY_C:MBEDTLS_HMAC_DRBG_C */ |
| 58 | void random_twice_with_hmac_drbg( int md_type ) |
| 59 | { |
| 60 | mbedtls_entropy_context entropy; |
| 61 | mbedtls_hmac_drbg_context drbg; |
| 62 | unsigned char output1[OUTPUT_SIZE]; |
| 63 | unsigned char output2[OUTPUT_SIZE]; |
| 64 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_type ); |
| 65 | |
| 66 | /* First round */ |
| 67 | mbedtls_entropy_init( &entropy ); |
| 68 | mbedtls_hmac_drbg_init( &drbg ); |
| 69 | TEST_EQUAL( 0, mbedtls_hmac_drbg_seed( &drbg, md_info, |
| 70 | mbedtls_entropy_func, &entropy, |
| 71 | NULL, 0 ) ); |
| 72 | TEST_EQUAL( 0, mbedtls_hmac_drbg_random( &drbg, |
| 73 | output1, sizeof( output1 ) ) ); |
| 74 | mbedtls_hmac_drbg_free( &drbg ); |
| 75 | mbedtls_entropy_free( &entropy ); |
| 76 | |
| 77 | /* Second round */ |
| 78 | mbedtls_entropy_init( &entropy ); |
| 79 | mbedtls_hmac_drbg_init( &drbg ); |
| 80 | TEST_EQUAL( 0, mbedtls_hmac_drbg_seed( &drbg, md_info, |
| 81 | mbedtls_entropy_func, &entropy, |
| 82 | NULL, 0 ) ); |
| 83 | TEST_EQUAL( 0, mbedtls_hmac_drbg_random( &drbg, |
| 84 | output2, sizeof( output2 ) ) ); |
| 85 | mbedtls_hmac_drbg_free( &drbg ); |
| 86 | mbedtls_entropy_free( &entropy ); |
| 87 | |
| 88 | /* The two rounds must generate different random data. */ |
| 89 | TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 ); |
| 90 | |
| 91 | exit: |
| 92 | mbedtls_hmac_drbg_free( &drbg ); |
| 93 | mbedtls_entropy_free( &entropy ); |
| 94 | } |
| 95 | /* END_CASE */ |
| 96 | |
| 97 | /* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_C:!MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 98 | void random_twice_with_psa_from_psa( ) |
| 99 | { |
| 100 | unsigned char output1[OUTPUT_SIZE]; |
| 101 | unsigned char output2[OUTPUT_SIZE]; |
| 102 | |
| 103 | /* First round */ |
| 104 | PSA_ASSERT( psa_crypto_init( ) ); |
| 105 | PSA_ASSERT( psa_generate_random( output1, sizeof( output1 ) ) ); |
| 106 | PSA_DONE( ); |
| 107 | |
| 108 | /* Second round */ |
| 109 | PSA_ASSERT( psa_crypto_init( ) ); |
| 110 | PSA_ASSERT( psa_generate_random( output2, sizeof( output2 ) ) ); |
| 111 | PSA_DONE( ); |
| 112 | |
| 113 | /* The two rounds must generate different random data. */ |
| 114 | TEST_ASSERT( memcmp( output1, output2, OUTPUT_SIZE ) != 0 ); |
| 115 | |
| 116 | exit: |
| 117 | PSA_DONE( ); |
| 118 | } |
| 119 | /* END_CASE */ |