Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public key-based simple decryption program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 7 | * |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 8 | * 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 36 | #if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_PK_PARSE_C) && \ |
| 37 | defined(POLARSSL_FS_IO) && defined(POLARSSL_ENTROPY_C) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 38 | defined(POLARSSL_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 39 | #include "mbedtls/error.h" |
| 40 | #include "mbedtls/pk.h" |
| 41 | #include "mbedtls/entropy.h" |
| 42 | #include "mbedtls/ctr_drbg.h" |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 43 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include <string.h> |
| 46 | #endif |
| 47 | |
| 48 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 49 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \ |
| 50 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \ |
| 51 | !defined(POLARSSL_CTR_DRBG_C) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 52 | int main( void ) |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 53 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 54 | polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or " |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 55 | "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or " |
| 56 | "POLARSSL_CTR_DRBG_C not defined.\n"); |
| 57 | return( 0 ); |
| 58 | } |
| 59 | #else |
| 60 | int main( int argc, char *argv[] ) |
| 61 | { |
| 62 | FILE *f; |
| 63 | int ret, c; |
| 64 | size_t i, olen = 0; |
| 65 | pk_context pk; |
| 66 | entropy_context entropy; |
| 67 | ctr_drbg_context ctr_drbg; |
| 68 | unsigned char result[1024]; |
| 69 | unsigned char buf[512]; |
| 70 | const char *pers = "pk_decrypt"; |
| 71 | ((void) argv); |
| 72 | |
| 73 | memset(result, 0, sizeof( result ) ); |
| 74 | ret = 1; |
| 75 | |
| 76 | if( argc != 2 ) |
| 77 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 78 | polarssl_printf( "usage: pk_decrypt <key_file>\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 79 | |
| 80 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 81 | polarssl_printf( "\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 82 | #endif |
| 83 | |
| 84 | goto exit; |
| 85 | } |
| 86 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 87 | polarssl_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 88 | fflush( stdout ); |
| 89 | |
| 90 | entropy_init( &entropy ); |
| 91 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 92 | (const unsigned char *) pers, |
| 93 | strlen( pers ) ) ) != 0 ) |
| 94 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 95 | polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 96 | goto exit; |
| 97 | } |
| 98 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 99 | polarssl_printf( "\n . Reading private key from '%s'", argv[1] ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 100 | fflush( stdout ); |
| 101 | |
| 102 | pk_init( &pk ); |
| 103 | |
| 104 | if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 ) |
| 105 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 106 | polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 107 | goto exit; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * Extract the RSA encrypted value from the text file |
| 112 | */ |
| 113 | ret = 1; |
| 114 | |
| 115 | if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL ) |
| 116 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 117 | polarssl_printf( "\n ! Could not open %s\n\n", "result-enc.txt" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 118 | goto exit; |
| 119 | } |
| 120 | |
| 121 | i = 0; |
| 122 | |
| 123 | while( fscanf( f, "%02X", &c ) > 0 && |
| 124 | i < (int) sizeof( buf ) ) |
| 125 | buf[i++] = (unsigned char) c; |
| 126 | |
| 127 | fclose( f ); |
| 128 | |
| 129 | /* |
| 130 | * Decrypt the encrypted RSA data and print the result. |
| 131 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 132 | polarssl_printf( "\n . Decrypting the encrypted data" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 133 | fflush( stdout ); |
| 134 | |
| 135 | if( ( ret = pk_decrypt( &pk, buf, i, result, &olen, sizeof(result), |
| 136 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
| 137 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 138 | polarssl_printf( " failed\n ! pk_decrypt returned -0x%04x\n", -ret ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 139 | goto exit; |
| 140 | } |
| 141 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 142 | polarssl_printf( "\n . OK\n\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 143 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 144 | polarssl_printf( "The decrypted result is: '%s'\n\n", result ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 145 | |
| 146 | ret = 0; |
| 147 | |
| 148 | exit: |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 149 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 150 | entropy_free( &entropy ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 151 | |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 152 | #if defined(POLARSSL_ERROR_C) |
| 153 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 154 | polarssl_printf( " ! Last error was: %s\n", buf ); |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 155 | #endif |
| 156 | |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 157 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 158 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 940f9ce | 2013-09-18 15:34:57 +0200 | [diff] [blame] | 159 | fflush( stdout ); getchar(); |
| 160 | #endif |
| 161 | |
| 162 | return( ret ); |
| 163 | } |
| 164 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO && |
| 165 | POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */ |