bignum: make mpi_init() and mpi_free() accept a single argument
[tropicssl.git] / programs / pkey / rsa_genkey.c
blob6e77b70c50921c049d84cd2728a3e44e6fa8a63f
1 /*
2 * Example RSA key generation program
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
38 #endif
40 #include <stdio.h>
42 #include "tropicssl/havege.h"
43 #include "tropicssl/bignum.h"
44 #include "tropicssl/x509.h"
45 #include "tropicssl/rsa.h"
47 #define KEY_SIZE 1024
48 #define EXPONENT 65537
50 int main(void)
52 int ret;
53 rsa_context rsa;
54 havege_state hs;
55 FILE *fpub = NULL;
56 FILE *fpriv = NULL;
58 x509_raw cert;
61 printf("\n . Seeding the random number generator...");
62 fflush(stdout);
64 havege_init(&hs);
66 printf(" ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
67 fflush(stdout);
69 rsa_init(&rsa, RSA_PKCS_V15, 0, havege_rand, &hs);
71 if ((ret = rsa_gen_key(&rsa, KEY_SIZE, EXPONENT)) != 0) {
72 printf(" failed\n ! rsa_gen_key returned %d\n\n", ret);
73 goto exit;
76 printf(" ok\n . Exporting the public key in rsa_pub.txt....");
77 fflush(stdout);
79 if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
80 printf
81 (" failed\n ! could not open rsa_pub.txt for writing\n\n");
82 ret = 1;
83 goto exit;
86 if ((ret = mpi_write_file("N = ", &rsa.N, 16, fpub)) != 0 ||
87 (ret = mpi_write_file("E = ", &rsa.E, 16, fpub)) != 0) {
88 printf(" failed\n ! mpi_write_file returned %d\n\n", ret);
89 goto exit;
92 printf(" ok\n . Exporting the private key in rsa_priv.txt...");
93 fflush(stdout);
95 if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
96 printf
97 (" failed\n ! could not open rsa_priv.txt for writing\n");
98 ret = 1;
99 goto exit;
102 if ((ret = mpi_write_file("N = ", &rsa.N, 16, fpriv)) != 0 ||
103 (ret = mpi_write_file("E = ", &rsa.E, 16, fpriv)) != 0 ||
104 (ret = mpi_write_file("D = ", &rsa.D, 16, fpriv)) != 0 ||
105 (ret = mpi_write_file("P = ", &rsa.P, 16, fpriv)) != 0 ||
106 (ret = mpi_write_file("Q = ", &rsa.Q, 16, fpriv)) != 0 ||
107 (ret = mpi_write_file("DP = ", &rsa.DP, 16, fpriv)) != 0 ||
108 (ret = mpi_write_file("DQ = ", &rsa.DQ, 16, fpriv)) != 0 ||
109 (ret = mpi_write_file("QP = ", &rsa.QP, 16, fpriv)) != 0) {
110 printf(" failed\n ! mpi_write_file returned %d\n\n", ret);
111 goto exit;
114 printf( " ok\n . Generating the certificate..." );
116 x509write_init_raw( &cert );
117 x509write_add_pubkey( &cert, &rsa );
118 x509write_add_subject( &cert, "CN='localhost'" );
119 x509write_add_validity( &cert, "2007-09-06 17:00:32",
120 "2010-09-06 17:00:32" );
121 x509write_create_selfsign( &cert, &rsa );
122 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
123 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
124 x509write_free_raw( &cert );
126 printf(" ok\n\n");
128 exit:
130 if (fpub != NULL)
131 fclose(fpub);
133 if (fpriv != NULL)
134 fclose(fpriv);
136 rsa_free(&rsa);
138 #ifdef WIN32
139 printf(" Press Enter to exit this program.\n");
140 fflush(stdout);
141 getchar();
142 #endif
144 return (ret);