blob: 9e594f82f94c8329ea1a295a1c20096b7afe1481 [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
59 ret = 1;
60 if( argc != 1 )
61 {
62 printf( "usage: rsa_decrypt\n" );
63
Paul Bakkercce9d772011-11-18 14:26:47 +000064#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +000065 printf( "\n" );
66#endif
67
68 goto exit;
69 }
70
Paul Bakkerd246ed32011-10-06 13:18:27 +000071 printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000072 fflush( stdout );
73
Paul Bakkerd246ed32011-10-06 13:18:27 +000074 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000075 {
Paul Bakkerd246ed32011-10-06 13:18:27 +000076 printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +000077 " ! Please run rsa_genkey first\n\n" );
78 goto exit;
79 }
80
81 rsa_init( &rsa, RSA_PKCS_V15, 0 );
82
Paul Bakkerd246ed32011-10-06 13:18:27 +000083 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
84 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
85 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
86 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
87 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
88 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
89 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
90 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000091 {
92 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
93 goto exit;
94 }
95
96 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
97
98 fclose( f );
99
100 /*
101 * Extract the RSA encrypted value from the text file
102 */
103 ret = 1;
104
105 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
106 {
107 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
108 goto exit;
109 }
110
111 i = 0;
112
113 while( fscanf( f, "%02X", &c ) > 0 &&
114 i < (int) sizeof( buf ) )
115 buf[i++] = (unsigned char) c;
116
117 fclose( f );
118
119 if( i != rsa.len )
120 {
121 printf( "\n ! Invalid RSA signature format\n\n" );
122 goto exit;
123 }
124
125 /*
126 * Decrypt the encrypted RSA data and print the result.
127 */
128 printf( "\n . Decrypting the encrypted data" );
129 fflush( stdout );
130
Paul Bakkerd246ed32011-10-06 13:18:27 +0000131 if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &i, buf, result,
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000132 1024 ) ) != 0 )
133 {
134 printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
135 goto exit;
136 }
137
138 printf( "\n . OK\n\n" );
139
140 printf( "The decrypted result is: '%s'\n\n", result );
141
142 ret = 0;
143
144exit:
145
Paul Bakkercce9d772011-11-18 14:26:47 +0000146#if defined(_WIN32)
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000147 printf( " + Press Enter to exit this program.\n" );
148 fflush( stdout ); getchar();
149#endif
150
151 return( ret );
152}
153#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */