/* * Generate an RSA keypair */ int rsa_gen_key( rsa_context *ctx, int nbits, int exponent, ulong (*rng_fn)(void *), void *rng_st ) { int ret; mpi P1, Q1, H, G; mpi_init( &P1, &Q1, &H, &G, NULL ); memset( ctx, 0, sizeof( rsa_context ) ); /* * find primes P and Q with Q < P so that: * GCD( E, (P-1)*(Q-1) ) == 1 */ CHK( mpi_lset( &ctx->E, exponent ) ); nbits >>= 1; do { CHK( mpi_gen_prime( &ctx->P, nbits, 0, rng_fn, rng_st ) ); CHK( mpi_gen_prime( &ctx->Q, nbits, 0, rng_fn, rng_st ) ); if( mpi_cmp_mpi( &ctx->P, &ctx->Q ) < 0 ) mpi_swap( &ctx->P, &ctx->Q ); CHK( mpi_mul_mpi( &ctx->N, &ctx->P, &ctx->Q ) ); CHK( mpi_sub_int( &P1, &ctx->P, 1 ) ); CHK( mpi_sub_int( &Q1, &ctx->Q, 1 ) ); CHK( mpi_mul_mpi( &H, &P1, &Q1 ) ); CHK( mpi_gcd( &G, &ctx->E, &H ) ); } while( mpi_cmp_int( &G, 1 ) != 0 ); /* * D = E^-1 mod ((P-1)*(Q-1)) * DP = D mod (P - 1) * DQ = D mod (Q - 1) * QP = Q^-1 mod P */ CHK( mpi_inv_mod( &ctx->D , &ctx->E, &H ) ); CHK( mpi_mod_mpi( &ctx->DP, &ctx->D, &P1 ) ); CHK( mpi_mod_mpi( &ctx->DQ, &ctx->D, &Q1 ) ); CHK( mpi_inv_mod( &ctx->QP, &ctx->Q, &ctx->P ) ); ctx->len = ( mpi_size( &ctx->N ) + 7 ) >> 3; cleanup: mpi_free( &P1, &Q1, &H, &G, NULL ); if( ret != 0 ) { rsa_free( ctx ); return( ERR_RSA_KEYGEN_FAILED | ret ); } return( 0 ); }
void test_prime_num_gen() { mpi p; mpi_init(&p); clock_t start, end; float diff; srand(time(0)); for (size_t i = 256; i <= 2048; i <<= 1) { start = clock(); mpi_gen_prime(&p, i, 0, myrand, NULL); end = clock(); diff = ((float)(end - start)) / CLOCKS_PER_SEC; printf("%d-bit prime number generation time: %f seconds\n", i, diff); } mpi_free(&p); }
int main( int argc, char *argv[] ) { int ret = 1; mpi G, P, Q; entropy_context entropy; ctr_drbg_context ctr_drbg; const char *pers = "dh_genprime"; FILE *fout; ((void) argc); ((void) argv); mpi_init( &G ); mpi_init( &P ); mpi_init( &Q ); entropy_init( &entropy ); if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 ) { polarssl_printf( " failed\n ! mpi_read_string returned %d\n", ret ); goto exit; } polarssl_printf( "\nWARNING: You should not generate and use your own DHM primes\n" ); polarssl_printf( " unless you are very certain of what you are doing!\n" ); polarssl_printf( " Failing to follow this instruction may result in\n" ); polarssl_printf( " weak security for your connections! Use the\n" ); polarssl_printf( " predefined DHM parameters from dhm.h instead!\n\n" ); polarssl_printf( "============================================================\n\n" ); polarssl_printf( " ! Generating large primes may take minutes!\n" ); polarssl_printf( "\n . Seeding the random number generator..." ); fflush( stdout ); if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) pers, strlen( pers ) ) ) != 0 ) { polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); goto exit; } polarssl_printf( " ok\n . Generating the modulus, please wait..." ); fflush( stdout ); /* * This can take a long time... */ if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1, ctr_drbg_random, &ctr_drbg ) ) != 0 ) { polarssl_printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret ); goto exit; } polarssl_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); fflush( stdout ); if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) { polarssl_printf( " failed\n ! mpi_sub_int returned %d\n\n", ret ); goto exit; } if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) { polarssl_printf( " failed\n ! mpi_div_int returned %d\n\n", ret ); goto exit; } if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 ) { polarssl_printf( " failed\n ! mpi_is_prime returned %d\n\n", ret ); goto exit; } polarssl_printf( " ok\n . Exporting the value in dh_prime.txt..." ); fflush( stdout ); if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) { ret = 1; polarssl_printf( " failed\n ! Could not create dh_prime.txt\n\n" ); goto exit; } if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) { polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); goto exit; } polarssl_printf( " ok\n\n" ); fclose( fout ); exit: mpi_free( &G ); mpi_free( &P ); mpi_free( &Q ); ctr_drbg_free( &ctr_drbg ); entropy_free( &entropy ); #if defined(_WIN32) polarssl_printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }
int main( void ) { int ret = 1; #if defined(XYSSL_GENPRIME) mpi G, P, Q; havege_state hs; FILE *fout; mpi_init( &G, &P, &Q, NULL ); mpi_read_string( &G, 10, GENERATOR ); printf( "\n . Seeding the random number generator..." ); fflush( stdout ); havege_init( &hs ); printf( " ok\n . Generating the modulus, please wait..." ); fflush( stdout ); /* * This can take a long time... */ if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1, havege_rand, &hs ) ) != 0 ) { printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret ); goto exit; } printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); fflush( stdout ); if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) { printf( " failed\n ! mpi_sub_int returned %d\n\n", ret ); goto exit; } if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) { printf( " failed\n ! mpi_div_int returned %d\n\n", ret ); goto exit; } if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 ) { printf( " failed\n ! mpi_is_prime returned %d\n\n", ret ); goto exit; } printf( " ok\n . Exporting the value in dh_prime.txt..." ); fflush( stdout ); if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) { ret = 1; printf( " failed\n ! Could not create dh_prime.txt\n\n" ); goto exit; } if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) { printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); goto exit; } printf( " ok\n\n" ); fclose( fout ); exit: mpi_free( &Q, &P, &G, NULL ); #else printf( "\n ! Prime-number generation is not available.\n\n" ); #endif #ifdef WIN32 printf( " Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }