gen_key.c 14 KB

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