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