Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Example RSA key generation program |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 27 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 28 | #endif |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 32 | #include "polarssl/config.h" |
| 33 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 34 | #include "polarssl/entropy.h" |
| 35 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 36 | #include "polarssl/bignum.h" |
| 37 | #include "polarssl/x509.h" |
| 38 | #include "polarssl/rsa.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 39 | |
| 40 | #define KEY_SIZE 1024 |
| 41 | #define EXPONENT 65537 |
| 42 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 43 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 44 | !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \ |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 45 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 46 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 47 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 48 | ((void) argc); |
| 49 | ((void) argv); |
| 50 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 51 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 52 | "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or " |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 53 | "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 54 | return( 0 ); |
| 55 | } |
| 56 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 57 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | { |
| 59 | int ret; |
| 60 | rsa_context rsa; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 61 | entropy_context entropy; |
| 62 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | FILE *fpub = NULL; |
| 64 | FILE *fpriv = NULL; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 65 | const char *pers = "rsa_genkey"; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 66 | |
| 67 | ((void) argc); |
| 68 | ((void) argv); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
| 70 | printf( "\n . Seeding the random number generator..." ); |
| 71 | fflush( stdout ); |
| 72 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 73 | entropy_init( &entropy ); |
| 74 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 75 | (const unsigned char *) pers, |
| 76 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 77 | { |
| 78 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 79 | goto exit; |
| 80 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 81 | |
| 82 | printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE ); |
| 83 | fflush( stdout ); |
| 84 | |
Paul Bakker | a802e1a | 2010-08-16 11:56:45 +0000 | [diff] [blame] | 85 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 87 | if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE, |
| 88 | EXPONENT ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | { |
| 90 | printf( " failed\n ! rsa_gen_key returned %d\n\n", ret ); |
| 91 | goto exit; |
| 92 | } |
| 93 | |
| 94 | printf( " ok\n . Exporting the public key in rsa_pub.txt...." ); |
| 95 | fflush( stdout ); |
| 96 | |
| 97 | if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL ) |
| 98 | { |
| 99 | printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" ); |
| 100 | ret = 1; |
| 101 | goto exit; |
| 102 | } |
| 103 | |
| 104 | if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 || |
| 105 | ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 ) |
| 106 | { |
| 107 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 108 | goto exit; |
| 109 | } |
| 110 | |
| 111 | printf( " ok\n . Exporting the private key in rsa_priv.txt..." ); |
| 112 | fflush( stdout ); |
| 113 | |
| 114 | if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL ) |
| 115 | { |
| 116 | printf( " failed\n ! could not open rsa_priv.txt for writing\n" ); |
| 117 | ret = 1; |
| 118 | goto exit; |
| 119 | } |
| 120 | |
| 121 | if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 || |
| 122 | ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 || |
| 123 | ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 || |
| 124 | ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 || |
| 125 | ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 || |
| 126 | ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 || |
| 127 | ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 || |
| 128 | ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 ) |
| 129 | { |
| 130 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 131 | goto exit; |
| 132 | } |
| 133 | /* |
| 134 | printf( " ok\n . Generating the certificate..." ); |
| 135 | |
| 136 | x509write_init_raw( &cert ); |
| 137 | x509write_add_pubkey( &cert, &rsa ); |
| 138 | x509write_add_subject( &cert, "CN='localhost'" ); |
| 139 | x509write_add_validity( &cert, "2007-09-06 17:00:32", |
| 140 | "2010-09-06 17:00:32" ); |
| 141 | x509write_create_selfsign( &cert, &rsa ); |
| 142 | x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER ); |
| 143 | x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM ); |
| 144 | x509write_free_raw( &cert ); |
| 145 | */ |
| 146 | printf( " ok\n\n" ); |
| 147 | |
| 148 | exit: |
| 149 | |
| 150 | if( fpub != NULL ) |
| 151 | fclose( fpub ); |
| 152 | |
| 153 | if( fpriv != NULL ) |
| 154 | fclose( fpriv ); |
| 155 | |
| 156 | rsa_free( &rsa ); |
| 157 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 158 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | printf( " Press Enter to exit this program.\n" ); |
| 160 | fflush( stdout ); getchar(); |
| 161 | #endif |
| 162 | |
| 163 | return( ret ); |
| 164 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 165 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C && |
| 166 | POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */ |