blob: 4ecb6132951b12db283763eebb52de1880c3aac4 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdio.h>
32
33#include "polarssl/config.h"
34
35#include "polarssl/rsa.h"
Paul Bakker548957d2013-08-30 10:30:02 +020036#include "polarssl/entropy.h"
37#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000038
39#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker548957d2013-08-30 10:30:02 +020040 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
41 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000042int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000043{
Paul Bakkercce9d772011-11-18 14:26:47 +000044 ((void) argc);
45 ((void) argv);
46
Paul Bakker7bc05ff2011-08-09 10:30:36 +000047 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker548957d2013-08-30 10:30:02 +020048 "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
49 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000050 return( 0 );
51}
52#else
53int main( int argc, char *argv[] )
54{
55 FILE *f;
56 int ret, c;
57 size_t i;
58 rsa_context rsa;
Paul Bakker548957d2013-08-30 10:30:02 +020059 entropy_context entropy;
60 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000061 unsigned char result[1024];
62 unsigned char buf[512];
Paul Bakker548957d2013-08-30 10:30:02 +020063 const char *pers = "rsa_decrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000064 ((void) argv);
65
Paul Bakker310c25e2011-12-04 17:06:56 +000066 memset(result, 0, sizeof( result ) );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000067 ret = 1;
Paul Bakker310c25e2011-12-04 17:06:56 +000068
Paul Bakker7bc05ff2011-08-09 10:30:36 +000069 if( argc != 1 )
70 {
71 printf( "usage: rsa_decrypt\n" );
72
Paul Bakkercce9d772011-11-18 14:26:47 +000073#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000074 printf( "\n" );
75#endif
76
77 goto exit;
78 }
79
Paul Bakker548957d2013-08-30 10:30:02 +020080 printf( "\n . Seeding the random number generator..." );
81 fflush( stdout );
82
83 entropy_init( &entropy );
84 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
85 (const unsigned char *) pers,
86 strlen( pers ) ) ) != 0 )
87 {
88 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
89 goto exit;
90 }
91
Paul Bakkerd246ed32011-10-06 13:18:27 +000092 printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000093 fflush( stdout );
94
Paul Bakkerd246ed32011-10-06 13:18:27 +000095 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000096 {
Paul Bakkerd246ed32011-10-06 13:18:27 +000097 printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000098 " ! Please run rsa_genkey first\n\n" );
99 goto exit;
100 }
101
102 rsa_init( &rsa, RSA_PKCS_V15, 0 );
103
Paul Bakkerd246ed32011-10-06 13:18:27 +0000104 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
105 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
106 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
107 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
108 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
109 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
110 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
111 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000112 {
113 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
114 goto exit;
115 }
116
117 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
118
119 fclose( f );
120
121 /*
122 * Extract the RSA encrypted value from the text file
123 */
124 ret = 1;
125
126 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
127 {
128 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
129 goto exit;
130 }
131
132 i = 0;
133
134 while( fscanf( f, "%02X", &c ) > 0 &&
135 i < (int) sizeof( buf ) )
136 buf[i++] = (unsigned char) c;
137
138 fclose( f );
139
140 if( i != rsa.len )
141 {
142 printf( "\n ! Invalid RSA signature format\n\n" );
143 goto exit;
144 }
145
146 /*
147 * Decrypt the encrypted RSA data and print the result.
148 */
149 printf( "\n . Decrypting the encrypted data" );
150 fflush( stdout );
151
Paul Bakker548957d2013-08-30 10:30:02 +0200152 if( ( ret = rsa_pkcs1_decrypt( &rsa, ctr_drbg_random, &ctr_drbg,
153 RSA_PRIVATE, &i, buf, result,
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000154 1024 ) ) != 0 )
155 {
156 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
157 goto exit;
158 }
159
160 printf( "\n . OK\n\n" );
161
162 printf( "The decrypted result is: '%s'\n\n", result );
163
164 ret = 0;
165
166exit:
167
Paul Bakkercce9d772011-11-18 14:26:47 +0000168#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000169 printf( " + Press Enter to exit this program.\n" );
170 fflush( stdout ); getchar();
171#endif
172
173 return( ret );
174}
175#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */