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