Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Example RSA key generation program |
| 3 | * |
| 4 | * Copyright (C) 2006-2007 Christophe Devine |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along |
| 17 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 22 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 23 | #endif |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "xyssl/havege.h" |
| 28 | #include "xyssl/bignum.h" |
| 29 | #include "xyssl/x509.h" |
| 30 | #include "xyssl/rsa.h" |
| 31 | |
| 32 | #define KEY_SIZE 1024 |
| 33 | #define EXPONENT 65537 |
| 34 | |
| 35 | int main( void ) |
| 36 | { |
| 37 | int ret; |
| 38 | rsa_context rsa; |
| 39 | havege_state hs; |
| 40 | FILE *fpub = NULL; |
| 41 | FILE *fpriv = NULL; |
| 42 | x509_raw cert; |
| 43 | |
| 44 | printf( "\n . Seeding the random number generator..." ); |
| 45 | fflush( stdout ); |
| 46 | |
| 47 | havege_init( &hs ); |
| 48 | |
| 49 | printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE ); |
| 50 | fflush( stdout ); |
| 51 | |
| 52 | rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs ); |
| 53 | |
| 54 | if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 ) |
| 55 | { |
| 56 | printf( " failed\n ! rsa_gen_key returned %d\n\n", ret ); |
| 57 | goto exit; |
| 58 | } |
| 59 | |
| 60 | printf( " ok\n . Exporting the public key in rsa_pub.txt...." ); |
| 61 | fflush( stdout ); |
| 62 | |
| 63 | if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL ) |
| 64 | { |
| 65 | printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" ); |
| 66 | ret = 1; |
| 67 | goto exit; |
| 68 | } |
| 69 | |
| 70 | if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 || |
| 71 | ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 ) |
| 72 | { |
| 73 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 74 | goto exit; |
| 75 | } |
| 76 | |
| 77 | printf( " ok\n . Exporting the private key in rsa_priv.txt..." ); |
| 78 | fflush( stdout ); |
| 79 | |
| 80 | if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL ) |
| 81 | { |
| 82 | printf( " failed\n ! could not open rsa_priv.txt for writing\n" ); |
| 83 | ret = 1; |
| 84 | goto exit; |
| 85 | } |
| 86 | |
| 87 | if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 || |
| 88 | ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 || |
| 89 | ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 || |
| 90 | ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 || |
| 91 | ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 || |
| 92 | ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 || |
| 93 | ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 || |
| 94 | ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 ) |
| 95 | { |
| 96 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 97 | goto exit; |
| 98 | } |
| 99 | /* |
| 100 | printf( " ok\n . Generating the certificate..." ); |
| 101 | |
| 102 | x509write_init_raw( &cert ); |
| 103 | x509write_add_pubkey( &cert, &rsa ); |
| 104 | x509write_add_subject( &cert, "CN='localhost'" ); |
| 105 | x509write_add_validity( &cert, "2007-09-06 17:00:32", |
| 106 | "2010-09-06 17:00:32" ); |
| 107 | x509write_create_selfsign( &cert, &rsa ); |
| 108 | x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER ); |
| 109 | x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM ); |
| 110 | x509write_free_raw( &cert ); |
| 111 | */ |
| 112 | printf( " ok\n\n" ); |
| 113 | |
| 114 | exit: |
| 115 | |
| 116 | if( fpub != NULL ) |
| 117 | fclose( fpub ); |
| 118 | |
| 119 | if( fpriv != NULL ) |
| 120 | fclose( fpriv ); |
| 121 | |
| 122 | rsa_free( &rsa ); |
| 123 | |
| 124 | #ifdef WIN32 |
| 125 | printf( " Press Enter to exit this program.\n" ); |
| 126 | fflush( stdout ); getchar(); |
| 127 | #endif |
| 128 | |
| 129 | return( ret ); |
| 130 | } |