Minor formatting improvements in pk_encrypt and pk_decrypt examples

This commit is contained in:
Hanno Becker 2018-08-23 14:39:04 +01:00
parent a70fb95c82
commit ae513a5396
2 changed files with 22 additions and 14 deletions

View File

@ -93,11 +93,12 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Seeding the random number generator..." );
fflush( stdout );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n",
ret );
goto exit;
}
@ -121,10 +122,11 @@ int main( int argc, char *argv[] )
}
i = 0;
while( fscanf( f, "%02X", &c ) > 0 &&
i < (int) sizeof( buf ) )
{
buf[i++] = (unsigned char) c;
}
fclose( f );
@ -137,7 +139,8 @@ int main( int argc, char *argv[] )
if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n", -ret );
mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
-ret );
goto exit;
}
@ -156,7 +159,7 @@ exit:
#if defined(MBEDTLS_ERROR_C)
if( exit_code != MBEDTLS_EXIT_SUCCESS )
{
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
mbedtls_printf( " ! Last error was: %s\n", buf );
}
#endif

View File

@ -90,11 +90,12 @@ int main( int argc, char *argv[] )
mbedtls_printf( "\n . Seeding the random number generator..." );
fflush( stdout );
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
(const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
&entropy, (const unsigned char *) pers,
strlen( pers ) ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
-ret );
goto exit;
}
@ -125,7 +126,8 @@ int main( int argc, char *argv[] )
buf, &olen, sizeof(buf),
mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret );
mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n",
-ret );
goto exit;
}
@ -134,14 +136,17 @@ int main( int argc, char *argv[] )
*/
if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL )
{
mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" );
mbedtls_printf( " failed\n ! Could not create %s\n\n",
"result-enc.txt" );
ret = 1;
goto exit;
}
for( i = 0; i < olen; i++ )
{
mbedtls_fprintf( f, "%02X%s", buf[i],
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
}
fclose( f );
@ -158,7 +163,7 @@ exit:
#if defined(MBEDTLS_ERROR_C)
if( exit_code != MBEDTLS_EXIT_SUCCESS )
{
mbedtls_strerror( ret, (char *) buf, sizeof(buf) );
mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
mbedtls_printf( " ! Last error was: %s\n", buf );
}
#endif