gen_key.c 13 KB

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