blob: 8df5f0074b6cd1f1335ceb907a07747c62bc730d [file] [log] [blame]
Paul Bakker7bc05ff2011-08-09 10:30:36 +00001/*
2 * RSA simple decryption program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakker7bc05ff2011-08-09 10:30:36 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker7bc05ff2011-08-09 10:30:36 +00007 *
Paul Bakker7bc05ff2011-08-09 10:30:36 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker7bc05ff2011-08-09 10:30:36 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker7bc05ff2011-08-09 10:30:36 +000035#include <string.h>
36#include <stdio.h>
37
Paul Bakker7bc05ff2011-08-09 10:30:36 +000038#include "polarssl/rsa.h"
Paul Bakker548957d2013-08-30 10:30:02 +020039#include "polarssl/entropy.h"
40#include "polarssl/ctr_drbg.h"
Paul Bakker7bc05ff2011-08-09 10:30:36 +000041
42#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \
Paul Bakker548957d2013-08-30 10:30:02 +020043 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
44 !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Rich Evansf90016a2015-01-19 14:26:37 +000050 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or "
Paul Bakker548957d2013-08-30 10:30:02 +020051 "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
52 "POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker7bc05ff2011-08-09 10:30:36 +000053 return( 0 );
54}
55#else
56int main( int argc, char *argv[] )
57{
58 FILE *f;
59 int ret, c;
60 size_t i;
61 rsa_context rsa;
Paul Bakker548957d2013-08-30 10:30:02 +020062 entropy_context entropy;
63 ctr_drbg_context ctr_drbg;
Paul Bakker7bc05ff2011-08-09 10:30:36 +000064 unsigned char result[1024];
65 unsigned char buf[512];
Paul Bakker548957d2013-08-30 10:30:02 +020066 const char *pers = "rsa_decrypt";
Paul Bakker7bc05ff2011-08-09 10:30:36 +000067 ((void) argv);
68
Paul Bakker310c25e2011-12-04 17:06:56 +000069 memset(result, 0, sizeof( result ) );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000070 ret = 1;
Paul Bakker310c25e2011-12-04 17:06:56 +000071
Paul Bakker7bc05ff2011-08-09 10:30:36 +000072 if( argc != 1 )
73 {
Rich Evansf90016a2015-01-19 14:26:37 +000074 polarssl_printf( "usage: rsa_decrypt\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000075
Paul Bakkercce9d772011-11-18 14:26:47 +000076#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000077 polarssl_printf( "\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000078#endif
79
80 goto exit;
81 }
82
Rich Evansf90016a2015-01-19 14:26:37 +000083 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker548957d2013-08-30 10:30:02 +020084 fflush( stdout );
85
86 entropy_init( &entropy );
87 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
88 (const unsigned char *) pers,
89 strlen( pers ) ) ) != 0 )
90 {
Rich Evansf90016a2015-01-19 14:26:37 +000091 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker548957d2013-08-30 10:30:02 +020092 goto exit;
93 }
94
Rich Evansf90016a2015-01-19 14:26:37 +000095 polarssl_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +000096 fflush( stdout );
97
Paul Bakkerd246ed32011-10-06 13:18:27 +000098 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
Paul Bakker7bc05ff2011-08-09 10:30:36 +000099 {
Rich Evansf90016a2015-01-19 14:26:37 +0000100 polarssl_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000101 " ! Please run rsa_genkey first\n\n" );
102 goto exit;
103 }
104
105 rsa_init( &rsa, RSA_PKCS_V15, 0 );
106
Paul Bakkerd246ed32011-10-06 13:18:27 +0000107 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
108 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
109 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
110 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
111 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
112 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
113 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
114 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000115 {
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000117 goto exit;
118 }
119
120 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
121
122 fclose( f );
123
124 /*
125 * Extract the RSA encrypted value from the text file
126 */
127 ret = 1;
128
129 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
130 {
Rich Evansf90016a2015-01-19 14:26:37 +0000131 polarssl_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000132 goto exit;
133 }
134
135 i = 0;
136
137 while( fscanf( f, "%02X", &c ) > 0 &&
138 i < (int) sizeof( buf ) )
139 buf[i++] = (unsigned char) c;
140
141 fclose( f );
142
143 if( i != rsa.len )
144 {
Rich Evansf90016a2015-01-19 14:26:37 +0000145 polarssl_printf( "\n ! Invalid RSA signature format\n\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000146 goto exit;
147 }
148
149 /*
150 * Decrypt the encrypted RSA data and print the result.
151 */
Rich Evansf90016a2015-01-19 14:26:37 +0000152 polarssl_printf( "\n . Decrypting the encrypted data" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000153 fflush( stdout );
154
Paul Bakker548957d2013-08-30 10:30:02 +0200155 if( ( ret = rsa_pkcs1_decrypt( &rsa, ctr_drbg_random, &ctr_drbg,
156 RSA_PRIVATE, &i, buf, result,
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000157 1024 ) ) != 0 )
158 {
Rich Evansf90016a2015-01-19 14:26:37 +0000159 polarssl_printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000160 goto exit;
161 }
162
Rich Evansf90016a2015-01-19 14:26:37 +0000163 polarssl_printf( "\n . OK\n\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000164
Rich Evansf90016a2015-01-19 14:26:37 +0000165 polarssl_printf( "The decrypted result is: '%s'\n\n", result );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000166
167 ret = 0;
168
169exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200170 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200171 entropy_free( &entropy );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000172
Paul Bakkercce9d772011-11-18 14:26:47 +0000173#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000174 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker7bc05ff2011-08-09 10:30:36 +0000175 fflush( stdout ); getchar();
176#endif
177
178 return( ret );
179}
180#endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */