mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2025-01-11 19:25:39 +00:00
test of generate_random: focus on testing the output buffer size
In the test generate_random, focus on testing that psa_generate_random is writing all the bytes of the output buffer and no more. Add a check that it is writing to each byte of the output buffer. Do not try to look for repeating output as the structure of a unit test isn't likely to catch that sort of problem anyway.
This commit is contained in:
parent
9ad29e2bee
commit
a50d7396f3
|
@ -581,19 +581,22 @@ depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_RSA_C:MBEDTLS_PKCS1_V15
|
|||
asymmetric_decrypt_fail:PSA_KEY_TYPE_RSA_KEYPAIR:"3082025e02010002818100af057d396ee84fb75fdbb5c2b13c7fe5a654aa8aa2470b541ee1feb0b12d25c79711531249e1129628042dbbb6c120d1443524ef4c0e6e1d8956eeb2077af12349ddeee54483bc06c2c61948cd02b202e796aebd94d3a7cbf859c2c1819c324cb82b9cd34ede263a2abffe4733f077869e8660f7d6834da53d690ef7985f6bc3020301000102818100874bf0ffc2f2a71d14671ddd0171c954d7fdbf50281e4f6d99ea0e1ebcf82faa58e7b595ffb293d1abe17f110b37c48cc0f36c37e84d876621d327f64bbe08457d3ec4098ba2fa0a319fba411c2841ed7be83196a8cdf9daa5d00694bc335fc4c32217fe0488bce9cb7202e59468b1ead119000477db2ca797fac19eda3f58c1024100e2ab760841bb9d30a81d222de1eb7381d82214407f1b975cbbfe4e1a9467fd98adbd78f607836ca5be1928b9d160d97fd45c12d6b52e2c9871a174c66b488113024100c5ab27602159ae7d6f20c3c2ee851e46dc112e689e28d5fcbbf990a99ef8a90b8bb44fd36467e7fc1789ceb663abda338652c3c73f111774902e840565927091024100b6cdbd354f7df579a63b48b3643e353b84898777b48b15f94e0bfc0567a6ae5911d57ad6409cf7647bf96264e9bd87eb95e263b7110b9a1f9f94acced0fafa4d024071195eec37e8d257decfc672b07ae639f10cbb9b0c739d0c809968d644a94e3fd6ed9287077a14583f379058f76a8aecd43c62dc8c0f41766650d725275ac4a1024100bb32d133edc2e048d463388b7be9cb4be29f4b6250be603e70e3647501c97ddde20a4e71be95fd5e71784e25aca4baf25be5738aae59bbfe1c997781447a2b24":PSA_ALG_RSA_PKCS1V15_CRYPT:"0099ffde2fcc00c9cc01972ebfa7779b298dbbaf7f50707a7405296dd2783456fc792002f462e760500e02afa25a859ace8701cb5d3b0262116431c43af8eb08f5a88301057cf1c156a2a5193c143e7a5b03fac132b7e89e6dcd8f4c82c9b28452329c260d30bc39b3816b7c46b41b37b4850d2ae74e729f99c6621fbbe2e46872":PSA_ERROR_INVALID_ARGUMENT
|
||||
|
||||
PSA generate random: 0 bytes
|
||||
generate_random:0:0
|
||||
generate_random:0
|
||||
|
||||
PSA generate random: 1 byte
|
||||
generate_random:1:8
|
||||
generate_random:1
|
||||
|
||||
PSA generate random: 4 bytes
|
||||
generate_random:1:2
|
||||
generate_random:4
|
||||
|
||||
PSA generate random: 16 bytes
|
||||
generate_random:16:0
|
||||
generate_random:16
|
||||
|
||||
PSA generate random: 19 bytes
|
||||
generate_random:19:0
|
||||
generate_random:19
|
||||
|
||||
PSA generate random: 260 bytes
|
||||
generate_random:260
|
||||
|
||||
PSA generate key: bad type (0xffffffff)
|
||||
generate_key:0xffffffff:128:PSA_KEY_USAGE_EXPORT:0:PSA_ERROR_NOT_SUPPORTED
|
||||
|
|
|
@ -1871,43 +1871,51 @@ exit:
|
|||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void generate_random( int bytes, int retries )
|
||||
void generate_random( int bytes_arg )
|
||||
{
|
||||
const unsigned char trail[] = "foobar";
|
||||
unsigned char *buffer1 = mbedtls_calloc( 1, bytes + sizeof( trail ) );
|
||||
unsigned char *buffer2 = mbedtls_calloc( 1, bytes );
|
||||
size_t bytes = bytes_arg;
|
||||
const unsigned char trail[] = "don't overwrite me";
|
||||
unsigned char *output = mbedtls_calloc( 1, bytes + sizeof( trail ) );
|
||||
unsigned char *changed = mbedtls_calloc( 1, bytes );
|
||||
size_t i;
|
||||
unsigned run;
|
||||
|
||||
TEST_ASSERT( buffer1 != NULL );
|
||||
TEST_ASSERT( buffer2 != NULL );
|
||||
memcpy( buffer1 + bytes, trail, sizeof( trail ) );
|
||||
TEST_ASSERT( output != NULL );
|
||||
TEST_ASSERT( changed != NULL );
|
||||
memcpy( output + bytes, trail, sizeof( trail ) );
|
||||
|
||||
TEST_ASSERT( psa_crypto_init( ) == PSA_SUCCESS );
|
||||
|
||||
TEST_ASSERT( psa_generate_random( buffer1, bytes ) == PSA_SUCCESS );
|
||||
|
||||
/* Check that no more than bytes have been overwritten */
|
||||
TEST_ASSERT( memcmp( buffer1 + bytes, trail, sizeof( trail ) ) == 0 );
|
||||
|
||||
if( bytes == 0 )
|
||||
goto exit;
|
||||
|
||||
/* We can't validate that the data is really random, but we can
|
||||
* validate that it doesn't repeat between calls. There's a
|
||||
* 1/256^bytes chance that it does repeat, of course, so allow
|
||||
* a few retries. */
|
||||
++retries; /* The first time isn't a REtry */
|
||||
do
|
||||
/* Run several times, to ensure that every output byte will be
|
||||
* nonzero at least once with overwhelming probability
|
||||
* (2^(-8*number_of_runs)). */
|
||||
for( run = 0; run < 10; run++ )
|
||||
{
|
||||
--retries;
|
||||
TEST_ASSERT( psa_generate_random( buffer2, bytes ) == PSA_SUCCESS );
|
||||
memset( output, 0, bytes );
|
||||
TEST_ASSERT( psa_generate_random( output, bytes ) == PSA_SUCCESS );
|
||||
|
||||
/* Check that no more than bytes have been overwritten */
|
||||
TEST_ASSERT( memcmp( output + bytes, trail, sizeof( trail ) ) == 0 );
|
||||
|
||||
for( i = 0; i < bytes; i++ )
|
||||
{
|
||||
if( output[i] != 0 )
|
||||
++changed[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* Check that every byte was changed to nonzero at least once. This
|
||||
* validates that psa_generate_random is overwriting every byte of
|
||||
* the output buffer. */
|
||||
for( i = 0; i < bytes; i++ )
|
||||
{
|
||||
TEST_ASSERT( changed[i] != 0 );
|
||||
}
|
||||
while( memcmp( buffer1, buffer2, bytes ) == 0 && retries >= -1 );
|
||||
TEST_ASSERT( retries >= 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_psa_crypto_free( );
|
||||
mbedtls_free( buffer1 );
|
||||
mbedtls_free( buffer2 );
|
||||
mbedtls_free( output );
|
||||
mbedtls_free( changed );
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
|
|
Loading…
Reference in a new issue