blob: b8f7943d6232341e4745d1cdbef493afa0b54ae2 [file] [log] [blame]
Paul Bakker940f9ce2013-09-18 15:34:57 +02001/*
2 * Public key-based simple decryption program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker940f9ce2013-09-18 15:34:57 +02006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +02009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_PK_PARSE_C) && \
13 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_ENTROPY_C) && \
14 defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/error.h"
16#include "mbedtls/pk.h"
17#include "mbedtls/entropy.h"
18#include "mbedtls/ctr_drbg.h"
Paul Bakker940f9ce2013-09-18 15:34:57 +020019
Rich Evans18b78c72015-02-11 14:06:19 +000020#include <stdio.h>
21#include <string.h>
22#endif
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_PK_PARSE_C) || \
25 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
26 !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010027int main(void)
Paul Bakker940f9ce2013-09-18 15:34:57 +020028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_PK_PARSE_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010030 "MBEDTLS_FS_IO and/or MBEDTLS_ENTROPY_C and/or "
31 "MBEDTLS_CTR_DRBG_C not defined.\n");
32 mbedtls_exit(0);
Paul Bakker940f9ce2013-09-18 15:34:57 +020033}
34#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000035
Simon Butcher63cb97e2018-12-06 17:43:31 +000036
Gilles Peskine449bd832023-01-11 14:50:10 +010037int main(int argc, char *argv[])
Paul Bakker940f9ce2013-09-18 15:34:57 +020038{
39 FILE *f;
Gilles Peskinea5fc9392020-04-14 19:34:19 +020040 int ret = 1;
41 unsigned c;
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +010042 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker940f9ce2013-09-18 15:34:57 +020043 size_t i, olen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_pk_context pk;
45 mbedtls_entropy_context entropy;
46 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker940f9ce2013-09-18 15:34:57 +020047 unsigned char result[1024];
48 unsigned char buf[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 const char *pers = "mbedtls_pk_decrypt";
Paul Bakker940f9ce2013-09-18 15:34:57 +020050 ((void) argv);
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_pk_init(&pk);
53 mbedtls_entropy_init(&entropy);
54 mbedtls_ctr_drbg_init(&ctr_drbg);
Hanno Beckerbd336c12017-10-08 16:44:10 +010055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 memset(result, 0, sizeof(result));
Paul Bakker940f9ce2013-09-18 15:34:57 +020057
Przemek Stekiel2c1ef092023-04-19 09:38:12 +020058#if defined(MBEDTLS_USE_PSA_CRYPTO)
59 psa_status_t status = psa_crypto_init();
60 if (status != PSA_SUCCESS) {
61 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
62 (int) status);
63 goto exit;
64 }
65#endif /* MBEDTLS_USE_PSA_CRYPTO */
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (argc != 2) {
68 mbedtls_printf("usage: mbedtls_pk_decrypt <key_file>\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020069
70#if defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_printf("\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +020072#endif
73
74 goto exit;
75 }
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_printf("\n . Seeding the random number generator...");
78 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func,
81 &entropy, (const unsigned char *) pers,
82 strlen(pers))) != 0) {
83 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
84 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020085 goto exit;
86 }
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_printf("\n . Reading private key from '%s'", argv[1]);
89 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +020090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if ((ret = mbedtls_pk_parse_keyfile(&pk, argv[1], "",
92 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
93 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n",
94 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +020095 goto exit;
96 }
97
98 /*
99 * Extract the RSA encrypted value from the text file
100 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if ((f = fopen("result-enc.txt", "rb")) == NULL) {
102 mbedtls_printf("\n ! Could not open %s\n\n", "result-enc.txt");
Hanno Beckerbd336c12017-10-08 16:44:10 +0100103 ret = 1;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200104 goto exit;
105 }
106
107 i = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 while (fscanf(f, "%02X", (unsigned int *) &c) > 0 &&
109 i < (int) sizeof(buf)) {
Paul Bakker940f9ce2013-09-18 15:34:57 +0200110 buf[i++] = (unsigned char) c;
Hanno Beckerae513a52018-08-23 14:39:04 +0100111 }
Paul Bakker940f9ce2013-09-18 15:34:57 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 fclose(f);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200114
115 /*
116 * Decrypt the encrypted RSA data and print the result.
117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_printf("\n . Decrypting the encrypted data");
119 fflush(stdout);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if ((ret = mbedtls_pk_decrypt(&pk, buf, i, result, &olen, sizeof(result),
122 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
123 mbedtls_printf(" failed\n ! mbedtls_pk_decrypt returned -0x%04x\n",
124 (unsigned int) -ret);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200125 goto exit;
126 }
127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_printf("\n . OK\n\n");
Paul Bakker940f9ce2013-09-18 15:34:57 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_printf("The decrypted result is: '%s'\n\n", result);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200131
Andres Amaya Garcia0a7522c2018-04-29 22:23:22 +0100132 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker940f9ce2013-09-18 15:34:57 +0200133
134exit:
Hanno Beckerbd336c12017-10-08 16:44:10 +0100135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_pk_free(&pk);
137 mbedtls_entropy_free(&entropy);
138 mbedtls_ctr_drbg_free(&ctr_drbg);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200140 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker940f9ce2013-09-18 15:34:57 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143#if defined(MBEDTLS_ERROR_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
145 mbedtls_strerror(ret, (char *) buf, sizeof(buf));
146 mbedtls_printf(" ! Last error was: %s\n", buf);
Manuel Pégourié-Gonnardcf9ab632015-08-27 22:03:33 +0200147 }
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200148#endif
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 mbedtls_exit(exit_code);
Paul Bakker940f9ce2013-09-18 15:34:57 +0200151}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
153 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */