source: trunk/programs/pkey/rsa_genkey.c @ 1143

Revision 1143, 5.1 KB checked in by paul, 6 months ago (diff)
  • Moved all examples programs to use the new entropy and CTR_DRBG
Line 
1/*
2 *  Example RSA key generation program
3 *
4 *  Copyright (C) 2006-2011, Brainspark B.V.
5 *
6 *  This file is part of PolarSSL (http://www.polarssl.org)
7 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 *  All rights reserved.
10 *
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
32#include "polarssl/config.h"
33
34#include "polarssl/entropy.h"
35#include "polarssl/ctr_drbg.h"
36#include "polarssl/bignum.h"
37#include "polarssl/x509.h"
38#include "polarssl/rsa.h"
39
40#define KEY_SIZE 1024
41#define EXPONENT 65537
42
43#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) ||   \
44    !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) ||      \
45    !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
46int main( int argc, char *argv[] )
47{
48    ((void) argc);
49    ((void) argv);
50
51    printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
52           "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
53           "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
54    return( 0 );
55}
56#else
57int main( int argc, char *argv[] )
58{
59    int ret;
60    rsa_context rsa;
61    entropy_context entropy;
62    ctr_drbg_context ctr_drbg;
63    FILE *fpub  = NULL;
64    FILE *fpriv = NULL;
65    char *pers = "rsa_genkey";
66
67    ((void) argc);
68    ((void) argv);
69
70    printf( "\n  . Seeding the random number generator..." );
71    fflush( stdout );
72
73    entropy_init( &entropy );
74    if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
75                               (unsigned char *) pers, strlen( pers ) ) ) != 0 )
76    {
77        printf( " failed\n  ! ctr_drbg_init returned %d\n", ret );
78        goto exit;
79    }
80
81    printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
82    fflush( stdout );
83
84    rsa_init( &rsa, RSA_PKCS_V15, 0 );
85   
86    if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
87                             EXPONENT ) ) != 0 )
88    {
89        printf( " failed\n  ! rsa_gen_key returned %d\n\n", ret );
90        goto exit;
91    }
92
93    printf( " ok\n  . Exporting the public  key in rsa_pub.txt...." );
94    fflush( stdout );
95
96    if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
97    {
98        printf( " failed\n  ! could not open rsa_pub.txt for writing\n\n" );
99        ret = 1;
100        goto exit;
101    }
102
103    if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
104        ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
105    {
106        printf( " failed\n  ! mpi_write_file returned %d\n\n", ret );
107        goto exit;
108    }
109
110    printf( " ok\n  . Exporting the private key in rsa_priv.txt..." );
111    fflush( stdout );
112
113    if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
114    {
115        printf( " failed\n  ! could not open rsa_priv.txt for writing\n" );
116        ret = 1;
117        goto exit;
118    }
119
120    if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
121        ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
122        ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
123        ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
124        ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
125        ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
126        ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
127        ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
128    {
129        printf( " failed\n  ! mpi_write_file returned %d\n\n", ret );
130        goto exit;
131    }
132/*
133    printf( " ok\n  . Generating the certificate..." );
134
135    x509write_init_raw( &cert );
136    x509write_add_pubkey( &cert, &rsa );
137    x509write_add_subject( &cert, "CN='localhost'" );
138    x509write_add_validity( &cert, "2007-09-06 17:00:32",
139                                   "2010-09-06 17:00:32" );
140    x509write_create_selfsign( &cert, &rsa );
141    x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
142    x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
143    x509write_free_raw( &cert );
144*/
145    printf( " ok\n\n" );
146
147exit:
148
149    if( fpub  != NULL )
150        fclose( fpub );
151
152    if( fpriv != NULL )
153        fclose( fpriv );
154
155    rsa_free( &rsa );
156
157#if defined(_WIN32)
158    printf( "  Press Enter to exit this program.\n" );
159    fflush( stdout ); getchar();
160#endif
161
162    return( ret );
163}
164#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
165          POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */
Note: See TracBrowser for help on using the repository browser.

What are you looking for?