blob: 36ac80953afb1088be98e38e33830d562acff13c [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple decryption program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakker7bc05ff2011-08-09 10:30:36 +00005 *
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
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000027
28#include <string.h>
29#include <stdio.h>
30
Paul Bakker7bc05ff2011-08-09 10:30:36 +000031#include "polarssl/rsa.h"
Paul Bakker548957d2013-08-30 10:30:02 +020032#include "polarssl/entropy.h"
33#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000034
35#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker548957d2013-08-30 10:30:02 +020036 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
37 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000038int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000039{
Paul Bakkercce9d772011-11-18 14:26:47 +000040 ((void) argc);
41 ((void) argv);
42
Paul Bakker7bc05ff2011-08-09 10:30:36 +000043 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker548957d2013-08-30 10:30:02 +020044 "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
45 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000046 return( 0 );
47}
48#else
49int main( int argc, char *argv[] )
50{
51 FILE *f;
52 int ret, c;
53 size_t i;
54 rsa_context rsa;
Paul Bakker548957d2013-08-30 10:30:02 +020055 entropy_context entropy;
56 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000057 unsigned char result[1024];
58 unsigned char buf[512];
Paul Bakker548957d2013-08-30 10:30:02 +020059 const char *pers = "rsa_decrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000060 ((void) argv);
61
Paul Bakker310c25e2011-12-04 17:06:56 +000062 memset(result, 0, sizeof( result ) );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000063 ret = 1;
Paul Bakker310c25e2011-12-04 17:06:56 +000064
Paul Bakker7bc05ff2011-08-09 10:30:36 +000065 if( argc != 1 )
66 {
67 printf( "usage: rsa_decrypt\n" );
68
Paul Bakkercce9d772011-11-18 14:26:47 +000069#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000070 printf( "\n" );
71#endif
72
73 goto exit;
74 }
75
Paul Bakker548957d2013-08-30 10:30:02 +020076 printf( "\n . Seeding the random number generator..." );
77 fflush( stdout );
78
79 entropy_init( &entropy );
80 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
81 (const unsigned char *) pers,
82 strlen( pers ) ) ) != 0 )
83 {
84 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
85 goto exit;
86 }
87
Paul Bakkerd246ed32011-10-06 13:18:27 +000088 printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000089 fflush( stdout );
90
Paul Bakkerd246ed32011-10-06 13:18:27 +000091 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000092 {
Paul Bakkerd246ed32011-10-06 13:18:27 +000093 printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000094 " ! Please run rsa_genkey first\n\n" );
95 goto exit;
96 }
97
98 rsa_init( &rsa, RSA_PKCS_V15, 0 );
99
Paul Bakkerd246ed32011-10-06 13:18:27 +0000100 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
101 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
102 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
103 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
104 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
105 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
106 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
107 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000108 {
109 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
110 goto exit;
111 }
112
113 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
114
115 fclose( f );
116
117 /*
118 * Extract the RSA encrypted value from the text file
119 */
120 ret = 1;
121
122 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
123 {
124 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
125 goto exit;
126 }
127
128 i = 0;
129
130 while( fscanf( f, "%02X", &c ) > 0 &&
131 i < (int) sizeof( buf ) )
132 buf[i++] = (unsigned char) c;
133
134 fclose( f );
135
136 if( i != rsa.len )
137 {
138 printf( "\n ! Invalid RSA signature format\n\n" );
139 goto exit;
140 }
141
142 /*
143 * Decrypt the encrypted RSA data and print the result.
144 */
145 printf( "\n . Decrypting the encrypted data" );
146 fflush( stdout );
147
Paul Bakker548957d2013-08-30 10:30:02 +0200148 if( ( ret = rsa_pkcs1_decrypt( &rsa, ctr_drbg_random, &ctr_drbg,
149 RSA_PRIVATE, &i, buf, result,
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000150 1024 ) ) != 0 )
151 {
152 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
153 goto exit;
154 }
155
156 printf( "\n . OK\n\n" );
157
158 printf( "The decrypted result is: '%s'\n\n", result );
159
160 ret = 0;
161
162exit:
163
Paul Bakkercce9d772011-11-18 14:26:47 +0000164#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000165 printf( " + Press Enter to exit this program.\n" );
166 fflush( stdout ); getchar();
167#endif
168
169 return( ret );
170}
171#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */