#include "mbedtls/config.h"
#include "mbedtls/platform.h"
#include "mbedtls/rsa.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include
unsigned char g_szModulus[] = "C38E2CD8DB8CC677BDDDB150AB34F48E4C0EDCF82E820BCF6D8F49CB"
"7252C1B803414144485B2E7B44DF6D7B27225FE521A9EFB391AA9C57"
"C58659657139858C34A40DCAE7002D5201B79732D5F0E0B1947E98C2"
"C16EC39EE159E577191F87D8C405515A94E7843A7F3E744576C80F68"
"3B8A10D0DDE0BE1EBC793A46D870CA21";
unsigned char g_szIndex[] = "10001";
static int g_iIndex = 12;
int random( void *p_rng, unsigned char *output, size_t output_len)
{
g_iIndex++;
*output = g_iIndex % gettime();
if (*output == 00) {
*output = 48;
}
printf("random %d %02x\n", output_len, *output);
return 0;
}
int MT_rsa_Test()
{
int ret = 1;
int exit_code = MBEDTLS_EXIT_FAILURE;
size_t i;
mbedtls_rsa_context rsa;
//mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
unsigned char input[1024];
unsigned char buf[512];
const char *pers = "rsa_encrypt";
mbedtls_mpi N, E;
printf( "MT_rsa_Test 1\n");
mbedtls_mpi_init( &N ); mbedtls_mpi_init( &E );
mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
//mbedtls_ctr_drbg_init( &ctr_drbg );
//mbedtls_entropy_init( &entropy );
#if 1
//ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
// &entropy, (const unsigned char *) pers,
// strlen( pers ) );
ret = 0;
if( ret != 0 )
{
printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
goto exit;
}
if( ( ret = mbedtls_mpi_read_string( &N, 16, g_szModulus) ) != 0 ||
( ret = mbedtls_mpi_read_string( &E, 16, g_szIndex) ) != 0 )
{
printf( " failed\n ! mbedtls_mpi_read_string returned %d\n\n",
ret );
goto exit;
}
if((ret = mbedtls_rsa_import( &rsa, &N, NULL, NULL, NULL, &E)) != 0)
{
printf( " failed\n ! mbedtls_rsa_import returned %d\n\n", ret);
goto exit;
}
sprintf(input, "1234567890");
/*
* Calculate the RSA encryption of the hash.
*/
printf( "\n . Generating the RSA encrypted value\n");
/*mbedtls_ctr_drbg_random*/
ret = mbedtls_rsa_pkcs1_encrypt( &rsa, random,
&ctr_drbg, MBEDTLS_RSA_PUBLIC,
strlen(input), input, buf );
if( ret != 0 )
{
printf(" failed\n ! mbedtls_rsa_pkcs1_encrypt returned %d\n\n", ret);
goto exit;
}
for( i = 0; i < rsa.len; i++ )
printf("%02X%s", buf[i],
( i + 1 ) % 16 == 0 ? "\r\n" : " " );
printf( "\n . Done\n\n");
exit_code = MBEDTLS_EXIT_SUCCESS;
exit:
mbedtls_mpi_free( &N );
mbedtls_mpi_free( &E );
// mbedtls_ctr_drbg_free( &ctr_drbg );
//mbedtls_entropy_free( &entropy );
mbedtls_rsa_free( &rsa );
#endif
return( exit_code );
}