blob: 416bbeccc23459812f5aa72c4be5a48e82d88727 [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
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)
43int 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
54int 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 {
100 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
101 printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret,
102 buf );
103 goto exit;
104 }
105
106 /*
107 * Extract the RSA encrypted value from the text file
108 */
109 ret = 1;
110
111 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
112 {
113 printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
114 goto exit;
115 }
116
117 i = 0;
118
119 while( fscanf( f, "%02X", &c ) > 0 &&
120 i < (int) sizeof( buf ) )
121 buf[i++] = (unsigned char) c;
122
123 fclose( f );
124
125 /*
126 * Decrypt the encrypted RSA data and print the result.
127 */
128 printf( "\n . Decrypting the encrypted data" );
129 fflush( stdout );
130
131 if( ( ret = pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
132 ctr_drbg_random, &ctr_drbg ) ) != 0 )
133 {
134 polarssl_strerror( ret, (char *) buf, sizeof(buf) );
135 printf( " failed\n ! pk_decrypt returned -0x%04x - %s\n\n", -ret,
136 buf );
137 goto exit;
138 }
139
140 printf( "\n . OK\n\n" );
141
142 printf( "The decrypted result is: '%s'\n\n", result );
143
144 ret = 0;
145
146exit:
147
148#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 */