blob: 86446986021846ac6c57505f253153ca09747a9b [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based simple decryption program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker940f9ce2013-09-18 15:34:57 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker940f9ce2013-09-18 15:34:57 +02007 *
Paul Bakker940f9ce2013-09-18 15:34:57 +02008 * 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 Bakker940f9ce2013-09-18 15:34:57 +020028
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 Bakker940f9ce2013-09-18 15:34:57 +020035#include <string.h>
36#include <stdio.h>
37
Paul Bakker940f9ce2013-09-18 15:34:57 +020038#include "polarssl/error.h"
39#include "polarssl/pk.h"
40#include "polarssl/entropy.h"
41#include "polarssl/ctr_drbg.h"
42
43#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
44 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
45 !defined(POLARSSL_CTR_DRBG_C)
46int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
Rich Evansf90016a2015-01-19 14:26:37 +000051 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
Paul Bakker940f9ce2013-09-18 15:34:57 +020052 "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
53 "POLARSSL_CTR_DRBG_C not defined.\n");
54 return( 0 );
55}
56#else
57int main( int argc, char *argv[] )
58{
59 FILE *f;
60 int ret, c;
61 size_t i, olen = 0;
62 pk_context pk;
63 entropy_context entropy;
64 ctr_drbg_context ctr_drbg;
65 unsigned char result[1024];
66 unsigned char buf[512];
67 const char *pers = "pk_decrypt";
68 ((void) argv);
69
70 memset(result, 0, sizeof( result ) );
71 ret = 1;
72
73 if( argc != 2 )
74 {
Rich Evansf90016a2015-01-19 14:26:37 +000075 polarssl_printf( "usage: pk_decrypt <key_file>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020076
77#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +020079#endif
80
81 goto exit;
82 }
83
Rich Evansf90016a2015-01-19 14:26:37 +000084 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +020085 fflush( stdout );
86
87 entropy_init( &entropy );
88 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
89 (const unsigned char *) pers,
90 strlen( pers ) ) ) != 0 )
91 {
Rich Evansf90016a2015-01-19 14:26:37 +000092 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020093 goto exit;
94 }
95
Rich Evansf90016a2015-01-19 14:26:37 +000096 polarssl_printf( "\n . Reading private key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +020097 fflush( stdout );
98
99 pk_init( &pk );
100
101 if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
102 {
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104 goto exit;
105 }
106
107 /*
108 * Extract the RSA encrypted value from the text file
109 */
110 ret = 1;
111
112 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
113 {
Rich Evansf90016a2015-01-19 14:26:37 +0000114 polarssl_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200115 goto exit;
116 }
117
118 i = 0;
119
120 while( fscanf( f, "%02X", &c ) > 0 &&
121 i < (int) sizeof( buf ) )
122 buf[i++] = (unsigned char) c;
123
124 fclose( f );
125
126 /*
127 * Decrypt the encrypted RSA data and print the result.
128 */
Rich Evansf90016a2015-01-19 14:26:37 +0000129 polarssl_printf( "\n . Decrypting the encrypted data" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130 fflush( stdout );
131
132 if( ( ret = pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
133 ctr_drbg_random, &ctr_drbg ) ) != 0 )
134 {
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( " failed\n ! pk_decrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200136 goto exit;
137 }
138
Rich Evansf90016a2015-01-19 14:26:37 +0000139 polarssl_printf( "\n . OK\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200140
Rich Evansf90016a2015-01-19 14:26:37 +0000141 polarssl_printf( "The decrypted result is: '%s'\n\n", result );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142
143 ret = 0;
144
145exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200146 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200147 entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200148
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200149#if defined(POLARSSL_ERROR_C)
150 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
Rich Evansf90016a2015-01-19 14:26:37 +0000151 polarssl_printf( " ! Last error was: %s\n", buf );
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200152#endif
153
Paul Bakker940f9ce2013-09-18 15:34:57 +0200154#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000155 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200156 fflush( stdout ); getchar();
157#endif
158
159 return( ret );
160}
161#endif /* POLARSSL_BIGNUM_C && POLARSSL_PK_PARSE_C && POLARSSL_FS_IO &&
162 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */