blob: d00cdaae79645e714f970b4c7b1c62de2aa8d1f6 [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
68 printf( "\n . Reading public key from rsa_pub.txt" );
69 fflush( stdout );
70
71 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
72 {
73 printf( " failed\n ! Could not open rsa_pub.txt\n" \
74 " ! Please run rsa_genkey first\n\n" );
75 goto exit;
76 }
77
78 rsa_init( &rsa, RSA_PKCS_V15, 0 );
79
80 if( ( ret = mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
81 ( ret = mpi_read_file( &rsa.E, 16, f ) ) != 0 )
82 {
83 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
84 goto exit;
85 }
86
87 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
88
89 fclose( f );
90
91 /*
92 * Extract the RSA encrypted value from the text file
93 */
94 ret = 1;
95
96 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
97 {
98 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
99 goto exit;
100 }
101
102 i = 0;
103
104 while( fscanf( f, "%02X", &c ) > 0 &&
105 i < (int) sizeof( buf ) )
106 buf[i++] = (unsigned char) c;
107
108 fclose( f );
109
110 if( i != rsa.len )
111 {
112 printf( "\n ! Invalid RSA signature format\n\n" );
113 goto exit;
114 }
115
116 /*
117 * Decrypt the encrypted RSA data and print the result.
118 */
119 printf( "\n . Decrypting the encrypted data" );
120 fflush( stdout );
121
122 if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PUBLIC, &i, buf, result,
123 1024 ) ) != 0 )
124 {
125 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
126 goto exit;
127 }
128
129 printf( "\n . OK\n\n" );
130
131 printf( "The decrypted result is: '%s'\n\n", result );
132
133 ret = 0;
134
135exit:
136
137#ifdef WIN32
138 printf( " + Press Enter to exit this program.\n" );
139 fflush( stdout ); getchar();
140#endif
141
142 return( ret );
143}
144#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */