kaa_error_t kaa_init_rsa_keypair(void) { #ifdef KAA_RUNTIME_KEY_GENERATION /* Initialization should be performed only once */ static bool initialized = false; if (!initialized) { if (mbedtls_pk_parse_keyfile(&kaa_pk_context_, KAA_PRIVATE_KEY_STORAGE, NULL)) { if (rsa_genkey(&kaa_pk_context_)) { mbedtls_pk_free(&kaa_pk_context_); return KAA_ERR_BADDATA; } if (write_rsa_key(&kaa_pk_context_, KAA_PRIVATE_KEY_STORAGE, PRIVATE_KEY)) { mbedtls_pk_free(&kaa_pk_context_); return KAA_ERR_BADDATA; } if (write_rsa_key(&kaa_pk_context_, KAA_PUBLIC_KEY_STORAGE, PUBLIC_KEY)) { mbedtls_pk_free(&kaa_pk_context_); return KAA_ERR_BADDATA; } } if (mbedtls_pk_parse_public_keyfile(&pk_pub_context, KAA_PUBLIC_KEY_STORAGE)) { pk_pub_context = kaa_pk_context_; } initialized = true; } #else if (mbedtls_pk_parse_key(&kaa_pk_context_, KAA_RSA_PRIVATE_KEY, KAA_RSA_PRIVATE_KEY_LENGTH, NULL, 0)) { return KAA_ERR_BADDATA; } #endif /* KAA_RUNTIME_KEY_GENERATION */ return KAA_ERR_NONE; }
int pkencrypt_main( int argc, char *argv[] ) { FILE *f; int ret; size_t i, olen = 0; mbedtls_pk_context pk; mbedtls_entropy_context entropy; mbedtls_ctr_drbg_context ctr_drbg; unsigned char input[1024]; unsigned char buf[512]; const char *pers = "mbedtls_pk_encrypt"; ret = 1; mbedtls_ctr_drbg_init( &ctr_drbg ); if( argc != 3 ) { mbedtls_printf( "usage: mbedtls_pk_encrypt <key_file> <string of max 100 characters>\n" ); #if defined(_WIN32) mbedtls_printf( "\n" ); #endif goto exit; } mbedtls_printf( "\n . Seeding the random number generator..." ); fflush( stdout ); mbedtls_entropy_init( &entropy ); 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 ); goto exit; } mbedtls_printf( "\n . Reading public key from '%s'", argv[1] ); fflush( stdout ); mbedtls_pk_init( &pk ); if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); goto exit; } if( strlen( argv[2] ) > 100 ) { mbedtls_printf( " Input data larger than 100 characters.\n\n" ); goto exit; } memcpy( input, argv[2], strlen( argv[2] ) ); /* * Calculate the RSA encryption of the hash. */ mbedtls_printf( "\n . Generating the encrypted value" ); fflush( stdout ); if( ( ret = mbedtls_pk_encrypt( &pk, input, strlen( argv[2] ), buf, &olen, sizeof(buf), mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_encrypt returned -0x%04x\n", -ret ); goto exit; } /* * Write the signature into result-enc.txt */ if( ( f = fopen( "result-enc.txt", "wb+" ) ) == NULL ) { ret = 1; mbedtls_printf( " failed\n ! Could not create %s\n\n", "result-enc.txt" ); goto exit; } for( i = 0; i < olen; i++ ) mbedtls_fprintf( f, "%02X%s", buf[i], ( i + 1 ) % 16 == 0 ? "\r\n" : " " ); fclose( f ); mbedtls_printf( "\n . Done (created \"%s\")\n\n", "result-enc.txt" ); exit: mbedtls_ctr_drbg_free( &ctr_drbg ); mbedtls_entropy_free( &entropy ); #if defined(MBEDTLS_ERROR_C) if( ret != 0 ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); } #endif #if defined(_WIN32) mbedtls_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }
int main( int argc, char *argv[] ) { int ret = 0; mbedtls_pk_context key; char buf[1024]; int i; char *p, *q; /* * Set to sane values */ mbedtls_pk_init( &key ); memset( buf, 0, sizeof( buf ) ); if( argc == 0 ) { usage: ret = 1; mbedtls_printf( USAGE ); goto exit; } opt.mode = DFL_MODE; opt.filename = DFL_FILENAME; opt.output_mode = DFL_OUTPUT_MODE; opt.output_file = DFL_OUTPUT_FILENAME; opt.output_format = DFL_OUTPUT_FORMAT; for( i = 1; i < argc; i++ ) { p = argv[i]; if( ( q = strchr( p, '=' ) ) == NULL ) goto usage; *q++ = '\0'; if( strcmp( p, "mode" ) == 0 ) { if( strcmp( q, "private" ) == 0 ) opt.mode = MODE_PRIVATE; else if( strcmp( q, "public" ) == 0 ) opt.mode = MODE_PUBLIC; else goto usage; } else if( strcmp( p, "output_mode" ) == 0 ) { if( strcmp( q, "private" ) == 0 ) opt.output_mode = OUTPUT_MODE_PRIVATE; else if( strcmp( q, "public" ) == 0 ) opt.output_mode = OUTPUT_MODE_PUBLIC; else goto usage; } else if( strcmp( p, "output_format" ) == 0 ) { #if defined(MBEDTLS_PEM_WRITE_C) if( strcmp( q, "pem" ) == 0 ) opt.output_format = OUTPUT_FORMAT_PEM; else #endif if( strcmp( q, "der" ) == 0 ) opt.output_format = OUTPUT_FORMAT_DER; else goto usage; } else if( strcmp( p, "filename" ) == 0 ) opt.filename = q; else if( strcmp( p, "output_file" ) == 0 ) opt.output_file = q; else goto usage; } if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE ) { mbedtls_printf( "\nCannot output a key without reading one.\n"); goto exit; } if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE ) { mbedtls_printf( "\nCannot output a private key from a public key.\n"); goto exit; } if( opt.mode == MODE_PRIVATE ) { /* * 1.1. Load the key */ mbedtls_printf( "\n . Loading the private key ..." ); fflush( stdout ); ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL ); if( ret != 0 ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } mbedtls_printf( " ok\n" ); /* * 1.2 Print the key */ mbedtls_printf( " . Key information ...\n" ); #if defined(MBEDTLS_RSA_C) if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA ) { mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key ); mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ); mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ); mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL ); mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL ); mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); } else #endif #if defined(MBEDTLS_ECP_C) if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY ) { mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key ); mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ); } else #endif mbedtls_printf("key type not supported yet\n"); } else if( opt.mode == MODE_PUBLIC ) { /* * 1.1. Load the key */ mbedtls_printf( "\n . Loading the public key ..." ); fflush( stdout ); ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename ); if( ret != 0 ) { mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf ); goto exit; } mbedtls_printf( " ok\n" ); /* * 1.2 Print the key */ mbedtls_printf( " . Key information ...\n" ); #if defined(MBEDTLS_RSA_C) if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA ) { mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key ); mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL ); mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL ); } else #endif #if defined(MBEDTLS_ECP_C) if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY ) { mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key ); mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); } else #endif mbedtls_printf("key type not supported yet\n"); } else goto usage; if( opt.output_mode == OUTPUT_MODE_PUBLIC ) { write_public_key( &key, opt.output_file ); } if( opt.output_mode == OUTPUT_MODE_PRIVATE ) { write_private_key( &key, opt.output_file ); } exit: if( ret != 0 && ret != 1) { #ifdef MBEDTLS_ERROR_C mbedtls_strerror( ret, buf, sizeof( buf ) ); mbedtls_printf( " - %s\n", buf ); #else mbedtls_printf("\n"); #endif } mbedtls_pk_free( &key ); #if defined(_WIN32) mbedtls_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }
int main( int argc, char *argv[] ) { FILE *f; int ret = 1; size_t i; mbedtls_pk_context pk; unsigned char hash[20]; unsigned char buf[MBEDTLS_MPI_MAX_SIZE]; char filename[512]; mbedtls_pk_init( &pk ); if( argc != 3 ) { mbedtls_printf( "usage: mbedtls_pk_verify <key_file> <filename>\n" ); #if defined(_WIN32) mbedtls_printf( "\n" ); #endif goto exit; } mbedtls_printf( "\n . Reading public key from '%s'", argv[1] ); fflush( stdout ); if( ( ret = mbedtls_pk_parse_public_keyfile( &pk, argv[1] ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); goto exit; } /* * Extract the signature from the text file */ ret = 1; mbedtls_snprintf( filename, sizeof(filename), "%s.sig", argv[2] ); if( ( f = fopen( filename, "rb" ) ) == NULL ) { mbedtls_printf( "\n ! Could not open %s\n\n", filename ); goto exit; } i = fread( buf, 1, sizeof(buf), f ); fclose( f ); /* * Compute the SHA-256 hash of the input file and compare * it with the hash decrypted from the signature. */ mbedtls_printf( "\n . Verifying the SHA-256 signature" ); fflush( stdout ); if( ( ret = mbedtls_md_file( mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), argv[2], hash ) ) != 0 ) { mbedtls_printf( " failed\n ! Could not open or read %s\n\n", argv[2] ); goto exit; } if( ( ret = mbedtls_pk_verify( &pk, MBEDTLS_MD_SHA256, hash, 0, buf, i ) ) != 0 ) { mbedtls_printf( " failed\n ! mbedtls_pk_verify returned -0x%04x\n", -ret ); goto exit; } mbedtls_printf( "\n . OK (the decrypted SHA-256 hash matches)\n\n" ); ret = 0; exit: mbedtls_pk_free( &pk ); #if defined(MBEDTLS_ERROR_C) mbedtls_strerror( ret, (char *) buf, sizeof(buf) ); mbedtls_printf( " ! Last error was: %s\n", buf ); #endif #if defined(_WIN32) mbedtls_printf( " + Press Enter to exit this program.\n" ); fflush( stdout ); getchar(); #endif return( ret ); }