| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/entropy.h" | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 3 |  | 
|  | 4 | /* | 
|  | 5 | * Number of calls made to entropy_dummy_source() | 
|  | 6 | */ | 
|  | 7 | static size_t entropy_dummy_calls; | 
|  | 8 |  | 
|  | 9 | /* | 
|  | 10 | * Dummy entropy source | 
|  | 11 | * | 
|  | 12 | * If data is NULL, write exactly the requested length. | 
|  | 13 | * Otherwise, write the length indicated by data or error if negative | 
|  | 14 | */ | 
|  | 15 | static int entropy_dummy_source( void *data, unsigned char *output, | 
|  | 16 | size_t len, size_t *olen ) | 
|  | 17 | { | 
|  | 18 | entropy_dummy_calls++; | 
|  | 19 |  | 
|  | 20 | if( data == NULL ) | 
|  | 21 | *olen = len; | 
|  | 22 | else | 
|  | 23 | { | 
|  | 24 | int *d = (int *) data; | 
|  | 25 |  | 
|  | 26 | if( *d < 0 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 28 | else | 
|  | 29 | *olen = *d; | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | memset( output, 0x2a, *olen ); | 
|  | 33 |  | 
|  | 34 | return( 0 ); | 
|  | 35 | } | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 36 | /* END_HEADER */ | 
|  | 37 |  | 
|  | 38 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | * depends_on:MBEDTLS_ENTROPY_C | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 40 | * END_DEPENDENCIES | 
|  | 41 | */ | 
|  | 42 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO */ | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 44 | void entropy_seed_file( char *path, int ret ) | 
|  | 45 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | mbedtls_entropy_context ctx; | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 47 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | mbedtls_entropy_init( &ctx ); | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 49 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) == ret ); | 
|  | 51 | TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) == ret ); | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 52 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 53 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | mbedtls_entropy_free( &ctx ); | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 55 | } | 
|  | 56 | /* END_CASE */ | 
|  | 57 |  | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 58 | /* BEGIN_CASE */ | 
|  | 59 | void entropy_too_many_sources( ) | 
|  | 60 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | mbedtls_entropy_context ctx; | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 62 | size_t i; | 
|  | 63 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | mbedtls_entropy_init( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 65 |  | 
|  | 66 | /* | 
|  | 67 | * It's hard to tell precisely when the error will occur, | 
|  | 68 | * since we don't know how many sources were automatically added. | 
|  | 69 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | for( i = 0; i < MBEDTLS_ENTROPY_MAX_SOURCES; i++ ) | 
| Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 71 | (void) mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, | 
|  | 72 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 73 |  | 
| Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 74 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, | 
|  | 75 | 16, MBEDTLS_ENTROPY_SOURCE_WEAK ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | == MBEDTLS_ERR_ENTROPY_MAX_SOURCES ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 77 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 78 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | mbedtls_entropy_free( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 80 | } | 
|  | 81 | /* END_CASE */ | 
|  | 82 |  | 
|  | 83 | /* BEGIN_CASE */ | 
|  | 84 | void entropy_func_len( int len, int ret ) | 
|  | 85 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | mbedtls_entropy_context ctx; | 
|  | 87 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; | 
|  | 88 | unsigned char acc[MBEDTLS_ENTROPY_BLOCK_SIZE + 10] = { 0 }; | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 89 | size_t i, j; | 
|  | 90 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | mbedtls_entropy_init( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 92 |  | 
|  | 93 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | * See comments in mbedtls_entropy_self_test() | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 95 | */ | 
|  | 96 | for( i = 0; i < 8; i++ ) | 
|  | 97 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, len ) == ret ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 99 | for( j = 0; j < sizeof( buf ); j++ ) | 
|  | 100 | acc[j] |= buf[j]; | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | if( ret == 0 ) | 
|  | 104 | for( j = 0; j < (size_t) len; j++ ) | 
|  | 105 | TEST_ASSERT( acc[j] != 0 ); | 
|  | 106 |  | 
|  | 107 | for( j = len; j < sizeof( buf ); j++ ) | 
|  | 108 | TEST_ASSERT( acc[j] == 0 ); | 
|  | 109 | } | 
|  | 110 | /* END_CASE */ | 
|  | 111 |  | 
|  | 112 | /* BEGIN_CASE */ | 
|  | 113 | void entropy_source_fail( char *path ) | 
|  | 114 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_entropy_context ctx; | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 116 | int fail = -1; | 
|  | 117 | unsigned char buf[16]; | 
|  | 118 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 119 | mbedtls_entropy_init( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 120 |  | 
| Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 121 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, | 
|  | 122 | &fail, 16, | 
|  | 123 | MBEDTLS_ENTROPY_SOURCE_WEAK ) | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 124 | == 0 ); | 
|  | 125 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | TEST_ASSERT( mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) | 
|  | 127 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); | 
|  | 128 | TEST_ASSERT( mbedtls_entropy_gather( &ctx ) | 
|  | 129 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); | 
|  | 130 | #if defined(MBEDTLS_FS_IO) | 
|  | 131 | TEST_ASSERT( mbedtls_entropy_write_seed_file( &ctx, path ) | 
|  | 132 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); | 
|  | 133 | TEST_ASSERT( mbedtls_entropy_update_seed_file( &ctx, path ) | 
|  | 134 | == MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 135 | #else | 
|  | 136 | ((void) path); | 
|  | 137 | #endif | 
|  | 138 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 139 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_entropy_free( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 141 | } | 
|  | 142 | /* END_CASE */ | 
|  | 143 |  | 
|  | 144 | /* BEGIN_CASE */ | 
|  | 145 | void entropy_threshold( int threshold, int chunk_size, int result ) | 
|  | 146 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_entropy_context ctx; | 
|  | 148 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE] = { 0 }; | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 149 | int ret; | 
|  | 150 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | mbedtls_entropy_init( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 152 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | TEST_ASSERT( mbedtls_entropy_add_source( &ctx, entropy_dummy_source, | 
| Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 154 | &chunk_size, threshold, | 
|  | 155 | MBEDTLS_ENTROPY_SOURCE_WEAK ) == 0 ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 156 |  | 
|  | 157 | entropy_dummy_calls = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 159 |  | 
|  | 160 | if( result >= 0 ) | 
|  | 161 | { | 
|  | 162 | TEST_ASSERT( ret == 0 ); | 
|  | 163 | TEST_ASSERT( entropy_dummy_calls == (size_t) result ); | 
|  | 164 | } | 
|  | 165 | else | 
|  | 166 | { | 
|  | 167 | TEST_ASSERT( ret == result ); | 
|  | 168 | } | 
|  | 169 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 170 | exit: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | mbedtls_entropy_free( &ctx ); | 
| Manuel Pégourié-Gonnard | c7c56b2 | 2014-05-30 11:42:01 +0200 | [diff] [blame] | 172 | } | 
|  | 173 | /* END_CASE */ | 
|  | 174 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 176 | void entropy_selftest( ) | 
|  | 177 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 178 | TEST_ASSERT( mbedtls_entropy_self_test( 0 ) == 0 ); | 
| Manuel Pégourié-Gonnard | 2c25eb0 | 2014-05-30 10:38:18 +0200 | [diff] [blame] | 179 | } | 
|  | 180 | /* END_CASE */ |