Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based simple decryption program |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, 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/error.h" |
| 36 | #include "polarssl/pk.h" |
| 37 | #include "polarssl/entropy.h" |
| 38 | #include "polarssl/ctr_drbg.h" |
| 39 | |
| 40 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \ |
| 41 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \ |
| 42 | !defined(POLARSSL_CTR_DRBG_C) |
| 43 | int main( int argc, char *argv[] ) |
| 44 | { |
| 45 | ((void) argc); |
| 46 | ((void) argv); |
| 47 | |
| 48 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or " |
| 49 | "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or " |
| 50 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 51 | return( 0 ); |
| 52 | } |
| 53 | #else |
| 54 | int main( int argc, char *argv[] ) |
| 55 | { |
| 56 | FILE *f; |
| 57 | int ret, c; |
| 58 | size_t i, olen = 0; |
| 59 | pk_context pk; |
| 60 | entropy_context entropy; |
| 61 | ctr_drbg_context ctr_drbg; |
| 62 | unsigned char result[1024]; |
| 63 | unsigned char buf[512]; |
| 64 | const char *pers = "pk_decrypt"; |
| 65 | ((void) argv); |
| 66 | |
| 67 | memset(result, 0, sizeof( result ) ); |
| 68 | ret = 1; |
| 69 | |
| 70 | if( argc != 2 ) |
| 71 | { |
| 72 | printf( "usage: pk_decrypt <key_file>\n" ); |
| 73 | |
| 74 | #if defined(_WIN32) |
| 75 | printf( "\n" ); |
| 76 | #endif |
| 77 | |
| 78 | goto exit; |
| 79 | } |
| 80 | |
| 81 | printf( "\n . Seeding the random number generator..." ); |
| 82 | fflush( stdout ); |
| 83 | |
| 84 | entropy_init( &entropy ); |
| 85 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 86 | (const unsigned char *) pers, |
| 87 | strlen( pers ) ) ) != 0 ) |
| 88 | { |
| 89 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 90 | goto exit; |
| 91 | } |
| 92 | |
| 93 | printf( "\n . Reading private key from '%s'", argv[1] ); |
| 94 | fflush( stdout ); |
| 95 | |
| 96 | pk_init( &pk ); |
| 97 | |
| 98 | if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 ) |
| 99 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame^] | 100 | printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 101 | goto exit; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Extract the RSA encrypted value from the text file |
| 106 | */ |
| 107 | ret = 1; |
| 108 | |
| 109 | if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) |
| 110 | { |
| 111 | printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); |
| 112 | goto exit; |
| 113 | } |
| 114 | |
| 115 | i = 0; |
| 116 | |
| 117 | while( fscanf( f, "%02X", &c ) > 0 && |
| 118 | i < (int) sizeof( buf ) ) |
| 119 | buf[i++] = (unsigned char) c; |
| 120 | |
| 121 | fclose( f ); |
| 122 | |
| 123 | /* |
| 124 | * Decrypt the encrypted RSA data and print the result. |
| 125 | */ |
| 126 | printf( "\n . Decrypting the encrypted data" ); |
| 127 | fflush( stdout ); |
| 128 | |
| 129 | if( ( ret = pk_decrypt( &pk, buf, i, result, &olen, sizeof(result), |
| 130 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
| 131 | { |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame^] | 132 | printf( " failed\n ! pk_decrypt returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 133 | goto exit; |
| 134 | } |
| 135 | |
| 136 | printf( "\n . OK\n\n" ); |
| 137 | |
| 138 | printf( "The decrypted result is: '%s'\n\n", result ); |
| 139 | |
| 140 | ret = 0; |
| 141 | |
| 142 | exit: |
| 143 | |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame^] | 144 | #if defined(POLARSSL_ERROR_C) |
| 145 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
| 146 | printf( " ! Last error was: %s\n", buf ); |
| 147 | #endif |
| 148 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 149 | #if defined(_WIN32) |
| 150 | printf( " + Press Enter to exit this program.\n" ); |
| 151 | fflush( stdout ); getchar(); |
| 152 | #endif |
| 153 | |
| 154 | return( ret ); |
| 155 | } |
| 156 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO && |
| 157 | POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */ |