blob: 384ae926cf669fce220b0d73f90c40b42539eaeb [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple decryption program
3 *
4 * Copyright (C) 2006-2010, 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 <string.h>
31#include <stdio.h>
32
33#include "polarssl/config.h"
34
35#include "polarssl/rsa.h"
36
37#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
38 !defined(POLARSSL_FS_IO)
39int main( void )
40{
41 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
42 "POLARSSL_FS_IO not defined.\n");
43 return( 0 );
44}
45#else
46int main( int argc, char *argv[] )
47{
48 FILE *f;
49 int ret, c;
50 size_t i;
51 rsa_context rsa;
52 unsigned char result[1024];
53 unsigned char buf[512];
54 ((void) argv);
55
56 ret = 1;
57 if( argc != 1 )
58 {
59 printf( "usage: rsa_decrypt\n" );
60
61#ifdef WIN32
62 printf( "\n" );
63#endif
64
65 goto exit;
66 }
67
Paul Bakkerd246ed32011-10-06 13:18:27 +000068 printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000069 fflush( stdout );
70
Paul Bakkerd246ed32011-10-06 13:18:27 +000071 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000072 {
Paul Bakkerd246ed32011-10-06 13:18:27 +000073 printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000074 " ! Please run rsa_genkey first\n\n" );
75 goto exit;
76 }
77
78 rsa_init( &rsa, RSA_PKCS_V15, 0 );
79
Paul Bakkerd246ed32011-10-06 13:18:27 +000080 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
81 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
82 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
83 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
84 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
85 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
86 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000088 {
89 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
90 goto exit;
91 }
92
93 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
94
95 fclose( f );
96
97 /*
98 * Extract the RSA encrypted value from the text file
99 */
100 ret = 1;
101
102 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
103 {
104 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
105 goto exit;
106 }
107
108 i = 0;
109
110 while( fscanf( f, "%02X", &c ) > 0 &&
111 i < (int) sizeof( buf ) )
112 buf[i++] = (unsigned char) c;
113
114 fclose( f );
115
116 if( i != rsa.len )
117 {
118 printf( "\n ! Invalid RSA signature format\n\n" );
119 goto exit;
120 }
121
122 /*
123 * Decrypt the encrypted RSA data and print the result.
124 */
125 printf( "\n . Decrypting the encrypted data" );
126 fflush( stdout );
127
Paul Bakkerd246ed32011-10-06 13:18:27 +0000128 if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &i, buf, result,
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000129 1024 ) ) != 0 )
130 {
131 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
132 goto exit;
133 }
134
135 printf( "\n . OK\n\n" );
136
137 printf( "The decrypted result is: '%s'\n\n", result );
138
139 ret = 0;
140
141exit:
142
143#ifdef WIN32
144 printf( " + Press Enter to exit this program.\n" );
145 fflush( stdout ); getchar();
146#endif
147
148 return( ret );
149}
150#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */