Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * RSA simple decryption program |
| 3 | * |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 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" |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 36 | #include "polarssl/entropy.h" |
| 37 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 38 | |
| 39 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 40 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \ |
| 41 | !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 42 | int main( int argc, char *argv[] ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 43 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 44 | ((void) argc); |
| 45 | ((void) argv); |
| 46 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 47 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 48 | "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or " |
| 49 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
| 53 | int main( int argc, char *argv[] ) |
| 54 | { |
| 55 | FILE *f; |
| 56 | int ret, c; |
| 57 | size_t i; |
| 58 | rsa_context rsa; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 59 | entropy_context entropy; |
| 60 | ctr_drbg_context ctr_drbg; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 61 | unsigned char result[1024]; |
| 62 | unsigned char buf[512]; |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 63 | const char *pers = "rsa_decrypt"; |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 64 | ((void) argv); |
| 65 | |
Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame] | 66 | memset(result, 0, sizeof( result ) ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 67 | ret = 1; |
Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame] | 68 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 69 | if( argc != 1 ) |
| 70 | { |
| 71 | printf( "usage: rsa_decrypt\n" ); |
| 72 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 73 | #if defined(_WIN32) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 74 | printf( "\n" ); |
| 75 | #endif |
| 76 | |
| 77 | goto exit; |
| 78 | } |
| 79 | |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 80 | printf( "\n . Seeding the random number generator..." ); |
| 81 | fflush( stdout ); |
| 82 | |
| 83 | entropy_init( &entropy ); |
| 84 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 85 | (const unsigned char *) pers, |
| 86 | strlen( pers ) ) ) != 0 ) |
| 87 | { |
| 88 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 89 | goto exit; |
| 90 | } |
| 91 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 92 | printf( "\n . Reading private key from rsa_priv.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 93 | fflush( stdout ); |
| 94 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 95 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 96 | { |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 97 | printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 98 | " ! Please run rsa_genkey first\n\n" ); |
| 99 | goto exit; |
| 100 | } |
| 101 | |
| 102 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 103 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 104 | if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 || |
| 105 | ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 || |
| 106 | ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 || |
| 107 | ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 || |
| 108 | ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 || |
| 109 | ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 || |
| 110 | ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 || |
| 111 | ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 112 | { |
| 113 | printf( " failed\n ! mpi_read_file returned %d\n\n", ret ); |
| 114 | goto exit; |
| 115 | } |
| 116 | |
| 117 | rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3; |
| 118 | |
| 119 | fclose( f ); |
| 120 | |
| 121 | /* |
| 122 | * Extract the RSA encrypted value from the text file |
| 123 | */ |
| 124 | ret = 1; |
| 125 | |
| 126 | if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) |
| 127 | { |
| 128 | printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); |
| 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | i = 0; |
| 133 | |
| 134 | while( fscanf( f, "%02X", &c ) > 0 && |
| 135 | i < (int) sizeof( buf ) ) |
| 136 | buf[i++] = (unsigned char) c; |
| 137 | |
| 138 | fclose( f ); |
| 139 | |
| 140 | if( i != rsa.len ) |
| 141 | { |
| 142 | printf( "\n ! Invalid RSA signature format\n\n" ); |
| 143 | goto exit; |
| 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Decrypt the encrypted RSA data and print the result. |
| 148 | */ |
| 149 | printf( "\n . Decrypting the encrypted data" ); |
| 150 | fflush( stdout ); |
| 151 | |
Paul Bakker | 548957d | 2013-08-30 10:30:02 +0200 | [diff] [blame^] | 152 | if( ( ret = rsa_pkcs1_decrypt( &rsa, ctr_drbg_random, &ctr_drbg, |
| 153 | RSA_PRIVATE, &i, buf, result, |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 154 | 1024 ) ) != 0 ) |
| 155 | { |
| 156 | printf( " failed\n ! rsa_pkcs1_decrypt returned %d\n\n", ret ); |
| 157 | goto exit; |
| 158 | } |
| 159 | |
| 160 | printf( "\n . OK\n\n" ); |
| 161 | |
| 162 | printf( "The decrypted result is: '%s'\n\n", result ); |
| 163 | |
| 164 | ret = 0; |
| 165 | |
| 166 | exit: |
| 167 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 168 | #if defined(_WIN32) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 169 | printf( " + Press Enter to exit this program.\n" ); |
| 170 | fflush( stdout ); getchar(); |
| 171 | #endif |
| 172 | |
| 173 | return( ret ); |
| 174 | } |
| 175 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && POLARSSL_FS_IO */ |