blob: 0b168f4425720e716843ce2082dce4caf10a295a [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"
36
37#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
38 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000039int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000040{
Paul Bakkercce9d772011-11-18 14:26:47 +000041 ((void) argc);
42 ((void) argv);
43
Paul Bakker7bc05ff2011-08-09 10:30:36 +000044 printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
45 "POLARSSL_FS_IO not defined.\n");
46 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;
55 unsigned char result[1024];
56 unsigned char buf[512];
57 ((void) argv);
58
Paul Bakker310c25e2011-12-04 17:06:56 +000059 memset(result, 0, sizeof( result ) );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000060 ret = 1;
Paul Bakker310c25e2011-12-04 17:06:56 +000061
Paul Bakker7bc05ff2011-08-09 10:30:36 +000062 if( argc != 1 )
63 {
64 printf( "usage: rsa_decrypt\n" );
65
Paul Bakkercce9d772011-11-18 14:26:47 +000066#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000067 printf( "\n" );
68#endif
69
70 goto exit;
71 }
72
Paul Bakkerd246ed32011-10-06 13:18:27 +000073 printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000074 fflush( stdout );
75
Paul Bakkerd246ed32011-10-06 13:18:27 +000076 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000077 {
Paul Bakkerd246ed32011-10-06 13:18:27 +000078 printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000079 " ! Please run rsa_genkey first\n\n" );
80 goto exit;
81 }
82
83 rsa_init( &rsa, RSA_PKCS_V15, 0 );
84
Paul Bakkerd246ed32011-10-06 13:18:27 +000085 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
86 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
88 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
89 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
90 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
91 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
92 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000093 {
94 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
95 goto exit;
96 }
97
98 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
99
100 fclose( f );
101
102 /*
103 * Extract the RSA encrypted value from the text file
104 */
105 ret = 1;
106
107 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
108 {
109 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
110 goto exit;
111 }
112
113 i = 0;
114
115 while( fscanf( f, "%02X", &c ) > 0 &&
116 i < (int) sizeof( buf ) )
117 buf[i++] = (unsigned char) c;
118
119 fclose( f );
120
121 if( i != rsa.len )
122 {
123 printf( "\n ! Invalid RSA signature format\n\n" );
124 goto exit;
125 }
126
127 /*
128 * Decrypt the encrypted RSA data and print the result.
129 */
130 printf( "\n . Decrypting the encrypted data" );
131 fflush( stdout );
132
Paul Bakkerd246ed32011-10-06 13:18:27 +0000133 if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &i, buf, result,
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000134 1024 ) ) != 0 )
135 {
136 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
137 goto exit;
138 }
139
140 printf( "\n . OK\n\n" );
141
142 printf( "The decrypted result is: '%s'\n\n", result );
143
144 ret = 0;
145
146exit:
147
Paul Bakkercce9d772011-11-18 14:26:47 +0000148#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000149 printf( " + Press Enter to exit this program.\n" );
150 fflush( stdout ); getchar();
151#endif
152
153 return( ret );
154}
155#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */