Change examples to use the new MD API and check ret code
This commit is contained in:
parent
d21d625e1f
commit
1ff60f437f
@ -29,7 +29,9 @@
|
|||||||
#include "mbedtls/platform.h"
|
#include "mbedtls/platform.h"
|
||||||
#else
|
#else
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#define mbedtls_printf printf
|
#define mbedtls_printf printf
|
||||||
|
#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
|
||||||
|
#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_MD5_C)
|
#if defined(MBEDTLS_MD5_C)
|
||||||
@ -45,13 +47,14 @@ int main( void )
|
|||||||
#else
|
#else
|
||||||
int main( void )
|
int main( void )
|
||||||
{
|
{
|
||||||
int i;
|
int i, ret;
|
||||||
unsigned char digest[16];
|
unsigned char digest[16];
|
||||||
char str[] = "Hello, world!";
|
char str[] = "Hello, world!";
|
||||||
|
|
||||||
mbedtls_printf( "\n MD5('%s') = ", str );
|
mbedtls_printf( "\n MD5('%s') = ", str );
|
||||||
|
|
||||||
mbedtls_md5( (unsigned char *) str, 13, digest );
|
if( ( ret = mbedtls_md5_ext( (unsigned char *) str, 13, digest ) ) != 0 )
|
||||||
|
return( MBEDTLS_EXIT_FAILURE );
|
||||||
|
|
||||||
for( i = 0; i < 16; i++ )
|
for( i = 0; i < 16; i++ )
|
||||||
mbedtls_printf( "%02x", digest[i] );
|
mbedtls_printf( "%02x", digest[i] );
|
||||||
@ -63,6 +66,6 @@ int main( void )
|
|||||||
fflush( stdout ); getchar();
|
fflush( stdout ); getchar();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return( 0 );
|
return( MBEDTLS_EXIT_SUCCESS );
|
||||||
}
|
}
|
||||||
#endif /* MBEDTLS_MD5_C */
|
#endif /* MBEDTLS_MD5_C */
|
||||||
|
@ -212,7 +212,11 @@ int main( void )
|
|||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
mbedtls_sha1( buf, (int)( p - 2 - buf ), hash );
|
if( ( ret = mbedtls_sha1_ext( buf, (int)( p - 2 - buf ), hash ) ) != 0 )
|
||||||
|
{
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_sha1_ext returned %d\n\n", ret );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
|
if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
|
||||||
MBEDTLS_MD_SHA256, 0, hash, p ) ) != 0 )
|
MBEDTLS_MD_SHA256, 0, hash, p ) ) != 0 )
|
||||||
|
@ -203,7 +203,11 @@ int main( void )
|
|||||||
/*
|
/*
|
||||||
* 5. Sign the parameters and send them
|
* 5. Sign the parameters and send them
|
||||||
*/
|
*/
|
||||||
mbedtls_sha1( buf, n, hash );
|
if( ( ret = mbedtls_sha1_ext( buf, n, hash ) ) != 0 )
|
||||||
|
{
|
||||||
|
mbedtls_printf( " failed\n ! mbedtls_sha1_ext returned %d\n\n", ret );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
buf[n ] = (unsigned char)( rsa.len >> 8 );
|
buf[n ] = (unsigned char)( rsa.len >> 8 );
|
||||||
buf[n + 1] = (unsigned char)( rsa.len );
|
buf[n + 1] = (unsigned char)( rsa.len );
|
||||||
|
@ -102,7 +102,6 @@ int main( int argc, char *argv[] )
|
|||||||
mbedtls_ecdsa_context ctx_sign, ctx_verify;
|
mbedtls_ecdsa_context ctx_sign, ctx_verify;
|
||||||
mbedtls_entropy_context entropy;
|
mbedtls_entropy_context entropy;
|
||||||
mbedtls_ctr_drbg_context ctr_drbg;
|
mbedtls_ctr_drbg_context ctr_drbg;
|
||||||
mbedtls_sha256_context sha256_ctx;
|
|
||||||
unsigned char message[100];
|
unsigned char message[100];
|
||||||
unsigned char hash[32];
|
unsigned char hash[32];
|
||||||
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
unsigned char sig[MBEDTLS_ECDSA_MAX_LEN];
|
||||||
@ -113,7 +112,6 @@ int main( int argc, char *argv[] )
|
|||||||
mbedtls_ecdsa_init( &ctx_sign );
|
mbedtls_ecdsa_init( &ctx_sign );
|
||||||
mbedtls_ecdsa_init( &ctx_verify );
|
mbedtls_ecdsa_init( &ctx_verify );
|
||||||
mbedtls_ctr_drbg_init( &ctr_drbg );
|
mbedtls_ctr_drbg_init( &ctr_drbg );
|
||||||
mbedtls_sha256_init( &sha256_ctx );
|
|
||||||
|
|
||||||
memset( sig, 0, sizeof( sig ) );
|
memset( sig, 0, sizeof( sig ) );
|
||||||
memset( message, 0x25, sizeof( message ) );
|
memset( message, 0x25, sizeof( message ) );
|
||||||
@ -165,9 +163,11 @@ int main( int argc, char *argv[] )
|
|||||||
mbedtls_printf( " . Computing message hash..." );
|
mbedtls_printf( " . Computing message hash..." );
|
||||||
fflush( stdout );
|
fflush( stdout );
|
||||||
|
|
||||||
mbedtls_sha256_starts( &sha256_ctx, 0 );
|
if( ( ret = mbedtls_sha256_ext( message, sizeof( message ), hash, 0 ) ) != 0 )
|
||||||
mbedtls_sha256_update( &sha256_ctx, message, sizeof( message ) );
|
{
|
||||||
mbedtls_sha256_finish( &sha256_ctx, hash );
|
mbedtls_printf( " failed\n ! mbedtls_sha256_ext returned %d\n", ret );
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
mbedtls_printf( " ok\n" );
|
mbedtls_printf( " ok\n" );
|
||||||
|
|
||||||
@ -242,7 +242,6 @@ exit:
|
|||||||
mbedtls_ecdsa_free( &ctx_sign );
|
mbedtls_ecdsa_free( &ctx_sign );
|
||||||
mbedtls_ctr_drbg_free( &ctr_drbg );
|
mbedtls_ctr_drbg_free( &ctr_drbg );
|
||||||
mbedtls_entropy_free( &entropy );
|
mbedtls_entropy_free( &entropy );
|
||||||
mbedtls_sha256_free( &sha256_ctx );
|
|
||||||
|
|
||||||
return( ret );
|
return( ret );
|
||||||
}
|
}
|
||||||
|
@ -327,32 +327,32 @@ int main( int argc, char *argv[] )
|
|||||||
|
|
||||||
#if defined(MBEDTLS_MD4_C)
|
#if defined(MBEDTLS_MD4_C)
|
||||||
if( todo.md4 )
|
if( todo.md4 )
|
||||||
TIME_AND_TSC( "MD4", mbedtls_md4( buf, BUFSIZE, tmp ) );
|
TIME_AND_TSC( "MD4", mbedtls_md4_ext( buf, BUFSIZE, tmp ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_MD5_C)
|
#if defined(MBEDTLS_MD5_C)
|
||||||
if( todo.md5 )
|
if( todo.md5 )
|
||||||
TIME_AND_TSC( "MD5", mbedtls_md5( buf, BUFSIZE, tmp ) );
|
TIME_AND_TSC( "MD5", mbedtls_md5_ext( buf, BUFSIZE, tmp ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_RIPEMD160_C)
|
#if defined(MBEDTLS_RIPEMD160_C)
|
||||||
if( todo.ripemd160 )
|
if( todo.ripemd160 )
|
||||||
TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160( buf, BUFSIZE, tmp ) );
|
TIME_AND_TSC( "RIPEMD160", mbedtls_ripemd160_ext( buf, BUFSIZE, tmp ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_SHA1_C)
|
#if defined(MBEDTLS_SHA1_C)
|
||||||
if( todo.sha1 )
|
if( todo.sha1 )
|
||||||
TIME_AND_TSC( "SHA-1", mbedtls_sha1( buf, BUFSIZE, tmp ) );
|
TIME_AND_TSC( "SHA-1", mbedtls_sha1_ext( buf, BUFSIZE, tmp ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_SHA256_C)
|
#if defined(MBEDTLS_SHA256_C)
|
||||||
if( todo.sha256 )
|
if( todo.sha256 )
|
||||||
TIME_AND_TSC( "SHA-256", mbedtls_sha256( buf, BUFSIZE, tmp, 0 ) );
|
TIME_AND_TSC( "SHA-256", mbedtls_sha256_ext( buf, BUFSIZE, tmp, 0 ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_SHA512_C)
|
#if defined(MBEDTLS_SHA512_C)
|
||||||
if( todo.sha512 )
|
if( todo.sha512 )
|
||||||
TIME_AND_TSC( "SHA-512", mbedtls_sha512( buf, BUFSIZE, tmp, 0 ) );
|
TIME_AND_TSC( "SHA-512", mbedtls_sha512_ext( buf, BUFSIZE, tmp, 0 ) );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_ARC4_C)
|
#if defined(MBEDTLS_ARC4_C)
|
||||||
|
Loading…
Reference in New Issue
Block a user