blob: f80c1e23421185c2ba5ae167973a6a117297f955 [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdio.h>
31
Paul Bakker940f9ce2013-09-18 15:34:57 +020032#include "polarssl/error.h"
33#include "polarssl/pk.h"
34#include "polarssl/entropy.h"
35#include "polarssl/ctr_drbg.h"
36
37#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_PK_PARSE_C) || \
38 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_ENTROPY_C) || \
39 !defined(POLARSSL_CTR_DRBG_C)
40int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
45 printf("POLARSSL_BIGNUM_C and/or POLARSSL_PK_PARSE_C and/or "
46 "POLARSSL_FS_IO and/or POLARSSL_ENTROPY_C and/or "
47 "POLARSSL_CTR_DRBG_C not defined.\n");
48 return( 0 );
49}
50#else
51int main( int argc, char *argv[] )
52{
53 FILE *f;
54 int ret, c;
55 size_t i, olen = 0;
56 pk_context pk;
57 entropy_context entropy;
58 ctr_drbg_context ctr_drbg;
59 unsigned char result[1024];
60 unsigned char buf[512];
61 const char *pers = "pk_decrypt";
62 ((void) argv);
63
64 memset(result, 0, sizeof( result ) );
65 ret = 1;
66
67 if( argc != 2 )
68 {
69 printf( "usage: pk_decrypt <key_file>\n" );
70
71#if defined(_WIN32)
72 printf( "\n" );
73#endif
74
75 goto exit;
76 }
77
78 printf( "\n . Seeding the random number generator..." );
79 fflush( stdout );
80
81 entropy_init( &entropy );
82 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
83 (const unsigned char *) pers,
84 strlen( pers ) ) ) != 0 )
85 {
86 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
87 goto exit;
88 }
89
90 printf( "\n . Reading private key from '%s'", argv[1] );
91 fflush( stdout );
92
93 pk_init( &pk );
94
95 if( ( ret = pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
96 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020097 printf( " failed\n ! pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +020098 goto exit;
99 }
100
101 /*
102 * Extract the RSA encrypted value from the text file
103 */
104 ret = 1;
105
106 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
107 {
108 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
109 goto exit;
110 }
111
112 i = 0;
113
114 while( fscanf( f, "%02X", &c ) > 0 &&
115 i < (int) sizeof( buf ) )
116 buf[i++] = (unsigned char) c;
117
118 fclose( f );
119
120 /*
121 * Decrypt the encrypted RSA data and print the result.
122 */
123 printf( "\n . Decrypting the encrypted data" );
124 fflush( stdout );
125
126 if( ( ret = pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
127 ctr_drbg_random, &ctr_drbg ) ) != 0 )
128 {
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200129 printf( " failed\n ! pk_decrypt returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130 goto exit;
131 }
132
133 printf( "\n . OK\n\n" );
134
135 printf( "The decrypted result is: '%s'\n\n", result );
136
137 ret = 0;
138
139exit:
Paul Bakkera317a982014-06-18 16:44:11 +0200140 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200141 entropy_free( &entropy );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200143#if defined(POLARSSL_ERROR_C)
144 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
145 printf( " ! Last error was: %s\n", buf );
146#endif
147
Paul Bakker940f9ce2013-09-18 15:34:57 +0200148#if defined(_WIN32)
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_PK_PARSE_C && POLARSSL_FS_IO &&
156 POLARSSL_ENTROPY_C && POLARSSL_CTR_DRBG_C */