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" |
| 36 | |
| 37 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 38 | !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 39 | int main( int argc, char *argv[] ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 40 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 41 | ((void) argc); |
| 42 | ((void) argv); |
| 43 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 44 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 45 | "POLARSSL_FS_IO not defined.\n"); |
| 46 | return( 0 ); |
| 47 | } |
| 48 | #else |
| 49 | int 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 Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame^] | 59 | memset(result, 0, sizeof( result ) ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 60 | ret = 1; |
Paul Bakker | 310c25e | 2011-12-04 17:06:56 +0000 | [diff] [blame^] | 61 | |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 62 | if( argc != 1 ) |
| 63 | { |
| 64 | printf( "usage: rsa_decrypt\n" ); |
| 65 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 66 | #if defined(_WIN32) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 67 | printf( "\n" ); |
| 68 | #endif |
| 69 | |
| 70 | goto exit; |
| 71 | } |
| 72 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 73 | printf( "\n . Reading private key from rsa_priv.txt" ); |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 74 | fflush( stdout ); |
| 75 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 76 | if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL ) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 77 | { |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 78 | printf( " failed\n ! Could not open rsa_priv.txt\n" \ |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 79 | " ! Please run rsa_genkey first\n\n" ); |
| 80 | goto exit; |
| 81 | } |
| 82 | |
| 83 | rsa_init( &rsa, RSA_PKCS_V15, 0 ); |
| 84 | |
Paul Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 85 | 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 Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 93 | { |
| 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 Bakker | d246ed3 | 2011-10-06 13:18:27 +0000 | [diff] [blame] | 133 | if( ( ret = rsa_pkcs1_decrypt( &rsa, RSA_PRIVATE, &i, buf, result, |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 134 | 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 | |
| 146 | exit: |
| 147 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 148 | #if defined(_WIN32) |
Paul Bakker | 7bc05ff | 2011-08-09 10:30:36 +0000 | [diff] [blame] | 149 | 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 */ |