gen_key.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Key generation application
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_exit exit
  31. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  32. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  33. #endif /* MBEDTLS_PLATFORM_C */
  34. #if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
  35. defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
  36. #include "mbedtls/error.h"
  37. #include "mbedtls/pk.h"
  38. #include "mbedtls/ecdsa.h"
  39. #include "mbedtls/rsa.h"
  40. #include "mbedtls/error.h"
  41. #include "mbedtls/entropy.h"
  42. #include "mbedtls/ctr_drbg.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #if !defined(_WIN32)
  47. #include <unistd.h>
  48. #define DEV_RANDOM_THRESHOLD 32
  49. int dev_random_entropy_poll( void *data, unsigned char *output,
  50. size_t len, size_t *olen )
  51. {
  52. FILE *file;
  53. size_t ret, left = len;
  54. unsigned char *p = output;
  55. ((void) data);
  56. *olen = 0;
  57. file = fopen( "/dev/random", "rb" );
  58. if( file == NULL )
  59. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  60. while( left > 0 )
  61. {
  62. /* /dev/random can return much less than requested. If so, try again */
  63. ret = fread( p, 1, left, file );
  64. if( ret == 0 && ferror( file ) )
  65. {
  66. fclose( file );
  67. return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
  68. }
  69. p += ret;
  70. left -= ret;
  71. sleep( 1 );
  72. }
  73. fclose( file );
  74. *olen = len;
  75. return( 0 );
  76. }
  77. #endif /* !_WIN32 */
  78. #endif
  79. #if defined(MBEDTLS_ECP_C)
  80. #define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
  81. #else
  82. #define DFL_EC_CURVE 0
  83. #endif
  84. #if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
  85. #define USAGE_DEV_RANDOM \
  86. " use_dev_random=0|1 default: 0\n"
  87. #else
  88. #define USAGE_DEV_RANDOM ""
  89. #endif /* !_WIN32 && MBEDTLS_FS_IO */
  90. #define FORMAT_PEM 0
  91. #define FORMAT_DER 1
  92. #define DFL_TYPE MBEDTLS_PK_RSA
  93. #define DFL_RSA_KEYSIZE 4096
  94. #define DFL_FILENAME "keyfile.key"
  95. #define DFL_FORMAT FORMAT_PEM
  96. #define DFL_USE_DEV_RANDOM 0
  97. #define USAGE \
  98. "\n usage: gen_key param=<>...\n" \
  99. "\n acceptable parameters:\n" \
  100. " type=rsa|ec default: rsa\n" \
  101. " rsa_keysize=%%d default: 4096\n" \
  102. " ec_curve=%%s see below\n" \
  103. " filename=%%s default: keyfile.key\n" \
  104. " format=pem|der default: pem\n" \
  105. USAGE_DEV_RANDOM \
  106. "\n"
  107. #if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
  108. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
  109. !defined(MBEDTLS_CTR_DRBG_C)
  110. int main( void )
  111. {
  112. mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
  113. "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
  114. "MBEDTLS_PEM_WRITE_C"
  115. "not defined.\n" );
  116. mbedtls_exit( 0 );
  117. }
  118. #else
  119. /*
  120. * global options
  121. */
  122. struct options
  123. {
  124. int type; /* the type of key to generate */
  125. int rsa_keysize; /* length of key in bits */
  126. int ec_curve; /* curve identifier for EC keys */
  127. const char *filename; /* filename of the key file */
  128. int format; /* the output format to use */
  129. int use_dev_random; /* use /dev/random as entropy source */
  130. } opt;
  131. static int write_private_key( mbedtls_pk_context *key, const char *output_file )
  132. {
  133. int ret;
  134. FILE *f;
  135. unsigned char output_buf[16000];
  136. unsigned char *c = output_buf;
  137. size_t len = 0;
  138. memset(output_buf, 0, 16000);
  139. if( opt.format == FORMAT_PEM )
  140. {
  141. if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
  142. return( ret );
  143. len = strlen( (char *) output_buf );
  144. }
  145. else
  146. {
  147. if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
  148. return( ret );
  149. len = ret;
  150. c = output_buf + sizeof(output_buf) - len;
  151. }
  152. if( ( f = fopen( output_file, "wb" ) ) == NULL )
  153. return( -1 );
  154. if( fwrite( c, 1, len, f ) != len )
  155. {
  156. fclose( f );
  157. return( -1 );
  158. }
  159. fclose( f );
  160. return( 0 );
  161. }
  162. int main( int argc, char *argv[] )
  163. {
  164. int ret = 1;
  165. int exit_code = MBEDTLS_EXIT_FAILURE;
  166. mbedtls_pk_context key;
  167. char buf[1024];
  168. int i;
  169. char *p, *q;
  170. mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
  171. mbedtls_entropy_context entropy;
  172. mbedtls_ctr_drbg_context ctr_drbg;
  173. const char *pers = "gen_key";
  174. #if defined(MBEDTLS_ECP_C)
  175. const mbedtls_ecp_curve_info *curve_info;
  176. #endif
  177. /*
  178. * Set to sane values
  179. */
  180. mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  181. mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
  182. mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
  183. mbedtls_pk_init( &key );
  184. mbedtls_ctr_drbg_init( &ctr_drbg );
  185. memset( buf, 0, sizeof( buf ) );
  186. if( argc == 0 )
  187. {
  188. usage:
  189. mbedtls_printf( USAGE );
  190. #if defined(MBEDTLS_ECP_C)
  191. mbedtls_printf( " available ec_curve values:\n" );
  192. curve_info = mbedtls_ecp_curve_list();
  193. mbedtls_printf( " %s (default)\n", curve_info->name );
  194. while( ( ++curve_info )->name != NULL )
  195. mbedtls_printf( " %s\n", curve_info->name );
  196. #endif /* MBEDTLS_ECP_C */
  197. goto exit;
  198. }
  199. opt.type = DFL_TYPE;
  200. opt.rsa_keysize = DFL_RSA_KEYSIZE;
  201. opt.ec_curve = DFL_EC_CURVE;
  202. opt.filename = DFL_FILENAME;
  203. opt.format = DFL_FORMAT;
  204. opt.use_dev_random = DFL_USE_DEV_RANDOM;
  205. for( i = 1; i < argc; i++ )
  206. {
  207. p = argv[i];
  208. if( ( q = strchr( p, '=' ) ) == NULL )
  209. goto usage;
  210. *q++ = '\0';
  211. if( strcmp( p, "type" ) == 0 )
  212. {
  213. if( strcmp( q, "rsa" ) == 0 )
  214. opt.type = MBEDTLS_PK_RSA;
  215. else if( strcmp( q, "ec" ) == 0 )
  216. opt.type = MBEDTLS_PK_ECKEY;
  217. else
  218. goto usage;
  219. }
  220. else if( strcmp( p, "format" ) == 0 )
  221. {
  222. if( strcmp( q, "pem" ) == 0 )
  223. opt.format = FORMAT_PEM;
  224. else if( strcmp( q, "der" ) == 0 )
  225. opt.format = FORMAT_DER;
  226. else
  227. goto usage;
  228. }
  229. else if( strcmp( p, "rsa_keysize" ) == 0 )
  230. {
  231. opt.rsa_keysize = atoi( q );
  232. if( opt.rsa_keysize < 1024 ||
  233. opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
  234. goto usage;
  235. }
  236. #if defined(MBEDTLS_ECP_C)
  237. else if( strcmp( p, "ec_curve" ) == 0 )
  238. {
  239. if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
  240. goto usage;
  241. opt.ec_curve = curve_info->grp_id;
  242. }
  243. #endif
  244. else if( strcmp( p, "filename" ) == 0 )
  245. opt.filename = q;
  246. else if( strcmp( p, "use_dev_random" ) == 0 )
  247. {
  248. opt.use_dev_random = atoi( q );
  249. if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
  250. goto usage;
  251. }
  252. else
  253. goto usage;
  254. }
  255. mbedtls_printf( "\n . Seeding the random number generator..." );
  256. fflush( stdout );
  257. mbedtls_entropy_init( &entropy );
  258. #if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
  259. if( opt.use_dev_random )
  260. {
  261. if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
  262. NULL, DEV_RANDOM_THRESHOLD,
  263. MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
  264. {
  265. mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", (unsigned int) -ret );
  266. goto exit;
  267. }
  268. mbedtls_printf("\n Using /dev/random, so can take a long time! " );
  269. fflush( stdout );
  270. }
  271. #endif /* !_WIN32 && MBEDTLS_FS_IO */
  272. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  273. (const unsigned char *) pers,
  274. strlen( pers ) ) ) != 0 )
  275. {
  276. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", (unsigned int) -ret );
  277. goto exit;
  278. }
  279. /*
  280. * 1.1. Generate the key
  281. */
  282. mbedtls_printf( "\n . Generating the private key ..." );
  283. fflush( stdout );
  284. if( ( ret = mbedtls_pk_setup( &key,
  285. mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
  286. {
  287. mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret );
  288. goto exit;
  289. }
  290. #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
  291. if( opt.type == MBEDTLS_PK_RSA )
  292. {
  293. ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
  294. opt.rsa_keysize, 65537 );
  295. if( ret != 0 )
  296. {
  297. mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", (unsigned int) -ret );
  298. goto exit;
  299. }
  300. }
  301. else
  302. #endif /* MBEDTLS_RSA_C */
  303. #if defined(MBEDTLS_ECP_C)
  304. if( opt.type == MBEDTLS_PK_ECKEY )
  305. {
  306. ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
  307. mbedtls_pk_ec( key ),
  308. mbedtls_ctr_drbg_random, &ctr_drbg );
  309. if( ret != 0 )
  310. {
  311. mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", (unsigned int) -ret );
  312. goto exit;
  313. }
  314. }
  315. else
  316. #endif /* MBEDTLS_ECP_C */
  317. {
  318. mbedtls_printf( " failed\n ! key type not supported\n" );
  319. goto exit;
  320. }
  321. /*
  322. * 1.2 Print the key
  323. */
  324. mbedtls_printf( " ok\n . Key information:\n" );
  325. #if defined(MBEDTLS_RSA_C)
  326. if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
  327. {
  328. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
  329. if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
  330. ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
  331. {
  332. mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
  333. goto exit;
  334. }
  335. mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
  336. mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
  337. mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
  338. mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
  339. mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
  340. mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
  341. mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
  342. mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
  343. }
  344. else
  345. #endif
  346. #if defined(MBEDTLS_ECP_C)
  347. if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
  348. {
  349. mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
  350. mbedtls_printf( "curve: %s\n",
  351. mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
  352. mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
  353. mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
  354. mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
  355. }
  356. else
  357. #endif
  358. mbedtls_printf(" ! key type not supported\n");
  359. /*
  360. * 1.3 Export key
  361. */
  362. mbedtls_printf( " . Writing key to file..." );
  363. if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
  364. {
  365. mbedtls_printf( " failed\n" );
  366. goto exit;
  367. }
  368. mbedtls_printf( " ok\n" );
  369. exit_code = MBEDTLS_EXIT_SUCCESS;
  370. exit:
  371. if( exit_code != MBEDTLS_EXIT_SUCCESS )
  372. {
  373. #ifdef MBEDTLS_ERROR_C
  374. mbedtls_strerror( ret, buf, sizeof( buf ) );
  375. mbedtls_printf( " - %s\n", buf );
  376. #else
  377. mbedtls_printf("\n");
  378. #endif
  379. }
  380. mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  381. mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
  382. mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
  383. mbedtls_pk_free( &key );
  384. mbedtls_ctr_drbg_free( &ctr_drbg );
  385. mbedtls_entropy_free( &entropy );
  386. #if defined(_WIN32)
  387. mbedtls_printf( " + Press Enter to exit this program.\n" );
  388. fflush( stdout ); getchar();
  389. #endif
  390. mbedtls_exit( exit_code );
  391. }
  392. #endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
  393. * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */