gen_key.c 12 KB

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