blob: e53d6fd1f895f3b3881d2c2f0c985bb9a972c7fe [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based simple decryption program
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker940f9ce2013-09-18 15:34:57 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker940f9ce2013-09-18 15:34:57 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker940f9ce2013-09-18 15:34:57 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garciabce5f782018-04-30 22:43:11 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010059#define mbedtls_exit exit
Andres Amaya Garciabce5f782018-04-30 22:43:11 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
61#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
65 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
66 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/error.h"
68#include "mbedtls/pk.h"
69#include "mbedtls/entropy.h"
70#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020071
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <stdio.h>
73#include <string.h>
74#endif
75
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
77 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
78 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000079int main( void )
Paul Bakker940f9ce2013-09-18 15:34:57 +020080{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
82 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
83 "MBEDTLS_CTR_DRBG_C not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020084 mbedtls_exit( 0 );
Paul Bakker940f9ce2013-09-18 15:34:57 +020085}
86#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000087
Simon Butcher63cb97e2018-12-06 17:43:31 +000088
Paul Bakker940f9ce2013-09-18 15:34:57 +020089int main( int argc, char *argv[] )
90{
91 FILE *f;
Gilles Peskine971e5e92020-04-14 19:34:19 +020092 int ret = 1;
93 unsigned c;
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +010094 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_pk_context pk;
97 mbedtls_entropy_context entropy;
98 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020099 unsigned char result[1024];
100 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 const char *pers = "mbedtls_pk_decrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +0200102 ((void) argv);
103
Hanno Beckerbd336c12017-10-08 16:44:10 +0100104 mbedtls_pk_init( &pk );
105 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200106 mbedtls_ctr_drbg_init( &ctr_drbg );
Hanno Beckerbd336c12017-10-08 16:44:10 +0100107
Paul Bakker940f9ce2013-09-18 15:34:57 +0200108 memset(result, 0, sizeof( result ) );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200109
110 if( argc != 2 )
111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( "usage: mbedtls_pk_decrypt <key_file>\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200113
114#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( "\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200116#endif
117
118 goto exit;
119 }
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200122 fflush( stdout );
123
Hanno Beckerae513a52018-08-23 14:39:04 +0100124 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
125 &entropy, (const unsigned char *) pers,
126 strlen( pers ) ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200127 {
Hanno Beckera63c1c32018-08-23 15:56:03 +0100128 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
129 -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200130 goto exit;
131 }
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 mbedtls_printf( "\n . Reading private key from '%s'", argv[1] );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200134 fflush( stdout );
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 if( ( ret = mbedtls_pk_parse_keyfile( &pk, argv[1], "" ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200139 goto exit;
140 }
141
142 /*
143 * Extract the RSA encrypted value from the text file
144 */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200145 if( ( f = fopen( "result-enc.txt", "rb" ) ) == NULL )
146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( "\n ! Could not open %s\n\n", "result-enc.txt" );
Hanno Beckerbd336c12017-10-08 16:44:10 +0100148 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200149 goto exit;
150 }
151
152 i = 0;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200153 while( fscanf( f, "%02X", &c ) > 0 &&
154 i < (int) sizeof( buf ) )
Hanno Beckerae513a52018-08-23 14:39:04 +0100155 {
Paul Bakker940f9ce2013-09-18 15:34:57 +0200156 buf[i++] = (unsigned char) c;
Hanno Beckerae513a52018-08-23 14:39:04 +0100157 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200158
159 fclose( f );
160
161 /*
162 * Decrypt the encrypted RSA data and print the result.
163 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_printf( "\n . Decrypting the encrypted data" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200165 fflush( stdout );
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_pk_decrypt( &pk, buf, i, result, &olen, sizeof(result),
168 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker940f9ce2013-09-18 15:34:57 +0200169 {
Hanno Beckerae513a52018-08-23 14:39:04 +0100170 mbedtls_printf( " failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
171 -ret );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200172 goto exit;
173 }
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_printf( "\n . OK\n\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( "The decrypted result is: '%s'\n\n", result );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200178
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +0100179 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200180
181exit:
Hanno Beckerbd336c12017-10-08 16:44:10 +0100182
183 mbedtls_pk_free( &pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_entropy_free( &entropy );
Hanno Beckerbd336c12017-10-08 16:44:10 +0100185 mbedtls_ctr_drbg_free( &ctr_drbg );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_ERROR_C)
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +0100188 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200189 {
Hanno Beckerae513a52018-08-23 14:39:04 +0100190 mbedtls_strerror( ret, (char *) buf, sizeof( buf ) );
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200191 mbedtls_printf( " ! Last error was: %s\n", buf );
192 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200193#endif
194
Paul Bakker940f9ce2013-09-18 15:34:57 +0200195#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200197 fflush( stdout ); getchar();
198#endif
199
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200200 mbedtls_exit( exit_code );
Paul Bakker940f9ce2013-09-18 15:34:57 +0200201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
203 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */